blob: df63fb7f3ecec1bbe4182489cf289ea2599b927a [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 Moolenaar7db5fc82010-05-24 11:59:29 +0200953 vim_memset(abKeystate, 0, sizeof (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 Moolenaar912bc4a2019-12-01 18:58:11 +01001785# define TYPEAHEADLEN 20
Bram Moolenaar0f873732019-12-05 20:28:46 +01001786 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 static int typeaheadlen = 0;
1788
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001789# ifdef VIMDLL
1790 if (gui.in_use)
1791 return 0;
1792# endif
1793
Bram Moolenaar0f873732019-12-05 20:28:46 +01001794 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (typeaheadlen > 0)
1796 goto theend;
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (time >= 0)
1799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001803 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001805 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar3918c952005-03-15 22:34:55 +00001807 /*
1808 * If there is no character available within 2 seconds (default)
1809 * write the autoscript file to disk. Or cause the CursorHold event
1810 * to be triggered.
1811 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001812 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001814 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001816 buf[0] = K_SPECIAL;
1817 buf[1] = KS_EXTRA;
1818 buf[2] = (int)KE_CURSORHOLD;
1819 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001821 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 }
1824
1825 /*
1826 * Try to read as many characters as there are, until the buffer is full.
1827 */
1828
Bram Moolenaar0f873732019-12-05 20:28:46 +01001829 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 g_fCBrkPressed = FALSE;
1831
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001832# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (fdDump)
1834 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaar0f873732019-12-05 20:28:46 +01001837 // Keep looping until there is something in the typeahead buffer and more
1838 // to get and still room in the buffer (up to two bytes for a char and
1839 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001840 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 && typeaheadlen + 5 <= TYPEAHEADLEN)
1842 {
1843 if (typebuf_changed(tb_change_cnt))
1844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001845 // "buf" may be invalid now if a client put something in the
1846 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 typeaheadlen = 0;
1848 break;
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (g_nMouseClick != -1)
1851 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001852# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (fdDump)
1854 fprintf(fdDump, "{%02x @ %d, %d}",
1855 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 typeahead[typeaheadlen++] = ESC + 128;
1858 typeahead[typeaheadlen++] = 'M';
1859 typeahead[typeaheadlen++] = g_nMouseClick;
1860 typeahead[typeaheadlen++] = g_xMouse + '!';
1861 typeahead[typeaheadlen++] = g_yMouse + '!';
1862 g_nMouseClick = -1;
1863 }
1864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001866 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int modifiers = 0;
1868
1869 c = tgetch(&modifiers, &ch2);
1870
1871 if (typebuf_changed(tb_change_cnt))
1872 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001873 // "buf" may be invalid now if a client put something in the
1874 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 typeaheadlen = 0;
1876 break;
1877 }
1878
1879 if (c == Ctrl_C && ctrl_c_interrupts)
1880 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001881# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 got_int = TRUE;
1885 }
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 int n = 1;
1890
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001891 if (ch2 == NUL)
1892 {
1893 int i;
1894 char_u *p;
1895 WCHAR ch[2];
1896
1897 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001898 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001899 {
1900 ch[1] = tgetch(&modifiers, &ch2);
1901 n++;
1902 }
1903 p = utf16_to_enc(ch, &n);
1904 if (p != NULL)
1905 {
1906 for (i = 0; i < n; i++)
1907 typeahead[typeaheadlen + i] = p[i];
1908 vim_free(p);
1909 }
1910 }
1911 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001912 typeahead[typeaheadlen] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (ch2 != NUL)
1914 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001915 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001916 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001917 switch (ch2)
1918 {
1919 case (WCHAR)'\324': // SHIFT+Insert
1920 case (WCHAR)'\325': // CTRL+Insert
1921 case (WCHAR)'\327': // SHIFT+Delete
1922 case (WCHAR)'\330': // CTRL+Delete
1923 typeahead[typeaheadlen + n] = (char_u)ch2;
1924 n++;
1925 break;
1926
1927 default:
1928 typeahead[typeaheadlen + n] = 3;
1929 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1930 n += 2;
1931 break;
1932 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001933 }
1934 else
1935 {
1936 typeahead[typeaheadlen + n] = 3;
1937 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1938 n += 2;
1939 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001940 }
1941
Bram Moolenaar0f873732019-12-05 20:28:46 +01001942 // Use the ALT key to set the 8th bit of the character
1943 // when it's one byte, the 8th bit isn't set yet and not
1944 // using a double-byte encoding (would become a lead
1945 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if ((modifiers & MOD_MASK_ALT)
1947 && n == 1
1948 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 )
1951 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001952 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1953 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 modifiers &= ~MOD_MASK_ALT;
1955 }
1956
1957 if (modifiers != 0)
1958 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001959 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 mch_memmove(typeahead + typeaheadlen + 3,
1961 typeahead + typeaheadlen, n);
1962 typeahead[typeaheadlen++] = K_SPECIAL;
1963 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1964 typeahead[typeaheadlen++] = modifiers;
1965 }
1966
1967 typeaheadlen += n;
1968
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001969# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (fdDump)
1971 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974 }
1975 }
1976
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001977# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (fdDump)
1979 {
1980 fputs("]\n", fdDump);
1981 fflush(fdDump);
1982 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001986 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 len = 0;
1988 while (len < maxlen && typeaheadlen > 0)
1989 {
1990 buf[len++] = typeahead[0];
1991 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
1992 }
1993 return len;
1994
Bram Moolenaar0f873732019-12-05 20:28:46 +01001995#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001997#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998}
1999
Bram Moolenaar82881492012-11-20 16:53:39 +01002000#ifndef PROTO
2001# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002002# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#endif
2005
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002006/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002007 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2008 * If "use_path" is FALSE: Return TRUE if "name" exists.
2009 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2010 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002011 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002014executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002016 WCHAR *p;
2017 WCHAR fnamew[_MAX_PATH];
2018 WCHAR *dumw;
2019 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002020 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar43663192017-03-05 14:29:12 +01002022 if (!use_path)
2023 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002024 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002025 {
2026 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002027 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002028 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002029 *path = vim_strsave((char_u *)name);
2030 else
2031 *path = FullName_save((char_u *)name, FALSE);
2032 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002033 return TRUE;
2034 }
2035 return FALSE;
2036 }
2037
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002038 p = enc_to_utf16((char_u *)name, NULL);
2039 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002040 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002041
2042 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002043 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002044 if (wnewpath == NULL)
2045 return FALSE;
2046 wcscpy(wnewpath, L".;");
2047 wcscat(wnewpath, wcurpath);
2048 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2049 vim_free(wnewpath);
2050 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002051 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002052 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002053 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002054 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002055 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002056 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002057 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058}
2059
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002060#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2061 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002062/*
2063 * Bad parameter handler.
2064 *
2065 * Certain MS CRT functions will intentionally crash when passed invalid
2066 * parameters to highlight possible security holes. Setting this function as
2067 * the bad parameter handler will prevent the crash.
2068 *
2069 * In debug builds the parameters contain CRT information that might help track
2070 * down the source of a problem, but in non-debug builds the arguments are all
2071 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2072 * worth allowing these to make debugging of issues easier.
2073 */
2074 static void
2075bad_param_handler(const wchar_t *expression,
2076 const wchar_t *function,
2077 const wchar_t *file,
2078 unsigned int line,
2079 uintptr_t pReserved)
2080{
2081}
2082
2083# define SET_INVALID_PARAM_HANDLER \
2084 ((void)_set_invalid_parameter_handler(bad_param_handler))
2085#else
2086# define SET_INVALID_PARAM_HANDLER
2087#endif
2088
Bram Moolenaar4f974752019-02-17 17:44:42 +01002089#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091/*
2092 * GUI version of mch_init().
2093 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002094 static void
2095mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002097# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
Bram Moolenaar0f873732019-12-05 20:28:46 +01002101 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002102 SET_INVALID_PARAM_HANDLER;
2103
Bram Moolenaar0f873732019-12-05 20:28:46 +01002104 // Let critical errors result in a failure, not in a dialog box. Required
2105 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 SetErrorMode(SEM_FAILCRITICALERRORS);
2107
Bram Moolenaar0f873732019-12-05 20:28:46 +01002108 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
Bram Moolenaar0f873732019-12-05 20:28:46 +01002110 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 Rows = 25;
2112 Columns = 80;
2113
Bram Moolenaar0f873732019-12-05 20:28:46 +01002114 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116 char_u vimrun_location[_MAX_PATH + 4];
2117
Bram Moolenaar0f873732019-12-05 20:28:46 +01002118 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 STRCPY(vimrun_location, exe_name);
2120 STRCPY(gettail(vimrun_location), "vimrun.exe");
2121 if (mch_getperm(vimrun_location) >= 0)
2122 {
2123 if (*skiptowhite(vimrun_location) != NUL)
2124 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002125 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 mch_memmove(vimrun_location + 1, vimrun_location,
2127 STRLEN(vimrun_location) + 1);
2128 *vimrun_location = '"';
2129 STRCPY(gettail(vimrun_location), "vimrun\" ");
2130 }
2131 else
2132 STRCPY(gettail(vimrun_location), "vimrun ");
2133
2134 vimrun_path = (char *)vim_strsave(vimrun_location);
2135 s_dont_use_vimrun = FALSE;
2136 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002137 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 s_dont_use_vimrun = FALSE;
2139
Bram Moolenaar0f873732019-12-05 20:28:46 +01002140 // Don't give the warning for a missing vimrun.exe right now, but only
2141 // when vimrun was supposed to be used. Don't bother people that do
2142 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (s_dont_use_vimrun)
2144 need_vimrun_warning = TRUE;
2145 }
2146
2147 /*
2148 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2149 * Otherwise the default "findstr /n" is used.
2150 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002151 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2153
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002154# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002155 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002156# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002157
2158 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159}
2160
2161
Bram Moolenaar0f873732019-12-05 20:28:46 +01002162#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002163
2164#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002166# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2167# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169/*
2170 * ClearConsoleBuffer()
2171 * Description:
2172 * Clears the entire contents of the console screen buffer, using the
2173 * specified attribute.
2174 * Returns:
2175 * TRUE on success
2176 */
2177 static BOOL
2178ClearConsoleBuffer(WORD wAttribute)
2179{
2180 CONSOLE_SCREEN_BUFFER_INFO csbi;
2181 COORD coord;
2182 DWORD NumCells, dummy;
2183
2184 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2185 return FALSE;
2186
2187 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2188 coord.X = 0;
2189 coord.Y = 0;
2190 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2191 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2194 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 return TRUE;
2198}
2199
2200/*
2201 * FitConsoleWindow()
2202 * Description:
2203 * Checks if the console window will fit within given buffer dimensions.
2204 * Also, if requested, will shrink the window to fit.
2205 * Returns:
2206 * TRUE on success
2207 */
2208 static BOOL
2209FitConsoleWindow(
2210 COORD dwBufferSize,
2211 BOOL WantAdjust)
2212{
2213 CONSOLE_SCREEN_BUFFER_INFO csbi;
2214 COORD dwWindowSize;
2215 BOOL NeedAdjust = FALSE;
2216
2217 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2218 {
2219 /*
2220 * A buffer resize will fail if the current console window does
2221 * not lie completely within that buffer. To avoid this, we might
2222 * have to move and possibly shrink the window.
2223 */
2224 if (csbi.srWindow.Right >= dwBufferSize.X)
2225 {
2226 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2227 if (dwWindowSize.X > dwBufferSize.X)
2228 dwWindowSize.X = dwBufferSize.X;
2229 csbi.srWindow.Right = dwBufferSize.X - 1;
2230 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2231 NeedAdjust = TRUE;
2232 }
2233 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2234 {
2235 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2236 if (dwWindowSize.Y > dwBufferSize.Y)
2237 dwWindowSize.Y = dwBufferSize.Y;
2238 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2239 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2240 NeedAdjust = TRUE;
2241 }
2242 if (NeedAdjust && WantAdjust)
2243 {
2244 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2245 return FALSE;
2246 }
2247 return TRUE;
2248 }
2249
2250 return FALSE;
2251}
2252
2253typedef struct ConsoleBufferStruct
2254{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002255 BOOL IsValid;
2256 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002257 PCHAR_INFO Buffer;
2258 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002259 PSMALL_RECT Regions;
2260 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261} ConsoleBuffer;
2262
2263/*
2264 * SaveConsoleBuffer()
2265 * Description:
2266 * Saves important information about the console buffer, including the
2267 * actual buffer contents. The saved information is suitable for later
2268 * restoration by RestoreConsoleBuffer().
2269 * Returns:
2270 * TRUE if all information was saved; FALSE otherwise
2271 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2272 */
2273 static BOOL
2274SaveConsoleBuffer(
2275 ConsoleBuffer *cb)
2276{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002277 DWORD NumCells;
2278 COORD BufferCoord;
2279 SMALL_RECT ReadRegion;
2280 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002281 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (cb == NULL)
2284 return FALSE;
2285
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002286 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 cb->IsValid = FALSE;
2289 return FALSE;
2290 }
2291 cb->IsValid = TRUE;
2292
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002293 /*
2294 * Allocate a buffer large enough to hold the entire console screen
2295 * buffer. If this ConsoleBuffer structure has already been initialized
2296 * with a buffer of the correct size, then just use that one.
2297 */
2298 if (!cb->IsValid || cb->Buffer == NULL ||
2299 cb->BufferSize.X != cb->Info.dwSize.X ||
2300 cb->BufferSize.Y != cb->Info.dwSize.Y)
2301 {
2302 cb->BufferSize.X = cb->Info.dwSize.X;
2303 cb->BufferSize.Y = cb->Info.dwSize.Y;
2304 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2305 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002306 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002307 if (cb->Buffer == NULL)
2308 return FALSE;
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002312 * We will now copy the console screen buffer into our buffer.
2313 * ReadConsoleOutput() seems to be limited as far as how much you
2314 * can read at a time. Empirically, this number seems to be about
2315 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2316 * in chunks until it is all copied. The chunks will all have the
2317 * same horizontal characteristics, so initialize them now. The
2318 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002320 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002321 ReadRegion.Left = 0;
2322 ReadRegion.Right = cb->Info.dwSize.X - 1;
2323 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002324
2325 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2326 if (cb->Regions == NULL || numregions != cb->NumRegions)
2327 {
2328 cb->NumRegions = numregions;
2329 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002330 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002331 if (cb->Regions == NULL)
2332 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002333 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002334 return FALSE;
2335 }
2336 }
2337
2338 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002340 /*
2341 * Read into position (0, Y) in our buffer.
2342 */
2343 BufferCoord.Y = Y;
2344 /*
2345 * Read the region whose top left corner is (0, Y) and whose bottom
2346 * right corner is (width - 1, Y + Y_incr - 1). This should define
2347 * a region of size width by Y_incr. Don't worry if this region is
2348 * too large for the remaining buffer; it will be cropped.
2349 */
2350 ReadRegion.Top = Y;
2351 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002352 if (!ReadConsoleOutputW(g_hConOut, // output handle
2353 cb->Buffer, // our buffer
2354 cb->BufferSize, // dimensions of our buffer
2355 BufferCoord, // offset in our buffer
2356 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002357 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002358 VIM_CLEAR(cb->Buffer);
2359 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002360 return FALSE;
2361 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002362 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364
2365 return TRUE;
2366}
2367
2368/*
2369 * RestoreConsoleBuffer()
2370 * Description:
2371 * Restores important information about the console buffer, including the
2372 * actual buffer contents, if desired. The information to restore is in
2373 * the same format used by SaveConsoleBuffer().
2374 * Returns:
2375 * TRUE on success
2376 */
2377 static BOOL
2378RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002379 ConsoleBuffer *cb,
2380 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002382 COORD BufferCoord;
2383 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002384 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 if (cb == NULL || !cb->IsValid)
2387 return FALSE;
2388
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002389 /*
2390 * Before restoring the buffer contents, clear the current buffer, and
2391 * restore the cursor position and window information. Doing this now
2392 * prevents old buffer contents from "flashing" onto the screen.
2393 */
2394 if (RestoreScreen)
2395 ClearConsoleBuffer(cb->Info.wAttributes);
2396
2397 FitConsoleWindow(cb->Info.dwSize, TRUE);
2398 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2399 return FALSE;
2400 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2401 return FALSE;
2402
2403 if (!RestoreScreen)
2404 {
2405 /*
2406 * No need to restore the screen buffer contents, so we're done.
2407 */
2408 return TRUE;
2409 }
2410
2411 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2412 return FALSE;
2413 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2414 return FALSE;
2415
2416 /*
2417 * Restore the screen buffer contents.
2418 */
2419 if (cb->Buffer != NULL)
2420 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002421 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002422 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002423 BufferCoord.X = cb->Regions[i].Left;
2424 BufferCoord.Y = cb->Regions[i].Top;
2425 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002426 if (!WriteConsoleOutputW(g_hConOut, // output handle
2427 cb->Buffer, // our buffer
2428 cb->BufferSize, // dimensions of our buffer
2429 BufferCoord, // offset in our buffer
2430 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002431 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002432 }
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 return TRUE;
2436}
2437
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002438# define FEAT_RESTORE_ORIG_SCREEN
2439# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002440static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442static ConsoleBuffer g_cbNonTermcap = { 0 };
2443static ConsoleBuffer g_cbTermcap = { 0 };
2444
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002445# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002447HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448static HICON g_hOrigIconSmall = NULL;
2449static HICON g_hOrigIcon = NULL;
2450static HICON g_hVimIcon = NULL;
2451static BOOL g_fCanChangeIcon = FALSE;
2452
Bram Moolenaar0f873732019-12-05 20:28:46 +01002453// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002454# ifndef ICON_SMALL
2455# define ICON_SMALL 0
2456# endif
2457# ifndef ICON_BIG
2458# define ICON_BIG 1
2459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460/*
2461 * GetConsoleIcon()
2462 * Description:
2463 * Attempts to retrieve the small icon and/or the big icon currently in
2464 * use by a given window.
2465 * Returns:
2466 * TRUE on success
2467 */
2468 static BOOL
2469GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002470 HWND hWnd,
2471 HICON *phIconSmall,
2472 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 if (hWnd == NULL)
2475 return FALSE;
2476
2477 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002478 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2479 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002481 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2482 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return TRUE;
2484}
2485
2486/*
2487 * SetConsoleIcon()
2488 * Description:
2489 * Attempts to change 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
2495SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002496 HWND hWnd,
2497 HICON hIconSmall,
2498 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (hWnd == NULL)
2501 return FALSE;
2502
2503 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002504 SendMessage(hWnd, WM_SETICON,
2505 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002507 SendMessage(hWnd, WM_SETICON,
2508 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return TRUE;
2510}
2511
2512/*
2513 * SaveConsoleTitleAndIcon()
2514 * Description:
2515 * Saves the current console window title in g_szOrigTitle, for later
2516 * restoration. Also, attempts to obtain a handle to the console window,
2517 * and use it to save the small and big icons currently in use by the
2518 * console window. This is not always possible on some versions of Windows;
2519 * nor is it possible when running Vim remotely using Telnet (since the
2520 * console window the user sees is owned by a remote process).
2521 */
2522 static void
2523SaveConsoleTitleAndIcon(void)
2524{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002525 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2527 return;
2528
2529 /*
2530 * Obtain a handle to the console window using GetConsoleWindow() from
2531 * KERNEL32.DLL; we need to handle in order to change the window icon.
2532 * This function only exists on NT-based Windows, starting with Windows
2533 * 2000. On older operating systems, we can't change the window icon
2534 * anyway.
2535 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002536 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (g_hWnd == NULL)
2538 return;
2539
Bram Moolenaar0f873732019-12-05 20:28:46 +01002540 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2542 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2543 return;
2544
Bram Moolenaar0f873732019-12-05 20:28:46 +01002545 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002546 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002547 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (g_hVimIcon != NULL)
2549 g_fCanChangeIcon = TRUE;
2550}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553static int g_fWindInitCalled = FALSE;
2554static int g_fTermcapMode = FALSE;
2555static CONSOLE_CURSOR_INFO g_cci;
2556static DWORD g_cmodein = 0;
2557static DWORD g_cmodeout = 0;
2558
2559/*
2560 * non-GUI version of mch_init().
2561 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002562 static void
2563mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002565# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002566 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002567# endif
2568# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar0f873732019-12-05 20:28:46 +01002572 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002573 SET_INVALID_PARAM_HANDLER;
2574
Bram Moolenaar0f873732019-12-05 20:28:46 +01002575 // Let critical errors result in a failure, not in a dialog box. Required
2576 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 SetErrorMode(SEM_FAILCRITICALERRORS);
2578
Bram Moolenaar0f873732019-12-05 20:28:46 +01002579 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 out_flush();
2581
Bram Moolenaar0f873732019-12-05 20:28:46 +01002582 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (read_cmd_fd == 0)
2584 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2585 else
2586 create_conin();
2587 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2588
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002589# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002590 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002591 SaveConsoleBuffer(&g_cbOrig);
2592 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002593# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002595 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2596 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (cterm_normal_fg_color == 0)
2599 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2600 if (cterm_normal_bg_color == 0)
2601 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2602
Bram Moolenaarbdace832019-03-02 10:13:42 +01002603 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002604 g_color_index_fg = g_attrDefault & 0xf;
2605 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2606
Bram Moolenaar0f873732019-12-05 20:28:46 +01002607 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 update_tcap(g_attrCurrent);
2609
2610 GetConsoleCursorInfo(g_hConOut, &g_cci);
2611 GetConsoleMode(g_hConIn, &g_cmodein);
2612 GetConsoleMode(g_hConOut, &g_cmodeout);
2613
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002614# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 SaveConsoleTitleAndIcon();
2616 /*
2617 * Set both the small and big icons of the console window to Vim's icon.
2618 * Note that Vim presently only has one size of icon (32x32), but it
2619 * automatically gets scaled down to 16x16 when setting the small icon.
2620 */
2621 if (g_fCanChangeIcon)
2622 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 ui_get_shellsize();
2626
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002627# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 fdDump = fopen("dump", "wt");
2629
2630 if (fdDump)
2631 {
2632 time_t t;
2633
2634 time(&t);
2635 fputs(ctime(&t), fdDump);
2636 fflush(fdDump);
2637 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640 g_fWindInitCalled = TRUE;
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002644# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002645 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002646# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002647
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002648 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002649 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650}
2651
2652/*
2653 * non-GUI version of mch_exit().
2654 * Shut down and exit with status `r'
2655 * Careful: mch_exit() may be called before mch_init()!
2656 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002657 static void
2658mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002660 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002662 vtp_exit();
2663
Bram Moolenaar955f1982017-02-05 15:10:51 +01002664 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (g_fWindInitCalled)
2666 settmode(TMODE_COOK);
2667
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 if (g_fWindInitCalled)
2671 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002672# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002673 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 /*
2675 * Restore both the small and big icons of the console window to
2676 * what they were at startup. Don't do this when the window is
2677 * closed, Vim would hang here.
2678 */
2679 if (g_fCanChangeIcon && !g_fForceExit)
2680 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002683# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (fdDump)
2685 {
2686 time_t t;
2687
2688 time(&t);
2689 fputs(ctime(&t), fdDump);
2690 fclose(fdDump);
2691 }
2692 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695
2696 SetConsoleCursorInfo(g_hConOut, &g_cci);
2697 SetConsoleMode(g_hConIn, g_cmodein);
2698 SetConsoleMode(g_hConOut, g_cmodeout);
2699
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002700# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 exit(r);
2705}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002706#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002708 void
2709mch_init(void)
2710{
2711#ifdef VIMDLL
2712 if (gui.starting)
2713 mch_init_g();
2714 else
2715 mch_init_c();
2716#elif defined(FEAT_GUI_MSWIN)
2717 mch_init_g();
2718#else
2719 mch_init_c();
2720#endif
2721}
2722
2723 void
2724mch_exit(int r)
2725{
2726#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002727 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002728 mch_exit_g(r);
2729 else
2730 mch_exit_c(r);
2731#elif defined(FEAT_GUI_MSWIN)
2732 mch_exit_g(r);
2733#else
2734 mch_exit_c(r);
2735#endif
2736}
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738/*
2739 * Do we have an interactive window?
2740 */
2741 int
2742mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002743 int argc UNUSED,
2744 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 get_exe_name();
2747
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002748#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002749 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002751# ifdef VIMDLL
2752 if (gui.in_use)
2753 return OK;
2754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (isatty(1))
2756 return OK;
2757 return FAIL;
2758#endif
2759}
2760
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002761/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002762 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * When "len" is > 0, also expand short to long filenames.
2764 */
2765 void
2766fname_case(
2767 char_u *name,
2768 int len)
2769{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002770 int flen;
2771 WCHAR *p;
2772 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002774 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002775 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 return;
2777
2778 slash_adjust(name);
2779
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002780 p = enc_to_utf16(name, NULL);
2781 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002782 return;
2783
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002784 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002786 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002788 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002790 if (len > 0 || flen >= (int)STRLEN(q))
2791 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2792 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002795 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796}
2797
2798
2799/*
2800 * Insert user name in s[len].
2801 */
2802 int
2803mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002804 char_u *s,
2805 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002807 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002808 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002810 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002811 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002812 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002813
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002814 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002815 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002816 vim_strncpy(s, p, len - 1);
2817 vim_free(p);
2818 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002819 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 s[0] = NUL;
2822 return FAIL;
2823}
2824
2825
2826/*
2827 * Insert host name in s[len].
2828 */
2829 void
2830mch_get_host_name(
2831 char_u *s,
2832 int len)
2833{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002834 WCHAR wszHostName[256 + 1];
2835 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002837 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002838 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002839 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002840
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002841 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002842 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002843 vim_strncpy(s, p, len - 1);
2844 vim_free(p);
2845 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002846 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848}
2849
2850
2851/*
2852 * return process ID
2853 */
2854 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002855mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 return (long)GetCurrentProcessId();
2858}
2859
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002860/*
2861 * return TRUE if process "pid" is still running
2862 */
2863 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002864mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002865{
2866 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2867 DWORD status = 0;
2868 int ret = FALSE;
2869
2870 if (hProcess == NULL)
2871 return FALSE; // might not have access
2872 if (GetExitCodeProcess(hProcess, &status) )
2873 ret = status == STILL_ACTIVE;
2874 CloseHandle(hProcess);
2875 return ret;
2876}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878/*
2879 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2880 * Return OK for success, FAIL for failure.
2881 */
2882 int
2883mch_dirname(
2884 char_u *buf,
2885 int len)
2886{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002887 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 /*
2890 * Originally this was:
2891 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2892 * But the Win32s known bug list says that getcwd() doesn't work
2893 * so use the Win32 system call instead. <Negri>
2894 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002895 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002897 WCHAR wcbuf[_MAX_PATH + 1];
2898 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002900 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002902 p = utf16_to_enc(wcbuf, NULL);
2903 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002904 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002905 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002907 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002910 if (p == NULL)
2911 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002912
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002913 if (p != NULL)
2914 {
2915 vim_strncpy(buf, p, len - 1);
2916 vim_free(p);
2917 return OK;
2918 }
2919 }
2920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
2923/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002924 * Get file permissions for "name".
2925 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002928mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002930 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002931 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002933 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002934 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
2937
2938/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002939 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002940 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002941 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 */
2943 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002944mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002946 long n;
2947 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002948
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002949 p = enc_to_utf16(name, NULL);
2950 if (p == NULL)
2951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002953 n = _wchmod(p, perm);
2954 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002955 if (n == -1)
2956 return FAIL;
2957
2958 win32_set_archive(name);
2959
2960 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
2963/*
2964 * Set hidden flag for "name".
2965 */
2966 void
2967mch_hide(char_u *name)
2968{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002969 int attrs = win32_getattrs(name);
2970 if (attrs == -1)
2971 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002973 attrs |= FILE_ATTRIBUTE_HIDDEN;
2974 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975}
2976
2977/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002978 * Return TRUE if file "name" exists and is hidden.
2979 */
2980 int
2981mch_ishidden(char_u *name)
2982{
2983 int f = win32_getattrs(name);
2984
2985 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002986 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002987
2988 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
2989}
2990
2991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 * return TRUE if "name" is a directory
2993 * return FALSE if "name" is not a directory or upon error
2994 */
2995 int
2996mch_isdir(char_u *name)
2997{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002998 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003001 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3004}
3005
3006/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003007 * return TRUE if "name" is a directory, NOT a symlink to a directory
3008 * return FALSE if "name" is not a directory
3009 * return FALSE for error
3010 */
3011 int
3012mch_isrealdir(char_u *name)
3013{
3014 return mch_isdir(name) && !mch_is_symbolic_link(name);
3015}
3016
3017/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003018 * Create directory "name".
3019 * Return 0 on success, -1 on error.
3020 */
3021 int
3022mch_mkdir(char_u *name)
3023{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003024 WCHAR *p;
3025 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003026
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003027 p = enc_to_utf16(name, NULL);
3028 if (p == NULL)
3029 return -1;
3030 retval = _wmkdir(p);
3031 vim_free(p);
3032 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003033}
3034
3035/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003036 * Delete directory "name".
3037 * Return 0 on success, -1 on error.
3038 */
3039 int
3040mch_rmdir(char_u *name)
3041{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003042 WCHAR *p;
3043 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003044
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003045 p = enc_to_utf16(name, NULL);
3046 if (p == NULL)
3047 return -1;
3048 retval = _wrmdir(p);
3049 vim_free(p);
3050 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003051}
3052
3053/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003054 * Return TRUE if file "fname" has more than one link.
3055 */
3056 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003057mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003058{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003059 BY_HANDLE_FILE_INFORMATION info;
3060
3061 return win32_fileinfo(fname, &info) == FILEINFO_OK
3062 && info.nNumberOfLinks > 1;
3063}
3064
3065/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003066 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003067 */
3068 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003069mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003070{
3071 HANDLE hFind;
3072 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003073 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003074 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003075 WIN32_FIND_DATAW findDataW;
3076
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003077 wn = enc_to_utf16(name, NULL);
3078 if (wn == NULL)
3079 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003080
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003081 hFind = FindFirstFileW(wn, &findDataW);
3082 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003083 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003084 {
3085 fileFlags = findDataW.dwFileAttributes;
3086 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003087 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003088 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003089
3090 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003091 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3092 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003093 res = TRUE;
3094
3095 return res;
3096}
3097
3098/*
3099 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3100 * link.
3101 */
3102 int
3103mch_is_linked(char_u *fname)
3104{
3105 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3106 return TRUE;
3107 return FALSE;
3108}
3109
3110/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003111 * Get the by-handle-file-information for "fname".
3112 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003113 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003114 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3115 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3116 */
3117 int
3118win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3119{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003120 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003121 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003122 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003123
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003124 wn = enc_to_utf16(fname, NULL);
3125 if (wn == NULL)
3126 return FILEINFO_ENC_FAIL;
3127
3128 hFile = CreateFileW(wn, // file name
3129 GENERIC_READ, // access mode
3130 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3131 NULL, // security descriptor
3132 OPEN_EXISTING, // creation disposition
3133 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3134 NULL); // handle to template file
3135 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003136
3137 if (hFile != INVALID_HANDLE_VALUE)
3138 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003139 if (GetFileInformationByHandle(hFile, info) != 0)
3140 res = FILEINFO_OK;
3141 else
3142 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003143 CloseHandle(hFile);
3144 }
3145
Bram Moolenaar03f48552006-02-28 23:52:23 +00003146 return res;
3147}
3148
3149/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003150 * get file attributes for `name'
3151 * -1 : error
3152 * else FILE_ATTRIBUTE_* defined in winnt.h
3153 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003154 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003155win32_getattrs(char_u *name)
3156{
3157 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003158 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003159
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003160 p = enc_to_utf16(name, NULL);
3161 if (p == NULL)
3162 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003163
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003164 attr = GetFileAttributesW(p);
3165 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003166
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003167 return attr;
3168}
3169
3170/*
3171 * set file attributes for `name' to `attrs'
3172 *
3173 * return -1 for failure, 0 otherwise
3174 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003175 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003176win32_setattrs(char_u *name, int attrs)
3177{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003178 int res;
3179 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003180
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003181 p = enc_to_utf16(name, NULL);
3182 if (p == NULL)
3183 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003184
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003185 res = SetFileAttributesW(p, attrs);
3186 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003187
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003188 return res ? 0 : -1;
3189}
3190
3191/*
3192 * Set archive flag for "name".
3193 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003194 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003195win32_set_archive(char_u *name)
3196{
3197 int attrs = win32_getattrs(name);
3198 if (attrs == -1)
3199 return -1;
3200
3201 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3202 return win32_setattrs(name, attrs);
3203}
3204
3205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 * Return TRUE if file or directory "name" is writable (not readonly).
3207 * Strange semantics of Win32: a readonly directory is writable, but you can't
3208 * delete a file. Let's say this means it is writable.
3209 */
3210 int
3211mch_writable(char_u *name)
3212{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003213 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003215 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3216 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217}
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003220 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003221 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003222 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3223 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 */
3225 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003226mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
Bram Moolenaar86621892019-03-30 21:51:28 +01003228 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3229 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3230 // UTF-8.
3231 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003232 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003233 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003234
Bram Moolenaar86621892019-03-30 21:51:28 +01003235 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003236 return FALSE;
3237
Bram Moolenaar86621892019-03-30 21:51:28 +01003238 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003239 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003240 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003241 return TRUE;
3242
3243 /*
3244 * Loop over all extensions in $PATHEXT.
3245 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003246 p = mch_getenv("PATHEXT");
3247 if (p == NULL)
3248 p = (char_u *)".com;.exe;.bat;.cmd";
3249 saved = vim_strsave(p);
3250 if (saved == NULL)
3251 return FALSE;
3252 p = saved;
3253 while (*p)
3254 {
3255 char_u *tmp = vim_strchr(p, ';');
3256
3257 if (tmp != NULL)
3258 *tmp = NUL;
3259 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3260 && executable_exists((char *)name, path, use_path))
3261 {
3262 vim_free(saved);
3263 return TRUE;
3264 }
3265 if (tmp == NULL)
3266 break;
3267 p = tmp + 1;
3268 }
3269 vim_free(saved);
3270
Bram Moolenaar86621892019-03-30 21:51:28 +01003271 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003272 p = mch_getenv("PATHEXT");
3273 if (p == NULL)
3274 p = (char_u *)".com;.exe;.bat;.cmd";
3275 while (*p)
3276 {
3277 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3278 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003279 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003280 buf[len] = NUL;
3281 ++p;
3282 if (*p)
3283 ++p;
3284 }
3285 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003286 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003287 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003288 return TRUE;
3289 }
3290 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293/*
3294 * Check what "name" is:
3295 * NODE_NORMAL: file or directory (or doesn't exist)
3296 * NODE_WRITABLE: writable device, socket, fifo, etc.
3297 * NODE_OTHER: non-writable things
3298 */
3299 int
3300mch_nodetype(char_u *name)
3301{
3302 HANDLE hFile;
3303 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003304 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar0f873732019-12-05 20:28:46 +01003306 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3307 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3308 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003309 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3310 return NODE_WRITABLE;
3311
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003312 wn = enc_to_utf16(name, NULL);
3313 if (wn == NULL)
3314 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003315
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003316 hFile = CreateFileW(wn, // file name
3317 GENERIC_WRITE, // access mode
3318 0, // share mode
3319 NULL, // security descriptor
3320 OPEN_EXISTING, // creation disposition
3321 0, // file attributes
3322 NULL); // handle to template file
3323 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (hFile == INVALID_HANDLE_VALUE)
3325 return NODE_NORMAL;
3326
3327 type = GetFileType(hFile);
3328 CloseHandle(hFile);
3329 if (type == FILE_TYPE_CHAR)
3330 return NODE_WRITABLE;
3331 if (type == FILE_TYPE_DISK)
3332 return NODE_NORMAL;
3333 return NODE_OTHER;
3334}
3335
3336#ifdef HAVE_ACL
3337struct my_acl
3338{
3339 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3340 PSID pSidOwner;
3341 PSID pSidGroup;
3342 PACL pDacl;
3343 PACL pSacl;
3344};
3345#endif
3346
3347/*
3348 * Return a pointer to the ACL of file "fname" in allocated memory.
3349 * Return NULL if the ACL is not available for whatever reason.
3350 */
3351 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003352mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354#ifndef HAVE_ACL
3355 return (vim_acl_T)NULL;
3356#else
3357 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003358 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003360 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003361 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003363 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003364
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003365 wn = enc_to_utf16(fname, NULL);
3366 if (wn == NULL)
3367 return NULL;
3368
3369 // Try to retrieve the entire security descriptor.
3370 err = GetNamedSecurityInfoW(
3371 wn, // Abstract filename
3372 SE_FILE_OBJECT, // File Object
3373 OWNER_SECURITY_INFORMATION |
3374 GROUP_SECURITY_INFORMATION |
3375 DACL_SECURITY_INFORMATION |
3376 SACL_SECURITY_INFORMATION,
3377 &p->pSidOwner, // Ownership information.
3378 &p->pSidGroup, // Group membership.
3379 &p->pDacl, // Discretionary information.
3380 &p->pSacl, // For auditing purposes.
3381 &p->pSecurityDescriptor);
3382 if (err == ERROR_ACCESS_DENIED ||
3383 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003384 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003385 // Retrieve only DACL.
3386 (void)GetNamedSecurityInfoW(
3387 wn,
3388 SE_FILE_OBJECT,
3389 DACL_SECURITY_INFORMATION,
3390 NULL,
3391 NULL,
3392 &p->pDacl,
3393 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003394 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003395 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003396 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003397 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003398 mch_free_acl((vim_acl_T)p);
3399 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003401 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403
3404 return (vim_acl_T)p;
3405#endif
3406}
3407
Bram Moolenaar27515922013-06-29 15:36:26 +02003408#ifdef HAVE_ACL
3409/*
3410 * Check if "acl" contains inherited ACE.
3411 */
3412 static BOOL
3413is_acl_inherited(PACL acl)
3414{
3415 DWORD i;
3416 ACL_SIZE_INFORMATION acl_info;
3417 PACCESS_ALLOWED_ACE ace;
3418
3419 acl_info.AceCount = 0;
3420 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3421 for (i = 0; i < acl_info.AceCount; i++)
3422 {
3423 GetAce(acl, i, (LPVOID *)&ace);
3424 if (ace->Header.AceFlags & INHERITED_ACE)
3425 return TRUE;
3426 }
3427 return FALSE;
3428}
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431/*
3432 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3433 * Errors are ignored.
3434 * This must only be called with "acl" equal to what mch_get_acl() returned.
3435 */
3436 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003437mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439#ifdef HAVE_ACL
3440 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003441 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003442 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003444 if (p == NULL)
3445 return;
3446
3447 wn = enc_to_utf16(fname, NULL);
3448 if (wn == NULL)
3449 return;
3450
3451 // Set security flags
3452 if (p->pSidOwner)
3453 sec_info |= OWNER_SECURITY_INFORMATION;
3454 if (p->pSidGroup)
3455 sec_info |= GROUP_SECURITY_INFORMATION;
3456 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003457 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003458 sec_info |= DACL_SECURITY_INFORMATION;
3459 // Do not inherit its parent's DACL.
3460 // If the DACL is inherited, Cygwin permissions would be changed.
3461 if (!is_acl_inherited(p->pDacl))
3462 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003463 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003464 if (p->pSacl)
3465 sec_info |= SACL_SECURITY_INFORMATION;
3466
3467 (void)SetNamedSecurityInfoW(
3468 wn, // Abstract filename
3469 SE_FILE_OBJECT, // File Object
3470 sec_info,
3471 p->pSidOwner, // Ownership information.
3472 p->pSidGroup, // Group membership.
3473 p->pDacl, // Discretionary information.
3474 p->pSacl // For auditing purposes.
3475 );
3476 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477#endif
3478}
3479
3480 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003481mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483#ifdef HAVE_ACL
3484 struct my_acl *p = (struct my_acl *)acl;
3485
3486 if (p != NULL)
3487 {
3488 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3489 vim_free(p);
3490 }
3491#endif
3492}
3493
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003494#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496/*
3497 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3498 */
3499 static BOOL WINAPI
3500handler_routine(
3501 DWORD dwCtrlType)
3502{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003503 INPUT_RECORD ir;
3504 DWORD out;
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 switch (dwCtrlType)
3507 {
3508 case CTRL_C_EVENT:
3509 if (ctrl_c_interrupts)
3510 g_fCtrlCPressed = TRUE;
3511 return TRUE;
3512
3513 case CTRL_BREAK_EVENT:
3514 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003515 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003516 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003517 ir.EventType = KEY_EVENT;
3518 ir.Event.KeyEvent.bKeyDown = TRUE;
3519 ir.Event.KeyEvent.wRepeatCount = 1;
3520 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3521 ir.Event.KeyEvent.wVirtualScanCode = 0;
3522 ir.Event.KeyEvent.dwControlKeyState = 0;
3523 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3524 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return TRUE;
3526
Bram Moolenaar0f873732019-12-05 20:28:46 +01003527 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 case CTRL_CLOSE_EVENT:
3529 case CTRL_LOGOFF_EVENT:
3530 case CTRL_SHUTDOWN_EVENT:
3531 windgoto((int)Rows - 1, 0);
3532 g_fForceExit = TRUE;
3533
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003534 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 (dwCtrlType == CTRL_CLOSE_EVENT
3536 ? _("close")
3537 : dwCtrlType == CTRL_LOGOFF_EVENT
3538 ? _("logoff")
3539 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003540# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar0f873732019-12-05 20:28:46 +01003544 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar0f873732019-12-05 20:28:46 +01003546 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 default:
3549 return FALSE;
3550 }
3551}
3552
3553
3554/*
3555 * set the tty in (raw) ? "raw" : "cooked" mode
3556 */
3557 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003558mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 DWORD cmodein;
3561 DWORD cmodeout;
3562 BOOL bEnableHandler;
3563
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003564# ifdef VIMDLL
3565 if (gui.in_use)
3566 return;
3567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 GetConsoleMode(g_hConIn, &cmodein);
3569 GetConsoleMode(g_hConOut, &cmodeout);
3570 if (tmode == TMODE_RAW)
3571 {
3572 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3573 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (g_fMouseActive)
3575 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003576 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003577# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003578 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3579 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003580 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003581# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003582 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003583# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003584 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 bEnableHandler = TRUE;
3586 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003587 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3590 ENABLE_ECHO_INPUT);
3591 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3592 bEnableHandler = FALSE;
3593 }
3594 SetConsoleMode(g_hConIn, cmodein);
3595 SetConsoleMode(g_hConOut, cmodeout);
3596 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3597
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003598# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (fdDump)
3600 {
3601 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3602 tmode == TMODE_RAW ? "raw" :
3603 tmode == TMODE_COOK ? "cooked" : "normal",
3604 cmodein, cmodeout);
3605 fflush(fdDump);
3606 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608}
3609
3610
3611/*
3612 * Get the size of the current window in `Rows' and `Columns'
3613 * Return OK when size could be determined, FAIL otherwise.
3614 */
3615 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003616mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 CONSOLE_SCREEN_BUFFER_INFO csbi;
3619
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003620# ifdef VIMDLL
3621 if (gui.in_use)
3622 return OK;
3623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3625 {
3626 /*
3627 * For some reason, we are trying to get the screen dimensions
3628 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3629 * variables are really intended to mean the size of Vim screen
3630 * while in termcap mode.
3631 */
3632 Rows = g_cbTermcap.Info.dwSize.Y;
3633 Columns = g_cbTermcap.Info.dwSize.X;
3634 }
3635 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3636 {
3637 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3638 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3639 }
3640 else
3641 {
3642 Rows = 25;
3643 Columns = 80;
3644 }
3645 return OK;
3646}
3647
3648/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003649 * Resize console buffer to 'COORD'
3650 */
3651 static void
3652ResizeConBuf(
3653 HANDLE hConsole,
3654 COORD coordScreen)
3655{
3656 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3657 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003658# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003659 if (fdDump)
3660 {
3661 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3662 GetLastError());
3663 fflush(fdDump);
3664 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003665# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003666 }
3667}
3668
3669/*
3670 * Resize console window size to 'srWindowRect'
3671 */
3672 static void
3673ResizeWindow(
3674 HANDLE hConsole,
3675 SMALL_RECT srWindowRect)
3676{
3677 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3678 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003679# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003680 if (fdDump)
3681 {
3682 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3683 GetLastError());
3684 fflush(fdDump);
3685 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003686# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003687 }
3688}
3689
3690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 * Set a console window to `xSize' * `ySize'
3692 */
3693 static void
3694ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003695 HANDLE hConsole,
3696 int xSize,
3697 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003699 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3700 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003702 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003703 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003705# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (fdDump)
3707 {
3708 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3709 fflush(fdDump);
3710 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar0f873732019-12-05 20:28:46 +01003713 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 coordScreen = GetLargestConsoleWindowSize(hConsole);
3715
Bram Moolenaar0f873732019-12-05 20:28:46 +01003716 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3718 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3719 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3720
3721 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3722 {
3723 int sx, sy;
3724
3725 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3726 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3727 if (sy < ySize || sx < xSize)
3728 {
3729 /*
3730 * Increasing number of lines/columns, do buffer first.
3731 * Use the maximal size in x and y direction.
3732 */
3733 if (sy < ySize)
3734 coordScreen.Y = ySize;
3735 else
3736 coordScreen.Y = sy;
3737 if (sx < xSize)
3738 coordScreen.X = xSize;
3739 else
3740 coordScreen.X = sx;
3741 SetConsoleScreenBufferSize(hConsole, coordScreen);
3742 }
3743 }
3744
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003745 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 coordScreen.X = xSize;
3747 coordScreen.Y = ySize;
3748
Bram Moolenaar2551c032018-08-23 22:38:31 +02003749 // In the new console call API, only the first time in reverse order
3750 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003752 ResizeWindow(hConsole, srWindowRect);
3753 ResizeConBuf(hConsole, coordScreen);
3754 }
3755 else
3756 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003757 // Workaround for a Windows 10 bug
3758 cursor.X = srWindowRect.Left;
3759 cursor.Y = srWindowRect.Top;
3760 SetConsoleCursorPosition(hConsole, cursor);
3761
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003762 ResizeConBuf(hConsole, coordScreen);
3763 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003764 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766}
3767
3768
3769/*
3770 * Set the console window to `Rows' * `Columns'
3771 */
3772 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003773mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 COORD coordScreen;
3776
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003777# ifdef VIMDLL
3778 if (gui.in_use)
3779 return;
3780# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003781 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (suppress_winsize != 0)
3783 {
3784 suppress_winsize = 2;
3785 return;
3786 }
3787
3788 if (term_console)
3789 {
3790 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3791
Bram Moolenaar0f873732019-12-05 20:28:46 +01003792 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (Rows > coordScreen.Y)
3794 Rows = coordScreen.Y;
3795 if (Columns > coordScreen.X)
3796 Columns = coordScreen.X;
3797
3798 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3799 }
3800}
3801
3802/*
3803 * Rows and/or Columns has changed.
3804 */
3805 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003806mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003808# ifdef VIMDLL
3809 if (gui.in_use)
3810 return;
3811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3813}
3814
3815
3816/*
3817 * Called when started up, to set the winsize that was delayed.
3818 */
3819 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003820mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 if (suppress_winsize == 2)
3823 {
3824 suppress_winsize = 0;
3825 mch_set_shellsize();
3826 shell_resized();
3827 }
3828 suppress_winsize = 0;
3829}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003830#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003832 static BOOL
3833vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003834 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003835 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003836 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003837 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003838 PROCESS_INFORMATION *pi,
3839 LPVOID *env,
3840 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003841{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003842 BOOL ret = FALSE;
3843 WCHAR *wcmd, *wcwd = NULL;
3844
3845 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3846 if (wcmd == NULL)
3847 return FALSE;
3848 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003849 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003850 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3851 if (wcwd == NULL)
3852 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003853 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003854
3855 ret = CreateProcessW(
3856 NULL, // Executable name
3857 wcmd, // Command to execute
3858 NULL, // Process security attributes
3859 NULL, // Thread security attributes
3860 inherit_handles, // Inherit handles
3861 flags, // Creation flags
3862 env, // Environment
3863 wcwd, // Current directory
3864 (LPSTARTUPINFOW)si, // Startup information
3865 pi); // Process information
3866theend:
3867 vim_free(wcmd);
3868 vim_free(wcwd);
3869 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003873 static HINSTANCE
3874vim_shell_execute(
3875 char *cmd,
3876 INT n_show_cmd)
3877{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003878 HINSTANCE ret;
3879 WCHAR *wcmd;
3880
3881 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3882 if (wcmd == NULL)
3883 return (HINSTANCE) 0;
3884
3885 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3886 vim_free(wcmd);
3887 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003888}
3889
3890
Bram Moolenaar4f974752019-02-17 17:44:42 +01003891#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893/*
3894 * Specialised version of system() for Win32 GUI mode.
3895 * This version proceeds as follows:
3896 * 1. Create a console window for use by the subprocess
3897 * 2. Run the subprocess (it gets the allocated console by default)
3898 * 3. Wait for the subprocess to terminate and get its exit code
3899 * 4. Prompt the user to press a key to close the console window
3900 */
3901 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003902mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 STARTUPINFO si;
3905 PROCESS_INFORMATION pi;
3906 DWORD ret = 0;
3907 HWND hwnd = GetFocus();
3908
3909 si.cb = sizeof(si);
3910 si.lpReserved = NULL;
3911 si.lpDesktop = NULL;
3912 si.lpTitle = NULL;
3913 si.dwFlags = STARTF_USESHOWWINDOW;
3914 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003915 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003916 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003918 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003919 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 else
3921 si.wShowWindow = SW_SHOWNORMAL;
3922 si.cbReserved2 = 0;
3923 si.lpReserved2 = NULL;
3924
Bram Moolenaar0f873732019-12-05 20:28:46 +01003925 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003926 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003927 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3928 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar0f873732019-12-05 20:28:46 +01003930 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003932# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int delay = 1;
3934
Bram Moolenaar0f873732019-12-05 20:28:46 +01003935 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 for (;;)
3937 {
3938 MSG msg;
3939
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003940 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
3942 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003943 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003944 delay = 1;
3945 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3948 break;
3949
Bram Moolenaar0f873732019-12-05 20:28:46 +01003950 // We start waiting for a very short time and then increase it, so
3951 // that we respond quickly when the process is quick, and don't
3952 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (delay < 50)
3954 delay += 10;
3955 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003956# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
Bram Moolenaar0f873732019-12-05 20:28:46 +01003960 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 GetExitCodeProcess(pi.hProcess, &ret);
3962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaar0f873732019-12-05 20:28:46 +01003964 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 CloseHandle(pi.hThread);
3966 CloseHandle(pi.hProcess);
3967
Bram Moolenaar0f873732019-12-05 20:28:46 +01003968 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3970
3971 return ret;
3972}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003973
3974/*
3975 * Thread launched by the gui to send the current buffer data to the
3976 * process. This way avoid to hang up vim totally if the children
3977 * process take a long time to process the lines.
3978 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02003979 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003980sub_process_writer(LPVOID param)
3981{
3982 HANDLE g_hChildStd_IN_Wr = param;
3983 linenr_T lnum = curbuf->b_op_start.lnum;
3984 DWORD len = 0;
3985 DWORD l;
3986 char_u *lp = ml_get(lnum);
3987 char_u *s;
3988 int written = 0;
3989
3990 for (;;)
3991 {
3992 l = (DWORD)STRLEN(lp + written);
3993 if (l == 0)
3994 len = 0;
3995 else if (lp[written] == NL)
3996 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003997 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003998 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
3999 }
4000 else
4001 {
4002 s = vim_strchr(lp + written, NL);
4003 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4004 s == NULL ? l : (DWORD)(s - (lp + written)),
4005 &len, NULL);
4006 }
4007 if (len == (int)l)
4008 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009 // Finished a line, add a NL, unless this line should not have
4010 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004011 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004012 || (!curbuf->b_p_bin
4013 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004014 || (lnum != curbuf->b_no_eol_lnum
4015 && (lnum != curbuf->b_ml.ml_line_count
4016 || curbuf->b_p_eol)))
4017 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004018 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4019 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004020 }
4021
4022 ++lnum;
4023 if (lnum > curbuf->b_op_end.lnum)
4024 break;
4025
4026 lp = ml_get(lnum);
4027 written = 0;
4028 }
4029 else if (len > 0)
4030 written += len;
4031 }
4032
Bram Moolenaar0f873732019-12-05 20:28:46 +01004033 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004034 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004035 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004036}
4037
4038
Bram Moolenaar0f873732019-12-05 20:28:46 +01004039# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004040
4041/*
4042 * This function read from the children's stdout and write the
4043 * data on screen or in the buffer accordingly.
4044 */
4045 static void
4046dump_pipe(int options,
4047 HANDLE g_hChildStd_OUT_Rd,
4048 garray_T *ga,
4049 char_u buffer[],
4050 DWORD *buffer_off)
4051{
4052 DWORD availableBytes = 0;
4053 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004054 int ret;
4055 DWORD len;
4056 DWORD toRead;
4057 int repeatCount;
4058
Bram Moolenaar0f873732019-12-05 20:28:46 +01004059 // we query the pipe to see if there is any data to read
4060 // to avoid to perform a blocking read
4061 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4062 NULL, // optional buffer
4063 0, // buffer size
4064 NULL, // number of read bytes
4065 &availableBytes, // available bytes total
4066 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004067
4068 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004069 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004070 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004071 {
4072 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004073 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004074 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004075 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004076
Bram Moolenaar0f873732019-12-05 20:28:46 +01004077 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004078 if (len == 0)
4079 break;
4080
4081 availableBytes -= len;
4082
4083 if (options & SHELL_READ)
4084 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004085 // Do NUL -> NL translation, append NL separated
4086 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004087 for (i = 0; i < len; ++i)
4088 {
4089 if (buffer[i] == NL)
4090 append_ga_line(ga);
4091 else if (buffer[i] == NUL)
4092 ga_append(ga, NL);
4093 else
4094 ga_append(ga, buffer[i]);
4095 }
4096 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004097 else if (has_mbyte)
4098 {
4099 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004100 int c;
4101 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004102
4103 len += *buffer_off;
4104 buffer[len] = NUL;
4105
Bram Moolenaar0f873732019-12-05 20:28:46 +01004106 // Check if the last character in buffer[] is
4107 // incomplete, keep these bytes for the next
4108 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004109 for (p = buffer; p < buffer + len; p += l)
4110 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004111 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004112 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004113 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004114 else if (MB_BYTE2LEN(*p) != l)
4115 break;
4116 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004117 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004118 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004119 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004120 if (len >= 12)
4121 ++p;
4122 else
4123 {
4124 *buffer_off = len;
4125 return;
4126 }
4127 }
4128 c = *p;
4129 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004130 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004131 if (p < buffer + len)
4132 {
4133 *p = c;
4134 *buffer_off = (DWORD)((buffer + len) - p);
4135 mch_memmove(buffer, p, *buffer_off);
4136 return;
4137 }
4138 *buffer_off = 0;
4139 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004140 else
4141 {
4142 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004143 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004144 }
4145
4146 windgoto(msg_row, msg_col);
4147 cursor_on();
4148 out_flush();
4149 }
4150}
4151
4152/*
4153 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4154 * for communication and doesn't open any new window.
4155 */
4156 static int
4157mch_system_piped(char *cmd, int options)
4158{
4159 STARTUPINFO si;
4160 PROCESS_INFORMATION pi;
4161 DWORD ret = 0;
4162
4163 HANDLE g_hChildStd_IN_Rd = NULL;
4164 HANDLE g_hChildStd_IN_Wr = NULL;
4165 HANDLE g_hChildStd_OUT_Rd = NULL;
4166 HANDLE g_hChildStd_OUT_Wr = NULL;
4167
Bram Moolenaar0f873732019-12-05 20:28:46 +01004168 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004169 DWORD len;
4170
Bram Moolenaar0f873732019-12-05 20:28:46 +01004171 // buffer used to receive keys
4172 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4173 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004174
4175 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004176 int noread_cnt = 0;
4177 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004178 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004179 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004180 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004181
4182 SECURITY_ATTRIBUTES saAttr;
4183
Bram Moolenaar0f873732019-12-05 20:28:46 +01004184 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004185 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4186 saAttr.bInheritHandle = TRUE;
4187 saAttr.lpSecurityDescriptor = NULL;
4188
4189 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004190 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004191 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004192 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004193 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004194 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004195 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004196 {
4197 CloseHandle(g_hChildStd_IN_Rd);
4198 CloseHandle(g_hChildStd_IN_Wr);
4199 CloseHandle(g_hChildStd_OUT_Rd);
4200 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004202 }
4203
4204 si.cb = sizeof(si);
4205 si.lpReserved = NULL;
4206 si.lpDesktop = NULL;
4207 si.lpTitle = NULL;
4208 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4209
Bram Moolenaar0f873732019-12-05 20:28:46 +01004210 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004211 si.hStdError = g_hChildStd_OUT_Wr;
4212 si.hStdOutput = g_hChildStd_OUT_Wr;
4213 si.hStdInput = g_hChildStd_IN_Rd;
4214 si.wShowWindow = SW_HIDE;
4215 si.cbReserved2 = 0;
4216 si.lpReserved2 = NULL;
4217
4218 if (options & SHELL_READ)
4219 ga_init2(&ga, 1, BUFLEN);
4220
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004221 if (cmd != NULL)
4222 {
4223 p = (char *)vim_strsave((char_u *)cmd);
4224 if (p != NULL)
4225 unescape_shellxquote((char_u *)p, p_sxe);
4226 else
4227 p = cmd;
4228 }
4229
Bram Moolenaar0f873732019-12-05 20:28:46 +01004230 // Now, run the command.
4231 // About "Inherit handles" being TRUE: this command can be litigious,
4232 // handle inheritance was deactivated for pending temp file, but, if we
4233 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004234 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4235 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004236
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004237 if (p != cmd)
4238 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004239
Bram Moolenaar0f873732019-12-05 20:28:46 +01004240 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004241 CloseHandle(g_hChildStd_IN_Rd);
4242 CloseHandle(g_hChildStd_OUT_Wr);
4243
4244 if (options & SHELL_WRITE)
4245 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004246 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004247 _beginthreadex(NULL, // security attributes
4248 0, // default stack size
4249 sub_process_writer, // function to be executed
4250 g_hChildStd_IN_Wr, // parameter
4251 0, // creation flag, start immediately
4252 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004253 CloseHandle(thread);
4254 g_hChildStd_IN_Wr = NULL;
4255 }
4256
Bram Moolenaar0f873732019-12-05 20:28:46 +01004257 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004258 for (;;)
4259 {
4260 MSG msg;
4261
Bram Moolenaar175d0702013-12-11 18:36:33 +01004262 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004263 {
4264 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004265 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004266 }
4267
Bram Moolenaar0f873732019-12-05 20:28:46 +01004268 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004269 if ((options & (SHELL_READ|SHELL_WRITE))
4270# ifdef FEAT_GUI
4271 || gui.in_use
4272# endif
4273 )
4274 {
4275 len = 0;
4276 if (!(options & SHELL_EXPAND)
4277 && ((options &
4278 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4279 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4280# ifdef FEAT_GUI
4281 || gui.in_use
4282# endif
4283 )
4284 && (ta_len > 0 || noread_cnt > 4))
4285 {
4286 if (ta_len == 0)
4287 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 // Get extra characters when we don't have any. Reset the
4289 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004290 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004291 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4292 }
4293 if (ta_len > 0 || len > 0)
4294 {
4295 /*
4296 * For pipes: Check for CTRL-C: send interrupt signal to
4297 * child. Check for CTRL-D: EOF, close pipe to child.
4298 */
4299 if (len == 1 && cmd != NULL)
4300 {
4301 if (ta_buf[ta_len] == Ctrl_C)
4302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004303 // Learn what exit code is expected, for
4304 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004305 TerminateProcess(pi.hProcess, 9);
4306 }
4307 if (ta_buf[ta_len] == Ctrl_D)
4308 {
4309 CloseHandle(g_hChildStd_IN_Wr);
4310 g_hChildStd_IN_Wr = NULL;
4311 }
4312 }
4313
Bram Moolenaarf4140482020-02-15 23:06:45 +01004314 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004315
4316 /*
4317 * For pipes: echo the typed characters. For a pty this
4318 * does not seem to work.
4319 */
4320 for (i = ta_len; i < ta_len + len; ++i)
4321 {
4322 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4323 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004324 else if (has_mbyte)
4325 {
4326 int l = (*mb_ptr2len)(ta_buf + i);
4327
4328 msg_outtrans_len(ta_buf + i, l);
4329 i += l - 1;
4330 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004331 else
4332 msg_outtrans_len(ta_buf + i, 1);
4333 }
4334 windgoto(msg_row, msg_col);
4335 out_flush();
4336
4337 ta_len += len;
4338
4339 /*
4340 * Write the characters to the child, unless EOF has been
4341 * typed for pipes. Write one character at a time, to
4342 * avoid losing too much typeahead. When writing buffer
4343 * lines, drop the typed characters (only check for
4344 * CTRL-C).
4345 */
4346 if (options & SHELL_WRITE)
4347 ta_len = 0;
4348 else if (g_hChildStd_IN_Wr != NULL)
4349 {
4350 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4351 1, &len, NULL);
4352 // if we are typing in, we want to keep things reactive
4353 delay = 1;
4354 if (len > 0)
4355 {
4356 ta_len -= len;
4357 mch_memmove(ta_buf, ta_buf + len, ta_len);
4358 }
4359 }
4360 }
4361 }
4362 }
4363
4364 if (ta_len)
4365 ui_inchar_undo(ta_buf, ta_len);
4366
4367 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4368 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004369 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004370 break;
4371 }
4372
4373 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004374 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004375
Bram Moolenaar0f873732019-12-05 20:28:46 +01004376 // We start waiting for a very short time and then increase it, so
4377 // that we respond quickly when the process is quick, and don't
4378 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004379 if (delay < 50)
4380 delay += 10;
4381 }
4382
Bram Moolenaar0f873732019-12-05 20:28:46 +01004383 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004384 CloseHandle(g_hChildStd_OUT_Rd);
4385 if (g_hChildStd_IN_Wr != NULL)
4386 CloseHandle(g_hChildStd_IN_Wr);
4387
4388 WaitForSingleObject(pi.hProcess, INFINITE);
4389
Bram Moolenaar0f873732019-12-05 20:28:46 +01004390 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004391 GetExitCodeProcess(pi.hProcess, &ret);
4392
4393 if (options & SHELL_READ)
4394 {
4395 if (ga.ga_len > 0)
4396 {
4397 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004398 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004399 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4400 }
4401 else
4402 curbuf->b_no_eol_lnum = 0;
4403 ga_clear(&ga);
4404 }
4405
Bram Moolenaar0f873732019-12-05 20:28:46 +01004406 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004407 CloseHandle(pi.hThread);
4408 CloseHandle(pi.hProcess);
4409
4410 return ret;
4411}
4412
4413 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004414mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004415{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004417 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004418 return mch_system_piped(cmd, options);
4419 else
4420 return mch_system_classic(cmd, options);
4421}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004424#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004425 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004426mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004427{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004428 int ret;
4429 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004430 char_u *buf;
4431 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004432
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004433 // If the command starts and ends with double quotes, enclose the command
4434 // in parentheses.
4435 len = STRLEN(cmd);
4436 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4437 {
4438 len += 3;
4439 buf = alloc(len);
4440 if (buf == NULL)
4441 return -1;
4442 vim_snprintf((char *)buf, len, "(%s)", cmd);
4443 wcmd = enc_to_utf16(buf, NULL);
4444 free(buf);
4445 }
4446 else
4447 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4448
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004449 if (wcmd == NULL)
4450 return -1;
4451
4452 ret = _wsystem(wcmd);
4453 vim_free(wcmd);
4454 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457#endif
4458
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004459 static int
4460mch_system(char *cmd, int options)
4461{
4462#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004463 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004464 return mch_system_g(cmd, options);
4465 else
4466 return mch_system_c(cmd, options);
4467#elif defined(FEAT_GUI_MSWIN)
4468 return mch_system_g(cmd, options);
4469#else
4470 return mch_system_c(cmd, options);
4471#endif
4472}
4473
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004474#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4475/*
4476 * Use a terminal window to run a shell command in.
4477 */
4478 static int
4479mch_call_shell_terminal(
4480 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004482{
4483 jobopt_T opt;
4484 char_u *newcmd = NULL;
4485 typval_T argvar[2];
4486 long_u cmdlen;
4487 int retval = -1;
4488 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004489 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004490 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004491 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004492
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004493 if (cmd == NULL)
4494 cmdlen = STRLEN(p_sh) + 1;
4495 else
4496 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004497 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004498 if (newcmd == NULL)
4499 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004500 if (cmd == NULL)
4501 {
4502 STRCPY(newcmd, p_sh);
4503 ch_log(NULL, "starting terminal to run a shell");
4504 }
4505 else
4506 {
4507 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4508 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4509 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004510
4511 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004512
4513 argvar[0].v_type = VAR_STRING;
4514 argvar[0].vval.v_string = newcmd;
4515 argvar[1].v_type = VAR_UNKNOWN;
4516 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004517 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004518 {
4519 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004520 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004521 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004522
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004523 job = term_getjob(buf->b_term);
4524 ++job->jv_refcount;
4525
Bram Moolenaar0f873732019-12-05 20:28:46 +01004526 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004527 aucmd_prepbuf(&aco, buf);
4528
4529 clear_oparg(&oa);
4530 while (term_use_loop())
4531 {
4532 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4533 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004534 // If terminal_loop() returns OK we got a key that is handled
4535 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004536 if (terminal_loop(TRUE) == OK)
4537 normal_cmd(&oa, TRUE);
4538 }
4539 else
4540 normal_cmd(&oa, TRUE);
4541 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004542 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004543 ch_log(NULL, "system command finished");
4544
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004545 job_unref(job);
4546
Bram Moolenaar0f873732019-12-05 20:28:46 +01004547 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004548 aucmd_restbuf(&aco);
4549
4550 wait_return(TRUE);
4551 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4552
4553 vim_free(newcmd);
4554 return retval;
4555}
4556#endif
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558/*
4559 * Either execute a command by calling the shell or start a new shell
4560 */
4561 int
4562mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004563 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
4566 int x = 0;
4567 int tmode = cur_tmode;
4568#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004569 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004570
Bram Moolenaar0f873732019-12-05 20:28:46 +01004571 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004572 if (GetConsoleTitleW(szShellTitle,
4573 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004574 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004575 if (cmd == NULL)
4576 wcscat(szShellTitle, L" :sh");
4577 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004578 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004579 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004580
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004581 if (wn != NULL)
4582 {
4583 wcscat(szShellTitle, L" - !");
4584 if ((wcslen(szShellTitle) + wcslen(wn) <
4585 sizeof(szShellTitle)/sizeof(WCHAR)))
4586 wcscat(szShellTitle, wn);
4587 SetConsoleTitleW(szShellTitle);
4588 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004589 }
4590 }
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593
4594 out_flush();
4595
4596#ifdef MCH_WRITE_DUMP
4597 if (fdDump)
4598 {
4599 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4600 fflush(fdDump);
4601 }
4602#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004603#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004604 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004605 if (
4606# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004607 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004608# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004609 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004610 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4611 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004612 char_u *cmdbase = cmd;
4613
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004614 if (cmdbase != NULL)
4615 // Skip a leading quote and (.
4616 while (*cmdbase == '"' || *cmdbase == '(')
4617 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004618
4619 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004620 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4621 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004622 {
4623 // Use a terminal window to run the command in.
4624 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004625# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004626 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004627# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004628 return x;
4629 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004630 }
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 /*
4634 * Catch all deadly signals while running the external command, because a
4635 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4636 */
4637 signal(SIGINT, SIG_IGN);
4638#if defined(__GNUC__) && !defined(__MINGW32__)
4639 signal(SIGKILL, SIG_IGN);
4640#else
4641 signal(SIGBREAK, SIG_IGN);
4642#endif
4643 signal(SIGILL, SIG_IGN);
4644 signal(SIGFPE, SIG_IGN);
4645 signal(SIGSEGV, SIG_IGN);
4646 signal(SIGTERM, SIG_IGN);
4647 signal(SIGABRT, SIG_IGN);
4648
4649 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 if (cmd == NULL)
4653 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004654 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 else
4657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004658 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004659 char_u *newcmd = NULL;
4660 char_u *cmdbase = cmd;
4661 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004662
Bram Moolenaar0f873732019-12-05 20:28:46 +01004663 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004664 if (*cmdbase == '"' )
4665 ++cmdbase;
4666 if (*cmdbase == '(')
4667 ++cmdbase;
4668
Bram Moolenaar1c465442017-03-12 20:10:05 +01004669 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004670 {
4671 STARTUPINFO si;
4672 PROCESS_INFORMATION pi;
4673 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004674 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004675 char_u *p;
4676
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004677 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004678 si.cb = sizeof(si);
4679 si.lpReserved = NULL;
4680 si.lpDesktop = NULL;
4681 si.lpTitle = NULL;
4682 si.dwFlags = 0;
4683 si.cbReserved2 = 0;
4684 si.lpReserved2 = NULL;
4685
4686 cmdbase = skipwhite(cmdbase + 5);
4687 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004688 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004689 {
4690 cmdbase = skipwhite(cmdbase + 4);
4691 si.dwFlags = STARTF_USESHOWWINDOW;
4692 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004693 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004694 }
4695 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004696 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004697 {
4698 cmdbase = skipwhite(cmdbase + 2);
4699 flags = CREATE_NO_WINDOW;
4700 si.dwFlags = STARTF_USESTDHANDLES;
4701 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004702 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004703 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004704 NULL, // Security att.
4705 OPEN_EXISTING, // Open flags
4706 FILE_ATTRIBUTE_NORMAL, // File att.
4707 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004708 si.hStdOutput = si.hStdInput;
4709 si.hStdError = si.hStdInput;
4710 }
4711
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712 // Remove a trailing ", ) and )" if they have a match
4713 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004714 if (cmdbase > cmd)
4715 {
4716 p = cmdbase + STRLEN(cmdbase);
4717 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4718 *--p = NUL;
4719 if (p > cmdbase && p[-1] == ')'
4720 && (*cmd =='(' || cmd[1] == '('))
4721 *--p = NUL;
4722 }
4723
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004724 newcmd = cmdbase;
4725 unescape_shellxquote(cmdbase, p_sxe);
4726
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004727 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004728 * If creating new console, arguments are passed to the
4729 * 'cmd.exe' as-is. If it's not, arguments are not treated
4730 * correctly for current 'cmd.exe'. So unescape characters in
4731 * shellxescape except '|' for avoiding to be treated as
4732 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004733 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004734 if (flags != CREATE_NEW_CONSOLE)
4735 {
4736 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004737 char_u *cmd_shell = mch_getenv("COMSPEC");
4738
4739 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004740 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004741
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004742 subcmd = vim_strsave_escaped_ext(cmdbase,
4743 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004744 if (subcmd != NULL)
4745 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004746 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004747 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004748 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004749 if (newcmd != NULL)
4750 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004751 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004752 else
4753 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004754 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004755 }
4756 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004757
4758 /*
4759 * Now, start the command as a process, so that it doesn't
4760 * inherit our handles which causes unpleasant dangling swap
4761 * files if we exit before the spawned process
4762 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004763 if (vim_create_process((char *)newcmd, FALSE, flags,
4764 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004765 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004766 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4767 > (HINSTANCE)32)
4768 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004769 else
4770 {
4771 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004772#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004773# ifdef VIMDLL
4774 if (gui.in_use)
4775# endif
4776 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004777#endif
4778 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004779
4780 if (newcmd != cmdbase)
4781 vim_free(newcmd);
4782
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004783 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004784 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004785 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004786 CloseHandle(si.hStdInput);
4787 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004788 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004789 CloseHandle(pi.hThread);
4790 CloseHandle(pi.hProcess);
4791 }
4792 else
4793 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004794 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004795#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004796 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004797 (!s_dont_use_vimrun && p_stmp ?
4798 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4799 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004801 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004802
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004803 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004804 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004806#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004807 if (
4808# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004809 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004810# endif
4811 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004813 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4814 "External commands will not pause after completion.\n"
4815 "See :help win32-vimrun for more information.");
4816 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004817 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4818 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004819
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004820 if (wmsg != NULL && wtitle != NULL)
4821 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4822 vim_free(wmsg);
4823 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 need_vimrun_warning = FALSE;
4825 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004826 if (
4827# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004828 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004829# endif
4830 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004831 // Use vimrun to execute the command. It opens a console
4832 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004833 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 vimrun_path,
4835 (msg_silent != 0 || (options & SHELL_DOOUT))
4836 ? "-s " : "",
4837 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004838 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004839# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004840 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004841# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004842 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004843 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004844 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4845 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004848 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004849 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004851 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855
4856 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004857 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
Bram Moolenaar0f873732019-12-05 20:28:46 +01004859 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004861#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004862 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004863 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
4865 )
4866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 msg_putchar('\n');
4869 }
4870#ifdef FEAT_TITLE
4871 resettitle();
4872#endif
4873
4874 signal(SIGINT, SIG_DFL);
4875#if defined(__GNUC__) && !defined(__MINGW32__)
4876 signal(SIGKILL, SIG_DFL);
4877#else
4878 signal(SIGBREAK, SIG_DFL);
4879#endif
4880 signal(SIGILL, SIG_DFL);
4881 signal(SIGFPE, SIG_DFL);
4882 signal(SIGSEGV, SIG_DFL);
4883 signal(SIGTERM, SIG_DFL);
4884 signal(SIGABRT, SIG_DFL);
4885
4886 return x;
4887}
4888
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004889#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004890 static HANDLE
4891job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004892 char_u *fname,
4893 DWORD dwDesiredAccess,
4894 DWORD dwShareMode,
4895 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4896 DWORD dwCreationDisposition,
4897 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004898{
4899 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004900 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004901
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004902 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004903 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004904 return INVALID_HANDLE_VALUE;
4905
4906 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4907 lpSecurityAttributes, dwCreationDisposition,
4908 dwFlagsAndAttributes, NULL);
4909 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004910 return h;
4911}
4912
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004913/*
4914 * Turn the dictionary "env" into a NUL separated list that can be used as the
4915 * environment argument of vim_create_process().
4916 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004917 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004918win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004919{
4920 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004921 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004922 LPVOID base = GetEnvironmentStringsW();
4923
Bram Moolenaar0f873732019-12-05 20:28:46 +01004924 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004925 if (ga_grow(gap, 1) == FAIL)
4926 return;
4927
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004928 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004929 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004930 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004931 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004932 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004933 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004934 typval_T *item = &dict_lookup(hi)->di_tv;
4935 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004936 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004937 --todo;
4938 if (wkey != NULL && wval != NULL)
4939 {
4940 size_t n;
4941 size_t lkey = wcslen(wkey);
4942 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004943
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004944 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4945 continue;
4946 for (n = 0; n < lkey; n++)
4947 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4948 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4949 for (n = 0; n < lval; n++)
4950 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4951 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4952 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004953 vim_free(wkey);
4954 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004955 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004956 }
4957 }
4958
Bram Moolenaar355757a2020-02-10 22:06:32 +01004959 if (base)
4960 {
4961 WCHAR *p = (WCHAR*) base;
4962
4963 // for last \0
4964 if (ga_grow(gap, 1) == FAIL)
4965 return;
4966
4967 while (*p != 0 || *(p + 1) != 0)
4968 {
4969 if (ga_grow(gap, 1) == OK)
4970 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4971 p++;
4972 }
4973 FreeEnvironmentStrings(base);
4974 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4975 }
4976
Bram Moolenaar493359e2018-06-12 20:25:52 +02004977# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004978 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02004979# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004980 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004981 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004982# endif
4983# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004984 char_u *version = get_vim_var_str(VV_VERSION);
4985 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004986# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004987 // size of "VIM_SERVERNAME=" and value,
4988 // plus "VIM_TERMINAL=" and value,
4989 // plus two terminating NULs
4990 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02004991# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004992 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02004993# endif
4994# ifdef FEAT_TERMINAL
4995 + 13 + version_len + 2
4996# endif
4997 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004998
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004999 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005000 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005001# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005002 for (n = 0; n < 15; n++)
5003 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5004 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005005 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005006 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5007 (WCHAR)servername[n];
5008 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005009# endif
5010# ifdef FEAT_TERMINAL
5011 if (is_terminal)
5012 {
5013 for (n = 0; n < 13; n++)
5014 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5015 (WCHAR)"VIM_TERMINAL="[n];
5016 for (n = 0; n < version_len; n++)
5017 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5018 (WCHAR)version[n];
5019 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5020 }
5021# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005022 }
5023 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005024# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005025}
5026
Bram Moolenaarb091f302019-01-19 14:37:00 +01005027/*
5028 * Create a pair of pipes.
5029 * Return TRUE for success, FALSE for failure.
5030 */
5031 static BOOL
5032create_pipe_pair(HANDLE handles[2])
5033{
5034 static LONG s;
5035 char name[64];
5036 SECURITY_ATTRIBUTES sa;
5037
5038 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5039 GetCurrentProcessId(),
5040 InterlockedIncrement(&s));
5041
5042 // Create named pipe. Max size of named pipe is 65535.
5043 handles[1] = CreateNamedPipe(
5044 name,
5045 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5046 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005047 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005048
5049 if (handles[1] == INVALID_HANDLE_VALUE)
5050 return FALSE;
5051
5052 sa.nLength = sizeof(sa);
5053 sa.bInheritHandle = TRUE;
5054 sa.lpSecurityDescriptor = NULL;
5055
5056 handles[0] = CreateFile(name,
5057 FILE_GENERIC_READ,
5058 FILE_SHARE_READ, &sa,
5059 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5060
5061 if (handles[0] == INVALID_HANDLE_VALUE)
5062 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005063 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005064 return FALSE;
5065 }
5066
5067 return TRUE;
5068}
5069
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005070 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005071mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005072{
5073 STARTUPINFO si;
5074 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005075 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005076 SECURITY_ATTRIBUTES saAttr;
5077 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005078 HANDLE ifd[2];
5079 HANDLE ofd[2];
5080 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005081 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005082
5083 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5084 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5085 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5086 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5087 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5088 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5089 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5090
5091 if (use_out_for_err && use_null_for_out)
5092 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005093
5094 ifd[0] = INVALID_HANDLE_VALUE;
5095 ifd[1] = INVALID_HANDLE_VALUE;
5096 ofd[0] = INVALID_HANDLE_VALUE;
5097 ofd[1] = INVALID_HANDLE_VALUE;
5098 efd[0] = INVALID_HANDLE_VALUE;
5099 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005100 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005101
Bram Moolenaar14207f42016-10-27 21:13:10 +02005102 jo = CreateJobObject(NULL, NULL);
5103 if (jo == NULL)
5104 {
5105 job->jv_status = JOB_FAILED;
5106 goto failed;
5107 }
5108
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005109 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005110 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005111
Bram Moolenaar76467df2016-02-12 19:30:26 +01005112 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005113 ZeroMemory(&si, sizeof(si));
5114 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005115 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005116 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005117
Bram Moolenaard8070362016-02-15 21:56:54 +01005118 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5119 saAttr.bInheritHandle = TRUE;
5120 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005121
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005122 if (use_file_for_in)
5123 {
5124 char_u *fname = options->jo_io_name[PART_IN];
5125
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005126 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5127 FILE_SHARE_READ | FILE_SHARE_WRITE,
5128 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5129 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005131 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005132 goto failed;
5133 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005134 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005135 else if (!use_null_for_in
5136 && (!create_pipe_pair(ifd)
5137 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005138 goto failed;
5139
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005140 if (use_file_for_out)
5141 {
5142 char_u *fname = options->jo_io_name[PART_OUT];
5143
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005144 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5145 FILE_SHARE_READ | FILE_SHARE_WRITE,
5146 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5147 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005149 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005150 goto failed;
5151 }
5152 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005153 else if (!use_null_for_out &&
5154 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005155 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005156 goto failed;
5157
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005158 if (use_file_for_err)
5159 {
5160 char_u *fname = options->jo_io_name[PART_ERR];
5161
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005162 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5163 FILE_SHARE_READ | FILE_SHARE_WRITE,
5164 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5165 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005167 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005168 goto failed;
5169 }
5170 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005171 else if (!use_out_for_err && !use_null_for_err &&
5172 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005173 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005174 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005175
Bram Moolenaard8070362016-02-15 21:56:54 +01005176 si.dwFlags |= STARTF_USESTDHANDLES;
5177 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005178 si.hStdOutput = ofd[1];
5179 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5180
5181 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5182 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005183 if (options->jo_set & JO_CHANNEL)
5184 {
5185 channel = options->jo_channel;
5186 if (channel != NULL)
5187 ++channel->ch_refcount;
5188 }
5189 else
5190 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005191 if (channel == NULL)
5192 goto failed;
5193 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005194
5195 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005196 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005197 CREATE_DEFAULT_ERROR_MODE |
5198 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005199 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005200 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005201 &si, &pi,
5202 ga.ga_data,
5203 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005204 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005205 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005206 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005207 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005208 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005209
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005210 ga_clear(&ga);
5211
Bram Moolenaar14207f42016-10-27 21:13:10 +02005212 if (!AssignProcessToJobObject(jo, pi.hProcess))
5213 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005214 // if failing, switch the way to terminate
5215 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005216 CloseHandle(jo);
5217 jo = NULL;
5218 }
5219 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005220 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005221 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005222 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005223 job->jv_status = JOB_STARTED;
5224
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005225 CloseHandle(ifd[0]);
5226 CloseHandle(ofd[1]);
5227 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005228 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005229
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005230 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005231 if (channel != NULL)
5232 {
5233 channel_set_pipes(channel,
5234 use_file_for_in || use_null_for_in
5235 ? INVALID_FD : (sock_T)ifd[1],
5236 use_file_for_out || use_null_for_out
5237 ? INVALID_FD : (sock_T)ofd[0],
5238 use_out_for_err || use_file_for_err || use_null_for_err
5239 ? INVALID_FD : (sock_T)efd[0]);
5240 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005241 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005242 return;
5243
5244failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005245 CloseHandle(ifd[0]);
5246 CloseHandle(ofd[0]);
5247 CloseHandle(efd[0]);
5248 CloseHandle(ifd[1]);
5249 CloseHandle(ofd[1]);
5250 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005251 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005252 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005253}
5254
5255 char *
5256mch_job_status(job_T *job)
5257{
5258 DWORD dwExitCode = 0;
5259
Bram Moolenaar76467df2016-02-12 19:30:26 +01005260 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5261 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005262 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005263 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005264 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005265 {
5266 ch_log(job->jv_channel, "Job ended");
5267 job->jv_status = JOB_ENDED;
5268 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005269 return "dead";
5270 }
5271 return "run";
5272}
5273
Bram Moolenaar97792de2016-10-15 18:36:49 +02005274 job_T *
5275mch_detect_ended_job(job_T *job_list)
5276{
5277 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5278 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5279 job_T *job = job_list;
5280
5281 while (job != NULL)
5282 {
5283 DWORD n;
5284 DWORD result;
5285
5286 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5287 && job != NULL; job = job->jv_next)
5288 {
5289 if (job->jv_status == JOB_STARTED)
5290 {
5291 jobHandles[n] = job->jv_proc_info.hProcess;
5292 jobArray[n] = job;
5293 ++n;
5294 }
5295 }
5296 if (n == 0)
5297 continue;
5298 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5299 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5300 {
5301 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5302
5303 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5304 return wait_job;
5305 }
5306 }
5307 return NULL;
5308}
5309
Bram Moolenaarfb630902016-10-29 14:55:00 +02005310 static BOOL
5311terminate_all(HANDLE process, int code)
5312{
5313 PROCESSENTRY32 pe;
5314 HANDLE h = INVALID_HANDLE_VALUE;
5315 DWORD pid = GetProcessId(process);
5316
5317 if (pid != 0)
5318 {
5319 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5320 if (h != INVALID_HANDLE_VALUE)
5321 {
5322 pe.dwSize = sizeof(PROCESSENTRY32);
5323 if (!Process32First(h, &pe))
5324 goto theend;
5325
5326 do
5327 {
5328 if (pe.th32ParentProcessID == pid)
5329 {
5330 HANDLE ph = OpenProcess(
5331 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5332 if (ph != NULL)
5333 {
5334 terminate_all(ph, code);
5335 CloseHandle(ph);
5336 }
5337 }
5338 } while (Process32Next(h, &pe));
5339
5340 CloseHandle(h);
5341 }
5342 }
5343
5344theend:
5345 return TerminateProcess(process, code);
5346}
5347
5348/*
5349 * Send a (deadly) signal to "job".
5350 * Return FAIL if it didn't work.
5351 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005352 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005353mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005354{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005355 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005356
Bram Moolenaar923d9262016-02-25 20:56:01 +01005357 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005359 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005360 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005361 {
5362 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5363 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005364 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005365 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005366 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005367 }
5368
5369 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5370 return FAIL;
5371 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005372 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5373 job->jv_proc_info.dwProcessId)
5374 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005375 FreeConsole();
5376 return ret;
5377}
5378
5379/*
5380 * Clear the data related to "job".
5381 */
5382 void
5383mch_clear_job(job_T *job)
5384{
5385 if (job->jv_status != JOB_FAILED)
5386 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005387 if (job->jv_job_object != NULL)
5388 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005389 CloseHandle(job->jv_proc_info.hProcess);
5390 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005391}
5392#endif
5393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005395#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397/*
5398 * Start termcap mode
5399 */
5400 static void
5401termcap_mode_start(void)
5402{
5403 DWORD cmodein;
5404
5405 if (g_fTermcapMode)
5406 return;
5407
5408 SaveConsoleBuffer(&g_cbNonTermcap);
5409
5410 if (g_cbTermcap.IsValid)
5411 {
5412 /*
5413 * We've been in termcap mode before. Restore certain screen
5414 * characteristics, including the buffer size and the window
5415 * size. Since we will be redrawing the screen, we don't need
5416 * to restore the actual contents of the buffer.
5417 */
5418 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005419 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5421 Rows = g_cbTermcap.Info.dwSize.Y;
5422 Columns = g_cbTermcap.Info.dwSize.X;
5423 }
5424 else
5425 {
5426 /*
5427 * This is our first time entering termcap mode. Clear the console
5428 * screen buffer, and resize the buffer to match the current window
5429 * size. We will use this as the size of our editing environment.
5430 */
5431 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005432 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5434 }
5435
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005436# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (g_fMouseActive)
5442 cmodein |= ENABLE_MOUSE_INPUT;
5443 else
5444 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 cmodein |= ENABLE_WINDOW_INPUT;
5446 SetConsoleMode(g_hConIn, cmodein);
5447
5448 redraw_later_clear();
5449 g_fTermcapMode = TRUE;
5450}
5451
5452
5453/*
5454 * End termcap mode
5455 */
5456 static void
5457termcap_mode_end(void)
5458{
5459 DWORD cmodein;
5460 ConsoleBuffer *cb;
5461 COORD coord;
5462 DWORD dwDummy;
5463
5464 if (!g_fTermcapMode)
5465 return;
5466
5467 SaveConsoleBuffer(&g_cbTermcap);
5468
5469 GetConsoleMode(g_hConIn, &cmodein);
5470 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5471 SetConsoleMode(g_hConIn, cmodein);
5472
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005473# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005474 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005475# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005479 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 SetConsoleCursorInfo(g_hConOut, &g_cci);
5481
5482 if (p_rs || exiting)
5483 {
5484 /*
5485 * Clear anything that happens to be on the current line.
5486 */
5487 coord.X = 0;
5488 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5489 FillConsoleOutputCharacter(g_hConOut, ' ',
5490 cb->Info.dwSize.X, coord, &dwDummy);
5491 /*
5492 * The following is just for aesthetics. If we are exiting without
5493 * restoring the screen, then we want to have a prompt string
5494 * appear at the bottom line. However, the command interpreter
5495 * seems to always advance the cursor one line before displaying
5496 * the prompt string, which causes the screen to scroll. To
5497 * counter this, move the cursor up one line before exiting.
5498 */
5499 if (exiting && !p_rs)
5500 coord.Y--;
5501 /*
5502 * Position the cursor at the leftmost column of the desired row.
5503 */
5504 SetConsoleCursorPosition(g_hConOut, coord);
5505 }
5506
5507 g_fTermcapMode = FALSE;
5508}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005509#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
5511
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005512#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 void
5514mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005515 char_u *s UNUSED,
5516 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005518 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519}
5520
5521#else
5522
5523/*
5524 * clear `n' chars, starting from `coord'
5525 */
5526 static void
5527clear_chars(
5528 COORD coord,
5529 DWORD n)
5530{
5531 DWORD dwDummy;
5532
5533 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005534
5535 if (!USE_VTP)
5536 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5537 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005538 {
5539 set_console_color_rgb();
5540 gotoxy(coord.X + 1, coord.Y + 1);
5541 vtp_printf("\033[%dX", n);
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544
5545
5546/*
5547 * Clear the screen
5548 */
5549 static void
5550clear_screen(void)
5551{
5552 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005553
5554 if (!USE_VTP)
5555 clear_chars(g_coord, Rows * Columns);
5556 else
5557 {
5558 set_console_color_rgb();
5559 gotoxy(1, 1);
5560 vtp_printf("\033[2J");
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564
5565/*
5566 * Clear to end of display
5567 */
5568 static void
5569clear_to_end_of_display(void)
5570{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005571 COORD save = g_coord;
5572
5573 if (!USE_VTP)
5574 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005576 else
5577 {
5578 set_console_color_rgb();
5579 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5580 vtp_printf("\033[0J");
5581
5582 gotoxy(save.X + 1, save.Y + 1);
5583 g_coord = save;
5584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587
5588/*
5589 * Clear to end of line
5590 */
5591 static void
5592clear_to_end_of_line(void)
5593{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005594 COORD save = g_coord;
5595
5596 if (!USE_VTP)
5597 clear_chars(g_coord, Columns - g_coord.X);
5598 else
5599 {
5600 set_console_color_rgb();
5601 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5602 vtp_printf("\033[0K");
5603
5604 gotoxy(save.X + 1, save.Y + 1);
5605 g_coord = save;
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609
5610/*
5611 * Scroll the scroll region up by `cLines' lines
5612 */
5613 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005614scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 COORD oldcoord = g_coord;
5617
5618 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5619 delete_lines(cLines);
5620
5621 g_coord = oldcoord;
5622}
5623
5624
5625/*
5626 * Set the scroll region
5627 */
5628 static void
5629set_scroll_region(
5630 unsigned left,
5631 unsigned top,
5632 unsigned right,
5633 unsigned bottom)
5634{
5635 if (left >= right
5636 || top >= bottom
5637 || right > (unsigned) Columns - 1
5638 || bottom > (unsigned) Rows - 1)
5639 return;
5640
5641 g_srScrollRegion.Left = left;
5642 g_srScrollRegion.Top = top;
5643 g_srScrollRegion.Right = right;
5644 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005645}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005646
Bram Moolenaar6982f422019-02-16 16:48:01 +01005647 static void
5648set_scroll_region_tb(
5649 unsigned top,
5650 unsigned bottom)
5651{
5652 if (top >= bottom || bottom > (unsigned)Rows - 1)
5653 return;
5654
5655 g_srScrollRegion.Top = top;
5656 g_srScrollRegion.Bottom = bottom;
5657}
5658
5659 static void
5660set_scroll_region_lr(
5661 unsigned left,
5662 unsigned right)
5663{
5664 if (left >= right || right > (unsigned)Columns - 1)
5665 return;
5666
5667 g_srScrollRegion.Left = left;
5668 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670
5671
5672/*
5673 * Insert `cLines' lines at the current cursor position
5674 */
5675 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005676insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005678 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 COORD dest;
5680 CHAR_INFO fill;
5681
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005682 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5683
Bram Moolenaar6982f422019-02-16 16:48:01 +01005684 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 dest.Y = g_coord.Y + cLines;
5686
Bram Moolenaar6982f422019-02-16 16:48:01 +01005687 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 source.Top = g_coord.Y;
5689 source.Right = g_srScrollRegion.Right;
5690 source.Bottom = g_srScrollRegion.Bottom - cLines;
5691
Bram Moolenaar6982f422019-02-16 16:48:01 +01005692 clip.Left = g_srScrollRegion.Left;
5693 clip.Top = g_coord.Y;
5694 clip.Right = g_srScrollRegion.Right;
5695 clip.Bottom = g_srScrollRegion.Bottom;
5696
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005697 fill.Char.AsciiChar = ' ';
5698 if (!USE_VTP)
5699 fill.Attributes = g_attrCurrent;
5700 else
5701 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005703 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005704
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005705 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5706
Bram Moolenaar6982f422019-02-16 16:48:01 +01005707 // Here we have to deal with a win32 console flake: If the scroll
5708 // region looks like abc and we scroll c to a and fill with d we get
5709 // cbd... if we scroll block c one line at a time to a, we get cdd...
5710 // vim expects cdd consistently... So we have to deal with that
5711 // here... (this also occurs scrolling the same way in the other
5712 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 if (source.Bottom < dest.Y)
5715 {
5716 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005717 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaar6982f422019-02-16 16:48:01 +01005719 coord.X = source.Left;
5720 for (i = clip.Top; i < dest.Y; ++i)
5721 {
5722 coord.Y = i;
5723 clear_chars(coord, source.Right - source.Left + 1);
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726}
5727
5728
5729/*
5730 * Delete `cLines' lines at the current cursor position
5731 */
5732 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005733delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005735 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 COORD dest;
5737 CHAR_INFO fill;
5738 int nb;
5739
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005740 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5741
Bram Moolenaar6982f422019-02-16 16:48:01 +01005742 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 dest.Y = g_coord.Y;
5744
Bram Moolenaar6982f422019-02-16 16:48:01 +01005745 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 source.Top = g_coord.Y + cLines;
5747 source.Right = g_srScrollRegion.Right;
5748 source.Bottom = g_srScrollRegion.Bottom;
5749
Bram Moolenaar6982f422019-02-16 16:48:01 +01005750 clip.Left = g_srScrollRegion.Left;
5751 clip.Top = g_coord.Y;
5752 clip.Right = g_srScrollRegion.Right;
5753 clip.Bottom = g_srScrollRegion.Bottom;
5754
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005755 fill.Char.AsciiChar = ' ';
5756 if (!USE_VTP)
5757 fill.Attributes = g_attrCurrent;
5758 else
5759 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005761 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005762
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005763 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5764
Bram Moolenaar6982f422019-02-16 16:48:01 +01005765 // Here we have to deal with a win32 console flake; See insert_lines()
5766 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 nb = dest.Y + (source.Bottom - source.Top) + 1;
5769
5770 if (nb < source.Top)
5771 {
5772 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005773 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
Bram Moolenaar6982f422019-02-16 16:48:01 +01005775 coord.X = source.Left;
5776 for (i = nb; i < clip.Bottom; ++i)
5777 {
5778 coord.Y = i;
5779 clear_chars(coord, source.Right - source.Left + 1);
5780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782}
5783
5784
5785/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005786 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 */
5788 static void
5789gotoxy(
5790 unsigned x,
5791 unsigned y)
5792{
5793 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5794 return;
5795
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005796 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005797 {
5798 // external cursor coords are 1-based; internal are 0-based
5799 g_coord.X = x - 1;
5800 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005801 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005802 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005803 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005804 {
5805 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005806 // destruction. Insider build bug. Always enabled because it's cheap
5807 // and avoids mistakes with recognizing the build.
5808 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005809
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005810 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005811
5812 g_coord.X = x - 1;
5813 g_coord.Y = y - 1;
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
5817
5818/*
5819 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005820 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 */
5822 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005823textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005825 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827 SetConsoleTextAttribute(g_hConOut, wAttr);
5828}
5829
5830
5831 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005832textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005834 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005836 if (!USE_VTP)
5837 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5838 else
5839 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841
5842
5843 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005844textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005846 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005848 if (!USE_VTP)
5849 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5850 else
5851 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852}
5853
5854
5855/*
5856 * restore the default text attribute (whatever we started with)
5857 */
5858 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005859normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005861 if (!USE_VTP)
5862 textattr(g_attrDefault);
5863 else
5864 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865}
5866
5867
5868static WORD g_attrPreStandout = 0;
5869
5870/*
5871 * Make the text standout, by brightening it
5872 */
5873 static void
5874standout(void)
5875{
5876 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5879}
5880
5881
5882/*
5883 * Turn off standout mode
5884 */
5885 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005886standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005890
5891 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892}
5893
5894
5895/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005896 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 */
5898 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005899mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
5902 int n;
5903
5904 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5905 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005906 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005907# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005908 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005909# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005910 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
5912 p = T_ME + 2;
5913 n = getdigits(&p);
5914 if (*p == 'm' && n > 0)
5915 {
5916 cterm_normal_fg_color = (n & 0xf) + 1;
5917 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5918 }
5919 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005920# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005921 cterm_normal_fg_gui_color = INVALCOLOR;
5922 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924}
5925
5926
5927/*
5928 * visual bell: flash the screen
5929 */
5930 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005931visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 COORD coordOrigin = {0, 0};
5934 WORD attrFlash = ~g_attrCurrent & 0xff;
5935
5936 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005937 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
5939 if (oldattrs == NULL)
5940 return;
5941 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5942 coordOrigin, &dwDummy);
5943 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5944 coordOrigin, &dwDummy);
5945
Bram Moolenaar0f873732019-12-05 20:28:46 +01005946 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005947 if (!USE_VTP)
5948 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 coordOrigin, &dwDummy);
5950 vim_free(oldattrs);
5951}
5952
5953
5954/*
5955 * Make the cursor visible or invisible
5956 */
5957 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005958cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005961# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964}
5965
5966
5967/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005968 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005969 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005971 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005973 char_u *pchBuf,
5974 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005976 COORD coord = g_coord;
5977 DWORD written;
5978 DWORD n, cchwritten, cells;
5979 static WCHAR *unicodebuf = NULL;
5980 static int unibuflen = 0;
5981 int length;
5982 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005984 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
5985 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005986 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005987 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005988 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005989 unibuflen = length;
5990 }
5991 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
5992 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005994 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005995
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005996 if (!USE_VTP)
5997 {
5998 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
5999 coord, &written);
6000 // When writing fails or didn't write a single character, pretend one
6001 // character was written, otherwise we get stuck.
6002 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6003 coord, &cchwritten) == 0
6004 || cchwritten == 0 || cchwritten == (DWORD)-1)
6005 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006006 }
6007 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006008 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006009 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6010 NULL) == 0 || cchwritten == 0)
6011 cchwritten = 1;
6012 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006013
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006014 if (cchwritten == length)
6015 {
6016 written = cbToWrite;
6017 g_coord.X += (SHORT)cells;
6018 }
6019 else
6020 {
6021 char_u *p = pchBuf;
6022 for (n = 0; n < cchwritten; n++)
6023 MB_CPTR_ADV(p);
6024 written = p - pchBuf;
6025 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
6028 while (g_coord.X > g_srScrollRegion.Right)
6029 {
6030 g_coord.X -= (SHORT) Columns;
6031 if (g_coord.Y < g_srScrollRegion.Bottom)
6032 g_coord.Y++;
6033 }
6034
6035 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6036
6037 return written;
6038}
6039
6040
6041/*
6042 * mch_write(): write the output buffer to the screen, translating ESC
6043 * sequences into calls to console output routines.
6044 */
6045 void
6046mch_write(
6047 char_u *s,
6048 int len)
6049{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006050# ifdef VIMDLL
6051 if (gui.in_use)
6052 return;
6053# endif
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 s[len] = NUL;
6056
6057 if (!term_console)
6058 {
6059 write(1, s, (unsigned)len);
6060 return;
6061 }
6062
Bram Moolenaar0f873732019-12-05 20:28:46 +01006063 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 while (len--)
6065 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006066 // optimization: use one single write_chars for runs of text,
6067 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006068 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 if (p_wd)
6071 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006072 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (prefix != 0)
6074 prefix = 1;
6075 }
6076
6077 if (prefix != 0)
6078 {
6079 DWORD nWritten;
6080
6081 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006082# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (fdDump)
6084 {
6085 fputc('>', fdDump);
6086 fwrite(s, sizeof(char_u), nWritten, fdDump);
6087 fputs("<\n", fdDump);
6088 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 len -= (nWritten - 1);
6091 s += nWritten;
6092 }
6093 else if (s[0] == '\n')
6094 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006095 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (g_coord.Y == g_srScrollRegion.Bottom)
6097 {
6098 scroll(1);
6099 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6100 }
6101 else
6102 {
6103 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6104 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006105# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (fdDump)
6107 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 s++;
6110 }
6111 else if (s[0] == '\r')
6112 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006113 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006115# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (fdDump)
6117 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 s++;
6120 }
6121 else if (s[0] == '\b')
6122 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006123 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (g_coord.X > g_srScrollRegion.Left)
6125 g_coord.X--;
6126 else if (g_coord.Y > g_srScrollRegion.Top)
6127 {
6128 g_coord.X = g_srScrollRegion.Right;
6129 g_coord.Y--;
6130 }
6131 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006132# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (fdDump)
6134 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 s++;
6137 }
6138 else if (s[0] == '\a')
6139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006140 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006142# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (fdDump)
6144 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 s++;
6147 }
6148 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6149 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006150# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006151 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006152# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006153 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006154 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 switch (s[2])
6157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 case '0': case '1': case '2': case '3': case '4':
6159 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006160 p = s + 1;
6161 do
6162 {
6163 ++p;
6164 args[argc] = getdigits(&p);
6165 argc += (argc < 15) ? 1 : 0;
6166 if (p > s + len)
6167 break;
6168 } while (*p == ';');
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (p > s + len)
6171 break;
6172
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006173 arg1 = args[0];
6174 arg2 = args[1];
6175 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006177 if (argc == 1 && args[0] == 0)
6178 normvideo();
6179 else if (argc == 1)
6180 {
6181 if (USE_VTP)
6182 textcolor((WORD) arg1);
6183 else
6184 textattr((WORD) arg1);
6185 }
6186 else if (USE_VTP)
6187 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006189 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006191 gotoxy(arg2, arg1);
6192 }
6193 else if (argc == 2 && *p == 'r')
6194 {
6195 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6196 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006197 else if (argc == 2 && *p == 'R')
6198 {
6199 set_scroll_region_tb(arg1, arg2);
6200 }
6201 else if (argc == 2 && *p == 'V')
6202 {
6203 set_scroll_region_lr(arg1, arg2);
6204 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006205 else if (argc == 1 && *p == 'A')
6206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 gotoxy(g_coord.X + 1,
6208 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6209 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006210 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
6212 textbackground((WORD) arg1);
6213 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006214 else if (argc == 1 && *p == 'C')
6215 {
6216 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6217 g_coord.Y + 1);
6218 }
6219 else if (argc == 1 && *p == 'f')
6220 {
6221 textcolor((WORD) arg1);
6222 }
6223 else if (argc == 1 && *p == 'H')
6224 {
6225 gotoxy(1, arg1);
6226 }
6227 else if (argc == 1 && *p == 'L')
6228 {
6229 insert_lines(arg1);
6230 }
6231 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
6233 delete_lines(arg1);
6234 }
6235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006236 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 s = p + 1;
6238 break;
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 gotoxy(g_coord.X + 1,
6242 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6243 goto got3;
6244
6245 case 'B':
6246 visual_bell();
6247 goto got3;
6248
6249 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6251 g_coord.Y + 1);
6252 goto got3;
6253
6254 case 'E':
6255 termcap_mode_end();
6256 goto got3;
6257
6258 case 'F':
6259 standout();
6260 goto got3;
6261
6262 case 'f':
6263 standend();
6264 goto got3;
6265
6266 case 'H':
6267 gotoxy(1, 1);
6268 goto got3;
6269
6270 case 'j':
6271 clear_to_end_of_display();
6272 goto got3;
6273
6274 case 'J':
6275 clear_screen();
6276 goto got3;
6277
6278 case 'K':
6279 clear_to_end_of_line();
6280 goto got3;
6281
6282 case 'L':
6283 insert_lines(1);
6284 goto got3;
6285
6286 case 'M':
6287 delete_lines(1);
6288 goto got3;
6289
6290 case 'S':
6291 termcap_mode_start();
6292 goto got3;
6293
6294 case 'V':
6295 cursor_visible(TRUE);
6296 goto got3;
6297
6298 case 'v':
6299 cursor_visible(FALSE);
6300 goto got3;
6301
6302 got3:
6303 s += 3;
6304 len -= 2;
6305 }
6306
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006307# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (fdDump)
6309 {
6310 fputs("ESC | ", fdDump);
6311 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6312 fputc('\n', fdDump);
6313 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 else
6317 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006318 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 DWORD nWritten;
6320
6321 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006322# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (fdDump)
6324 {
6325 fputc('>', fdDump);
6326 fwrite(s, sizeof(char_u), nWritten, fdDump);
6327 fputs("<\n", fdDump);
6328 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 len -= (nWritten - 1);
6332 s += nWritten;
6333 }
6334 }
6335
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006336# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (fdDump)
6338 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340}
6341
Bram Moolenaar0f873732019-12-05 20:28:46 +01006342#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344
6345/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006346 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 */
6348 void
6349mch_delay(
6350 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006351 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006353#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006354 Sleep((int)msec); // never wait for input
6355#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006356# ifdef VIMDLL
6357 if (gui.in_use)
6358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006359 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006360 return;
6361 }
6362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006364# ifdef FEAT_MZSCHEME
6365 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6366 {
6367 int towait = p_mzq;
6368
Bram Moolenaar0f873732019-12-05 20:28:46 +01006369 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006370 while (msec > 0)
6371 {
6372 mzvim_check_threads();
6373 if (msec < towait)
6374 towait = msec;
6375 Sleep(towait);
6376 msec -= towait;
6377 }
6378 }
6379 else
6380# endif
6381 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006383 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384#endif
6385}
6386
6387
6388/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006389 * This version of remove is not scared by a readonly (backup) file.
6390 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 * Return 0 for success, -1 for failure.
6392 */
6393 int
6394mch_remove(char_u *name)
6395{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006396 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 int n;
6398
Bram Moolenaar203258c2016-01-17 22:15:16 +01006399 /*
6400 * On Windows, deleting a directory's symbolic link is done by
6401 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6402 */
6403 if (mch_isdir(name) && mch_is_symbolic_link(name))
6404 return mch_rmdir(name);
6405
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006406 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6407
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006408 wn = enc_to_utf16(name, NULL);
6409 if (wn == NULL)
6410 return -1;
6411
6412 n = DeleteFileW(wn) ? 0 : -1;
6413 vim_free(wn);
6414 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415}
6416
6417
6418/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006419 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 */
6421 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006422mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006424#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6425# ifdef VIMDLL
6426 if (!gui.in_use)
6427# endif
6428 if (g_fCtrlCPressed || g_fCBrkPressed)
6429 {
6430 ctrl_break_was_pressed = g_fCBrkPressed;
6431 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6432 got_int = TRUE;
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434#endif
6435}
6436
Bram Moolenaar0f873732019-12-05 20:28:46 +01006437// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006438#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006439
6440/*
6441 * How much main memory in KiB that can be used by VIM.
6442 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006443 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006444mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006445{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006446 MEMORYSTATUSEX ms;
6447
Bram Moolenaar0f873732019-12-05 20:28:46 +01006448 // Need to use GlobalMemoryStatusEx() when there is more memory than
6449 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006450 ms.dwLength = sizeof(MEMORYSTATUSEX);
6451 GlobalMemoryStatusEx(&ms);
6452 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006453 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006454 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006455 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006456 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006457 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006458 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006459 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006460 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006461 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006462 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006463 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006464}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006467 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6469 * file whose short file name is "FOO.BAR" (its long file name will
6470 * be correct: "foo.bar~"). Because a file can be accessed by
6471 * either its SFN or its LFN, "foo.bar" has effectively been
6472 * renamed to "foo.bar", which is not at all what was wanted. This
6473 * seems to happen only when renaming files with three-character
6474 * extensions by appending a suffix that does not include ".".
6475 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6476 *
6477 * There is another problem, which isn't really a bug but isn't right either:
6478 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6479 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6480 * service pack 6. Doesn't seem to happen on Windows 98.
6481 *
6482 * Like rename(), returns 0 upon success, non-zero upon failure.
6483 * Should probably set errno appropriately when errors occur.
6484 */
6485 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006486mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006488 WCHAR *p;
6489 int i;
6490 WCHAR szTempFile[_MAX_PATH + 1];
6491 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006494 // No need to play tricks unless the file name contains a "~" as the
6495 // seventh character.
6496 p = wold;
6497 for (i = 0; wold[i] != NUL; ++i)
6498 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6499 && wold[i + 1] != 0)
6500 p = wold + i + 1;
6501 if ((int)(wold + i - p) < 8 || p[6] != '~')
6502 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006504 // Get base path of new file name. Undocumented feature: If pszNewFile is
6505 // a directory, no error is returned and pszFilePart will be NULL.
6506 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006508 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006510 // Get (and create) a unique temporary file name in directory of new file
6511 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 return -2;
6513
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006514 // blow the temp file away
6515 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 return -3;
6517
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006518 // rename old file to the temp file
6519 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 return -4;
6521
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006522 // now create an empty file called pszOldFile; this prevents the operating
6523 // system using pszOldFile as an alias (SFN) if we're renaming within the
6524 // same directory. For example, we're editing a file called
6525 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6526 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6527 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6528 // cause all sorts of problems later in buf_write(). So, we create an
6529 // empty file called filena~1.txt and the system will have to find some
6530 // other SFN for filena~1.txt~, such as filena~2.txt
6531 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6533 return -5;
6534 if (!CloseHandle(hf))
6535 return -6;
6536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006537 // rename the temp file to the new file
6538 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 // Renaming failed. Rename the file back to its old name, so that it
6541 // looks like nothing happened.
6542 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 return -7;
6544 }
6545
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006546 // Seems to be left around on Novell filesystems
6547 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006549 // finally, remove the empty old file
6550 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 return -8;
6552
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006553 return 0;
6554}
6555
6556
6557/*
6558 * Converts the filenames to UTF-16, then call mch_wrename().
6559 * Like rename(), returns 0 upon success, non-zero upon failure.
6560 */
6561 int
6562mch_rename(
6563 const char *pszOldFile,
6564 const char *pszNewFile)
6565{
6566 WCHAR *wold = NULL;
6567 WCHAR *wnew = NULL;
6568 int retval = -1;
6569
6570 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6571 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6572 if (wold != NULL && wnew != NULL)
6573 retval = mch_wrename(wold, wnew);
6574 vim_free(wold);
6575 vim_free(wnew);
6576 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577}
6578
6579/*
6580 * Get the default shell for the current hardware platform
6581 */
6582 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006583default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006585 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586}
6587
6588/*
6589 * mch_access() extends access() to do more detailed check on network drives.
6590 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6591 */
6592 int
6593mch_access(char *n, int p)
6594{
6595 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006596 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006597 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006599 wn = enc_to_utf16((char_u *)n, NULL);
6600 if (wn == NULL)
6601 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006603 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
6607 if (p & R_OK)
6608 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006609 // Read check is performed by seeing if we can do a find file on
6610 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006611 int i;
6612 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006614 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6615 TempNameW[i] = wn[i];
6616 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6617 TempNameW[i++] = '\\';
6618 TempNameW[i++] = '*';
6619 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006621 hFile = FindFirstFileW(TempNameW, &d);
6622 if (hFile == INVALID_HANDLE_VALUE)
6623 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006624 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
6627
6628 if (p & W_OK)
6629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006630 // Trying to create a temporary file in the directory should catch
6631 // directories on read-only network shares. However, in
6632 // directories whose ACL allows writes but denies deletes will end
6633 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006634 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6635 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006636 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006637 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639 }
6640 else
6641 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006642 // Don't consider a file read-only if another process has opened it.
6643 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6644
Bram Moolenaar0f873732019-12-05 20:28:46 +01006645 // Trying to open the file for the required access does ACL, read-only
6646 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006647 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6648 | ((p & R_OK) ? GENERIC_READ : 0);
6649
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006650 hFile = CreateFileW(wn, access_mode, share_mode,
6651 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (hFile == INVALID_HANDLE_VALUE)
6653 goto getout;
6654 CloseHandle(hFile);
6655 }
6656
Bram Moolenaar0f873732019-12-05 20:28:46 +01006657 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 return retval;
6661}
6662
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006664 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 */
6666 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006667mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 WCHAR *wn;
6670 int f;
6671
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006672 wn = enc_to_utf16((char_u *)name, NULL);
6673 if (wn == NULL)
6674 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006676 f = _wopen(wn, flags, mode);
6677 vim_free(wn);
6678 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006682 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 */
6684 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006685mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 WCHAR *wn, *wm;
6688 FILE *f = NULL;
6689
Bram Moolenaara12a1612019-01-24 16:39:02 +01006690#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006691 // Work around an annoying assertion in the Microsoft debug CRT
6692 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006693 char newMode = mode[strlen(mode) - 1];
6694 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006695
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006696 _get_fmode(&oldMode);
6697 if (newMode == 't')
6698 _set_fmode(_O_TEXT);
6699 else if (newMode == 'b')
6700 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006701#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006702 wn = enc_to_utf16((char_u *)name, NULL);
6703 wm = enc_to_utf16((char_u *)mode, NULL);
6704 if (wn != NULL && wm != NULL)
6705 f = _wfopen(wn, wm);
6706 vim_free(wn);
6707 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006708
Bram Moolenaara12a1612019-01-24 16:39:02 +01006709#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006710 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006711#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006712 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715/*
6716 * SUB STREAM (aka info stream) handling:
6717 *
6718 * NTFS can have sub streams for each file. Normal contents of file is
6719 * stored in the main stream, and extra contents (author information and
6720 * title and so on) can be stored in sub stream. After Windows 2000, user
6721 * can access and store those informations in sub streams via explorer's
6722 * property menuitem in right click menu. Those informations in sub streams
6723 * were lost when copying only the main stream. So we have to copy sub
6724 * streams.
6725 *
6726 * Incomplete explanation:
6727 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6728 * More useful info and an example:
6729 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6730 */
6731
6732/*
6733 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6734 * and write to stream "substream" of file "to".
6735 * Errors are ignored.
6736 */
6737 static void
6738copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6739{
6740 HANDLE hTo;
6741 WCHAR *to_name;
6742
6743 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6744 wcscpy(to_name, to);
6745 wcscat(to_name, substream);
6746
6747 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6748 FILE_ATTRIBUTE_NORMAL, NULL);
6749 if (hTo != INVALID_HANDLE_VALUE)
6750 {
6751 long done;
6752 DWORD todo;
6753 DWORD readcnt, written;
6754 char buf[4096];
6755
Bram Moolenaar0f873732019-12-05 20:28:46 +01006756 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 for (done = 0; done < len; done += written)
6758 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006759 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006760 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6761 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6763 FALSE, FALSE, context)
6764 || readcnt != todo
6765 || !WriteFile(hTo, buf, todo, &written, NULL)
6766 || written != todo)
6767 break;
6768 }
6769 CloseHandle(hTo);
6770 }
6771
6772 free(to_name);
6773}
6774
6775/*
6776 * Copy info streams from file "from" to file "to".
6777 */
6778 static void
6779copy_infostreams(char_u *from, char_u *to)
6780{
6781 WCHAR *fromw;
6782 WCHAR *tow;
6783 HANDLE sh;
6784 WIN32_STREAM_ID sid;
6785 int headersize;
6786 WCHAR streamname[_MAX_PATH];
6787 DWORD readcount;
6788 void *context = NULL;
6789 DWORD lo, hi;
6790 int len;
6791
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006793 fromw = enc_to_utf16(from, NULL);
6794 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (fromw != NULL && tow != NULL)
6796 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006797 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6799 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6800 if (sh != INVALID_HANDLE_VALUE)
6801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 // Use BackupRead() to find the info streams. Repeat until we
6803 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 for (;;)
6805 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 // Get the header to find the length of the stream name. If
6807 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006809 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6811 &readcount, FALSE, FALSE, &context)
6812 || readcount == 0)
6813 break;
6814
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 // We only deal with streams that have a name. The normal
6816 // file data appears to be without a name, even though docs
6817 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (sid.dwStreamNameSize > 0)
6819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (!BackupRead(sh, (LPBYTE)streamname,
6822 sid.dwStreamNameSize,
6823 &readcount, FALSE, FALSE, &context))
6824 break;
6825
Bram Moolenaar0f873732019-12-05 20:28:46 +01006826 // Copy an info stream with a name ":anything:$DATA".
6827 // Skip "::$DATA", it has no stream name (examples suggest
6828 // it might be used for the normal file contents).
6829 // Note that BackupRead() counts bytes, but the name is in
6830 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 len = readcount / sizeof(WCHAR);
6832 streamname[len] = 0;
6833 if (len > 7 && wcsicmp(streamname + len - 6,
6834 L":$DATA") == 0)
6835 {
6836 streamname[len - 6] = 0;
6837 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006838 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
6840 }
6841
Bram Moolenaar0f873732019-12-05 20:28:46 +01006842 // Advance to the next stream. We might try seeking too far,
6843 // but BackupSeek() doesn't skip over stream borders, thus
6844 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006845 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 &lo, &hi, &context);
6847 }
6848
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6851
6852 CloseHandle(sh);
6853 }
6854 }
6855 vim_free(fromw);
6856 vim_free(tow);
6857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859/*
6860 * Copy file attributes from file "from" to file "to".
6861 * For Windows NT and later we copy info streams.
6862 * Always returns zero, errors are ignored.
6863 */
6864 int
6865mch_copy_file_attribute(char_u *from, char_u *to)
6866{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006867 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006868 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 return 0;
6870}
6871
6872#if defined(MYRESETSTKOFLW) || defined(PROTO)
6873/*
6874 * Recreate a destroyed stack guard page in win32.
6875 * Written by Benjamin Peterson.
6876 */
6877
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006879# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
6881/*
6882 * This function does the same thing as _resetstkoflw(), which is only
6883 * available in DevStudio .net and later.
6884 * Returns 0 for failure, 1 for success.
6885 */
6886 int
6887myresetstkoflw(void)
6888{
6889 BYTE *pStackPtr;
6890 BYTE *pGuardPage;
6891 BYTE *pStackBase;
6892 BYTE *pLowestPossiblePage;
6893 MEMORY_BASIC_INFORMATION mbi;
6894 SYSTEM_INFO si;
6895 DWORD nPageSize;
6896 DWORD dummy;
6897
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 GetSystemInfo(&si);
6900 nPageSize = si.dwPageSize;
6901
Bram Moolenaar0f873732019-12-05 20:28:46 +01006902 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 pStackPtr = (BYTE*)_alloca(1);
6904
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6907 return 0;
6908 pStackBase = (BYTE*)mbi.AllocationBase;
6909
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006910 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006912 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006915 // We want the first committed page in the stack Start at the stack
6916 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 BYTE *pBlock = pStackBase;
6918
Bram Moolenaara466c992005-07-09 21:03:22 +00006919 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
6921 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6922 return 0;
6923
6924 pBlock += mbi.RegionSize;
6925
6926 if (mbi.State & MEM_COMMIT)
6927 break;
6928 }
6929
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (mbi.Protect & PAGE_GUARD)
6932 return 1;
6933
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6936 pGuardPage = pLowestPossiblePage;
6937 else
6938 pGuardPage = (BYTE*)mbi.BaseAddress;
6939
Bram Moolenaar0f873732019-12-05 20:28:46 +01006940 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6942 return 0;
6943
Bram Moolenaar0f873732019-12-05 20:28:46 +01006944 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6946 &dummy))
6947 return 0;
6948 }
6949
6950 return 1;
6951}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006954
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006955/*
6956 * The command line arguments in UCS2
6957 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006958static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006959static LPWSTR *ArglistW = NULL;
6960static int global_argc = 0;
6961static char **global_argv;
6962
Bram Moolenaar0f873732019-12-05 20:28:46 +01006963static int used_file_argc = 0; // last argument in global_argv[] used
6964 // for the argument list.
6965static int *used_file_indexes = NULL; // indexes in global_argv[] for
6966 // command line arguments added to
6967 // the argument list
6968static int used_file_count = 0; // nr of entries in used_file_indexes
6969static int used_file_literal = FALSE; // take file names literally
6970static int used_file_full_path = FALSE; // file name was full path
6971static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006972static int used_alist_count = 0;
6973
6974
6975/*
6976 * Get the command line arguments. Unicode version.
6977 * Returns argc. Zero when something fails.
6978 */
6979 int
6980get_cmd_argsW(char ***argvp)
6981{
6982 char **argv = NULL;
6983 int argc = 0;
6984 int i;
6985
Bram Moolenaar14993322014-09-09 12:25:33 +02006986 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006987 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
6988 if (ArglistW != NULL)
6989 {
6990 argv = malloc((nArgsW + 1) * sizeof(char *));
6991 if (argv != NULL)
6992 {
6993 argc = nArgsW;
6994 argv[argc] = NULL;
6995 for (i = 0; i < argc; ++i)
6996 {
6997 int len;
6998
Bram Moolenaar0f873732019-12-05 20:28:46 +01006999 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007000 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007001 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007002 (LPSTR *)&argv[i], &len, 0, 0);
7003 if (argv[i] == NULL)
7004 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007006 while (i > 0)
7007 free(argv[--i]);
7008 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007009 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007010 argc = 0;
7011 }
7012 }
7013 }
7014 }
7015
7016 global_argc = argc;
7017 global_argv = argv;
7018 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007019 {
7020 if (used_file_indexes != NULL)
7021 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007022 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007023 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007024
7025 if (argvp != NULL)
7026 *argvp = argv;
7027 return argc;
7028}
7029
7030 void
7031free_cmd_argsW(void)
7032{
7033 if (ArglistW != NULL)
7034 {
7035 GlobalFree(ArglistW);
7036 ArglistW = NULL;
7037 }
7038}
7039
7040/*
7041 * Remember "name" is an argument that was added to the argument list.
7042 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7043 * is called.
7044 */
7045 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007046used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007047{
7048 int i;
7049
7050 if (used_file_indexes == NULL)
7051 return;
7052 for (i = used_file_argc + 1; i < global_argc; ++i)
7053 if (STRCMP(global_argv[i], name) == 0)
7054 {
7055 used_file_argc = i;
7056 used_file_indexes[used_file_count++] = i;
7057 break;
7058 }
7059 used_file_literal = literal;
7060 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007061 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007062}
7063
7064/*
7065 * Remember the length of the argument list as it was. If it changes then we
7066 * leave it alone when 'encoding' is set.
7067 */
7068 void
7069set_alist_count(void)
7070{
7071 used_alist_count = GARGCOUNT;
7072}
7073
7074/*
7075 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7076 * has been changed while starting up. Use the UCS-2 command line arguments
7077 * and convert them to 'encoding'.
7078 */
7079 void
7080fix_arg_enc(void)
7081{
7082 int i;
7083 int idx;
7084 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007085 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007086
Bram Moolenaar0f873732019-12-05 20:28:46 +01007087 // Safety checks:
7088 // - if argument count differs between the wide and non-wide argument
7089 // list, something must be wrong.
7090 // - the file name arguments must have been located.
7091 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007092 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007093 || ArglistW == NULL
7094 || used_file_indexes == NULL
7095 || used_file_count == 0
7096 || used_alist_count != GARGCOUNT)
7097 return;
7098
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007100 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007101 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007102 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007103 for (i = 0; i < GARGCOUNT; ++i)
7104 fnum_list[i] = GARGLIST[i].ae_fnum;
7105
Bram Moolenaar0f873732019-12-05 20:28:46 +01007106 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007107 alist_clear(&global_alist);
7108 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007109 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007110
7111 for (i = 0; i < used_file_count; ++i)
7112 {
7113 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007114 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007115 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007116 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007117 int literal = used_file_literal;
7118
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007119#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 // When using diff mode may need to concatenate file name to
7121 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007122 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7123 && !mch_isdir(alist_name(&GARGLIST[0])))
7124 {
7125 char_u *r;
7126
7127 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7128 if (r != NULL)
7129 {
7130 vim_free(str);
7131 str = r;
7132 }
7133 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007134#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007135 // Re-use the old buffer by renaming it. When not using literal
7136 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007137 if (used_file_literal)
7138 buf_set_name(fnum_list[i], str);
7139
Bram Moolenaar0f873732019-12-05 20:28:46 +01007140 // Check backtick literal. backtick literal is already expanded in
7141 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007142 if (literal == FALSE)
7143 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007144 size_t len = STRLEN(str);
7145
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007146 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7147 literal = TRUE;
7148 }
7149 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007150 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007151 }
7152
7153 if (!used_file_literal)
7154 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007155 // Now expand wildcards in the arguments.
7156 // Temporarily add '(' and ')' to 'isfname'. These are valid
7157 // filename characters but are excluded from 'isfname' to make
7158 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7159 // Also, unset wildignore to not be influenced by this option.
7160 // The arguments specified in command-line should be kept even if
7161 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007162 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007163 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007164 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007165 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007166 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007167 }
7168
Bram Moolenaar0f873732019-12-05 20:28:46 +01007169 // If wildcard expansion failed, we are editing the first file of the
7170 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007171 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7172 {
7173 do_cmdline_cmd((char_u *)":rewind");
7174 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007175 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007176 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007177
7178 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007179}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007180
7181 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007182mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007183{
7184 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007185 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007186
Bram Moolenaar964b3742019-05-24 18:54:09 +02007187 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007188 if (envbuf == NULL)
7189 return -1;
7190
7191 sprintf((char *)envbuf, "%s=%s", var, value);
7192
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007193 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007194
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007195 vim_free(envbuf);
7196 if (p == NULL)
7197 return -1;
7198 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007199#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007200 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007201#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007202 // Unlike Un*x systems, we can free the string for _wputenv().
7203 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007204
7205 return 0;
7206}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007207
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007208/*
7209 * Support for 256 colors and 24-bit colors was added in Windows 10
7210 * version 1703 (Creators update).
7211 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007212#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7213
7214/*
7215 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007216 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007217 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007218#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007219
7220/*
7221 * ConPTY differences between versions, need different logic.
7222 * version 1903 (May 2019 update).
7223 */
7224#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7225
7226/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007227 * version 1909 (November 2019 update).
7228 */
7229#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7230
7231/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007232 * Confirm until this version. Also the logic changes.
7233 * insider preview.
7234 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007235#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007236
7237/*
7238 * Not stable now.
7239 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007240#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007241
7242 static void
7243vtp_flag_init(void)
7244{
7245 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007246#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007247 DWORD mode;
7248 HANDLE out;
7249
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007250# ifdef VIMDLL
7251 if (!gui.in_use)
7252# endif
7253 {
7254 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007255
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007256 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7257 GetConsoleMode(out, &mode);
7258 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7259 if (SetConsoleMode(out, mode) == 0)
7260 vtp_working = 0;
7261 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007262#endif
7263
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007264 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007265 conpty_working = 1;
7266 if (ver >= CONPTY_STABLE_BUILD)
7267 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007268
Bram Moolenaar57da6982019-09-13 22:30:11 +02007269 if (ver <= CONPTY_INSIDER_BUILD)
7270 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007271 if (ver <= CONPTY_1909_BUILD)
7272 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007273 if (ver <= CONPTY_1903_BUILD)
7274 conpty_type = 2;
7275 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7276 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007277}
7278
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007279#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007280
7281 static void
7282vtp_init(void)
7283{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007284 HMODULE hKerneldll;
7285 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007286# ifdef FEAT_TERMGUICOLORS
7287 COLORREF fg, bg;
7288# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007289
Bram Moolenaar0f873732019-12-05 20:28:46 +01007290 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007291 hKerneldll = GetModuleHandle("kernel32.dll");
7292 if (hKerneldll != NULL)
7293 {
7294 pGetConsoleScreenBufferInfoEx =
7295 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7296 hKerneldll, "GetConsoleScreenBufferInfoEx");
7297 pSetConsoleScreenBufferInfoEx =
7298 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7299 hKerneldll, "SetConsoleScreenBufferInfoEx");
7300 if (pGetConsoleScreenBufferInfoEx != NULL
7301 && pSetConsoleScreenBufferInfoEx != NULL)
7302 has_csbiex = TRUE;
7303 }
7304
7305 csbi.cbSize = sizeof(csbi);
7306 if (has_csbiex)
7307 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007308 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7309 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007310 store_console_bg_rgb = save_console_bg_rgb;
7311 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007312
7313# ifdef FEAT_TERMGUICOLORS
7314 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7315 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7316 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7317 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7318 default_console_color_bg = bg;
7319 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007320# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007321
7322 set_console_color_rgb();
7323}
7324
7325 static void
7326vtp_exit(void)
7327{
Bram Moolenaardf543822020-01-30 11:53:59 +01007328 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007329}
7330
7331 static int
7332vtp_printf(
7333 char *format,
7334 ...)
7335{
7336 char_u buf[100];
7337 va_list list;
7338 DWORD result;
7339
7340 va_start(list, format);
7341 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7342 va_end(list);
7343 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7344 return (int)result;
7345}
7346
7347 static void
7348vtp_sgr_bulk(
7349 int arg)
7350{
7351 int args[1];
7352
7353 args[0] = arg;
7354 vtp_sgr_bulks(1, args);
7355}
7356
7357 static void
7358vtp_sgr_bulks(
7359 int argc,
7360 int *args
7361)
7362{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007363 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007364 char_u buf[2 + (4 * 16) + 1];
7365 char_u *p;
7366 int i;
7367
7368 p = buf;
7369 *p++ = '\033';
7370 *p++ = '[';
7371
7372 for (i = 0; i < argc; ++i)
7373 {
7374 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7375 *p++ = ';';
7376 }
7377 p--;
7378 *p++ = 'm';
7379 *p = NUL;
7380 vtp_printf((char *)buf);
7381}
7382
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007383# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007384 static int
7385ctermtoxterm(
7386 int cterm)
7387{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007388 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007389
7390 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7391 return (((int)r << 16) | ((int)g << 8) | (int)b);
7392}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007393# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007394
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007395 static void
7396set_console_color_rgb(void)
7397{
7398# ifdef FEAT_TERMGUICOLORS
7399 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007400 guicolor_T fg, bg;
7401 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007402
7403 if (!USE_VTP)
7404 return;
7405
Bram Moolenaara050b942019-12-02 21:35:31 +01007406 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7407
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007408 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7409 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7410
7411 csbi.cbSize = sizeof(csbi);
7412 if (has_csbiex)
7413 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7414
7415 csbi.cbSize = sizeof(csbi);
7416 csbi.srWindow.Right += 1;
7417 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007418 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7419 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007420 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7421 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007422 if (has_csbiex)
7423 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7424# endif
7425}
7426
Bram Moolenaara050b942019-12-02 21:35:31 +01007427# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7428 void
7429get_default_console_color(
7430 int *cterm_fg,
7431 int *cterm_bg,
7432 guicolor_T *gui_fg,
7433 guicolor_T *gui_bg)
7434{
7435 int id;
7436 guicolor_T guifg = INVALCOLOR;
7437 guicolor_T guibg = INVALCOLOR;
7438 int ctermfg = 0;
7439 int ctermbg = 0;
7440
7441 id = syn_name2id((char_u *)"Normal");
7442 if (id > 0 && p_tgc)
7443 syn_id2colors(id, &guifg, &guibg);
7444 if (guifg == INVALCOLOR)
7445 {
7446 ctermfg = -1;
7447 if (id > 0)
7448 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7449 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7450 : default_console_color_fg;
7451 cterm_normal_fg_gui_color = guifg;
7452 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7453 }
7454 if (guibg == INVALCOLOR)
7455 {
7456 ctermbg = -1;
7457 if (id > 0)
7458 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7459 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7460 : default_console_color_bg;
7461 cterm_normal_bg_gui_color = guibg;
7462 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7463 }
7464
7465 *cterm_fg = ctermfg;
7466 *cterm_bg = ctermbg;
7467 *gui_fg = guifg;
7468 *gui_bg = guibg;
7469}
7470# endif
7471
Bram Moolenaardf543822020-01-30 11:53:59 +01007472/*
7473 * Set the console colors to the original colors or the last set colors.
7474 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007475 static void
7476reset_console_color_rgb(void)
7477{
7478# ifdef FEAT_TERMGUICOLORS
7479 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7480
7481 csbi.cbSize = sizeof(csbi);
7482 if (has_csbiex)
7483 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7484
7485 csbi.cbSize = sizeof(csbi);
7486 csbi.srWindow.Right += 1;
7487 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007488 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7489 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7490 if (has_csbiex)
7491 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7492# endif
7493}
7494
7495/*
7496 * Set the console colors to the original colors.
7497 */
7498 static void
7499restore_console_color_rgb(void)
7500{
7501# ifdef FEAT_TERMGUICOLORS
7502 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7503
7504 csbi.cbSize = sizeof(csbi);
7505 if (has_csbiex)
7506 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7507
7508 csbi.cbSize = sizeof(csbi);
7509 csbi.srWindow.Right += 1;
7510 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007511 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7512 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007513 if (has_csbiex)
7514 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7515# endif
7516}
7517
7518 void
7519control_console_color_rgb(void)
7520{
7521 if (USE_VTP)
7522 set_console_color_rgb();
7523 else
7524 reset_console_color_rgb();
7525}
7526
7527 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007528use_vtp(void)
7529{
7530 return USE_VTP;
7531}
7532
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007533 int
7534is_term_win32(void)
7535{
7536 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7537}
7538
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007539 int
7540has_vtp_working(void)
7541{
7542 return vtp_working;
7543}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007544
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007545#endif
7546
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007547 int
7548has_conpty_working(void)
7549{
7550 return conpty_working;
7551}
7552
7553 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007554get_conpty_type(void)
7555{
7556 return conpty_type;
7557}
7558
7559 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007560is_conpty_stable(void)
7561{
7562 return conpty_stable;
7563}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007564
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007565#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007566 void
7567resize_console_buf(void)
7568{
7569 CONSOLE_SCREEN_BUFFER_INFO csbi;
7570 COORD coord;
7571 SMALL_RECT newsize;
7572
7573 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7574 {
7575 coord.X = SRWIDTH(csbi.srWindow);
7576 coord.Y = SRHEIGHT(csbi.srWindow);
7577 SetConsoleScreenBufferSize(g_hConOut, coord);
7578
7579 newsize.Left = 0;
7580 newsize.Top = 0;
7581 newsize.Right = coord.X - 1;
7582 newsize.Bottom = coord.Y - 1;
7583 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7584
7585 SetConsoleScreenBufferSize(g_hConOut, coord);
7586 }
7587}
7588#endif