blob: 7dc37a111493966d993918f76e64ad3ba113c640 [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;
4176 int c;
4177 int noread_cnt = 0;
4178 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004179 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004180 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004181 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004182
4183 SECURITY_ATTRIBUTES saAttr;
4184
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004186 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4187 saAttr.bInheritHandle = TRUE;
4188 saAttr.lpSecurityDescriptor = NULL;
4189
4190 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004191 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004192 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004193 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004194 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004195 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004196 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004197 {
4198 CloseHandle(g_hChildStd_IN_Rd);
4199 CloseHandle(g_hChildStd_IN_Wr);
4200 CloseHandle(g_hChildStd_OUT_Rd);
4201 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004202 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004203 }
4204
4205 si.cb = sizeof(si);
4206 si.lpReserved = NULL;
4207 si.lpDesktop = NULL;
4208 si.lpTitle = NULL;
4209 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4210
Bram Moolenaar0f873732019-12-05 20:28:46 +01004211 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004212 si.hStdError = g_hChildStd_OUT_Wr;
4213 si.hStdOutput = g_hChildStd_OUT_Wr;
4214 si.hStdInput = g_hChildStd_IN_Rd;
4215 si.wShowWindow = SW_HIDE;
4216 si.cbReserved2 = 0;
4217 si.lpReserved2 = NULL;
4218
4219 if (options & SHELL_READ)
4220 ga_init2(&ga, 1, BUFLEN);
4221
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004222 if (cmd != NULL)
4223 {
4224 p = (char *)vim_strsave((char_u *)cmd);
4225 if (p != NULL)
4226 unescape_shellxquote((char_u *)p, p_sxe);
4227 else
4228 p = cmd;
4229 }
4230
Bram Moolenaar0f873732019-12-05 20:28:46 +01004231 // Now, run the command.
4232 // About "Inherit handles" being TRUE: this command can be litigious,
4233 // handle inheritance was deactivated for pending temp file, but, if we
4234 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004235 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4236 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004237
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004238 if (p != cmd)
4239 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004240
Bram Moolenaar0f873732019-12-05 20:28:46 +01004241 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004242 CloseHandle(g_hChildStd_IN_Rd);
4243 CloseHandle(g_hChildStd_OUT_Wr);
4244
4245 if (options & SHELL_WRITE)
4246 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004247 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004248 _beginthreadex(NULL, // security attributes
4249 0, // default stack size
4250 sub_process_writer, // function to be executed
4251 g_hChildStd_IN_Wr, // parameter
4252 0, // creation flag, start immediately
4253 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004254 CloseHandle(thread);
4255 g_hChildStd_IN_Wr = NULL;
4256 }
4257
Bram Moolenaar0f873732019-12-05 20:28:46 +01004258 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004259 for (;;)
4260 {
4261 MSG msg;
4262
Bram Moolenaar175d0702013-12-11 18:36:33 +01004263 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004264 {
4265 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004266 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004267 }
4268
Bram Moolenaar0f873732019-12-05 20:28:46 +01004269 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004270 if ((options & (SHELL_READ|SHELL_WRITE))
4271# ifdef FEAT_GUI
4272 || gui.in_use
4273# endif
4274 )
4275 {
4276 len = 0;
4277 if (!(options & SHELL_EXPAND)
4278 && ((options &
4279 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4280 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4281# ifdef FEAT_GUI
4282 || gui.in_use
4283# endif
4284 )
4285 && (ta_len > 0 || noread_cnt > 4))
4286 {
4287 if (ta_len == 0)
4288 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004289 // Get extra characters when we don't have any. Reset the
4290 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004291 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004292 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4293 }
4294 if (ta_len > 0 || len > 0)
4295 {
4296 /*
4297 * For pipes: Check for CTRL-C: send interrupt signal to
4298 * child. Check for CTRL-D: EOF, close pipe to child.
4299 */
4300 if (len == 1 && cmd != NULL)
4301 {
4302 if (ta_buf[ta_len] == Ctrl_C)
4303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004304 // Learn what exit code is expected, for
4305 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004306 TerminateProcess(pi.hProcess, 9);
4307 }
4308 if (ta_buf[ta_len] == Ctrl_D)
4309 {
4310 CloseHandle(g_hChildStd_IN_Wr);
4311 g_hChildStd_IN_Wr = NULL;
4312 }
4313 }
4314
Bram Moolenaar0f873732019-12-05 20:28:46 +01004315 // replace K_BS by <BS> and K_DEL by <DEL>
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004316 for (i = ta_len; i < ta_len + len; ++i)
4317 {
4318 if (ta_buf[i] == CSI && len - i > 2)
4319 {
4320 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4321 if (c == K_DEL || c == K_KDEL || c == K_BS)
4322 {
4323 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4324 (size_t)(len - i - 2));
4325 if (c == K_DEL || c == K_KDEL)
4326 ta_buf[i] = DEL;
4327 else
4328 ta_buf[i] = Ctrl_H;
4329 len -= 2;
4330 }
4331 }
4332 else if (ta_buf[i] == '\r')
4333 ta_buf[i] = '\n';
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004334 if (has_mbyte)
4335 i += (*mb_ptr2len_len)(ta_buf + i,
4336 ta_len + len - i) - 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004337 }
4338
4339 /*
4340 * For pipes: echo the typed characters. For a pty this
4341 * does not seem to work.
4342 */
4343 for (i = ta_len; i < ta_len + len; ++i)
4344 {
4345 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4346 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004347 else if (has_mbyte)
4348 {
4349 int l = (*mb_ptr2len)(ta_buf + i);
4350
4351 msg_outtrans_len(ta_buf + i, l);
4352 i += l - 1;
4353 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004354 else
4355 msg_outtrans_len(ta_buf + i, 1);
4356 }
4357 windgoto(msg_row, msg_col);
4358 out_flush();
4359
4360 ta_len += len;
4361
4362 /*
4363 * Write the characters to the child, unless EOF has been
4364 * typed for pipes. Write one character at a time, to
4365 * avoid losing too much typeahead. When writing buffer
4366 * lines, drop the typed characters (only check for
4367 * CTRL-C).
4368 */
4369 if (options & SHELL_WRITE)
4370 ta_len = 0;
4371 else if (g_hChildStd_IN_Wr != NULL)
4372 {
4373 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4374 1, &len, NULL);
4375 // if we are typing in, we want to keep things reactive
4376 delay = 1;
4377 if (len > 0)
4378 {
4379 ta_len -= len;
4380 mch_memmove(ta_buf, ta_buf + len, ta_len);
4381 }
4382 }
4383 }
4384 }
4385 }
4386
4387 if (ta_len)
4388 ui_inchar_undo(ta_buf, ta_len);
4389
4390 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4391 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004392 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004393 break;
4394 }
4395
4396 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004397 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004398
Bram Moolenaar0f873732019-12-05 20:28:46 +01004399 // We start waiting for a very short time and then increase it, so
4400 // that we respond quickly when the process is quick, and don't
4401 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004402 if (delay < 50)
4403 delay += 10;
4404 }
4405
Bram Moolenaar0f873732019-12-05 20:28:46 +01004406 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004407 CloseHandle(g_hChildStd_OUT_Rd);
4408 if (g_hChildStd_IN_Wr != NULL)
4409 CloseHandle(g_hChildStd_IN_Wr);
4410
4411 WaitForSingleObject(pi.hProcess, INFINITE);
4412
Bram Moolenaar0f873732019-12-05 20:28:46 +01004413 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004414 GetExitCodeProcess(pi.hProcess, &ret);
4415
4416 if (options & SHELL_READ)
4417 {
4418 if (ga.ga_len > 0)
4419 {
4420 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004422 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4423 }
4424 else
4425 curbuf->b_no_eol_lnum = 0;
4426 ga_clear(&ga);
4427 }
4428
Bram Moolenaar0f873732019-12-05 20:28:46 +01004429 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004430 CloseHandle(pi.hThread);
4431 CloseHandle(pi.hProcess);
4432
4433 return ret;
4434}
4435
4436 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004437mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004438{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004439 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004440 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004441 return mch_system_piped(cmd, options);
4442 else
4443 return mch_system_classic(cmd, options);
4444}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004447#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004448 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004449mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004450{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004451 int ret;
4452 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004453 char_u *buf;
4454 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004455
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004456 // If the command starts and ends with double quotes, enclose the command
4457 // in parentheses.
4458 len = STRLEN(cmd);
4459 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4460 {
4461 len += 3;
4462 buf = alloc(len);
4463 if (buf == NULL)
4464 return -1;
4465 vim_snprintf((char *)buf, len, "(%s)", cmd);
4466 wcmd = enc_to_utf16(buf, NULL);
4467 free(buf);
4468 }
4469 else
4470 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4471
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004472 if (wcmd == NULL)
4473 return -1;
4474
4475 ret = _wsystem(wcmd);
4476 vim_free(wcmd);
4477 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480#endif
4481
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004482 static int
4483mch_system(char *cmd, int options)
4484{
4485#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004486 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004487 return mch_system_g(cmd, options);
4488 else
4489 return mch_system_c(cmd, options);
4490#elif defined(FEAT_GUI_MSWIN)
4491 return mch_system_g(cmd, options);
4492#else
4493 return mch_system_c(cmd, options);
4494#endif
4495}
4496
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004497#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4498/*
4499 * Use a terminal window to run a shell command in.
4500 */
4501 static int
4502mch_call_shell_terminal(
4503 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004504 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004505{
4506 jobopt_T opt;
4507 char_u *newcmd = NULL;
4508 typval_T argvar[2];
4509 long_u cmdlen;
4510 int retval = -1;
4511 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004512 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004513 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004514 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004515
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004516 if (cmd == NULL)
4517 cmdlen = STRLEN(p_sh) + 1;
4518 else
4519 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004520 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004521 if (newcmd == NULL)
4522 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004523 if (cmd == NULL)
4524 {
4525 STRCPY(newcmd, p_sh);
4526 ch_log(NULL, "starting terminal to run a shell");
4527 }
4528 else
4529 {
4530 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4531 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4532 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004533
4534 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004535
4536 argvar[0].v_type = VAR_STRING;
4537 argvar[0].vval.v_string = newcmd;
4538 argvar[1].v_type = VAR_UNKNOWN;
4539 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004540 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004541 {
4542 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004543 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004544 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004545
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004546 job = term_getjob(buf->b_term);
4547 ++job->jv_refcount;
4548
Bram Moolenaar0f873732019-12-05 20:28:46 +01004549 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004550 aucmd_prepbuf(&aco, buf);
4551
4552 clear_oparg(&oa);
4553 while (term_use_loop())
4554 {
4555 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4556 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004557 // If terminal_loop() returns OK we got a key that is handled
4558 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004559 if (terminal_loop(TRUE) == OK)
4560 normal_cmd(&oa, TRUE);
4561 }
4562 else
4563 normal_cmd(&oa, TRUE);
4564 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004565 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004566 ch_log(NULL, "system command finished");
4567
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004568 job_unref(job);
4569
Bram Moolenaar0f873732019-12-05 20:28:46 +01004570 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004571 aucmd_restbuf(&aco);
4572
4573 wait_return(TRUE);
4574 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4575
4576 vim_free(newcmd);
4577 return retval;
4578}
4579#endif
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581/*
4582 * Either execute a command by calling the shell or start a new shell
4583 */
4584 int
4585mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004586 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004587 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588{
4589 int x = 0;
4590 int tmode = cur_tmode;
4591#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004592 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004593
Bram Moolenaar0f873732019-12-05 20:28:46 +01004594 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004595 if (GetConsoleTitleW(szShellTitle,
4596 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004597 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004598 if (cmd == NULL)
4599 wcscat(szShellTitle, L" :sh");
4600 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004601 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004602 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004603
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004604 if (wn != NULL)
4605 {
4606 wcscat(szShellTitle, L" - !");
4607 if ((wcslen(szShellTitle) + wcslen(wn) <
4608 sizeof(szShellTitle)/sizeof(WCHAR)))
4609 wcscat(szShellTitle, wn);
4610 SetConsoleTitleW(szShellTitle);
4611 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004612 }
4613 }
4614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#endif
4616
4617 out_flush();
4618
4619#ifdef MCH_WRITE_DUMP
4620 if (fdDump)
4621 {
4622 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4623 fflush(fdDump);
4624 }
4625#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004626#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004627 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004628 if (
4629# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004630 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004631# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004632 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004633 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4634 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004635 char_u *cmdbase = cmd;
4636
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004637 if (cmdbase != NULL)
4638 // Skip a leading quote and (.
4639 while (*cmdbase == '"' || *cmdbase == '(')
4640 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004641
4642 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004643 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4644 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004645 {
4646 // Use a terminal window to run the command in.
4647 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004648# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004649 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004650# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004651 return x;
4652 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004653 }
4654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 /*
4657 * Catch all deadly signals while running the external command, because a
4658 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4659 */
4660 signal(SIGINT, SIG_IGN);
4661#if defined(__GNUC__) && !defined(__MINGW32__)
4662 signal(SIGKILL, SIG_IGN);
4663#else
4664 signal(SIGBREAK, SIG_IGN);
4665#endif
4666 signal(SIGILL, SIG_IGN);
4667 signal(SIGFPE, SIG_IGN);
4668 signal(SIGSEGV, SIG_IGN);
4669 signal(SIGTERM, SIG_IGN);
4670 signal(SIGABRT, SIG_IGN);
4671
4672 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004673 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 if (cmd == NULL)
4676 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004677 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 else
4680 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004681 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004682 char_u *newcmd = NULL;
4683 char_u *cmdbase = cmd;
4684 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004685
Bram Moolenaar0f873732019-12-05 20:28:46 +01004686 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004687 if (*cmdbase == '"' )
4688 ++cmdbase;
4689 if (*cmdbase == '(')
4690 ++cmdbase;
4691
Bram Moolenaar1c465442017-03-12 20:10:05 +01004692 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004693 {
4694 STARTUPINFO si;
4695 PROCESS_INFORMATION pi;
4696 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004697 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004698 char_u *p;
4699
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004700 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004701 si.cb = sizeof(si);
4702 si.lpReserved = NULL;
4703 si.lpDesktop = NULL;
4704 si.lpTitle = NULL;
4705 si.dwFlags = 0;
4706 si.cbReserved2 = 0;
4707 si.lpReserved2 = NULL;
4708
4709 cmdbase = skipwhite(cmdbase + 5);
4710 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004711 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004712 {
4713 cmdbase = skipwhite(cmdbase + 4);
4714 si.dwFlags = STARTF_USESHOWWINDOW;
4715 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004716 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004717 }
4718 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004719 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004720 {
4721 cmdbase = skipwhite(cmdbase + 2);
4722 flags = CREATE_NO_WINDOW;
4723 si.dwFlags = STARTF_USESTDHANDLES;
4724 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004725 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004726 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004727 NULL, // Security att.
4728 OPEN_EXISTING, // Open flags
4729 FILE_ATTRIBUTE_NORMAL, // File att.
4730 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004731 si.hStdOutput = si.hStdInput;
4732 si.hStdError = si.hStdInput;
4733 }
4734
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 // Remove a trailing ", ) and )" if they have a match
4736 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004737 if (cmdbase > cmd)
4738 {
4739 p = cmdbase + STRLEN(cmdbase);
4740 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4741 *--p = NUL;
4742 if (p > cmdbase && p[-1] == ')'
4743 && (*cmd =='(' || cmd[1] == '('))
4744 *--p = NUL;
4745 }
4746
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004747 newcmd = cmdbase;
4748 unescape_shellxquote(cmdbase, p_sxe);
4749
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004750 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004751 * If creating new console, arguments are passed to the
4752 * 'cmd.exe' as-is. If it's not, arguments are not treated
4753 * correctly for current 'cmd.exe'. So unescape characters in
4754 * shellxescape except '|' for avoiding to be treated as
4755 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004756 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004757 if (flags != CREATE_NEW_CONSOLE)
4758 {
4759 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004760 char_u *cmd_shell = mch_getenv("COMSPEC");
4761
4762 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004763 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004764
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004765 subcmd = vim_strsave_escaped_ext(cmdbase,
4766 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004767 if (subcmd != NULL)
4768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004769 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004770 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004771 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004772 if (newcmd != NULL)
4773 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004774 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004775 else
4776 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004777 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004778 }
4779 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004780
4781 /*
4782 * Now, start the command as a process, so that it doesn't
4783 * inherit our handles which causes unpleasant dangling swap
4784 * files if we exit before the spawned process
4785 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004786 if (vim_create_process((char *)newcmd, FALSE, flags,
4787 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004788 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004789 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4790 > (HINSTANCE)32)
4791 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004792 else
4793 {
4794 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004795#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004796# ifdef VIMDLL
4797 if (gui.in_use)
4798# endif
4799 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004800#endif
4801 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004802
4803 if (newcmd != cmdbase)
4804 vim_free(newcmd);
4805
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004806 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004808 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004809 CloseHandle(si.hStdInput);
4810 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004811 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004812 CloseHandle(pi.hThread);
4813 CloseHandle(pi.hProcess);
4814 }
4815 else
4816 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004817 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004818#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004819 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004820 (!s_dont_use_vimrun && p_stmp ?
4821 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4822 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004824 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004825
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004826 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004827 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004829#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004830 if (
4831# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004832 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004833# endif
4834 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004836 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4837 "External commands will not pause after completion.\n"
4838 "See :help win32-vimrun for more information.");
4839 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004840 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4841 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004842
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004843 if (wmsg != NULL && wtitle != NULL)
4844 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4845 vim_free(wmsg);
4846 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 need_vimrun_warning = FALSE;
4848 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004849 if (
4850# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004851 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004852# endif
4853 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004854 // Use vimrun to execute the command. It opens a console
4855 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004856 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 vimrun_path,
4858 (msg_silent != 0 || (options & SHELL_DOOUT))
4859 ? "-s " : "",
4860 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004861 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004862# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004863 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004864# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004865 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004866 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004867 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4868 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004871 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004872 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004874 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 }
4878
4879 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004880 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
Bram Moolenaar0f873732019-12-05 20:28:46 +01004882 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004884#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004885 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004886 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888 )
4889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004890 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 msg_putchar('\n');
4892 }
4893#ifdef FEAT_TITLE
4894 resettitle();
4895#endif
4896
4897 signal(SIGINT, SIG_DFL);
4898#if defined(__GNUC__) && !defined(__MINGW32__)
4899 signal(SIGKILL, SIG_DFL);
4900#else
4901 signal(SIGBREAK, SIG_DFL);
4902#endif
4903 signal(SIGILL, SIG_DFL);
4904 signal(SIGFPE, SIG_DFL);
4905 signal(SIGSEGV, SIG_DFL);
4906 signal(SIGTERM, SIG_DFL);
4907 signal(SIGABRT, SIG_DFL);
4908
4909 return x;
4910}
4911
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004912#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004913 static HANDLE
4914job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004915 char_u *fname,
4916 DWORD dwDesiredAccess,
4917 DWORD dwShareMode,
4918 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4919 DWORD dwCreationDisposition,
4920 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004921{
4922 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004923 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004924
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004925 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004926 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004927 return INVALID_HANDLE_VALUE;
4928
4929 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4930 lpSecurityAttributes, dwCreationDisposition,
4931 dwFlagsAndAttributes, NULL);
4932 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004933 return h;
4934}
4935
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004936/*
4937 * Turn the dictionary "env" into a NUL separated list that can be used as the
4938 * environment argument of vim_create_process().
4939 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004940 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004941win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004942{
4943 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004944 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004945 LPVOID base = GetEnvironmentStringsW();
4946
Bram Moolenaar0f873732019-12-05 20:28:46 +01004947 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004948 if (ga_grow(gap, 1) == FAIL)
4949 return;
4950
4951 if (base)
4952 {
4953 WCHAR *p = (WCHAR*) base;
4954
Bram Moolenaar0f873732019-12-05 20:28:46 +01004955 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004956 if (ga_grow(gap, 1) == FAIL)
4957 return;
4958
4959 while (*p != 0 || *(p + 1) != 0)
4960 {
4961 if (ga_grow(gap, 1) == OK)
4962 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4963 p++;
4964 }
4965 FreeEnvironmentStrings(base);
4966 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4967 }
4968
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004969 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004970 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004971 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004972 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004973 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004974 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004975 typval_T *item = &dict_lookup(hi)->di_tv;
4976 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004977 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004978 --todo;
4979 if (wkey != NULL && wval != NULL)
4980 {
4981 size_t n;
4982 size_t lkey = wcslen(wkey);
4983 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004984
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004985 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4986 continue;
4987 for (n = 0; n < lkey; n++)
4988 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4989 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4990 for (n = 0; n < lval; n++)
4991 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4992 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4993 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004994 vim_free(wkey);
4995 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004996 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004997 }
4998 }
4999
Bram Moolenaar493359e2018-06-12 20:25:52 +02005000# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005001 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005002# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005003 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005004 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005005# endif
5006# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005007 char_u *version = get_vim_var_str(VV_VERSION);
5008 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005009# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005010 // size of "VIM_SERVERNAME=" and value,
5011 // plus "VIM_TERMINAL=" and value,
5012 // plus two terminating NULs
5013 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005014# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005015 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005016# endif
5017# ifdef FEAT_TERMINAL
5018 + 13 + version_len + 2
5019# endif
5020 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005021
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005022 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005023 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005024# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005025 for (n = 0; n < 15; n++)
5026 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5027 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005028 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005029 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5030 (WCHAR)servername[n];
5031 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005032# endif
5033# ifdef FEAT_TERMINAL
5034 if (is_terminal)
5035 {
5036 for (n = 0; n < 13; n++)
5037 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5038 (WCHAR)"VIM_TERMINAL="[n];
5039 for (n = 0; n < version_len; n++)
5040 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5041 (WCHAR)version[n];
5042 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5043 }
5044# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005045 }
5046 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005047# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005048}
5049
Bram Moolenaarb091f302019-01-19 14:37:00 +01005050/*
5051 * Create a pair of pipes.
5052 * Return TRUE for success, FALSE for failure.
5053 */
5054 static BOOL
5055create_pipe_pair(HANDLE handles[2])
5056{
5057 static LONG s;
5058 char name[64];
5059 SECURITY_ATTRIBUTES sa;
5060
5061 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5062 GetCurrentProcessId(),
5063 InterlockedIncrement(&s));
5064
5065 // Create named pipe. Max size of named pipe is 65535.
5066 handles[1] = CreateNamedPipe(
5067 name,
5068 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5069 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005070 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005071
5072 if (handles[1] == INVALID_HANDLE_VALUE)
5073 return FALSE;
5074
5075 sa.nLength = sizeof(sa);
5076 sa.bInheritHandle = TRUE;
5077 sa.lpSecurityDescriptor = NULL;
5078
5079 handles[0] = CreateFile(name,
5080 FILE_GENERIC_READ,
5081 FILE_SHARE_READ, &sa,
5082 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5083
5084 if (handles[0] == INVALID_HANDLE_VALUE)
5085 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005086 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005087 return FALSE;
5088 }
5089
5090 return TRUE;
5091}
5092
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005093 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005094mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005095{
5096 STARTUPINFO si;
5097 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005098 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005099 SECURITY_ATTRIBUTES saAttr;
5100 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005101 HANDLE ifd[2];
5102 HANDLE ofd[2];
5103 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005104 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005105
5106 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5107 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5108 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5109 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5110 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5111 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5112 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5113
5114 if (use_out_for_err && use_null_for_out)
5115 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005116
5117 ifd[0] = INVALID_HANDLE_VALUE;
5118 ifd[1] = INVALID_HANDLE_VALUE;
5119 ofd[0] = INVALID_HANDLE_VALUE;
5120 ofd[1] = INVALID_HANDLE_VALUE;
5121 efd[0] = INVALID_HANDLE_VALUE;
5122 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005123 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005124
Bram Moolenaar14207f42016-10-27 21:13:10 +02005125 jo = CreateJobObject(NULL, NULL);
5126 if (jo == NULL)
5127 {
5128 job->jv_status = JOB_FAILED;
5129 goto failed;
5130 }
5131
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005132 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005133 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005134
Bram Moolenaar76467df2016-02-12 19:30:26 +01005135 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005136 ZeroMemory(&si, sizeof(si));
5137 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005138 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005139 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005140
Bram Moolenaard8070362016-02-15 21:56:54 +01005141 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5142 saAttr.bInheritHandle = TRUE;
5143 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005144
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005145 if (use_file_for_in)
5146 {
5147 char_u *fname = options->jo_io_name[PART_IN];
5148
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005149 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5150 FILE_SHARE_READ | FILE_SHARE_WRITE,
5151 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5152 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005154 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005155 goto failed;
5156 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005157 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005158 else if (!use_null_for_in
5159 && (!create_pipe_pair(ifd)
5160 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005161 goto failed;
5162
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005163 if (use_file_for_out)
5164 {
5165 char_u *fname = options->jo_io_name[PART_OUT];
5166
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005167 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5168 FILE_SHARE_READ | FILE_SHARE_WRITE,
5169 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5170 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005172 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005173 goto failed;
5174 }
5175 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005176 else if (!use_null_for_out &&
5177 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005178 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005179 goto failed;
5180
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005181 if (use_file_for_err)
5182 {
5183 char_u *fname = options->jo_io_name[PART_ERR];
5184
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005185 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5186 FILE_SHARE_READ | FILE_SHARE_WRITE,
5187 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5188 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005190 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005191 goto failed;
5192 }
5193 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005194 else if (!use_out_for_err && !use_null_for_err &&
5195 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005196 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005197 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005198
Bram Moolenaard8070362016-02-15 21:56:54 +01005199 si.dwFlags |= STARTF_USESTDHANDLES;
5200 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005201 si.hStdOutput = ofd[1];
5202 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5203
5204 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5205 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005206 if (options->jo_set & JO_CHANNEL)
5207 {
5208 channel = options->jo_channel;
5209 if (channel != NULL)
5210 ++channel->ch_refcount;
5211 }
5212 else
5213 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005214 if (channel == NULL)
5215 goto failed;
5216 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005217
5218 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005219 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005220 CREATE_DEFAULT_ERROR_MODE |
5221 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005222 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005223 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005224 &si, &pi,
5225 ga.ga_data,
5226 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005227 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005228 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005229 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005230 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005231 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005232
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005233 ga_clear(&ga);
5234
Bram Moolenaar14207f42016-10-27 21:13:10 +02005235 if (!AssignProcessToJobObject(jo, pi.hProcess))
5236 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005237 // if failing, switch the way to terminate
5238 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005239 CloseHandle(jo);
5240 jo = NULL;
5241 }
5242 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005243 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005244 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005245 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005246 job->jv_status = JOB_STARTED;
5247
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005248 CloseHandle(ifd[0]);
5249 CloseHandle(ofd[1]);
5250 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005251 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005252
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005253 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005254 if (channel != NULL)
5255 {
5256 channel_set_pipes(channel,
5257 use_file_for_in || use_null_for_in
5258 ? INVALID_FD : (sock_T)ifd[1],
5259 use_file_for_out || use_null_for_out
5260 ? INVALID_FD : (sock_T)ofd[0],
5261 use_out_for_err || use_file_for_err || use_null_for_err
5262 ? INVALID_FD : (sock_T)efd[0]);
5263 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005264 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005265 return;
5266
5267failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005268 CloseHandle(ifd[0]);
5269 CloseHandle(ofd[0]);
5270 CloseHandle(efd[0]);
5271 CloseHandle(ifd[1]);
5272 CloseHandle(ofd[1]);
5273 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005274 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005275 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005276}
5277
5278 char *
5279mch_job_status(job_T *job)
5280{
5281 DWORD dwExitCode = 0;
5282
Bram Moolenaar76467df2016-02-12 19:30:26 +01005283 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5284 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005285 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005286 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005287 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005288 {
5289 ch_log(job->jv_channel, "Job ended");
5290 job->jv_status = JOB_ENDED;
5291 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005292 return "dead";
5293 }
5294 return "run";
5295}
5296
Bram Moolenaar97792de2016-10-15 18:36:49 +02005297 job_T *
5298mch_detect_ended_job(job_T *job_list)
5299{
5300 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5301 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5302 job_T *job = job_list;
5303
5304 while (job != NULL)
5305 {
5306 DWORD n;
5307 DWORD result;
5308
5309 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5310 && job != NULL; job = job->jv_next)
5311 {
5312 if (job->jv_status == JOB_STARTED)
5313 {
5314 jobHandles[n] = job->jv_proc_info.hProcess;
5315 jobArray[n] = job;
5316 ++n;
5317 }
5318 }
5319 if (n == 0)
5320 continue;
5321 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5322 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5323 {
5324 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5325
5326 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5327 return wait_job;
5328 }
5329 }
5330 return NULL;
5331}
5332
Bram Moolenaarfb630902016-10-29 14:55:00 +02005333 static BOOL
5334terminate_all(HANDLE process, int code)
5335{
5336 PROCESSENTRY32 pe;
5337 HANDLE h = INVALID_HANDLE_VALUE;
5338 DWORD pid = GetProcessId(process);
5339
5340 if (pid != 0)
5341 {
5342 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5343 if (h != INVALID_HANDLE_VALUE)
5344 {
5345 pe.dwSize = sizeof(PROCESSENTRY32);
5346 if (!Process32First(h, &pe))
5347 goto theend;
5348
5349 do
5350 {
5351 if (pe.th32ParentProcessID == pid)
5352 {
5353 HANDLE ph = OpenProcess(
5354 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5355 if (ph != NULL)
5356 {
5357 terminate_all(ph, code);
5358 CloseHandle(ph);
5359 }
5360 }
5361 } while (Process32Next(h, &pe));
5362
5363 CloseHandle(h);
5364 }
5365 }
5366
5367theend:
5368 return TerminateProcess(process, code);
5369}
5370
5371/*
5372 * Send a (deadly) signal to "job".
5373 * Return FAIL if it didn't work.
5374 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005375 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005376mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005377{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005378 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005379
Bram Moolenaar923d9262016-02-25 20:56:01 +01005380 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005381 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005382 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005383 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005384 {
5385 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5386 job->jv_channel->ch_killing = TRUE;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005387 return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005388 }
Bram Moolenaarfb630902016-10-29 14:55:00 +02005389 return terminate_all(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005390 }
5391
5392 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5393 return FAIL;
5394 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005395 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5396 job->jv_proc_info.dwProcessId)
5397 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005398 FreeConsole();
5399 return ret;
5400}
5401
5402/*
5403 * Clear the data related to "job".
5404 */
5405 void
5406mch_clear_job(job_T *job)
5407{
5408 if (job->jv_status != JOB_FAILED)
5409 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005410 if (job->jv_job_object != NULL)
5411 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005412 CloseHandle(job->jv_proc_info.hProcess);
5413 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005414}
5415#endif
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005418#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420/*
5421 * Start termcap mode
5422 */
5423 static void
5424termcap_mode_start(void)
5425{
5426 DWORD cmodein;
5427
5428 if (g_fTermcapMode)
5429 return;
5430
5431 SaveConsoleBuffer(&g_cbNonTermcap);
5432
5433 if (g_cbTermcap.IsValid)
5434 {
5435 /*
5436 * We've been in termcap mode before. Restore certain screen
5437 * characteristics, including the buffer size and the window
5438 * size. Since we will be redrawing the screen, we don't need
5439 * to restore the actual contents of the buffer.
5440 */
5441 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005442 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5444 Rows = g_cbTermcap.Info.dwSize.Y;
5445 Columns = g_cbTermcap.Info.dwSize.X;
5446 }
5447 else
5448 {
5449 /*
5450 * This is our first time entering termcap mode. Clear the console
5451 * screen buffer, and resize the buffer to match the current window
5452 * size. We will use this as the size of our editing environment.
5453 */
5454 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005455 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5457 }
5458
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005459# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 if (g_fMouseActive)
5465 cmodein |= ENABLE_MOUSE_INPUT;
5466 else
5467 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 cmodein |= ENABLE_WINDOW_INPUT;
5469 SetConsoleMode(g_hConIn, cmodein);
5470
5471 redraw_later_clear();
5472 g_fTermcapMode = TRUE;
5473}
5474
5475
5476/*
5477 * End termcap mode
5478 */
5479 static void
5480termcap_mode_end(void)
5481{
5482 DWORD cmodein;
5483 ConsoleBuffer *cb;
5484 COORD coord;
5485 DWORD dwDummy;
5486
5487 if (!g_fTermcapMode)
5488 return;
5489
5490 SaveConsoleBuffer(&g_cbTermcap);
5491
5492 GetConsoleMode(g_hConIn, &cmodein);
5493 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5494 SetConsoleMode(g_hConIn, cmodein);
5495
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005496# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005497 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005498# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005502 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 SetConsoleCursorInfo(g_hConOut, &g_cci);
5504
5505 if (p_rs || exiting)
5506 {
5507 /*
5508 * Clear anything that happens to be on the current line.
5509 */
5510 coord.X = 0;
5511 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5512 FillConsoleOutputCharacter(g_hConOut, ' ',
5513 cb->Info.dwSize.X, coord, &dwDummy);
5514 /*
5515 * The following is just for aesthetics. If we are exiting without
5516 * restoring the screen, then we want to have a prompt string
5517 * appear at the bottom line. However, the command interpreter
5518 * seems to always advance the cursor one line before displaying
5519 * the prompt string, which causes the screen to scroll. To
5520 * counter this, move the cursor up one line before exiting.
5521 */
5522 if (exiting && !p_rs)
5523 coord.Y--;
5524 /*
5525 * Position the cursor at the leftmost column of the desired row.
5526 */
5527 SetConsoleCursorPosition(g_hConOut, coord);
5528 }
5529
5530 g_fTermcapMode = FALSE;
5531}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005532#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005535#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 void
5537mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005538 char_u *s UNUSED,
5539 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005541 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542}
5543
5544#else
5545
5546/*
5547 * clear `n' chars, starting from `coord'
5548 */
5549 static void
5550clear_chars(
5551 COORD coord,
5552 DWORD n)
5553{
5554 DWORD dwDummy;
5555
5556 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005557
5558 if (!USE_VTP)
5559 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5560 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005561 {
5562 set_console_color_rgb();
5563 gotoxy(coord.X + 1, coord.Y + 1);
5564 vtp_printf("\033[%dX", n);
5565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567
5568
5569/*
5570 * Clear the screen
5571 */
5572 static void
5573clear_screen(void)
5574{
5575 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005576
5577 if (!USE_VTP)
5578 clear_chars(g_coord, Rows * Columns);
5579 else
5580 {
5581 set_console_color_rgb();
5582 gotoxy(1, 1);
5583 vtp_printf("\033[2J");
5584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587
5588/*
5589 * Clear to end of display
5590 */
5591 static void
5592clear_to_end_of_display(void)
5593{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005594 COORD save = g_coord;
5595
5596 if (!USE_VTP)
5597 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005599 else
5600 {
5601 set_console_color_rgb();
5602 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5603 vtp_printf("\033[0J");
5604
5605 gotoxy(save.X + 1, save.Y + 1);
5606 g_coord = save;
5607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610
5611/*
5612 * Clear to end of line
5613 */
5614 static void
5615clear_to_end_of_line(void)
5616{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005617 COORD save = g_coord;
5618
5619 if (!USE_VTP)
5620 clear_chars(g_coord, Columns - g_coord.X);
5621 else
5622 {
5623 set_console_color_rgb();
5624 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5625 vtp_printf("\033[0K");
5626
5627 gotoxy(save.X + 1, save.Y + 1);
5628 g_coord = save;
5629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630}
5631
5632
5633/*
5634 * Scroll the scroll region up by `cLines' lines
5635 */
5636 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005637scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 COORD oldcoord = g_coord;
5640
5641 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5642 delete_lines(cLines);
5643
5644 g_coord = oldcoord;
5645}
5646
5647
5648/*
5649 * Set the scroll region
5650 */
5651 static void
5652set_scroll_region(
5653 unsigned left,
5654 unsigned top,
5655 unsigned right,
5656 unsigned bottom)
5657{
5658 if (left >= right
5659 || top >= bottom
5660 || right > (unsigned) Columns - 1
5661 || bottom > (unsigned) Rows - 1)
5662 return;
5663
5664 g_srScrollRegion.Left = left;
5665 g_srScrollRegion.Top = top;
5666 g_srScrollRegion.Right = right;
5667 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005668}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005669
Bram Moolenaar6982f422019-02-16 16:48:01 +01005670 static void
5671set_scroll_region_tb(
5672 unsigned top,
5673 unsigned bottom)
5674{
5675 if (top >= bottom || bottom > (unsigned)Rows - 1)
5676 return;
5677
5678 g_srScrollRegion.Top = top;
5679 g_srScrollRegion.Bottom = bottom;
5680}
5681
5682 static void
5683set_scroll_region_lr(
5684 unsigned left,
5685 unsigned right)
5686{
5687 if (left >= right || right > (unsigned)Columns - 1)
5688 return;
5689
5690 g_srScrollRegion.Left = left;
5691 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692}
5693
5694
5695/*
5696 * Insert `cLines' lines at the current cursor position
5697 */
5698 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005699insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005701 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 COORD dest;
5703 CHAR_INFO fill;
5704
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005705 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5706
Bram Moolenaar6982f422019-02-16 16:48:01 +01005707 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 dest.Y = g_coord.Y + cLines;
5709
Bram Moolenaar6982f422019-02-16 16:48:01 +01005710 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 source.Top = g_coord.Y;
5712 source.Right = g_srScrollRegion.Right;
5713 source.Bottom = g_srScrollRegion.Bottom - cLines;
5714
Bram Moolenaar6982f422019-02-16 16:48:01 +01005715 clip.Left = g_srScrollRegion.Left;
5716 clip.Top = g_coord.Y;
5717 clip.Right = g_srScrollRegion.Right;
5718 clip.Bottom = g_srScrollRegion.Bottom;
5719
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005720 fill.Char.AsciiChar = ' ';
5721 if (!USE_VTP)
5722 fill.Attributes = g_attrCurrent;
5723 else
5724 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005726 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005727
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005728 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5729
Bram Moolenaar6982f422019-02-16 16:48:01 +01005730 // Here we have to deal with a win32 console flake: If the scroll
5731 // region looks like abc and we scroll c to a and fill with d we get
5732 // cbd... if we scroll block c one line at a time to a, we get cdd...
5733 // vim expects cdd consistently... So we have to deal with that
5734 // here... (this also occurs scrolling the same way in the other
5735 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 if (source.Bottom < dest.Y)
5738 {
5739 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005740 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar6982f422019-02-16 16:48:01 +01005742 coord.X = source.Left;
5743 for (i = clip.Top; i < dest.Y; ++i)
5744 {
5745 coord.Y = i;
5746 clear_chars(coord, source.Right - source.Left + 1);
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749}
5750
5751
5752/*
5753 * Delete `cLines' lines at the current cursor position
5754 */
5755 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005756delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005758 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 COORD dest;
5760 CHAR_INFO fill;
5761 int nb;
5762
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005763 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5764
Bram Moolenaar6982f422019-02-16 16:48:01 +01005765 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 dest.Y = g_coord.Y;
5767
Bram Moolenaar6982f422019-02-16 16:48:01 +01005768 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 source.Top = g_coord.Y + cLines;
5770 source.Right = g_srScrollRegion.Right;
5771 source.Bottom = g_srScrollRegion.Bottom;
5772
Bram Moolenaar6982f422019-02-16 16:48:01 +01005773 clip.Left = g_srScrollRegion.Left;
5774 clip.Top = g_coord.Y;
5775 clip.Right = g_srScrollRegion.Right;
5776 clip.Bottom = g_srScrollRegion.Bottom;
5777
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005778 fill.Char.AsciiChar = ' ';
5779 if (!USE_VTP)
5780 fill.Attributes = g_attrCurrent;
5781 else
5782 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005784 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005785
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005786 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5787
Bram Moolenaar6982f422019-02-16 16:48:01 +01005788 // Here we have to deal with a win32 console flake; See insert_lines()
5789 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 nb = dest.Y + (source.Bottom - source.Top) + 1;
5792
5793 if (nb < source.Top)
5794 {
5795 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005796 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar6982f422019-02-16 16:48:01 +01005798 coord.X = source.Left;
5799 for (i = nb; i < clip.Bottom; ++i)
5800 {
5801 coord.Y = i;
5802 clear_chars(coord, source.Right - source.Left + 1);
5803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805}
5806
5807
5808/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005809 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 */
5811 static void
5812gotoxy(
5813 unsigned x,
5814 unsigned y)
5815{
5816 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5817 return;
5818
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005819 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005820 {
5821 // external cursor coords are 1-based; internal are 0-based
5822 g_coord.X = x - 1;
5823 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005824 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005825 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005826 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005827 {
5828 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005829 // destruction. Insider build bug. Always enabled because it's cheap
5830 // and avoids mistakes with recognizing the build.
5831 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005832
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005833 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005834
5835 g_coord.X = x - 1;
5836 g_coord.Y = y - 1;
5837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
5839
5840
5841/*
5842 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005843 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
5845 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005846textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005848 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 SetConsoleTextAttribute(g_hConOut, wAttr);
5851}
5852
5853
5854 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005855textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005857 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005859 if (!USE_VTP)
5860 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5861 else
5862 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864
5865
5866 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005867textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005869 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005871 if (!USE_VTP)
5872 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5873 else
5874 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875}
5876
5877
5878/*
5879 * restore the default text attribute (whatever we started with)
5880 */
5881 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005882normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005884 if (!USE_VTP)
5885 textattr(g_attrDefault);
5886 else
5887 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889
5890
5891static WORD g_attrPreStandout = 0;
5892
5893/*
5894 * Make the text standout, by brightening it
5895 */
5896 static void
5897standout(void)
5898{
5899 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5902}
5903
5904
5905/*
5906 * Turn off standout mode
5907 */
5908 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005909standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005913
5914 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915}
5916
5917
5918/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005919 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 */
5921 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005922mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 char_u *p;
5925 int n;
5926
5927 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5928 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005929 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005930# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005931 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005932# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005933 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
5935 p = T_ME + 2;
5936 n = getdigits(&p);
5937 if (*p == 'm' && n > 0)
5938 {
5939 cterm_normal_fg_color = (n & 0xf) + 1;
5940 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5941 }
5942 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005943# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005944 cterm_normal_fg_gui_color = INVALCOLOR;
5945 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947}
5948
5949
5950/*
5951 * visual bell: flash the screen
5952 */
5953 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005954visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 COORD coordOrigin = {0, 0};
5957 WORD attrFlash = ~g_attrCurrent & 0xff;
5958
5959 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005960 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
5962 if (oldattrs == NULL)
5963 return;
5964 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5965 coordOrigin, &dwDummy);
5966 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5967 coordOrigin, &dwDummy);
5968
Bram Moolenaar0f873732019-12-05 20:28:46 +01005969 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005970 if (!USE_VTP)
5971 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 coordOrigin, &dwDummy);
5973 vim_free(oldattrs);
5974}
5975
5976
5977/*
5978 * Make the cursor visible or invisible
5979 */
5980 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005981cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005984# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987}
5988
5989
5990/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005991 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005992 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005994 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005996 char_u *pchBuf,
5997 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005999 COORD coord = g_coord;
6000 DWORD written;
6001 DWORD n, cchwritten, cells;
6002 static WCHAR *unicodebuf = NULL;
6003 static int unibuflen = 0;
6004 int length;
6005 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006007 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6008 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006009 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006010 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006011 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006012 unibuflen = length;
6013 }
6014 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6015 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006017 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006018
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006019 if (!USE_VTP)
6020 {
6021 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6022 coord, &written);
6023 // When writing fails or didn't write a single character, pretend one
6024 // character was written, otherwise we get stuck.
6025 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6026 coord, &cchwritten) == 0
6027 || cchwritten == 0 || cchwritten == (DWORD)-1)
6028 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006029 }
6030 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006031 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006032 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6033 NULL) == 0 || cchwritten == 0)
6034 cchwritten = 1;
6035 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006036
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006037 if (cchwritten == length)
6038 {
6039 written = cbToWrite;
6040 g_coord.X += (SHORT)cells;
6041 }
6042 else
6043 {
6044 char_u *p = pchBuf;
6045 for (n = 0; n < cchwritten; n++)
6046 MB_CPTR_ADV(p);
6047 written = p - pchBuf;
6048 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051 while (g_coord.X > g_srScrollRegion.Right)
6052 {
6053 g_coord.X -= (SHORT) Columns;
6054 if (g_coord.Y < g_srScrollRegion.Bottom)
6055 g_coord.Y++;
6056 }
6057
6058 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6059
6060 return written;
6061}
6062
6063
6064/*
6065 * mch_write(): write the output buffer to the screen, translating ESC
6066 * sequences into calls to console output routines.
6067 */
6068 void
6069mch_write(
6070 char_u *s,
6071 int len)
6072{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006073# ifdef VIMDLL
6074 if (gui.in_use)
6075 return;
6076# endif
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 s[len] = NUL;
6079
6080 if (!term_console)
6081 {
6082 write(1, s, (unsigned)len);
6083 return;
6084 }
6085
Bram Moolenaar0f873732019-12-05 20:28:46 +01006086 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 while (len--)
6088 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006089 // optimization: use one single write_chars for runs of text,
6090 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006091 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
6093 if (p_wd)
6094 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006095 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (prefix != 0)
6097 prefix = 1;
6098 }
6099
6100 if (prefix != 0)
6101 {
6102 DWORD nWritten;
6103
6104 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006105# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (fdDump)
6107 {
6108 fputc('>', fdDump);
6109 fwrite(s, sizeof(char_u), nWritten, fdDump);
6110 fputs("<\n", fdDump);
6111 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 len -= (nWritten - 1);
6114 s += nWritten;
6115 }
6116 else if (s[0] == '\n')
6117 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006118 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (g_coord.Y == g_srScrollRegion.Bottom)
6120 {
6121 scroll(1);
6122 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6123 }
6124 else
6125 {
6126 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6127 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006128# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (fdDump)
6130 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 s++;
6133 }
6134 else if (s[0] == '\r')
6135 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006136 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006138# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (fdDump)
6140 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 s++;
6143 }
6144 else if (s[0] == '\b')
6145 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006146 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 if (g_coord.X > g_srScrollRegion.Left)
6148 g_coord.X--;
6149 else if (g_coord.Y > g_srScrollRegion.Top)
6150 {
6151 g_coord.X = g_srScrollRegion.Right;
6152 g_coord.Y--;
6153 }
6154 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006155# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (fdDump)
6157 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 s++;
6160 }
6161 else if (s[0] == '\a')
6162 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006163 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006165# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (fdDump)
6167 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 s++;
6170 }
6171 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6172 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006173# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006174 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006175# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006176 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006177 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 switch (s[2])
6180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 case '0': case '1': case '2': case '3': case '4':
6182 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006183 p = s + 1;
6184 do
6185 {
6186 ++p;
6187 args[argc] = getdigits(&p);
6188 argc += (argc < 15) ? 1 : 0;
6189 if (p > s + len)
6190 break;
6191 } while (*p == ';');
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (p > s + len)
6194 break;
6195
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006196 arg1 = args[0];
6197 arg2 = args[1];
6198 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006200 if (argc == 1 && args[0] == 0)
6201 normvideo();
6202 else if (argc == 1)
6203 {
6204 if (USE_VTP)
6205 textcolor((WORD) arg1);
6206 else
6207 textattr((WORD) arg1);
6208 }
6209 else if (USE_VTP)
6210 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006212 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006214 gotoxy(arg2, arg1);
6215 }
6216 else if (argc == 2 && *p == 'r')
6217 {
6218 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6219 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006220 else if (argc == 2 && *p == 'R')
6221 {
6222 set_scroll_region_tb(arg1, arg2);
6223 }
6224 else if (argc == 2 && *p == 'V')
6225 {
6226 set_scroll_region_lr(arg1, arg2);
6227 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006228 else if (argc == 1 && *p == 'A')
6229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 gotoxy(g_coord.X + 1,
6231 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6232 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006233 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 textbackground((WORD) arg1);
6236 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006237 else if (argc == 1 && *p == 'C')
6238 {
6239 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6240 g_coord.Y + 1);
6241 }
6242 else if (argc == 1 && *p == 'f')
6243 {
6244 textcolor((WORD) arg1);
6245 }
6246 else if (argc == 1 && *p == 'H')
6247 {
6248 gotoxy(1, arg1);
6249 }
6250 else if (argc == 1 && *p == 'L')
6251 {
6252 insert_lines(arg1);
6253 }
6254 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
6256 delete_lines(arg1);
6257 }
6258
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006259 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 s = p + 1;
6261 break;
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 gotoxy(g_coord.X + 1,
6265 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6266 goto got3;
6267
6268 case 'B':
6269 visual_bell();
6270 goto got3;
6271
6272 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6274 g_coord.Y + 1);
6275 goto got3;
6276
6277 case 'E':
6278 termcap_mode_end();
6279 goto got3;
6280
6281 case 'F':
6282 standout();
6283 goto got3;
6284
6285 case 'f':
6286 standend();
6287 goto got3;
6288
6289 case 'H':
6290 gotoxy(1, 1);
6291 goto got3;
6292
6293 case 'j':
6294 clear_to_end_of_display();
6295 goto got3;
6296
6297 case 'J':
6298 clear_screen();
6299 goto got3;
6300
6301 case 'K':
6302 clear_to_end_of_line();
6303 goto got3;
6304
6305 case 'L':
6306 insert_lines(1);
6307 goto got3;
6308
6309 case 'M':
6310 delete_lines(1);
6311 goto got3;
6312
6313 case 'S':
6314 termcap_mode_start();
6315 goto got3;
6316
6317 case 'V':
6318 cursor_visible(TRUE);
6319 goto got3;
6320
6321 case 'v':
6322 cursor_visible(FALSE);
6323 goto got3;
6324
6325 got3:
6326 s += 3;
6327 len -= 2;
6328 }
6329
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006330# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 if (fdDump)
6332 {
6333 fputs("ESC | ", fdDump);
6334 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6335 fputc('\n', fdDump);
6336 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 else
6340 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006341 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 DWORD nWritten;
6343
6344 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006345# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (fdDump)
6347 {
6348 fputc('>', fdDump);
6349 fwrite(s, sizeof(char_u), nWritten, fdDump);
6350 fputs("<\n", fdDump);
6351 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 len -= (nWritten - 1);
6355 s += nWritten;
6356 }
6357 }
6358
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006359# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (fdDump)
6361 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
Bram Moolenaar0f873732019-12-05 20:28:46 +01006365#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
6367
6368/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006369 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 */
6371 void
6372mch_delay(
6373 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006374 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006376#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006377 Sleep((int)msec); // never wait for input
6378#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006379# ifdef VIMDLL
6380 if (gui.in_use)
6381 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006382 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006383 return;
6384 }
6385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006387# ifdef FEAT_MZSCHEME
6388 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6389 {
6390 int towait = p_mzq;
6391
Bram Moolenaar0f873732019-12-05 20:28:46 +01006392 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006393 while (msec > 0)
6394 {
6395 mzvim_check_threads();
6396 if (msec < towait)
6397 towait = msec;
6398 Sleep(towait);
6399 msec -= towait;
6400 }
6401 }
6402 else
6403# endif
6404 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006406 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407#endif
6408}
6409
6410
6411/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006412 * This version of remove is not scared by a readonly (backup) file.
6413 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * Return 0 for success, -1 for failure.
6415 */
6416 int
6417mch_remove(char_u *name)
6418{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006419 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 int n;
6421
Bram Moolenaar203258c2016-01-17 22:15:16 +01006422 /*
6423 * On Windows, deleting a directory's symbolic link is done by
6424 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6425 */
6426 if (mch_isdir(name) && mch_is_symbolic_link(name))
6427 return mch_rmdir(name);
6428
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006429 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6430
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006431 wn = enc_to_utf16(name, NULL);
6432 if (wn == NULL)
6433 return -1;
6434
6435 n = DeleteFileW(wn) ? 0 : -1;
6436 vim_free(wn);
6437 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438}
6439
6440
6441/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006442 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 */
6444 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006445mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006447#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6448# ifdef VIMDLL
6449 if (!gui.in_use)
6450# endif
6451 if (g_fCtrlCPressed || g_fCBrkPressed)
6452 {
6453 ctrl_break_was_pressed = g_fCBrkPressed;
6454 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6455 got_int = TRUE;
6456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457#endif
6458}
6459
Bram Moolenaar0f873732019-12-05 20:28:46 +01006460// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006461#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006462
6463/*
6464 * How much main memory in KiB that can be used by VIM.
6465 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006466 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006467mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006468{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006469 MEMORYSTATUSEX ms;
6470
Bram Moolenaar0f873732019-12-05 20:28:46 +01006471 // Need to use GlobalMemoryStatusEx() when there is more memory than
6472 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006473 ms.dwLength = sizeof(MEMORYSTATUSEX);
6474 GlobalMemoryStatusEx(&ms);
6475 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006478 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006479 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006480 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006481 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006482 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006483 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006484 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006485 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006486 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006490 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6492 * file whose short file name is "FOO.BAR" (its long file name will
6493 * be correct: "foo.bar~"). Because a file can be accessed by
6494 * either its SFN or its LFN, "foo.bar" has effectively been
6495 * renamed to "foo.bar", which is not at all what was wanted. This
6496 * seems to happen only when renaming files with three-character
6497 * extensions by appending a suffix that does not include ".".
6498 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6499 *
6500 * There is another problem, which isn't really a bug but isn't right either:
6501 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6502 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6503 * service pack 6. Doesn't seem to happen on Windows 98.
6504 *
6505 * Like rename(), returns 0 upon success, non-zero upon failure.
6506 * Should probably set errno appropriately when errors occur.
6507 */
6508 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006509mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006511 WCHAR *p;
6512 int i;
6513 WCHAR szTempFile[_MAX_PATH + 1];
6514 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006517 // No need to play tricks unless the file name contains a "~" as the
6518 // seventh character.
6519 p = wold;
6520 for (i = 0; wold[i] != NUL; ++i)
6521 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6522 && wold[i + 1] != 0)
6523 p = wold + i + 1;
6524 if ((int)(wold + i - p) < 8 || p[6] != '~')
6525 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006527 // Get base path of new file name. Undocumented feature: If pszNewFile is
6528 // a directory, no error is returned and pszFilePart will be NULL.
6529 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006531 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006533 // Get (and create) a unique temporary file name in directory of new file
6534 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 return -2;
6536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006537 // blow the temp file away
6538 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 return -3;
6540
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006541 // rename old file to the temp file
6542 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 return -4;
6544
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006545 // now create an empty file called pszOldFile; this prevents the operating
6546 // system using pszOldFile as an alias (SFN) if we're renaming within the
6547 // same directory. For example, we're editing a file called
6548 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6549 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6550 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6551 // cause all sorts of problems later in buf_write(). So, we create an
6552 // empty file called filena~1.txt and the system will have to find some
6553 // other SFN for filena~1.txt~, such as filena~2.txt
6554 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6556 return -5;
6557 if (!CloseHandle(hf))
6558 return -6;
6559
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006560 // rename the temp file to the new file
6561 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 // Renaming failed. Rename the file back to its old name, so that it
6564 // looks like nothing happened.
6565 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return -7;
6567 }
6568
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006569 // Seems to be left around on Novell filesystems
6570 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006572 // finally, remove the empty old file
6573 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 return -8;
6575
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006576 return 0;
6577}
6578
6579
6580/*
6581 * Converts the filenames to UTF-16, then call mch_wrename().
6582 * Like rename(), returns 0 upon success, non-zero upon failure.
6583 */
6584 int
6585mch_rename(
6586 const char *pszOldFile,
6587 const char *pszNewFile)
6588{
6589 WCHAR *wold = NULL;
6590 WCHAR *wnew = NULL;
6591 int retval = -1;
6592
6593 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6594 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6595 if (wold != NULL && wnew != NULL)
6596 retval = mch_wrename(wold, wnew);
6597 vim_free(wold);
6598 vim_free(wnew);
6599 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600}
6601
6602/*
6603 * Get the default shell for the current hardware platform
6604 */
6605 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006606default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006608 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
6611/*
6612 * mch_access() extends access() to do more detailed check on network drives.
6613 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6614 */
6615 int
6616mch_access(char *n, int p)
6617{
6618 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006619 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006620 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006622 wn = enc_to_utf16((char_u *)n, NULL);
6623 if (wn == NULL)
6624 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006626 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 if (p & R_OK)
6631 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006632 // Read check is performed by seeing if we can do a find file on
6633 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006634 int i;
6635 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006637 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6638 TempNameW[i] = wn[i];
6639 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6640 TempNameW[i++] = '\\';
6641 TempNameW[i++] = '*';
6642 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006644 hFile = FindFirstFileW(TempNameW, &d);
6645 if (hFile == INVALID_HANDLE_VALUE)
6646 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
6650
6651 if (p & W_OK)
6652 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006653 // Trying to create a temporary file in the directory should catch
6654 // directories on read-only network shares. However, in
6655 // directories whose ACL allows writes but denies deletes will end
6656 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006657 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6658 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006659 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006660 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662 }
6663 else
6664 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006665 // Don't consider a file read-only if another process has opened it.
6666 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6667
Bram Moolenaar0f873732019-12-05 20:28:46 +01006668 // Trying to open the file for the required access does ACL, read-only
6669 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006670 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6671 | ((p & R_OK) ? GENERIC_READ : 0);
6672
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006673 hFile = CreateFileW(wn, access_mode, share_mode,
6674 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (hFile == INVALID_HANDLE_VALUE)
6676 goto getout;
6677 CloseHandle(hFile);
6678 }
6679
Bram Moolenaar0f873732019-12-05 20:28:46 +01006680 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 return retval;
6684}
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006687 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
6689 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006690mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 WCHAR *wn;
6693 int f;
6694
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006695 wn = enc_to_utf16((char_u *)name, NULL);
6696 if (wn == NULL)
6697 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006699 f = _wopen(wn, flags, mode);
6700 vim_free(wn);
6701 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702}
6703
6704/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006705 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 */
6707 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006708mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 WCHAR *wn, *wm;
6711 FILE *f = NULL;
6712
Bram Moolenaara12a1612019-01-24 16:39:02 +01006713#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 // Work around an annoying assertion in the Microsoft debug CRT
6715 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006716 char newMode = mode[strlen(mode) - 1];
6717 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006718
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006719 _get_fmode(&oldMode);
6720 if (newMode == 't')
6721 _set_fmode(_O_TEXT);
6722 else if (newMode == 'b')
6723 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006724#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006725 wn = enc_to_utf16((char_u *)name, NULL);
6726 wm = enc_to_utf16((char_u *)mode, NULL);
6727 if (wn != NULL && wm != NULL)
6728 f = _wfopen(wn, wm);
6729 vim_free(wn);
6730 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006731
Bram Moolenaara12a1612019-01-24 16:39:02 +01006732#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006733 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006734#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006735 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738/*
6739 * SUB STREAM (aka info stream) handling:
6740 *
6741 * NTFS can have sub streams for each file. Normal contents of file is
6742 * stored in the main stream, and extra contents (author information and
6743 * title and so on) can be stored in sub stream. After Windows 2000, user
6744 * can access and store those informations in sub streams via explorer's
6745 * property menuitem in right click menu. Those informations in sub streams
6746 * were lost when copying only the main stream. So we have to copy sub
6747 * streams.
6748 *
6749 * Incomplete explanation:
6750 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6751 * More useful info and an example:
6752 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6753 */
6754
6755/*
6756 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6757 * and write to stream "substream" of file "to".
6758 * Errors are ignored.
6759 */
6760 static void
6761copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6762{
6763 HANDLE hTo;
6764 WCHAR *to_name;
6765
6766 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6767 wcscpy(to_name, to);
6768 wcscat(to_name, substream);
6769
6770 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6771 FILE_ATTRIBUTE_NORMAL, NULL);
6772 if (hTo != INVALID_HANDLE_VALUE)
6773 {
6774 long done;
6775 DWORD todo;
6776 DWORD readcnt, written;
6777 char buf[4096];
6778
Bram Moolenaar0f873732019-12-05 20:28:46 +01006779 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 for (done = 0; done < len; done += written)
6781 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006783 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6784 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6786 FALSE, FALSE, context)
6787 || readcnt != todo
6788 || !WriteFile(hTo, buf, todo, &written, NULL)
6789 || written != todo)
6790 break;
6791 }
6792 CloseHandle(hTo);
6793 }
6794
6795 free(to_name);
6796}
6797
6798/*
6799 * Copy info streams from file "from" to file "to".
6800 */
6801 static void
6802copy_infostreams(char_u *from, char_u *to)
6803{
6804 WCHAR *fromw;
6805 WCHAR *tow;
6806 HANDLE sh;
6807 WIN32_STREAM_ID sid;
6808 int headersize;
6809 WCHAR streamname[_MAX_PATH];
6810 DWORD readcount;
6811 void *context = NULL;
6812 DWORD lo, hi;
6813 int len;
6814
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006816 fromw = enc_to_utf16(from, NULL);
6817 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (fromw != NULL && tow != NULL)
6819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6822 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6823 if (sh != INVALID_HANDLE_VALUE)
6824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006825 // Use BackupRead() to find the info streams. Repeat until we
6826 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 for (;;)
6828 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 // Get the header to find the length of the stream name. If
6830 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006832 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6834 &readcount, FALSE, FALSE, &context)
6835 || readcount == 0)
6836 break;
6837
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // We only deal with streams that have a name. The normal
6839 // file data appears to be without a name, even though docs
6840 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (sid.dwStreamNameSize > 0)
6842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (!BackupRead(sh, (LPBYTE)streamname,
6845 sid.dwStreamNameSize,
6846 &readcount, FALSE, FALSE, &context))
6847 break;
6848
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 // Copy an info stream with a name ":anything:$DATA".
6850 // Skip "::$DATA", it has no stream name (examples suggest
6851 // it might be used for the normal file contents).
6852 // Note that BackupRead() counts bytes, but the name is in
6853 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 len = readcount / sizeof(WCHAR);
6855 streamname[len] = 0;
6856 if (len > 7 && wcsicmp(streamname + len - 6,
6857 L":$DATA") == 0)
6858 {
6859 streamname[len - 6] = 0;
6860 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006861 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863 }
6864
Bram Moolenaar0f873732019-12-05 20:28:46 +01006865 // Advance to the next stream. We might try seeking too far,
6866 // but BackupSeek() doesn't skip over stream borders, thus
6867 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006868 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 &lo, &hi, &context);
6870 }
6871
Bram Moolenaar0f873732019-12-05 20:28:46 +01006872 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6874
6875 CloseHandle(sh);
6876 }
6877 }
6878 vim_free(fromw);
6879 vim_free(tow);
6880}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
6882/*
6883 * Copy file attributes from file "from" to file "to".
6884 * For Windows NT and later we copy info streams.
6885 * Always returns zero, errors are ignored.
6886 */
6887 int
6888mch_copy_file_attribute(char_u *from, char_u *to)
6889{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006891 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 return 0;
6893}
6894
6895#if defined(MYRESETSTKOFLW) || defined(PROTO)
6896/*
6897 * Recreate a destroyed stack guard page in win32.
6898 * Written by Benjamin Peterson.
6899 */
6900
Bram Moolenaar0f873732019-12-05 20:28:46 +01006901// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006902# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904/*
6905 * This function does the same thing as _resetstkoflw(), which is only
6906 * available in DevStudio .net and later.
6907 * Returns 0 for failure, 1 for success.
6908 */
6909 int
6910myresetstkoflw(void)
6911{
6912 BYTE *pStackPtr;
6913 BYTE *pGuardPage;
6914 BYTE *pStackBase;
6915 BYTE *pLowestPossiblePage;
6916 MEMORY_BASIC_INFORMATION mbi;
6917 SYSTEM_INFO si;
6918 DWORD nPageSize;
6919 DWORD dummy;
6920
Bram Moolenaar0f873732019-12-05 20:28:46 +01006921 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 GetSystemInfo(&si);
6923 nPageSize = si.dwPageSize;
6924
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 pStackPtr = (BYTE*)_alloca(1);
6927
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6930 return 0;
6931 pStackBase = (BYTE*)mbi.AllocationBase;
6932
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006933 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006935 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006938 // We want the first committed page in the stack Start at the stack
6939 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 BYTE *pBlock = pStackBase;
6941
Bram Moolenaara466c992005-07-09 21:03:22 +00006942 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {
6944 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6945 return 0;
6946
6947 pBlock += mbi.RegionSize;
6948
6949 if (mbi.State & MEM_COMMIT)
6950 break;
6951 }
6952
Bram Moolenaar0f873732019-12-05 20:28:46 +01006953 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (mbi.Protect & PAGE_GUARD)
6955 return 1;
6956
Bram Moolenaar0f873732019-12-05 20:28:46 +01006957 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6959 pGuardPage = pLowestPossiblePage;
6960 else
6961 pGuardPage = (BYTE*)mbi.BaseAddress;
6962
Bram Moolenaar0f873732019-12-05 20:28:46 +01006963 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6965 return 0;
6966
Bram Moolenaar0f873732019-12-05 20:28:46 +01006967 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6969 &dummy))
6970 return 0;
6971 }
6972
6973 return 1;
6974}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006977
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006978/*
6979 * The command line arguments in UCS2
6980 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006981static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006982static LPWSTR *ArglistW = NULL;
6983static int global_argc = 0;
6984static char **global_argv;
6985
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986static int used_file_argc = 0; // last argument in global_argv[] used
6987 // for the argument list.
6988static int *used_file_indexes = NULL; // indexes in global_argv[] for
6989 // command line arguments added to
6990 // the argument list
6991static int used_file_count = 0; // nr of entries in used_file_indexes
6992static int used_file_literal = FALSE; // take file names literally
6993static int used_file_full_path = FALSE; // file name was full path
6994static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006995static int used_alist_count = 0;
6996
6997
6998/*
6999 * Get the command line arguments. Unicode version.
7000 * Returns argc. Zero when something fails.
7001 */
7002 int
7003get_cmd_argsW(char ***argvp)
7004{
7005 char **argv = NULL;
7006 int argc = 0;
7007 int i;
7008
Bram Moolenaar14993322014-09-09 12:25:33 +02007009 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007010 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7011 if (ArglistW != NULL)
7012 {
7013 argv = malloc((nArgsW + 1) * sizeof(char *));
7014 if (argv != NULL)
7015 {
7016 argc = nArgsW;
7017 argv[argc] = NULL;
7018 for (i = 0; i < argc; ++i)
7019 {
7020 int len;
7021
Bram Moolenaar0f873732019-12-05 20:28:46 +01007022 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007023 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007024 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007025 (LPSTR *)&argv[i], &len, 0, 0);
7026 if (argv[i] == NULL)
7027 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007028 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007029 while (i > 0)
7030 free(argv[--i]);
7031 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007032 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007033 argc = 0;
7034 }
7035 }
7036 }
7037 }
7038
7039 global_argc = argc;
7040 global_argv = argv;
7041 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007042 {
7043 if (used_file_indexes != NULL)
7044 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007045 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007046 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007047
7048 if (argvp != NULL)
7049 *argvp = argv;
7050 return argc;
7051}
7052
7053 void
7054free_cmd_argsW(void)
7055{
7056 if (ArglistW != NULL)
7057 {
7058 GlobalFree(ArglistW);
7059 ArglistW = NULL;
7060 }
7061}
7062
7063/*
7064 * Remember "name" is an argument that was added to the argument list.
7065 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7066 * is called.
7067 */
7068 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007069used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007070{
7071 int i;
7072
7073 if (used_file_indexes == NULL)
7074 return;
7075 for (i = used_file_argc + 1; i < global_argc; ++i)
7076 if (STRCMP(global_argv[i], name) == 0)
7077 {
7078 used_file_argc = i;
7079 used_file_indexes[used_file_count++] = i;
7080 break;
7081 }
7082 used_file_literal = literal;
7083 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007084 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007085}
7086
7087/*
7088 * Remember the length of the argument list as it was. If it changes then we
7089 * leave it alone when 'encoding' is set.
7090 */
7091 void
7092set_alist_count(void)
7093{
7094 used_alist_count = GARGCOUNT;
7095}
7096
7097/*
7098 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7099 * has been changed while starting up. Use the UCS-2 command line arguments
7100 * and convert them to 'encoding'.
7101 */
7102 void
7103fix_arg_enc(void)
7104{
7105 int i;
7106 int idx;
7107 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007108 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007109
Bram Moolenaar0f873732019-12-05 20:28:46 +01007110 // Safety checks:
7111 // - if argument count differs between the wide and non-wide argument
7112 // list, something must be wrong.
7113 // - the file name arguments must have been located.
7114 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007115 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007116 || ArglistW == NULL
7117 || used_file_indexes == NULL
7118 || used_file_count == 0
7119 || used_alist_count != GARGCOUNT)
7120 return;
7121
Bram Moolenaar0f873732019-12-05 20:28:46 +01007122 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007123 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007124 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007125 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007126 for (i = 0; i < GARGCOUNT; ++i)
7127 fnum_list[i] = GARGLIST[i].ae_fnum;
7128
Bram Moolenaar0f873732019-12-05 20:28:46 +01007129 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007130 alist_clear(&global_alist);
7131 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007133
7134 for (i = 0; i < used_file_count; ++i)
7135 {
7136 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007137 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007138 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007139 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007140 int literal = used_file_literal;
7141
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007142#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007143 // When using diff mode may need to concatenate file name to
7144 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007145 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7146 && !mch_isdir(alist_name(&GARGLIST[0])))
7147 {
7148 char_u *r;
7149
7150 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7151 if (r != NULL)
7152 {
7153 vim_free(str);
7154 str = r;
7155 }
7156 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007157#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007158 // Re-use the old buffer by renaming it. When not using literal
7159 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007160 if (used_file_literal)
7161 buf_set_name(fnum_list[i], str);
7162
Bram Moolenaar0f873732019-12-05 20:28:46 +01007163 // Check backtick literal. backtick literal is already expanded in
7164 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007165 if (literal == FALSE)
7166 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007167 size_t len = STRLEN(str);
7168
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007169 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7170 literal = TRUE;
7171 }
7172 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007173 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007174 }
7175
7176 if (!used_file_literal)
7177 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007178 // Now expand wildcards in the arguments.
7179 // Temporarily add '(' and ')' to 'isfname'. These are valid
7180 // filename characters but are excluded from 'isfname' to make
7181 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7182 // Also, unset wildignore to not be influenced by this option.
7183 // The arguments specified in command-line should be kept even if
7184 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007185 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007186 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007187 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007188 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007189 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007190 }
7191
Bram Moolenaar0f873732019-12-05 20:28:46 +01007192 // If wildcard expansion failed, we are editing the first file of the
7193 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007194 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7195 {
7196 do_cmdline_cmd((char_u *)":rewind");
7197 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007198 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007199 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007200
7201 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007202}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007203
7204 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007205mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007206{
7207 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007208 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007209
Bram Moolenaar964b3742019-05-24 18:54:09 +02007210 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007211 if (envbuf == NULL)
7212 return -1;
7213
7214 sprintf((char *)envbuf, "%s=%s", var, value);
7215
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007216 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007217
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007218 vim_free(envbuf);
7219 if (p == NULL)
7220 return -1;
7221 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007222#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007223 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007224#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007225 // Unlike Un*x systems, we can free the string for _wputenv().
7226 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007227
7228 return 0;
7229}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007230
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007231/*
7232 * Support for 256 colors and 24-bit colors was added in Windows 10
7233 * version 1703 (Creators update).
7234 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007235#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7236
7237/*
7238 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007239 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007240 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007241#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007242
7243/*
7244 * ConPTY differences between versions, need different logic.
7245 * version 1903 (May 2019 update).
7246 */
7247#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7248
7249/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007250 * version 1909 (November 2019 update).
7251 */
7252#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7253
7254/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007255 * Confirm until this version. Also the logic changes.
7256 * insider preview.
7257 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007258#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007259
7260/*
7261 * Not stable now.
7262 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007263#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007264
7265 static void
7266vtp_flag_init(void)
7267{
7268 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007269#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007270 DWORD mode;
7271 HANDLE out;
7272
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007273# ifdef VIMDLL
7274 if (!gui.in_use)
7275# endif
7276 {
7277 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007278
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007279 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7280 GetConsoleMode(out, &mode);
7281 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7282 if (SetConsoleMode(out, mode) == 0)
7283 vtp_working = 0;
7284 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007285#endif
7286
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007287 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007288 conpty_working = 1;
7289 if (ver >= CONPTY_STABLE_BUILD)
7290 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007291
Bram Moolenaar57da6982019-09-13 22:30:11 +02007292 if (ver <= CONPTY_INSIDER_BUILD)
7293 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007294 if (ver <= CONPTY_1909_BUILD)
7295 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007296 if (ver <= CONPTY_1903_BUILD)
7297 conpty_type = 2;
7298 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7299 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007300}
7301
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007302#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007303
7304 static void
7305vtp_init(void)
7306{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007307 HMODULE hKerneldll;
7308 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007309# ifdef FEAT_TERMGUICOLORS
7310 COLORREF fg, bg;
7311# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007312
Bram Moolenaar0f873732019-12-05 20:28:46 +01007313 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007314 hKerneldll = GetModuleHandle("kernel32.dll");
7315 if (hKerneldll != NULL)
7316 {
7317 pGetConsoleScreenBufferInfoEx =
7318 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7319 hKerneldll, "GetConsoleScreenBufferInfoEx");
7320 pSetConsoleScreenBufferInfoEx =
7321 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7322 hKerneldll, "SetConsoleScreenBufferInfoEx");
7323 if (pGetConsoleScreenBufferInfoEx != NULL
7324 && pSetConsoleScreenBufferInfoEx != NULL)
7325 has_csbiex = TRUE;
7326 }
7327
7328 csbi.cbSize = sizeof(csbi);
7329 if (has_csbiex)
7330 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007331 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7332 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007333 store_console_bg_rgb = save_console_bg_rgb;
7334 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007335
7336# ifdef FEAT_TERMGUICOLORS
7337 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7338 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7339 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7340 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7341 default_console_color_bg = bg;
7342 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007343# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007344
7345 set_console_color_rgb();
7346}
7347
7348 static void
7349vtp_exit(void)
7350{
Bram Moolenaardf543822020-01-30 11:53:59 +01007351 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007352}
7353
7354 static int
7355vtp_printf(
7356 char *format,
7357 ...)
7358{
7359 char_u buf[100];
7360 va_list list;
7361 DWORD result;
7362
7363 va_start(list, format);
7364 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7365 va_end(list);
7366 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7367 return (int)result;
7368}
7369
7370 static void
7371vtp_sgr_bulk(
7372 int arg)
7373{
7374 int args[1];
7375
7376 args[0] = arg;
7377 vtp_sgr_bulks(1, args);
7378}
7379
7380 static void
7381vtp_sgr_bulks(
7382 int argc,
7383 int *args
7384)
7385{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007386 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007387 char_u buf[2 + (4 * 16) + 1];
7388 char_u *p;
7389 int i;
7390
7391 p = buf;
7392 *p++ = '\033';
7393 *p++ = '[';
7394
7395 for (i = 0; i < argc; ++i)
7396 {
7397 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7398 *p++ = ';';
7399 }
7400 p--;
7401 *p++ = 'm';
7402 *p = NUL;
7403 vtp_printf((char *)buf);
7404}
7405
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007406# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007407 static int
7408ctermtoxterm(
7409 int cterm)
7410{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007411 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007412
7413 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7414 return (((int)r << 16) | ((int)g << 8) | (int)b);
7415}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007416# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007417
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007418 static void
7419set_console_color_rgb(void)
7420{
7421# ifdef FEAT_TERMGUICOLORS
7422 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007423 guicolor_T fg, bg;
7424 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007425
7426 if (!USE_VTP)
7427 return;
7428
Bram Moolenaara050b942019-12-02 21:35:31 +01007429 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7430
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007431 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7432 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7433
7434 csbi.cbSize = sizeof(csbi);
7435 if (has_csbiex)
7436 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7437
7438 csbi.cbSize = sizeof(csbi);
7439 csbi.srWindow.Right += 1;
7440 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007441 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7442 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007443 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7444 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007445 if (has_csbiex)
7446 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7447# endif
7448}
7449
Bram Moolenaara050b942019-12-02 21:35:31 +01007450# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7451 void
7452get_default_console_color(
7453 int *cterm_fg,
7454 int *cterm_bg,
7455 guicolor_T *gui_fg,
7456 guicolor_T *gui_bg)
7457{
7458 int id;
7459 guicolor_T guifg = INVALCOLOR;
7460 guicolor_T guibg = INVALCOLOR;
7461 int ctermfg = 0;
7462 int ctermbg = 0;
7463
7464 id = syn_name2id((char_u *)"Normal");
7465 if (id > 0 && p_tgc)
7466 syn_id2colors(id, &guifg, &guibg);
7467 if (guifg == INVALCOLOR)
7468 {
7469 ctermfg = -1;
7470 if (id > 0)
7471 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7472 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7473 : default_console_color_fg;
7474 cterm_normal_fg_gui_color = guifg;
7475 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7476 }
7477 if (guibg == INVALCOLOR)
7478 {
7479 ctermbg = -1;
7480 if (id > 0)
7481 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7482 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7483 : default_console_color_bg;
7484 cterm_normal_bg_gui_color = guibg;
7485 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7486 }
7487
7488 *cterm_fg = ctermfg;
7489 *cterm_bg = ctermbg;
7490 *gui_fg = guifg;
7491 *gui_bg = guibg;
7492}
7493# endif
7494
Bram Moolenaardf543822020-01-30 11:53:59 +01007495/*
7496 * Set the console colors to the original colors or the last set colors.
7497 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007498 static void
7499reset_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 Moolenaardf543822020-01-30 11:53:59 +01007511 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7512 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7513 if (has_csbiex)
7514 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7515# endif
7516}
7517
7518/*
7519 * Set the console colors to the original colors.
7520 */
7521 static void
7522restore_console_color_rgb(void)
7523{
7524# ifdef FEAT_TERMGUICOLORS
7525 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7526
7527 csbi.cbSize = sizeof(csbi);
7528 if (has_csbiex)
7529 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7530
7531 csbi.cbSize = sizeof(csbi);
7532 csbi.srWindow.Right += 1;
7533 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007534 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7535 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007536 if (has_csbiex)
7537 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7538# endif
7539}
7540
7541 void
7542control_console_color_rgb(void)
7543{
7544 if (USE_VTP)
7545 set_console_color_rgb();
7546 else
7547 reset_console_color_rgb();
7548}
7549
7550 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007551use_vtp(void)
7552{
7553 return USE_VTP;
7554}
7555
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007556 int
7557is_term_win32(void)
7558{
7559 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7560}
7561
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007562 int
7563has_vtp_working(void)
7564{
7565 return vtp_working;
7566}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007567
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007568#endif
7569
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007570 int
7571has_conpty_working(void)
7572{
7573 return conpty_working;
7574}
7575
7576 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007577get_conpty_type(void)
7578{
7579 return conpty_type;
7580}
7581
7582 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007583is_conpty_stable(void)
7584{
7585 return conpty_stable;
7586}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007587
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007588#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007589 void
7590resize_console_buf(void)
7591{
7592 CONSOLE_SCREEN_BUFFER_INFO csbi;
7593 COORD coord;
7594 SMALL_RECT newsize;
7595
7596 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7597 {
7598 coord.X = SRWIDTH(csbi.srWindow);
7599 coord.Y = SRHEIGHT(csbi.srWindow);
7600 SetConsoleScreenBufferSize(g_hConOut, coord);
7601
7602 newsize.Left = 0;
7603 newsize.Top = 0;
7604 newsize.Right = coord.X - 1;
7605 newsize.Bottom = coord.Y - 1;
7606 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7607
7608 SetConsoleScreenBufferSize(g_hConOut, coord);
7609 }
7610}
7611#endif