blob: e9d22e417a4e2a47bdf88925573e3968acaf46bb [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 Moolenaar8f027fe2020-03-04 23:21:35 +01001785# ifdef VIMDLL
1786// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1787# define TYPEAHEADSPACE 6
1788# else
1789# define TYPEAHEADSPACE 0
1790# endif
1791# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001792 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 static int typeaheadlen = 0;
1794
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001795# ifdef VIMDLL
1796 if (gui.in_use)
1797 return 0;
1798# endif
1799
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (typeaheadlen > 0)
1802 goto theend;
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (time >= 0)
1805 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001806 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001809 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001811 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaar3918c952005-03-15 22:34:55 +00001813 /*
1814 * If there is no character available within 2 seconds (default)
1815 * write the autoscript file to disk. Or cause the CursorHold event
1816 * to be triggered.
1817 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001818 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001820 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001822 buf[0] = K_SPECIAL;
1823 buf[1] = KS_EXTRA;
1824 buf[2] = (int)KE_CURSORHOLD;
1825 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001827 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 }
1830
1831 /*
1832 * Try to read as many characters as there are, until the buffer is full.
1833 */
1834
Bram Moolenaar0f873732019-12-05 20:28:46 +01001835 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 g_fCBrkPressed = FALSE;
1837
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001838# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (fdDump)
1840 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar0f873732019-12-05 20:28:46 +01001843 // Keep looping until there is something in the typeahead buffer and more
1844 // to get and still room in the buffer (up to two bytes for a char and
1845 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001846 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001847 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 if (typebuf_changed(tb_change_cnt))
1850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001851 // "buf" may be invalid now if a client put something in the
1852 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 typeaheadlen = 0;
1854 break;
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (g_nMouseClick != -1)
1857 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001858# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (fdDump)
1860 fprintf(fdDump, "{%02x @ %d, %d}",
1861 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 typeahead[typeaheadlen++] = ESC + 128;
1864 typeahead[typeaheadlen++] = 'M';
1865 typeahead[typeaheadlen++] = g_nMouseClick;
1866 typeahead[typeaheadlen++] = g_xMouse + '!';
1867 typeahead[typeaheadlen++] = g_yMouse + '!';
1868 g_nMouseClick = -1;
1869 }
1870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001872 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int modifiers = 0;
1874
1875 c = tgetch(&modifiers, &ch2);
1876
1877 if (typebuf_changed(tb_change_cnt))
1878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001879 // "buf" may be invalid now if a client put something in the
1880 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 typeaheadlen = 0;
1882 break;
1883 }
1884
1885 if (c == Ctrl_C && ctrl_c_interrupts)
1886 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001887# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 got_int = TRUE;
1891 }
1892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 int n = 1;
1896
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001897 if (ch2 == NUL)
1898 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001899 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001900 char_u *p;
1901 WCHAR ch[2];
1902
1903 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001904 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001905 {
1906 ch[1] = tgetch(&modifiers, &ch2);
1907 n++;
1908 }
1909 p = utf16_to_enc(ch, &n);
1910 if (p != NULL)
1911 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001912 for (i = 0, j = 0; i < n; i++)
1913 {
1914 typeahead[typeaheadlen + j++] = p[i];
1915# ifdef VIMDLL
1916 if (p[i] == CSI)
1917 {
1918 typeahead[typeaheadlen + j++] = KS_EXTRA;
1919 typeahead[typeaheadlen + j++] = KE_CSI;
1920 }
1921# endif
1922 }
1923 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001924 vim_free(p);
1925 }
1926 }
1927 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001928 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001929 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001930# ifdef VIMDLL
1931 if (c == CSI)
1932 {
1933 typeahead[typeaheadlen + 1] = KS_EXTRA;
1934 typeahead[typeaheadlen + 2] = KE_CSI;
1935 n = 3;
1936 }
1937# endif
1938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (ch2 != NUL)
1940 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001941 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001942 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001943 switch (ch2)
1944 {
1945 case (WCHAR)'\324': // SHIFT+Insert
1946 case (WCHAR)'\325': // CTRL+Insert
1947 case (WCHAR)'\327': // SHIFT+Delete
1948 case (WCHAR)'\330': // CTRL+Delete
1949 typeahead[typeaheadlen + n] = (char_u)ch2;
1950 n++;
1951 break;
1952
1953 default:
1954 typeahead[typeaheadlen + n] = 3;
1955 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1956 n += 2;
1957 break;
1958 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001959 }
1960 else
1961 {
1962 typeahead[typeaheadlen + n] = 3;
1963 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1964 n += 2;
1965 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001966 }
1967
Bram Moolenaar0f873732019-12-05 20:28:46 +01001968 // Use the ALT key to set the 8th bit of the character
1969 // when it's one byte, the 8th bit isn't set yet and not
1970 // using a double-byte encoding (would become a lead
1971 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if ((modifiers & MOD_MASK_ALT)
1973 && n == 1
1974 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 )
1977 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001978 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1979 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 modifiers &= ~MOD_MASK_ALT;
1981 }
1982
1983 if (modifiers != 0)
1984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001985 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 mch_memmove(typeahead + typeaheadlen + 3,
1987 typeahead + typeaheadlen, n);
1988 typeahead[typeaheadlen++] = K_SPECIAL;
1989 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1990 typeahead[typeaheadlen++] = modifiers;
1991 }
1992
1993 typeaheadlen += n;
1994
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001995# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (fdDump)
1997 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001 }
2002
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002003# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (fdDump)
2005 {
2006 fputs("]\n", fdDump);
2007 fflush(fdDump);
2008 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002012 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 len = 0;
2014 while (len < maxlen && typeaheadlen > 0)
2015 {
2016 buf[len++] = typeahead[0];
2017 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2018 }
2019 return len;
2020
Bram Moolenaar0f873732019-12-05 20:28:46 +01002021#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002023#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
Bram Moolenaar82881492012-11-20 16:53:39 +01002026#ifndef PROTO
2027# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002028# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#endif
2031
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002032/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002033 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2034 * If "use_path" is FALSE: Return TRUE if "name" exists.
2035 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2036 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002037 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002040executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002042 WCHAR *p;
2043 WCHAR fnamew[_MAX_PATH];
2044 WCHAR *dumw;
2045 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002046 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
Bram Moolenaar43663192017-03-05 14:29:12 +01002048 if (!use_path)
2049 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002050 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002051 {
2052 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002053 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002054 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002055 *path = vim_strsave((char_u *)name);
2056 else
2057 *path = FullName_save((char_u *)name, FALSE);
2058 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002059 return TRUE;
2060 }
2061 return FALSE;
2062 }
2063
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002064 p = enc_to_utf16((char_u *)name, NULL);
2065 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002066 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002067
2068 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002069 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002070 if (wnewpath == NULL)
2071 return FALSE;
2072 wcscpy(wnewpath, L".;");
2073 wcscat(wnewpath, wcurpath);
2074 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2075 vim_free(wnewpath);
2076 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002077 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002078 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002079 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002080 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002081 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002082 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002083 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084}
2085
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002086#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2087 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002088/*
2089 * Bad parameter handler.
2090 *
2091 * Certain MS CRT functions will intentionally crash when passed invalid
2092 * parameters to highlight possible security holes. Setting this function as
2093 * the bad parameter handler will prevent the crash.
2094 *
2095 * In debug builds the parameters contain CRT information that might help track
2096 * down the source of a problem, but in non-debug builds the arguments are all
2097 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2098 * worth allowing these to make debugging of issues easier.
2099 */
2100 static void
2101bad_param_handler(const wchar_t *expression,
2102 const wchar_t *function,
2103 const wchar_t *file,
2104 unsigned int line,
2105 uintptr_t pReserved)
2106{
2107}
2108
2109# define SET_INVALID_PARAM_HANDLER \
2110 ((void)_set_invalid_parameter_handler(bad_param_handler))
2111#else
2112# define SET_INVALID_PARAM_HANDLER
2113#endif
2114
Bram Moolenaar4f974752019-02-17 17:44:42 +01002115#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117/*
2118 * GUI version of mch_init().
2119 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002120 static void
2121mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002123# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
Bram Moolenaar0f873732019-12-05 20:28:46 +01002127 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002128 SET_INVALID_PARAM_HANDLER;
2129
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130 // Let critical errors result in a failure, not in a dialog box. Required
2131 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 SetErrorMode(SEM_FAILCRITICALERRORS);
2133
Bram Moolenaar0f873732019-12-05 20:28:46 +01002134 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar0f873732019-12-05 20:28:46 +01002136 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 Rows = 25;
2138 Columns = 80;
2139
Bram Moolenaar0f873732019-12-05 20:28:46 +01002140 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 char_u vimrun_location[_MAX_PATH + 4];
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 STRCPY(vimrun_location, exe_name);
2146 STRCPY(gettail(vimrun_location), "vimrun.exe");
2147 if (mch_getperm(vimrun_location) >= 0)
2148 {
2149 if (*skiptowhite(vimrun_location) != NUL)
2150 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002151 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 mch_memmove(vimrun_location + 1, vimrun_location,
2153 STRLEN(vimrun_location) + 1);
2154 *vimrun_location = '"';
2155 STRCPY(gettail(vimrun_location), "vimrun\" ");
2156 }
2157 else
2158 STRCPY(gettail(vimrun_location), "vimrun ");
2159
2160 vimrun_path = (char *)vim_strsave(vimrun_location);
2161 s_dont_use_vimrun = FALSE;
2162 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002163 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 s_dont_use_vimrun = FALSE;
2165
Bram Moolenaar0f873732019-12-05 20:28:46 +01002166 // Don't give the warning for a missing vimrun.exe right now, but only
2167 // when vimrun was supposed to be used. Don't bother people that do
2168 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (s_dont_use_vimrun)
2170 need_vimrun_warning = TRUE;
2171 }
2172
2173 /*
2174 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2175 * Otherwise the default "findstr /n" is used.
2176 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002177 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2179
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002180# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002181 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002182# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002183
2184 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185}
2186
2187
Bram Moolenaar0f873732019-12-05 20:28:46 +01002188#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002189
2190#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002192# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2193# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195/*
2196 * ClearConsoleBuffer()
2197 * Description:
2198 * Clears the entire contents of the console screen buffer, using the
2199 * specified attribute.
2200 * Returns:
2201 * TRUE on success
2202 */
2203 static BOOL
2204ClearConsoleBuffer(WORD wAttribute)
2205{
2206 CONSOLE_SCREEN_BUFFER_INFO csbi;
2207 COORD coord;
2208 DWORD NumCells, dummy;
2209
2210 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2211 return FALSE;
2212
2213 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2214 coord.X = 0;
2215 coord.Y = 0;
2216 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2217 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2220 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 return TRUE;
2224}
2225
2226/*
2227 * FitConsoleWindow()
2228 * Description:
2229 * Checks if the console window will fit within given buffer dimensions.
2230 * Also, if requested, will shrink the window to fit.
2231 * Returns:
2232 * TRUE on success
2233 */
2234 static BOOL
2235FitConsoleWindow(
2236 COORD dwBufferSize,
2237 BOOL WantAdjust)
2238{
2239 CONSOLE_SCREEN_BUFFER_INFO csbi;
2240 COORD dwWindowSize;
2241 BOOL NeedAdjust = FALSE;
2242
2243 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2244 {
2245 /*
2246 * A buffer resize will fail if the current console window does
2247 * not lie completely within that buffer. To avoid this, we might
2248 * have to move and possibly shrink the window.
2249 */
2250 if (csbi.srWindow.Right >= dwBufferSize.X)
2251 {
2252 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2253 if (dwWindowSize.X > dwBufferSize.X)
2254 dwWindowSize.X = dwBufferSize.X;
2255 csbi.srWindow.Right = dwBufferSize.X - 1;
2256 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2257 NeedAdjust = TRUE;
2258 }
2259 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2260 {
2261 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2262 if (dwWindowSize.Y > dwBufferSize.Y)
2263 dwWindowSize.Y = dwBufferSize.Y;
2264 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2265 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2266 NeedAdjust = TRUE;
2267 }
2268 if (NeedAdjust && WantAdjust)
2269 {
2270 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2271 return FALSE;
2272 }
2273 return TRUE;
2274 }
2275
2276 return FALSE;
2277}
2278
2279typedef struct ConsoleBufferStruct
2280{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002281 BOOL IsValid;
2282 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002283 PCHAR_INFO Buffer;
2284 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002285 PSMALL_RECT Regions;
2286 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287} ConsoleBuffer;
2288
2289/*
2290 * SaveConsoleBuffer()
2291 * Description:
2292 * Saves important information about the console buffer, including the
2293 * actual buffer contents. The saved information is suitable for later
2294 * restoration by RestoreConsoleBuffer().
2295 * Returns:
2296 * TRUE if all information was saved; FALSE otherwise
2297 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2298 */
2299 static BOOL
2300SaveConsoleBuffer(
2301 ConsoleBuffer *cb)
2302{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002303 DWORD NumCells;
2304 COORD BufferCoord;
2305 SMALL_RECT ReadRegion;
2306 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002307 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (cb == NULL)
2310 return FALSE;
2311
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002312 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 cb->IsValid = FALSE;
2315 return FALSE;
2316 }
2317 cb->IsValid = TRUE;
2318
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002319 /*
2320 * Allocate a buffer large enough to hold the entire console screen
2321 * buffer. If this ConsoleBuffer structure has already been initialized
2322 * with a buffer of the correct size, then just use that one.
2323 */
2324 if (!cb->IsValid || cb->Buffer == NULL ||
2325 cb->BufferSize.X != cb->Info.dwSize.X ||
2326 cb->BufferSize.Y != cb->Info.dwSize.Y)
2327 {
2328 cb->BufferSize.X = cb->Info.dwSize.X;
2329 cb->BufferSize.Y = cb->Info.dwSize.Y;
2330 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2331 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002332 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002333 if (cb->Buffer == NULL)
2334 return FALSE;
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002338 * We will now copy the console screen buffer into our buffer.
2339 * ReadConsoleOutput() seems to be limited as far as how much you
2340 * can read at a time. Empirically, this number seems to be about
2341 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2342 * in chunks until it is all copied. The chunks will all have the
2343 * same horizontal characteristics, so initialize them now. The
2344 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002346 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002347 ReadRegion.Left = 0;
2348 ReadRegion.Right = cb->Info.dwSize.X - 1;
2349 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002350
2351 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2352 if (cb->Regions == NULL || numregions != cb->NumRegions)
2353 {
2354 cb->NumRegions = numregions;
2355 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002356 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002357 if (cb->Regions == NULL)
2358 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002359 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002360 return FALSE;
2361 }
2362 }
2363
2364 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002366 /*
2367 * Read into position (0, Y) in our buffer.
2368 */
2369 BufferCoord.Y = Y;
2370 /*
2371 * Read the region whose top left corner is (0, Y) and whose bottom
2372 * right corner is (width - 1, Y + Y_incr - 1). This should define
2373 * a region of size width by Y_incr. Don't worry if this region is
2374 * too large for the remaining buffer; it will be cropped.
2375 */
2376 ReadRegion.Top = Y;
2377 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002378 if (!ReadConsoleOutputW(g_hConOut, // output handle
2379 cb->Buffer, // our buffer
2380 cb->BufferSize, // dimensions of our buffer
2381 BufferCoord, // offset in our buffer
2382 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002383 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002384 VIM_CLEAR(cb->Buffer);
2385 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002386 return FALSE;
2387 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002388 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390
2391 return TRUE;
2392}
2393
2394/*
2395 * RestoreConsoleBuffer()
2396 * Description:
2397 * Restores important information about the console buffer, including the
2398 * actual buffer contents, if desired. The information to restore is in
2399 * the same format used by SaveConsoleBuffer().
2400 * Returns:
2401 * TRUE on success
2402 */
2403 static BOOL
2404RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002405 ConsoleBuffer *cb,
2406 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002408 COORD BufferCoord;
2409 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002410 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 if (cb == NULL || !cb->IsValid)
2413 return FALSE;
2414
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002415 /*
2416 * Before restoring the buffer contents, clear the current buffer, and
2417 * restore the cursor position and window information. Doing this now
2418 * prevents old buffer contents from "flashing" onto the screen.
2419 */
2420 if (RestoreScreen)
2421 ClearConsoleBuffer(cb->Info.wAttributes);
2422
2423 FitConsoleWindow(cb->Info.dwSize, TRUE);
2424 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2425 return FALSE;
2426 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2427 return FALSE;
2428
2429 if (!RestoreScreen)
2430 {
2431 /*
2432 * No need to restore the screen buffer contents, so we're done.
2433 */
2434 return TRUE;
2435 }
2436
2437 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2438 return FALSE;
2439 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2440 return FALSE;
2441
2442 /*
2443 * Restore the screen buffer contents.
2444 */
2445 if (cb->Buffer != NULL)
2446 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002447 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002448 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002449 BufferCoord.X = cb->Regions[i].Left;
2450 BufferCoord.Y = cb->Regions[i].Top;
2451 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002452 if (!WriteConsoleOutputW(g_hConOut, // output handle
2453 cb->Buffer, // our buffer
2454 cb->BufferSize, // dimensions of our buffer
2455 BufferCoord, // offset in our buffer
2456 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002457 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002458 }
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 return TRUE;
2462}
2463
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002464# define FEAT_RESTORE_ORIG_SCREEN
2465# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002466static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468static ConsoleBuffer g_cbNonTermcap = { 0 };
2469static ConsoleBuffer g_cbTermcap = { 0 };
2470
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002471# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002473HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474static HICON g_hOrigIconSmall = NULL;
2475static HICON g_hOrigIcon = NULL;
2476static HICON g_hVimIcon = NULL;
2477static BOOL g_fCanChangeIcon = FALSE;
2478
Bram Moolenaar0f873732019-12-05 20:28:46 +01002479// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002480# ifndef ICON_SMALL
2481# define ICON_SMALL 0
2482# endif
2483# ifndef ICON_BIG
2484# define ICON_BIG 1
2485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
2487 * GetConsoleIcon()
2488 * Description:
2489 * Attempts to retrieve the small icon and/or the big icon currently in
2490 * use by a given window.
2491 * Returns:
2492 * TRUE on success
2493 */
2494 static BOOL
2495GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002496 HWND hWnd,
2497 HICON *phIconSmall,
2498 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (hWnd == NULL)
2501 return FALSE;
2502
2503 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002504 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2505 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002507 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2508 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return TRUE;
2510}
2511
2512/*
2513 * SetConsoleIcon()
2514 * Description:
2515 * Attempts to change the small icon and/or the big icon currently in
2516 * use by a given window.
2517 * Returns:
2518 * TRUE on success
2519 */
2520 static BOOL
2521SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002522 HWND hWnd,
2523 HICON hIconSmall,
2524 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (hWnd == NULL)
2527 return FALSE;
2528
2529 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002530 SendMessage(hWnd, WM_SETICON,
2531 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002533 SendMessage(hWnd, WM_SETICON,
2534 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return TRUE;
2536}
2537
2538/*
2539 * SaveConsoleTitleAndIcon()
2540 * Description:
2541 * Saves the current console window title in g_szOrigTitle, for later
2542 * restoration. Also, attempts to obtain a handle to the console window,
2543 * and use it to save the small and big icons currently in use by the
2544 * console window. This is not always possible on some versions of Windows;
2545 * nor is it possible when running Vim remotely using Telnet (since the
2546 * console window the user sees is owned by a remote process).
2547 */
2548 static void
2549SaveConsoleTitleAndIcon(void)
2550{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002551 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2553 return;
2554
2555 /*
2556 * Obtain a handle to the console window using GetConsoleWindow() from
2557 * KERNEL32.DLL; we need to handle in order to change the window icon.
2558 * This function only exists on NT-based Windows, starting with Windows
2559 * 2000. On older operating systems, we can't change the window icon
2560 * anyway.
2561 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002562 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (g_hWnd == NULL)
2564 return;
2565
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2568 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2569 return;
2570
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002572 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002573 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (g_hVimIcon != NULL)
2575 g_fCanChangeIcon = TRUE;
2576}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579static int g_fWindInitCalled = FALSE;
2580static int g_fTermcapMode = FALSE;
2581static CONSOLE_CURSOR_INFO g_cci;
2582static DWORD g_cmodein = 0;
2583static DWORD g_cmodeout = 0;
2584
2585/*
2586 * non-GUI version of mch_init().
2587 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002588 static void
2589mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002591# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002592 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002593# endif
2594# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar0f873732019-12-05 20:28:46 +01002598 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002599 SET_INVALID_PARAM_HANDLER;
2600
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 // Let critical errors result in a failure, not in a dialog box. Required
2602 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 SetErrorMode(SEM_FAILCRITICALERRORS);
2604
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 out_flush();
2607
Bram Moolenaar0f873732019-12-05 20:28:46 +01002608 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (read_cmd_fd == 0)
2610 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2611 else
2612 create_conin();
2613 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2614
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002615# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002617 SaveConsoleBuffer(&g_cbOrig);
2618 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002619# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002620 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002621 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2622 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (cterm_normal_fg_color == 0)
2625 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2626 if (cterm_normal_bg_color == 0)
2627 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2628
Bram Moolenaarbdace832019-03-02 10:13:42 +01002629 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002630 g_color_index_fg = g_attrDefault & 0xf;
2631 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2632
Bram Moolenaar0f873732019-12-05 20:28:46 +01002633 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 update_tcap(g_attrCurrent);
2635
2636 GetConsoleCursorInfo(g_hConOut, &g_cci);
2637 GetConsoleMode(g_hConIn, &g_cmodein);
2638 GetConsoleMode(g_hConOut, &g_cmodeout);
2639
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002640# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 SaveConsoleTitleAndIcon();
2642 /*
2643 * Set both the small and big icons of the console window to Vim's icon.
2644 * Note that Vim presently only has one size of icon (32x32), but it
2645 * automatically gets scaled down to 16x16 when setting the small icon.
2646 */
2647 if (g_fCanChangeIcon)
2648 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 ui_get_shellsize();
2652
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002653# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 fdDump = fopen("dump", "wt");
2655
2656 if (fdDump)
2657 {
2658 time_t t;
2659
2660 time(&t);
2661 fputs(ctime(&t), fdDump);
2662 fflush(fdDump);
2663 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 g_fWindInitCalled = TRUE;
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002670# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002671 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002672# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002673
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002674 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002675 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676}
2677
2678/*
2679 * non-GUI version of mch_exit().
2680 * Shut down and exit with status `r'
2681 * Careful: mch_exit() may be called before mch_init()!
2682 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002683 static void
2684mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002686 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002688 vtp_exit();
2689
Bram Moolenaar955f1982017-02-05 15:10:51 +01002690 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (g_fWindInitCalled)
2692 settmode(TMODE_COOK);
2693
Bram Moolenaar0f873732019-12-05 20:28:46 +01002694 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 if (g_fWindInitCalled)
2697 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002698# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002699 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 /*
2701 * Restore both the small and big icons of the console window to
2702 * what they were at startup. Don't do this when the window is
2703 * closed, Vim would hang here.
2704 */
2705 if (g_fCanChangeIcon && !g_fForceExit)
2706 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002709# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (fdDump)
2711 {
2712 time_t t;
2713
2714 time(&t);
2715 fputs(ctime(&t), fdDump);
2716 fclose(fdDump);
2717 }
2718 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721
2722 SetConsoleCursorInfo(g_hConOut, &g_cci);
2723 SetConsoleMode(g_hConIn, g_cmodein);
2724 SetConsoleMode(g_hConOut, g_cmodeout);
2725
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002726# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 exit(r);
2731}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002734 void
2735mch_init(void)
2736{
2737#ifdef VIMDLL
2738 if (gui.starting)
2739 mch_init_g();
2740 else
2741 mch_init_c();
2742#elif defined(FEAT_GUI_MSWIN)
2743 mch_init_g();
2744#else
2745 mch_init_c();
2746#endif
2747}
2748
2749 void
2750mch_exit(int r)
2751{
2752#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002753 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002754 mch_exit_g(r);
2755 else
2756 mch_exit_c(r);
2757#elif defined(FEAT_GUI_MSWIN)
2758 mch_exit_g(r);
2759#else
2760 mch_exit_c(r);
2761#endif
2762}
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764/*
2765 * Do we have an interactive window?
2766 */
2767 int
2768mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002769 int argc UNUSED,
2770 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 get_exe_name();
2773
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002774#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002775 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002777# ifdef VIMDLL
2778 if (gui.in_use)
2779 return OK;
2780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 if (isatty(1))
2782 return OK;
2783 return FAIL;
2784#endif
2785}
2786
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002787/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002788 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * When "len" is > 0, also expand short to long filenames.
2790 */
2791 void
2792fname_case(
2793 char_u *name,
2794 int len)
2795{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002796 int flen;
2797 WCHAR *p;
2798 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002800 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002801 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return;
2803
2804 slash_adjust(name);
2805
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002806 p = enc_to_utf16(name, NULL);
2807 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002808 return;
2809
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002810 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002812 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002814 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002816 if (len > 0 || flen >= (int)STRLEN(q))
2817 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2818 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002821 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822}
2823
2824
2825/*
2826 * Insert user name in s[len].
2827 */
2828 int
2829mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002830 char_u *s,
2831 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002833 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002834 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002836 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002837 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002838 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002839
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002840 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002841 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002842 vim_strncpy(s, p, len - 1);
2843 vim_free(p);
2844 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002845 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 s[0] = NUL;
2848 return FAIL;
2849}
2850
2851
2852/*
2853 * Insert host name in s[len].
2854 */
2855 void
2856mch_get_host_name(
2857 char_u *s,
2858 int len)
2859{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002860 WCHAR wszHostName[256 + 1];
2861 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002863 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002864 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002865 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002866
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002867 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002868 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002869 vim_strncpy(s, p, len - 1);
2870 vim_free(p);
2871 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002872 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874}
2875
2876
2877/*
2878 * return process ID
2879 */
2880 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002881mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 return (long)GetCurrentProcessId();
2884}
2885
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002886/*
2887 * return TRUE if process "pid" is still running
2888 */
2889 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002890mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002891{
2892 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2893 DWORD status = 0;
2894 int ret = FALSE;
2895
2896 if (hProcess == NULL)
2897 return FALSE; // might not have access
2898 if (GetExitCodeProcess(hProcess, &status) )
2899 ret = status == STILL_ACTIVE;
2900 CloseHandle(hProcess);
2901 return ret;
2902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904/*
2905 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2906 * Return OK for success, FAIL for failure.
2907 */
2908 int
2909mch_dirname(
2910 char_u *buf,
2911 int len)
2912{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002913 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /*
2916 * Originally this was:
2917 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2918 * But the Win32s known bug list says that getcwd() doesn't work
2919 * so use the Win32 system call instead. <Negri>
2920 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002921 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002923 WCHAR wcbuf[_MAX_PATH + 1];
2924 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002926 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002928 p = utf16_to_enc(wcbuf, NULL);
2929 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002930 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002931 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002933 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002936 if (p == NULL)
2937 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002938
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002939 if (p != NULL)
2940 {
2941 vim_strncpy(buf, p, len - 1);
2942 vim_free(p);
2943 return OK;
2944 }
2945 }
2946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002950 * Get file permissions for "name".
2951 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
2953 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002954mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002956 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002957 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002959 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002960 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
2963
2964/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002965 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002966 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002967 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 */
2969 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002970mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002972 long n;
2973 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002974
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002975 p = enc_to_utf16(name, NULL);
2976 if (p == NULL)
2977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002979 n = _wchmod(p, perm);
2980 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002981 if (n == -1)
2982 return FAIL;
2983
2984 win32_set_archive(name);
2985
2986 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989/*
2990 * Set hidden flag for "name".
2991 */
2992 void
2993mch_hide(char_u *name)
2994{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002995 int attrs = win32_getattrs(name);
2996 if (attrs == -1)
2997 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002999 attrs |= FILE_ATTRIBUTE_HIDDEN;
3000 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002
3003/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003004 * Return TRUE if file "name" exists and is hidden.
3005 */
3006 int
3007mch_ishidden(char_u *name)
3008{
3009 int f = win32_getattrs(name);
3010
3011 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003012 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003013
3014 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3015}
3016
3017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 * return TRUE if "name" is a directory
3019 * return FALSE if "name" is not a directory or upon error
3020 */
3021 int
3022mch_isdir(char_u *name)
3023{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003024 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003027 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3030}
3031
3032/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003033 * return TRUE if "name" is a directory, NOT a symlink to a directory
3034 * return FALSE if "name" is not a directory
3035 * return FALSE for error
3036 */
3037 int
3038mch_isrealdir(char_u *name)
3039{
3040 return mch_isdir(name) && !mch_is_symbolic_link(name);
3041}
3042
3043/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003044 * Create directory "name".
3045 * Return 0 on success, -1 on error.
3046 */
3047 int
3048mch_mkdir(char_u *name)
3049{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003050 WCHAR *p;
3051 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003052
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003053 p = enc_to_utf16(name, NULL);
3054 if (p == NULL)
3055 return -1;
3056 retval = _wmkdir(p);
3057 vim_free(p);
3058 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003059}
3060
3061/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003062 * Delete directory "name".
3063 * Return 0 on success, -1 on error.
3064 */
3065 int
3066mch_rmdir(char_u *name)
3067{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003068 WCHAR *p;
3069 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003070
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003071 p = enc_to_utf16(name, NULL);
3072 if (p == NULL)
3073 return -1;
3074 retval = _wrmdir(p);
3075 vim_free(p);
3076 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003077}
3078
3079/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003080 * Return TRUE if file "fname" has more than one link.
3081 */
3082 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003083mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003084{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003085 BY_HANDLE_FILE_INFORMATION info;
3086
3087 return win32_fileinfo(fname, &info) == FILEINFO_OK
3088 && info.nNumberOfLinks > 1;
3089}
3090
3091/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003092 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003093 */
3094 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003095mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003096{
3097 HANDLE hFind;
3098 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003099 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003100 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003101 WIN32_FIND_DATAW findDataW;
3102
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003103 wn = enc_to_utf16(name, NULL);
3104 if (wn == NULL)
3105 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003106
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003107 hFind = FindFirstFileW(wn, &findDataW);
3108 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003109 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003110 {
3111 fileFlags = findDataW.dwFileAttributes;
3112 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003113 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003114 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003115
3116 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003117 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3118 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003119 res = TRUE;
3120
3121 return res;
3122}
3123
3124/*
3125 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3126 * link.
3127 */
3128 int
3129mch_is_linked(char_u *fname)
3130{
3131 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3132 return TRUE;
3133 return FALSE;
3134}
3135
3136/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003137 * Get the by-handle-file-information for "fname".
3138 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003139 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003140 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3141 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3142 */
3143 int
3144win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3145{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003146 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003147 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003148 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003149
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003150 wn = enc_to_utf16(fname, NULL);
3151 if (wn == NULL)
3152 return FILEINFO_ENC_FAIL;
3153
3154 hFile = CreateFileW(wn, // file name
3155 GENERIC_READ, // access mode
3156 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3157 NULL, // security descriptor
3158 OPEN_EXISTING, // creation disposition
3159 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3160 NULL); // handle to template file
3161 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003162
3163 if (hFile != INVALID_HANDLE_VALUE)
3164 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003165 if (GetFileInformationByHandle(hFile, info) != 0)
3166 res = FILEINFO_OK;
3167 else
3168 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003169 CloseHandle(hFile);
3170 }
3171
Bram Moolenaar03f48552006-02-28 23:52:23 +00003172 return res;
3173}
3174
3175/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003176 * get file attributes for `name'
3177 * -1 : error
3178 * else FILE_ATTRIBUTE_* defined in winnt.h
3179 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003180 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003181win32_getattrs(char_u *name)
3182{
3183 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003184 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003185
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003186 p = enc_to_utf16(name, NULL);
3187 if (p == NULL)
3188 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003189
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003190 attr = GetFileAttributesW(p);
3191 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003192
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003193 return attr;
3194}
3195
3196/*
3197 * set file attributes for `name' to `attrs'
3198 *
3199 * return -1 for failure, 0 otherwise
3200 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003201 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003202win32_setattrs(char_u *name, int attrs)
3203{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003204 int res;
3205 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003206
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003207 p = enc_to_utf16(name, NULL);
3208 if (p == NULL)
3209 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003210
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003211 res = SetFileAttributesW(p, attrs);
3212 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003213
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003214 return res ? 0 : -1;
3215}
3216
3217/*
3218 * Set archive flag for "name".
3219 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003220 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003221win32_set_archive(char_u *name)
3222{
3223 int attrs = win32_getattrs(name);
3224 if (attrs == -1)
3225 return -1;
3226
3227 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3228 return win32_setattrs(name, attrs);
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Return TRUE if file or directory "name" is writable (not readonly).
3233 * Strange semantics of Win32: a readonly directory is writable, but you can't
3234 * delete a file. Let's say this means it is writable.
3235 */
3236 int
3237mch_writable(char_u *name)
3238{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003239 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003241 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3242 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003246 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003247 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003248 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3249 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
3251 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003252mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar86621892019-03-30 21:51:28 +01003254 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3255 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3256 // UTF-8.
3257 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003258 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003259 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003260
Bram Moolenaar86621892019-03-30 21:51:28 +01003261 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003262 return FALSE;
3263
Bram Moolenaar86621892019-03-30 21:51:28 +01003264 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003265 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003266 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003267 return TRUE;
3268
3269 /*
3270 * Loop over all extensions in $PATHEXT.
3271 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003272 p = mch_getenv("PATHEXT");
3273 if (p == NULL)
3274 p = (char_u *)".com;.exe;.bat;.cmd";
3275 saved = vim_strsave(p);
3276 if (saved == NULL)
3277 return FALSE;
3278 p = saved;
3279 while (*p)
3280 {
3281 char_u *tmp = vim_strchr(p, ';');
3282
3283 if (tmp != NULL)
3284 *tmp = NUL;
3285 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3286 && executable_exists((char *)name, path, use_path))
3287 {
3288 vim_free(saved);
3289 return TRUE;
3290 }
3291 if (tmp == NULL)
3292 break;
3293 p = tmp + 1;
3294 }
3295 vim_free(saved);
3296
Bram Moolenaar86621892019-03-30 21:51:28 +01003297 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003298 p = mch_getenv("PATHEXT");
3299 if (p == NULL)
3300 p = (char_u *)".com;.exe;.bat;.cmd";
3301 while (*p)
3302 {
3303 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3304 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003305 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003306 buf[len] = NUL;
3307 ++p;
3308 if (*p)
3309 ++p;
3310 }
3311 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003312 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003313 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003314 return TRUE;
3315 }
3316 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319/*
3320 * Check what "name" is:
3321 * NODE_NORMAL: file or directory (or doesn't exist)
3322 * NODE_WRITABLE: writable device, socket, fifo, etc.
3323 * NODE_OTHER: non-writable things
3324 */
3325 int
3326mch_nodetype(char_u *name)
3327{
3328 HANDLE hFile;
3329 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003330 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar0f873732019-12-05 20:28:46 +01003332 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3333 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3334 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003335 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3336 return NODE_WRITABLE;
3337
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003338 wn = enc_to_utf16(name, NULL);
3339 if (wn == NULL)
3340 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003341
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003342 hFile = CreateFileW(wn, // file name
3343 GENERIC_WRITE, // access mode
3344 0, // share mode
3345 NULL, // security descriptor
3346 OPEN_EXISTING, // creation disposition
3347 0, // file attributes
3348 NULL); // handle to template file
3349 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (hFile == INVALID_HANDLE_VALUE)
3351 return NODE_NORMAL;
3352
3353 type = GetFileType(hFile);
3354 CloseHandle(hFile);
3355 if (type == FILE_TYPE_CHAR)
3356 return NODE_WRITABLE;
3357 if (type == FILE_TYPE_DISK)
3358 return NODE_NORMAL;
3359 return NODE_OTHER;
3360}
3361
3362#ifdef HAVE_ACL
3363struct my_acl
3364{
3365 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3366 PSID pSidOwner;
3367 PSID pSidGroup;
3368 PACL pDacl;
3369 PACL pSacl;
3370};
3371#endif
3372
3373/*
3374 * Return a pointer to the ACL of file "fname" in allocated memory.
3375 * Return NULL if the ACL is not available for whatever reason.
3376 */
3377 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003378mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380#ifndef HAVE_ACL
3381 return (vim_acl_T)NULL;
3382#else
3383 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003384 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003386 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003387 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003389 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003390
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003391 wn = enc_to_utf16(fname, NULL);
3392 if (wn == NULL)
3393 return NULL;
3394
3395 // Try to retrieve the entire security descriptor.
3396 err = GetNamedSecurityInfoW(
3397 wn, // Abstract filename
3398 SE_FILE_OBJECT, // File Object
3399 OWNER_SECURITY_INFORMATION |
3400 GROUP_SECURITY_INFORMATION |
3401 DACL_SECURITY_INFORMATION |
3402 SACL_SECURITY_INFORMATION,
3403 &p->pSidOwner, // Ownership information.
3404 &p->pSidGroup, // Group membership.
3405 &p->pDacl, // Discretionary information.
3406 &p->pSacl, // For auditing purposes.
3407 &p->pSecurityDescriptor);
3408 if (err == ERROR_ACCESS_DENIED ||
3409 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003410 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003411 // Retrieve only DACL.
3412 (void)GetNamedSecurityInfoW(
3413 wn,
3414 SE_FILE_OBJECT,
3415 DACL_SECURITY_INFORMATION,
3416 NULL,
3417 NULL,
3418 &p->pDacl,
3419 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003420 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003421 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003422 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003423 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003424 mch_free_acl((vim_acl_T)p);
3425 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003427 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429
3430 return (vim_acl_T)p;
3431#endif
3432}
3433
Bram Moolenaar27515922013-06-29 15:36:26 +02003434#ifdef HAVE_ACL
3435/*
3436 * Check if "acl" contains inherited ACE.
3437 */
3438 static BOOL
3439is_acl_inherited(PACL acl)
3440{
3441 DWORD i;
3442 ACL_SIZE_INFORMATION acl_info;
3443 PACCESS_ALLOWED_ACE ace;
3444
3445 acl_info.AceCount = 0;
3446 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3447 for (i = 0; i < acl_info.AceCount; i++)
3448 {
3449 GetAce(acl, i, (LPVOID *)&ace);
3450 if (ace->Header.AceFlags & INHERITED_ACE)
3451 return TRUE;
3452 }
3453 return FALSE;
3454}
3455#endif
3456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457/*
3458 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3459 * Errors are ignored.
3460 * This must only be called with "acl" equal to what mch_get_acl() returned.
3461 */
3462 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003463mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
3465#ifdef HAVE_ACL
3466 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003467 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003468 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003470 if (p == NULL)
3471 return;
3472
3473 wn = enc_to_utf16(fname, NULL);
3474 if (wn == NULL)
3475 return;
3476
3477 // Set security flags
3478 if (p->pSidOwner)
3479 sec_info |= OWNER_SECURITY_INFORMATION;
3480 if (p->pSidGroup)
3481 sec_info |= GROUP_SECURITY_INFORMATION;
3482 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003483 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003484 sec_info |= DACL_SECURITY_INFORMATION;
3485 // Do not inherit its parent's DACL.
3486 // If the DACL is inherited, Cygwin permissions would be changed.
3487 if (!is_acl_inherited(p->pDacl))
3488 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003489 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003490 if (p->pSacl)
3491 sec_info |= SACL_SECURITY_INFORMATION;
3492
3493 (void)SetNamedSecurityInfoW(
3494 wn, // Abstract filename
3495 SE_FILE_OBJECT, // File Object
3496 sec_info,
3497 p->pSidOwner, // Ownership information.
3498 p->pSidGroup, // Group membership.
3499 p->pDacl, // Discretionary information.
3500 p->pSacl // For auditing purposes.
3501 );
3502 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503#endif
3504}
3505
3506 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003507mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509#ifdef HAVE_ACL
3510 struct my_acl *p = (struct my_acl *)acl;
3511
3512 if (p != NULL)
3513 {
3514 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3515 vim_free(p);
3516 }
3517#endif
3518}
3519
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003520#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522/*
3523 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3524 */
3525 static BOOL WINAPI
3526handler_routine(
3527 DWORD dwCtrlType)
3528{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003529 INPUT_RECORD ir;
3530 DWORD out;
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 switch (dwCtrlType)
3533 {
3534 case CTRL_C_EVENT:
3535 if (ctrl_c_interrupts)
3536 g_fCtrlCPressed = TRUE;
3537 return TRUE;
3538
3539 case CTRL_BREAK_EVENT:
3540 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003541 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003542 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003543 ir.EventType = KEY_EVENT;
3544 ir.Event.KeyEvent.bKeyDown = TRUE;
3545 ir.Event.KeyEvent.wRepeatCount = 1;
3546 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3547 ir.Event.KeyEvent.wVirtualScanCode = 0;
3548 ir.Event.KeyEvent.dwControlKeyState = 0;
3549 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3550 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return TRUE;
3552
Bram Moolenaar0f873732019-12-05 20:28:46 +01003553 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case CTRL_CLOSE_EVENT:
3555 case CTRL_LOGOFF_EVENT:
3556 case CTRL_SHUTDOWN_EVENT:
3557 windgoto((int)Rows - 1, 0);
3558 g_fForceExit = TRUE;
3559
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003560 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 (dwCtrlType == CTRL_CLOSE_EVENT
3562 ? _("close")
3563 : dwCtrlType == CTRL_LOGOFF_EVENT
3564 ? _("logoff")
3565 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003566# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar0f873732019-12-05 20:28:46 +01003570 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar0f873732019-12-05 20:28:46 +01003572 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 default:
3575 return FALSE;
3576 }
3577}
3578
3579
3580/*
3581 * set the tty in (raw) ? "raw" : "cooked" mode
3582 */
3583 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003584mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
3586 DWORD cmodein;
3587 DWORD cmodeout;
3588 BOOL bEnableHandler;
3589
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003590# ifdef VIMDLL
3591 if (gui.in_use)
3592 return;
3593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 GetConsoleMode(g_hConIn, &cmodein);
3595 GetConsoleMode(g_hConOut, &cmodeout);
3596 if (tmode == TMODE_RAW)
3597 {
3598 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3599 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (g_fMouseActive)
3601 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003602 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003603# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003604 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3605 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003606 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003607# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003608 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003609# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003610 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 bEnableHandler = TRUE;
3612 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003613 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3616 ENABLE_ECHO_INPUT);
3617 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3618 bEnableHandler = FALSE;
3619 }
3620 SetConsoleMode(g_hConIn, cmodein);
3621 SetConsoleMode(g_hConOut, cmodeout);
3622 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3623
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003624# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (fdDump)
3626 {
3627 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3628 tmode == TMODE_RAW ? "raw" :
3629 tmode == TMODE_COOK ? "cooked" : "normal",
3630 cmodein, cmodeout);
3631 fflush(fdDump);
3632 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634}
3635
3636
3637/*
3638 * Get the size of the current window in `Rows' and `Columns'
3639 * Return OK when size could be determined, FAIL otherwise.
3640 */
3641 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003642mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 CONSOLE_SCREEN_BUFFER_INFO csbi;
3645
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003646# ifdef VIMDLL
3647 if (gui.in_use)
3648 return OK;
3649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3651 {
3652 /*
3653 * For some reason, we are trying to get the screen dimensions
3654 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3655 * variables are really intended to mean the size of Vim screen
3656 * while in termcap mode.
3657 */
3658 Rows = g_cbTermcap.Info.dwSize.Y;
3659 Columns = g_cbTermcap.Info.dwSize.X;
3660 }
3661 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3662 {
3663 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3664 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3665 }
3666 else
3667 {
3668 Rows = 25;
3669 Columns = 80;
3670 }
3671 return OK;
3672}
3673
3674/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003675 * Resize console buffer to 'COORD'
3676 */
3677 static void
3678ResizeConBuf(
3679 HANDLE hConsole,
3680 COORD coordScreen)
3681{
3682 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3683 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003684# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003685 if (fdDump)
3686 {
3687 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3688 GetLastError());
3689 fflush(fdDump);
3690 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003691# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003692 }
3693}
3694
3695/*
3696 * Resize console window size to 'srWindowRect'
3697 */
3698 static void
3699ResizeWindow(
3700 HANDLE hConsole,
3701 SMALL_RECT srWindowRect)
3702{
3703 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3704 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003705# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003706 if (fdDump)
3707 {
3708 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3709 GetLastError());
3710 fflush(fdDump);
3711 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003712# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003713 }
3714}
3715
3716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 * Set a console window to `xSize' * `ySize'
3718 */
3719 static void
3720ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003721 HANDLE hConsole,
3722 int xSize,
3723 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3726 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003728 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003729 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003731# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (fdDump)
3733 {
3734 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3735 fflush(fdDump);
3736 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar0f873732019-12-05 20:28:46 +01003739 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 coordScreen = GetLargestConsoleWindowSize(hConsole);
3741
Bram Moolenaar0f873732019-12-05 20:28:46 +01003742 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3744 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3745 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3746
3747 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3748 {
3749 int sx, sy;
3750
3751 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3752 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3753 if (sy < ySize || sx < xSize)
3754 {
3755 /*
3756 * Increasing number of lines/columns, do buffer first.
3757 * Use the maximal size in x and y direction.
3758 */
3759 if (sy < ySize)
3760 coordScreen.Y = ySize;
3761 else
3762 coordScreen.Y = sy;
3763 if (sx < xSize)
3764 coordScreen.X = xSize;
3765 else
3766 coordScreen.X = sx;
3767 SetConsoleScreenBufferSize(hConsole, coordScreen);
3768 }
3769 }
3770
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003771 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 coordScreen.X = xSize;
3773 coordScreen.Y = ySize;
3774
Bram Moolenaar2551c032018-08-23 22:38:31 +02003775 // In the new console call API, only the first time in reverse order
3776 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003778 ResizeWindow(hConsole, srWindowRect);
3779 ResizeConBuf(hConsole, coordScreen);
3780 }
3781 else
3782 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003783 // Workaround for a Windows 10 bug
3784 cursor.X = srWindowRect.Left;
3785 cursor.Y = srWindowRect.Top;
3786 SetConsoleCursorPosition(hConsole, cursor);
3787
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003788 ResizeConBuf(hConsole, coordScreen);
3789 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003790 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792}
3793
3794
3795/*
3796 * Set the console window to `Rows' * `Columns'
3797 */
3798 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003799mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 COORD coordScreen;
3802
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003803# ifdef VIMDLL
3804 if (gui.in_use)
3805 return;
3806# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003807 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (suppress_winsize != 0)
3809 {
3810 suppress_winsize = 2;
3811 return;
3812 }
3813
3814 if (term_console)
3815 {
3816 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3817
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (Rows > coordScreen.Y)
3820 Rows = coordScreen.Y;
3821 if (Columns > coordScreen.X)
3822 Columns = coordScreen.X;
3823
3824 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3825 }
3826}
3827
3828/*
3829 * Rows and/or Columns has changed.
3830 */
3831 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003832mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003834# ifdef VIMDLL
3835 if (gui.in_use)
3836 return;
3837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3839}
3840
3841
3842/*
3843 * Called when started up, to set the winsize that was delayed.
3844 */
3845 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003846mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848 if (suppress_winsize == 2)
3849 {
3850 suppress_winsize = 0;
3851 mch_set_shellsize();
3852 shell_resized();
3853 }
3854 suppress_winsize = 0;
3855}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003856#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003858 static BOOL
3859vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003860 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003861 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003862 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003863 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003864 PROCESS_INFORMATION *pi,
3865 LPVOID *env,
3866 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003867{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003868 BOOL ret = FALSE;
3869 WCHAR *wcmd, *wcwd = NULL;
3870
3871 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3872 if (wcmd == NULL)
3873 return FALSE;
3874 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003875 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003876 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3877 if (wcwd == NULL)
3878 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003879 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003880
3881 ret = CreateProcessW(
3882 NULL, // Executable name
3883 wcmd, // Command to execute
3884 NULL, // Process security attributes
3885 NULL, // Thread security attributes
3886 inherit_handles, // Inherit handles
3887 flags, // Creation flags
3888 env, // Environment
3889 wcwd, // Current directory
3890 (LPSTARTUPINFOW)si, // Startup information
3891 pi); // Process information
3892theend:
3893 vim_free(wcmd);
3894 vim_free(wcwd);
3895 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003899 static HINSTANCE
3900vim_shell_execute(
3901 char *cmd,
3902 INT n_show_cmd)
3903{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003904 HINSTANCE ret;
3905 WCHAR *wcmd;
3906
3907 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3908 if (wcmd == NULL)
3909 return (HINSTANCE) 0;
3910
3911 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3912 vim_free(wcmd);
3913 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003914}
3915
3916
Bram Moolenaar4f974752019-02-17 17:44:42 +01003917#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919/*
3920 * Specialised version of system() for Win32 GUI mode.
3921 * This version proceeds as follows:
3922 * 1. Create a console window for use by the subprocess
3923 * 2. Run the subprocess (it gets the allocated console by default)
3924 * 3. Wait for the subprocess to terminate and get its exit code
3925 * 4. Prompt the user to press a key to close the console window
3926 */
3927 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003928mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 STARTUPINFO si;
3931 PROCESS_INFORMATION pi;
3932 DWORD ret = 0;
3933 HWND hwnd = GetFocus();
3934
3935 si.cb = sizeof(si);
3936 si.lpReserved = NULL;
3937 si.lpDesktop = NULL;
3938 si.lpTitle = NULL;
3939 si.dwFlags = STARTF_USESHOWWINDOW;
3940 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003941 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003942 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003944 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003945 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
3947 si.wShowWindow = SW_SHOWNORMAL;
3948 si.cbReserved2 = 0;
3949 si.lpReserved2 = NULL;
3950
Bram Moolenaar0f873732019-12-05 20:28:46 +01003951 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003952 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003953 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3954 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar0f873732019-12-05 20:28:46 +01003956 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003958# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 int delay = 1;
3960
Bram Moolenaar0f873732019-12-05 20:28:46 +01003961 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 for (;;)
3963 {
3964 MSG msg;
3965
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003966 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
3968 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003969 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003970 delay = 1;
3971 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3974 break;
3975
Bram Moolenaar0f873732019-12-05 20:28:46 +01003976 // We start waiting for a very short time and then increase it, so
3977 // that we respond quickly when the process is quick, and don't
3978 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (delay < 50)
3980 delay += 10;
3981 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003982# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar0f873732019-12-05 20:28:46 +01003986 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 GetExitCodeProcess(pi.hProcess, &ret);
3988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
Bram Moolenaar0f873732019-12-05 20:28:46 +01003990 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 CloseHandle(pi.hThread);
3992 CloseHandle(pi.hProcess);
3993
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3996
3997 return ret;
3998}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003999
4000/*
4001 * Thread launched by the gui to send the current buffer data to the
4002 * process. This way avoid to hang up vim totally if the children
4003 * process take a long time to process the lines.
4004 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004005 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004006sub_process_writer(LPVOID param)
4007{
4008 HANDLE g_hChildStd_IN_Wr = param;
4009 linenr_T lnum = curbuf->b_op_start.lnum;
4010 DWORD len = 0;
4011 DWORD l;
4012 char_u *lp = ml_get(lnum);
4013 char_u *s;
4014 int written = 0;
4015
4016 for (;;)
4017 {
4018 l = (DWORD)STRLEN(lp + written);
4019 if (l == 0)
4020 len = 0;
4021 else if (lp[written] == NL)
4022 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004023 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004024 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4025 }
4026 else
4027 {
4028 s = vim_strchr(lp + written, NL);
4029 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4030 s == NULL ? l : (DWORD)(s - (lp + written)),
4031 &len, NULL);
4032 }
4033 if (len == (int)l)
4034 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004035 // Finished a line, add a NL, unless this line should not have
4036 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004037 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004038 || (!curbuf->b_p_bin
4039 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004040 || (lnum != curbuf->b_no_eol_lnum
4041 && (lnum != curbuf->b_ml.ml_line_count
4042 || curbuf->b_p_eol)))
4043 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004044 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4045 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004046 }
4047
4048 ++lnum;
4049 if (lnum > curbuf->b_op_end.lnum)
4050 break;
4051
4052 lp = ml_get(lnum);
4053 written = 0;
4054 }
4055 else if (len > 0)
4056 written += len;
4057 }
4058
Bram Moolenaar0f873732019-12-05 20:28:46 +01004059 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004060 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004061 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004062}
4063
4064
Bram Moolenaar0f873732019-12-05 20:28:46 +01004065# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004066
4067/*
4068 * This function read from the children's stdout and write the
4069 * data on screen or in the buffer accordingly.
4070 */
4071 static void
4072dump_pipe(int options,
4073 HANDLE g_hChildStd_OUT_Rd,
4074 garray_T *ga,
4075 char_u buffer[],
4076 DWORD *buffer_off)
4077{
4078 DWORD availableBytes = 0;
4079 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004080 int ret;
4081 DWORD len;
4082 DWORD toRead;
4083 int repeatCount;
4084
Bram Moolenaar0f873732019-12-05 20:28:46 +01004085 // we query the pipe to see if there is any data to read
4086 // to avoid to perform a blocking read
4087 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4088 NULL, // optional buffer
4089 0, // buffer size
4090 NULL, // number of read bytes
4091 &availableBytes, // available bytes total
4092 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004093
4094 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004095 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004096 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004097 {
4098 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004099 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004100 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004101 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004102
Bram Moolenaar0f873732019-12-05 20:28:46 +01004103 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004104 if (len == 0)
4105 break;
4106
4107 availableBytes -= len;
4108
4109 if (options & SHELL_READ)
4110 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004111 // Do NUL -> NL translation, append NL separated
4112 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004113 for (i = 0; i < len; ++i)
4114 {
4115 if (buffer[i] == NL)
4116 append_ga_line(ga);
4117 else if (buffer[i] == NUL)
4118 ga_append(ga, NL);
4119 else
4120 ga_append(ga, buffer[i]);
4121 }
4122 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004123 else if (has_mbyte)
4124 {
4125 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004126 int c;
4127 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004128
4129 len += *buffer_off;
4130 buffer[len] = NUL;
4131
Bram Moolenaar0f873732019-12-05 20:28:46 +01004132 // Check if the last character in buffer[] is
4133 // incomplete, keep these bytes for the next
4134 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004135 for (p = buffer; p < buffer + len; p += l)
4136 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004137 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004138 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004139 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004140 else if (MB_BYTE2LEN(*p) != l)
4141 break;
4142 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004143 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004144 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004145 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004146 if (len >= 12)
4147 ++p;
4148 else
4149 {
4150 *buffer_off = len;
4151 return;
4152 }
4153 }
4154 c = *p;
4155 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004156 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004157 if (p < buffer + len)
4158 {
4159 *p = c;
4160 *buffer_off = (DWORD)((buffer + len) - p);
4161 mch_memmove(buffer, p, *buffer_off);
4162 return;
4163 }
4164 *buffer_off = 0;
4165 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004166 else
4167 {
4168 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004169 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004170 }
4171
4172 windgoto(msg_row, msg_col);
4173 cursor_on();
4174 out_flush();
4175 }
4176}
4177
4178/*
4179 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4180 * for communication and doesn't open any new window.
4181 */
4182 static int
4183mch_system_piped(char *cmd, int options)
4184{
4185 STARTUPINFO si;
4186 PROCESS_INFORMATION pi;
4187 DWORD ret = 0;
4188
4189 HANDLE g_hChildStd_IN_Rd = NULL;
4190 HANDLE g_hChildStd_IN_Wr = NULL;
4191 HANDLE g_hChildStd_OUT_Rd = NULL;
4192 HANDLE g_hChildStd_OUT_Wr = NULL;
4193
Bram Moolenaar0f873732019-12-05 20:28:46 +01004194 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004195 DWORD len;
4196
Bram Moolenaar0f873732019-12-05 20:28:46 +01004197 // buffer used to receive keys
4198 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4199 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004200
4201 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004202 int noread_cnt = 0;
4203 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004204 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004205 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004206 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004207
4208 SECURITY_ATTRIBUTES saAttr;
4209
Bram Moolenaar0f873732019-12-05 20:28:46 +01004210 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004211 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4212 saAttr.bInheritHandle = TRUE;
4213 saAttr.lpSecurityDescriptor = NULL;
4214
4215 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004216 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004217 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004218 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004219 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004220 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004221 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004222 {
4223 CloseHandle(g_hChildStd_IN_Rd);
4224 CloseHandle(g_hChildStd_IN_Wr);
4225 CloseHandle(g_hChildStd_OUT_Rd);
4226 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004227 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004228 }
4229
4230 si.cb = sizeof(si);
4231 si.lpReserved = NULL;
4232 si.lpDesktop = NULL;
4233 si.lpTitle = NULL;
4234 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4235
Bram Moolenaar0f873732019-12-05 20:28:46 +01004236 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004237 si.hStdError = g_hChildStd_OUT_Wr;
4238 si.hStdOutput = g_hChildStd_OUT_Wr;
4239 si.hStdInput = g_hChildStd_IN_Rd;
4240 si.wShowWindow = SW_HIDE;
4241 si.cbReserved2 = 0;
4242 si.lpReserved2 = NULL;
4243
4244 if (options & SHELL_READ)
4245 ga_init2(&ga, 1, BUFLEN);
4246
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004247 if (cmd != NULL)
4248 {
4249 p = (char *)vim_strsave((char_u *)cmd);
4250 if (p != NULL)
4251 unescape_shellxquote((char_u *)p, p_sxe);
4252 else
4253 p = cmd;
4254 }
4255
Bram Moolenaar0f873732019-12-05 20:28:46 +01004256 // Now, run the command.
4257 // About "Inherit handles" being TRUE: this command can be litigious,
4258 // handle inheritance was deactivated for pending temp file, but, if we
4259 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004260 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4261 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004262
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004263 if (p != cmd)
4264 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004265
Bram Moolenaar0f873732019-12-05 20:28:46 +01004266 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004267 CloseHandle(g_hChildStd_IN_Rd);
4268 CloseHandle(g_hChildStd_OUT_Wr);
4269
4270 if (options & SHELL_WRITE)
4271 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004272 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004273 _beginthreadex(NULL, // security attributes
4274 0, // default stack size
4275 sub_process_writer, // function to be executed
4276 g_hChildStd_IN_Wr, // parameter
4277 0, // creation flag, start immediately
4278 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004279 CloseHandle(thread);
4280 g_hChildStd_IN_Wr = NULL;
4281 }
4282
Bram Moolenaar0f873732019-12-05 20:28:46 +01004283 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004284 for (;;)
4285 {
4286 MSG msg;
4287
Bram Moolenaar175d0702013-12-11 18:36:33 +01004288 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004289 {
4290 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004291 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004292 }
4293
Bram Moolenaar0f873732019-12-05 20:28:46 +01004294 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004295 if ((options & (SHELL_READ|SHELL_WRITE))
4296# ifdef FEAT_GUI
4297 || gui.in_use
4298# endif
4299 )
4300 {
4301 len = 0;
4302 if (!(options & SHELL_EXPAND)
4303 && ((options &
4304 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4305 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4306# ifdef FEAT_GUI
4307 || gui.in_use
4308# endif
4309 )
4310 && (ta_len > 0 || noread_cnt > 4))
4311 {
4312 if (ta_len == 0)
4313 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004314 // Get extra characters when we don't have any. Reset the
4315 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004316 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004317 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4318 }
4319 if (ta_len > 0 || len > 0)
4320 {
4321 /*
4322 * For pipes: Check for CTRL-C: send interrupt signal to
4323 * child. Check for CTRL-D: EOF, close pipe to child.
4324 */
4325 if (len == 1 && cmd != NULL)
4326 {
4327 if (ta_buf[ta_len] == Ctrl_C)
4328 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004329 // Learn what exit code is expected, for
4330 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004331 TerminateProcess(pi.hProcess, 9);
4332 }
4333 if (ta_buf[ta_len] == Ctrl_D)
4334 {
4335 CloseHandle(g_hChildStd_IN_Wr);
4336 g_hChildStd_IN_Wr = NULL;
4337 }
4338 }
4339
Bram Moolenaarf4140482020-02-15 23:06:45 +01004340 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004341
4342 /*
4343 * For pipes: echo the typed characters. For a pty this
4344 * does not seem to work.
4345 */
4346 for (i = ta_len; i < ta_len + len; ++i)
4347 {
4348 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4349 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004350 else if (has_mbyte)
4351 {
4352 int l = (*mb_ptr2len)(ta_buf + i);
4353
4354 msg_outtrans_len(ta_buf + i, l);
4355 i += l - 1;
4356 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004357 else
4358 msg_outtrans_len(ta_buf + i, 1);
4359 }
4360 windgoto(msg_row, msg_col);
4361 out_flush();
4362
4363 ta_len += len;
4364
4365 /*
4366 * Write the characters to the child, unless EOF has been
4367 * typed for pipes. Write one character at a time, to
4368 * avoid losing too much typeahead. When writing buffer
4369 * lines, drop the typed characters (only check for
4370 * CTRL-C).
4371 */
4372 if (options & SHELL_WRITE)
4373 ta_len = 0;
4374 else if (g_hChildStd_IN_Wr != NULL)
4375 {
4376 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4377 1, &len, NULL);
4378 // if we are typing in, we want to keep things reactive
4379 delay = 1;
4380 if (len > 0)
4381 {
4382 ta_len -= len;
4383 mch_memmove(ta_buf, ta_buf + len, ta_len);
4384 }
4385 }
4386 }
4387 }
4388 }
4389
4390 if (ta_len)
4391 ui_inchar_undo(ta_buf, ta_len);
4392
4393 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4394 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004395 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004396 break;
4397 }
4398
4399 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004400 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004401
Bram Moolenaar0f873732019-12-05 20:28:46 +01004402 // We start waiting for a very short time and then increase it, so
4403 // that we respond quickly when the process is quick, and don't
4404 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004405 if (delay < 50)
4406 delay += 10;
4407 }
4408
Bram Moolenaar0f873732019-12-05 20:28:46 +01004409 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004410 CloseHandle(g_hChildStd_OUT_Rd);
4411 if (g_hChildStd_IN_Wr != NULL)
4412 CloseHandle(g_hChildStd_IN_Wr);
4413
4414 WaitForSingleObject(pi.hProcess, INFINITE);
4415
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004417 GetExitCodeProcess(pi.hProcess, &ret);
4418
4419 if (options & SHELL_READ)
4420 {
4421 if (ga.ga_len > 0)
4422 {
4423 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004424 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004425 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4426 }
4427 else
4428 curbuf->b_no_eol_lnum = 0;
4429 ga_clear(&ga);
4430 }
4431
Bram Moolenaar0f873732019-12-05 20:28:46 +01004432 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004433 CloseHandle(pi.hThread);
4434 CloseHandle(pi.hProcess);
4435
4436 return ret;
4437}
4438
4439 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004440mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004441{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004442 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004443 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004444 return mch_system_piped(cmd, options);
4445 else
4446 return mch_system_classic(cmd, options);
4447}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004450#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004451 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004452mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004453{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004454 int ret;
4455 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004456 char_u *buf;
4457 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004458
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004459 // If the command starts and ends with double quotes, enclose the command
4460 // in parentheses.
4461 len = STRLEN(cmd);
4462 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4463 {
4464 len += 3;
4465 buf = alloc(len);
4466 if (buf == NULL)
4467 return -1;
4468 vim_snprintf((char *)buf, len, "(%s)", cmd);
4469 wcmd = enc_to_utf16(buf, NULL);
4470 free(buf);
4471 }
4472 else
4473 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4474
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004475 if (wcmd == NULL)
4476 return -1;
4477
4478 ret = _wsystem(wcmd);
4479 vim_free(wcmd);
4480 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
4483#endif
4484
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004485 static int
4486mch_system(char *cmd, int options)
4487{
4488#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004489 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004490 return mch_system_g(cmd, options);
4491 else
4492 return mch_system_c(cmd, options);
4493#elif defined(FEAT_GUI_MSWIN)
4494 return mch_system_g(cmd, options);
4495#else
4496 return mch_system_c(cmd, options);
4497#endif
4498}
4499
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004500#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4501/*
4502 * Use a terminal window to run a shell command in.
4503 */
4504 static int
4505mch_call_shell_terminal(
4506 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004507 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004508{
4509 jobopt_T opt;
4510 char_u *newcmd = NULL;
4511 typval_T argvar[2];
4512 long_u cmdlen;
4513 int retval = -1;
4514 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004515 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004516 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004517 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004518
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004519 if (cmd == NULL)
4520 cmdlen = STRLEN(p_sh) + 1;
4521 else
4522 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004523 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004524 if (newcmd == NULL)
4525 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004526 if (cmd == NULL)
4527 {
4528 STRCPY(newcmd, p_sh);
4529 ch_log(NULL, "starting terminal to run a shell");
4530 }
4531 else
4532 {
4533 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4534 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4535 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004536
4537 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004538
4539 argvar[0].v_type = VAR_STRING;
4540 argvar[0].vval.v_string = newcmd;
4541 argvar[1].v_type = VAR_UNKNOWN;
4542 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004543 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004544 {
4545 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004546 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004547 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004548
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004549 job = term_getjob(buf->b_term);
4550 ++job->jv_refcount;
4551
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004553 aucmd_prepbuf(&aco, buf);
4554
4555 clear_oparg(&oa);
4556 while (term_use_loop())
4557 {
4558 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4559 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004560 // If terminal_loop() returns OK we got a key that is handled
4561 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004562 if (terminal_loop(TRUE) == OK)
4563 normal_cmd(&oa, TRUE);
4564 }
4565 else
4566 normal_cmd(&oa, TRUE);
4567 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004568 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004569 ch_log(NULL, "system command finished");
4570
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004571 job_unref(job);
4572
Bram Moolenaar0f873732019-12-05 20:28:46 +01004573 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004574 aucmd_restbuf(&aco);
4575
4576 wait_return(TRUE);
4577 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4578
4579 vim_free(newcmd);
4580 return retval;
4581}
4582#endif
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584/*
4585 * Either execute a command by calling the shell or start a new shell
4586 */
4587 int
4588mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004589 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004590 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
4592 int x = 0;
4593 int tmode = cur_tmode;
4594#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004595 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004596
Bram Moolenaar0f873732019-12-05 20:28:46 +01004597 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004598 if (GetConsoleTitleW(szShellTitle,
4599 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004600 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004601 if (cmd == NULL)
4602 wcscat(szShellTitle, L" :sh");
4603 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004604 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004605 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004606
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004607 if (wn != NULL)
4608 {
4609 wcscat(szShellTitle, L" - !");
4610 if ((wcslen(szShellTitle) + wcslen(wn) <
4611 sizeof(szShellTitle)/sizeof(WCHAR)))
4612 wcscat(szShellTitle, wn);
4613 SetConsoleTitleW(szShellTitle);
4614 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004615 }
4616 }
4617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#endif
4619
4620 out_flush();
4621
4622#ifdef MCH_WRITE_DUMP
4623 if (fdDump)
4624 {
4625 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4626 fflush(fdDump);
4627 }
4628#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004629#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004630 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004631 if (
4632# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004633 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004634# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004635 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004636 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4637 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004638 char_u *cmdbase = cmd;
4639
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004640 if (cmdbase != NULL)
4641 // Skip a leading quote and (.
4642 while (*cmdbase == '"' || *cmdbase == '(')
4643 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004644
4645 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004646 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4647 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004648 {
4649 // Use a terminal window to run the command in.
4650 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004651# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004652 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004653# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004654 return x;
4655 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004656 }
4657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 /*
4660 * Catch all deadly signals while running the external command, because a
4661 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4662 */
4663 signal(SIGINT, SIG_IGN);
4664#if defined(__GNUC__) && !defined(__MINGW32__)
4665 signal(SIGKILL, SIG_IGN);
4666#else
4667 signal(SIGBREAK, SIG_IGN);
4668#endif
4669 signal(SIGILL, SIG_IGN);
4670 signal(SIGFPE, SIG_IGN);
4671 signal(SIGSEGV, SIG_IGN);
4672 signal(SIGTERM, SIG_IGN);
4673 signal(SIGABRT, SIG_IGN);
4674
4675 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 if (cmd == NULL)
4679 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004680 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 else
4683 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004684 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004685 char_u *newcmd = NULL;
4686 char_u *cmdbase = cmd;
4687 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004688
Bram Moolenaar0f873732019-12-05 20:28:46 +01004689 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004690 if (*cmdbase == '"' )
4691 ++cmdbase;
4692 if (*cmdbase == '(')
4693 ++cmdbase;
4694
Bram Moolenaar1c465442017-03-12 20:10:05 +01004695 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004696 {
4697 STARTUPINFO si;
4698 PROCESS_INFORMATION pi;
4699 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004700 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004701 char_u *p;
4702
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004703 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004704 si.cb = sizeof(si);
4705 si.lpReserved = NULL;
4706 si.lpDesktop = NULL;
4707 si.lpTitle = NULL;
4708 si.dwFlags = 0;
4709 si.cbReserved2 = 0;
4710 si.lpReserved2 = NULL;
4711
4712 cmdbase = skipwhite(cmdbase + 5);
4713 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004714 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004715 {
4716 cmdbase = skipwhite(cmdbase + 4);
4717 si.dwFlags = STARTF_USESHOWWINDOW;
4718 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004719 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004720 }
4721 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004722 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004723 {
4724 cmdbase = skipwhite(cmdbase + 2);
4725 flags = CREATE_NO_WINDOW;
4726 si.dwFlags = STARTF_USESTDHANDLES;
4727 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004728 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004729 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004730 NULL, // Security att.
4731 OPEN_EXISTING, // Open flags
4732 FILE_ATTRIBUTE_NORMAL, // File att.
4733 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004734 si.hStdOutput = si.hStdInput;
4735 si.hStdError = si.hStdInput;
4736 }
4737
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 // Remove a trailing ", ) and )" if they have a match
4739 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004740 if (cmdbase > cmd)
4741 {
4742 p = cmdbase + STRLEN(cmdbase);
4743 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4744 *--p = NUL;
4745 if (p > cmdbase && p[-1] == ')'
4746 && (*cmd =='(' || cmd[1] == '('))
4747 *--p = NUL;
4748 }
4749
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004750 newcmd = cmdbase;
4751 unescape_shellxquote(cmdbase, p_sxe);
4752
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004753 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004754 * If creating new console, arguments are passed to the
4755 * 'cmd.exe' as-is. If it's not, arguments are not treated
4756 * correctly for current 'cmd.exe'. So unescape characters in
4757 * shellxescape except '|' for avoiding to be treated as
4758 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004759 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004760 if (flags != CREATE_NEW_CONSOLE)
4761 {
4762 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004763 char_u *cmd_shell = mch_getenv("COMSPEC");
4764
4765 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004766 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004767
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004768 subcmd = vim_strsave_escaped_ext(cmdbase,
4769 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004770 if (subcmd != NULL)
4771 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004772 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004773 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004774 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004775 if (newcmd != NULL)
4776 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004777 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004778 else
4779 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004780 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004781 }
4782 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004783
4784 /*
4785 * Now, start the command as a process, so that it doesn't
4786 * inherit our handles which causes unpleasant dangling swap
4787 * files if we exit before the spawned process
4788 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004789 if (vim_create_process((char *)newcmd, FALSE, flags,
4790 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004791 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004792 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4793 > (HINSTANCE)32)
4794 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004795 else
4796 {
4797 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004798#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004799# ifdef VIMDLL
4800 if (gui.in_use)
4801# endif
4802 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004803#endif
4804 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004805
4806 if (newcmd != cmdbase)
4807 vim_free(newcmd);
4808
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004809 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004811 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004812 CloseHandle(si.hStdInput);
4813 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004814 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004815 CloseHandle(pi.hThread);
4816 CloseHandle(pi.hProcess);
4817 }
4818 else
4819 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004820 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004821#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004822 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004823 (!s_dont_use_vimrun && p_stmp ?
4824 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4825 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004827 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004828
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004829 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004830 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004832#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004833 if (
4834# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004835 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004836# endif
4837 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004839 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4840 "External commands will not pause after completion.\n"
4841 "See :help win32-vimrun for more information.");
4842 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004843 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4844 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004845
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004846 if (wmsg != NULL && wtitle != NULL)
4847 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4848 vim_free(wmsg);
4849 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 need_vimrun_warning = FALSE;
4851 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004852 if (
4853# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004854 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004855# endif
4856 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004857 // Use vimrun to execute the command. It opens a console
4858 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004859 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 vimrun_path,
4861 (msg_silent != 0 || (options & SHELL_DOOUT))
4862 ? "-s " : "",
4863 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004864 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004865# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004866 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004867# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004868 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004869 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004870 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4871 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004874 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004875 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004877 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 }
4881
4882 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004883 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
Bram Moolenaar0f873732019-12-05 20:28:46 +01004885 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004887#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004888 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004889 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891 )
4892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004893 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 msg_putchar('\n');
4895 }
4896#ifdef FEAT_TITLE
4897 resettitle();
4898#endif
4899
4900 signal(SIGINT, SIG_DFL);
4901#if defined(__GNUC__) && !defined(__MINGW32__)
4902 signal(SIGKILL, SIG_DFL);
4903#else
4904 signal(SIGBREAK, SIG_DFL);
4905#endif
4906 signal(SIGILL, SIG_DFL);
4907 signal(SIGFPE, SIG_DFL);
4908 signal(SIGSEGV, SIG_DFL);
4909 signal(SIGTERM, SIG_DFL);
4910 signal(SIGABRT, SIG_DFL);
4911
4912 return x;
4913}
4914
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004915#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004916 static HANDLE
4917job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004918 char_u *fname,
4919 DWORD dwDesiredAccess,
4920 DWORD dwShareMode,
4921 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4922 DWORD dwCreationDisposition,
4923 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004924{
4925 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004926 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004927
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004928 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004929 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004930 return INVALID_HANDLE_VALUE;
4931
4932 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4933 lpSecurityAttributes, dwCreationDisposition,
4934 dwFlagsAndAttributes, NULL);
4935 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004936 return h;
4937}
4938
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004939/*
4940 * Turn the dictionary "env" into a NUL separated list that can be used as the
4941 * environment argument of vim_create_process().
4942 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004943 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004944win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004945{
4946 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004947 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004948 LPVOID base = GetEnvironmentStringsW();
4949
Bram Moolenaar0f873732019-12-05 20:28:46 +01004950 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004951 if (ga_grow(gap, 1) == FAIL)
4952 return;
4953
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004954 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004955 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004956 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004957 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004958 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004959 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004960 typval_T *item = &dict_lookup(hi)->di_tv;
4961 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004963 --todo;
4964 if (wkey != NULL && wval != NULL)
4965 {
4966 size_t n;
4967 size_t lkey = wcslen(wkey);
4968 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004969
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004970 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4971 continue;
4972 for (n = 0; n < lkey; n++)
4973 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4974 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4975 for (n = 0; n < lval; n++)
4976 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4977 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4978 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004979 vim_free(wkey);
4980 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004981 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004982 }
4983 }
4984
Bram Moolenaar355757a2020-02-10 22:06:32 +01004985 if (base)
4986 {
4987 WCHAR *p = (WCHAR*) base;
4988
4989 // for last \0
4990 if (ga_grow(gap, 1) == FAIL)
4991 return;
4992
4993 while (*p != 0 || *(p + 1) != 0)
4994 {
4995 if (ga_grow(gap, 1) == OK)
4996 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4997 p++;
4998 }
4999 FreeEnvironmentStrings(base);
5000 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5001 }
5002
Bram Moolenaar493359e2018-06-12 20:25:52 +02005003# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005004 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005005# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005006 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005007 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005008# endif
5009# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005010 char_u *version = get_vim_var_str(VV_VERSION);
5011 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005012# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005013 // size of "VIM_SERVERNAME=" and value,
5014 // plus "VIM_TERMINAL=" and value,
5015 // plus two terminating NULs
5016 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005017# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005018 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005019# endif
5020# ifdef FEAT_TERMINAL
5021 + 13 + version_len + 2
5022# endif
5023 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005024
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005025 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005026 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005027# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005028 for (n = 0; n < 15; n++)
5029 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5030 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005031 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005032 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5033 (WCHAR)servername[n];
5034 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005035# endif
5036# ifdef FEAT_TERMINAL
5037 if (is_terminal)
5038 {
5039 for (n = 0; n < 13; n++)
5040 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5041 (WCHAR)"VIM_TERMINAL="[n];
5042 for (n = 0; n < version_len; n++)
5043 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5044 (WCHAR)version[n];
5045 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5046 }
5047# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005048 }
5049 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005050# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005051}
5052
Bram Moolenaarb091f302019-01-19 14:37:00 +01005053/*
5054 * Create a pair of pipes.
5055 * Return TRUE for success, FALSE for failure.
5056 */
5057 static BOOL
5058create_pipe_pair(HANDLE handles[2])
5059{
5060 static LONG s;
5061 char name[64];
5062 SECURITY_ATTRIBUTES sa;
5063
5064 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5065 GetCurrentProcessId(),
5066 InterlockedIncrement(&s));
5067
5068 // Create named pipe. Max size of named pipe is 65535.
5069 handles[1] = CreateNamedPipe(
5070 name,
5071 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5072 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005073 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005074
5075 if (handles[1] == INVALID_HANDLE_VALUE)
5076 return FALSE;
5077
5078 sa.nLength = sizeof(sa);
5079 sa.bInheritHandle = TRUE;
5080 sa.lpSecurityDescriptor = NULL;
5081
5082 handles[0] = CreateFile(name,
5083 FILE_GENERIC_READ,
5084 FILE_SHARE_READ, &sa,
5085 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5086
5087 if (handles[0] == INVALID_HANDLE_VALUE)
5088 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005089 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005090 return FALSE;
5091 }
5092
5093 return TRUE;
5094}
5095
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005096 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005097mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005098{
5099 STARTUPINFO si;
5100 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005101 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005102 SECURITY_ATTRIBUTES saAttr;
5103 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005104 HANDLE ifd[2];
5105 HANDLE ofd[2];
5106 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005107 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005108
5109 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5110 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5111 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5112 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5113 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5114 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5115 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5116
5117 if (use_out_for_err && use_null_for_out)
5118 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005119
5120 ifd[0] = INVALID_HANDLE_VALUE;
5121 ifd[1] = INVALID_HANDLE_VALUE;
5122 ofd[0] = INVALID_HANDLE_VALUE;
5123 ofd[1] = INVALID_HANDLE_VALUE;
5124 efd[0] = INVALID_HANDLE_VALUE;
5125 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005126 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005127
Bram Moolenaar14207f42016-10-27 21:13:10 +02005128 jo = CreateJobObject(NULL, NULL);
5129 if (jo == NULL)
5130 {
5131 job->jv_status = JOB_FAILED;
5132 goto failed;
5133 }
5134
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005135 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005136 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005137
Bram Moolenaar76467df2016-02-12 19:30:26 +01005138 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005139 ZeroMemory(&si, sizeof(si));
5140 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005141 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005142 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005143
Bram Moolenaard8070362016-02-15 21:56:54 +01005144 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5145 saAttr.bInheritHandle = TRUE;
5146 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005147
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005148 if (use_file_for_in)
5149 {
5150 char_u *fname = options->jo_io_name[PART_IN];
5151
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005152 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5153 FILE_SHARE_READ | FILE_SHARE_WRITE,
5154 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5155 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005157 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005158 goto failed;
5159 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005160 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005161 else if (!use_null_for_in
5162 && (!create_pipe_pair(ifd)
5163 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005164 goto failed;
5165
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005166 if (use_file_for_out)
5167 {
5168 char_u *fname = options->jo_io_name[PART_OUT];
5169
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005170 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5171 FILE_SHARE_READ | FILE_SHARE_WRITE,
5172 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5173 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005175 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005176 goto failed;
5177 }
5178 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005179 else if (!use_null_for_out &&
5180 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005181 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005182 goto failed;
5183
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005184 if (use_file_for_err)
5185 {
5186 char_u *fname = options->jo_io_name[PART_ERR];
5187
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005188 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5189 FILE_SHARE_READ | FILE_SHARE_WRITE,
5190 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5191 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005193 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005194 goto failed;
5195 }
5196 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005197 else if (!use_out_for_err && !use_null_for_err &&
5198 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005199 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005200 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005201
Bram Moolenaard8070362016-02-15 21:56:54 +01005202 si.dwFlags |= STARTF_USESTDHANDLES;
5203 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005204 si.hStdOutput = ofd[1];
5205 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5206
5207 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5208 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005209 if (options->jo_set & JO_CHANNEL)
5210 {
5211 channel = options->jo_channel;
5212 if (channel != NULL)
5213 ++channel->ch_refcount;
5214 }
5215 else
5216 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005217 if (channel == NULL)
5218 goto failed;
5219 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005220
5221 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005222 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005223 CREATE_DEFAULT_ERROR_MODE |
5224 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005225 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005226 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005227 &si, &pi,
5228 ga.ga_data,
5229 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005230 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005231 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005232 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005233 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005234 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005235
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005236 ga_clear(&ga);
5237
Bram Moolenaar14207f42016-10-27 21:13:10 +02005238 if (!AssignProcessToJobObject(jo, pi.hProcess))
5239 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005240 // if failing, switch the way to terminate
5241 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005242 CloseHandle(jo);
5243 jo = NULL;
5244 }
5245 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005246 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005247 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005248 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005249 job->jv_status = JOB_STARTED;
5250
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005251 CloseHandle(ifd[0]);
5252 CloseHandle(ofd[1]);
5253 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005254 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005255
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005256 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005257 if (channel != NULL)
5258 {
5259 channel_set_pipes(channel,
5260 use_file_for_in || use_null_for_in
5261 ? INVALID_FD : (sock_T)ifd[1],
5262 use_file_for_out || use_null_for_out
5263 ? INVALID_FD : (sock_T)ofd[0],
5264 use_out_for_err || use_file_for_err || use_null_for_err
5265 ? INVALID_FD : (sock_T)efd[0]);
5266 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005267 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005268 return;
5269
5270failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005271 CloseHandle(ifd[0]);
5272 CloseHandle(ofd[0]);
5273 CloseHandle(efd[0]);
5274 CloseHandle(ifd[1]);
5275 CloseHandle(ofd[1]);
5276 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005277 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005278 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005279}
5280
5281 char *
5282mch_job_status(job_T *job)
5283{
5284 DWORD dwExitCode = 0;
5285
Bram Moolenaar76467df2016-02-12 19:30:26 +01005286 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5287 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005288 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005289 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005290 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005291 {
5292 ch_log(job->jv_channel, "Job ended");
5293 job->jv_status = JOB_ENDED;
5294 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005295 return "dead";
5296 }
5297 return "run";
5298}
5299
Bram Moolenaar97792de2016-10-15 18:36:49 +02005300 job_T *
5301mch_detect_ended_job(job_T *job_list)
5302{
5303 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5304 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5305 job_T *job = job_list;
5306
5307 while (job != NULL)
5308 {
5309 DWORD n;
5310 DWORD result;
5311
5312 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5313 && job != NULL; job = job->jv_next)
5314 {
5315 if (job->jv_status == JOB_STARTED)
5316 {
5317 jobHandles[n] = job->jv_proc_info.hProcess;
5318 jobArray[n] = job;
5319 ++n;
5320 }
5321 }
5322 if (n == 0)
5323 continue;
5324 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5325 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5326 {
5327 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5328
5329 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5330 return wait_job;
5331 }
5332 }
5333 return NULL;
5334}
5335
Bram Moolenaarfb630902016-10-29 14:55:00 +02005336 static BOOL
5337terminate_all(HANDLE process, int code)
5338{
5339 PROCESSENTRY32 pe;
5340 HANDLE h = INVALID_HANDLE_VALUE;
5341 DWORD pid = GetProcessId(process);
5342
5343 if (pid != 0)
5344 {
5345 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5346 if (h != INVALID_HANDLE_VALUE)
5347 {
5348 pe.dwSize = sizeof(PROCESSENTRY32);
5349 if (!Process32First(h, &pe))
5350 goto theend;
5351
5352 do
5353 {
5354 if (pe.th32ParentProcessID == pid)
5355 {
5356 HANDLE ph = OpenProcess(
5357 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5358 if (ph != NULL)
5359 {
5360 terminate_all(ph, code);
5361 CloseHandle(ph);
5362 }
5363 }
5364 } while (Process32Next(h, &pe));
5365
5366 CloseHandle(h);
5367 }
5368 }
5369
5370theend:
5371 return TerminateProcess(process, code);
5372}
5373
5374/*
5375 * Send a (deadly) signal to "job".
5376 * Return FAIL if it didn't work.
5377 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005378 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005379mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005380{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005381 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005382
Bram Moolenaar923d9262016-02-25 20:56:01 +01005383 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005384 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005385 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005386 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005387 {
5388 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5389 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005390 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005391 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005392 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005393 }
5394
5395 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5396 return FAIL;
5397 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005398 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5399 job->jv_proc_info.dwProcessId)
5400 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005401 FreeConsole();
5402 return ret;
5403}
5404
5405/*
5406 * Clear the data related to "job".
5407 */
5408 void
5409mch_clear_job(job_T *job)
5410{
5411 if (job->jv_status != JOB_FAILED)
5412 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005413 if (job->jv_job_object != NULL)
5414 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005415 CloseHandle(job->jv_proc_info.hProcess);
5416 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005417}
5418#endif
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005421#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423/*
5424 * Start termcap mode
5425 */
5426 static void
5427termcap_mode_start(void)
5428{
5429 DWORD cmodein;
5430
5431 if (g_fTermcapMode)
5432 return;
5433
5434 SaveConsoleBuffer(&g_cbNonTermcap);
5435
5436 if (g_cbTermcap.IsValid)
5437 {
5438 /*
5439 * We've been in termcap mode before. Restore certain screen
5440 * characteristics, including the buffer size and the window
5441 * size. Since we will be redrawing the screen, we don't need
5442 * to restore the actual contents of the buffer.
5443 */
5444 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005445 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5447 Rows = g_cbTermcap.Info.dwSize.Y;
5448 Columns = g_cbTermcap.Info.dwSize.X;
5449 }
5450 else
5451 {
5452 /*
5453 * This is our first time entering termcap mode. Clear the console
5454 * screen buffer, and resize the buffer to match the current window
5455 * size. We will use this as the size of our editing environment.
5456 */
5457 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005458 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5460 }
5461
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005462# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (g_fMouseActive)
5468 cmodein |= ENABLE_MOUSE_INPUT;
5469 else
5470 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 cmodein |= ENABLE_WINDOW_INPUT;
5472 SetConsoleMode(g_hConIn, cmodein);
5473
5474 redraw_later_clear();
5475 g_fTermcapMode = TRUE;
5476}
5477
5478
5479/*
5480 * End termcap mode
5481 */
5482 static void
5483termcap_mode_end(void)
5484{
5485 DWORD cmodein;
5486 ConsoleBuffer *cb;
5487 COORD coord;
5488 DWORD dwDummy;
5489
5490 if (!g_fTermcapMode)
5491 return;
5492
5493 SaveConsoleBuffer(&g_cbTermcap);
5494
5495 GetConsoleMode(g_hConIn, &cmodein);
5496 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5497 SetConsoleMode(g_hConIn, cmodein);
5498
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005499# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005500 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005501# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005505 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 SetConsoleCursorInfo(g_hConOut, &g_cci);
5507
5508 if (p_rs || exiting)
5509 {
5510 /*
5511 * Clear anything that happens to be on the current line.
5512 */
5513 coord.X = 0;
5514 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5515 FillConsoleOutputCharacter(g_hConOut, ' ',
5516 cb->Info.dwSize.X, coord, &dwDummy);
5517 /*
5518 * The following is just for aesthetics. If we are exiting without
5519 * restoring the screen, then we want to have a prompt string
5520 * appear at the bottom line. However, the command interpreter
5521 * seems to always advance the cursor one line before displaying
5522 * the prompt string, which causes the screen to scroll. To
5523 * counter this, move the cursor up one line before exiting.
5524 */
5525 if (exiting && !p_rs)
5526 coord.Y--;
5527 /*
5528 * Position the cursor at the leftmost column of the desired row.
5529 */
5530 SetConsoleCursorPosition(g_hConOut, coord);
5531 }
5532
5533 g_fTermcapMode = FALSE;
5534}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005535#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005538#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 void
5540mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005541 char_u *s UNUSED,
5542 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005544 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
5546
5547#else
5548
5549/*
5550 * clear `n' chars, starting from `coord'
5551 */
5552 static void
5553clear_chars(
5554 COORD coord,
5555 DWORD n)
5556{
5557 DWORD dwDummy;
5558
5559 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005560
5561 if (!USE_VTP)
5562 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5563 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005564 {
5565 set_console_color_rgb();
5566 gotoxy(coord.X + 1, coord.Y + 1);
5567 vtp_printf("\033[%dX", n);
5568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571
5572/*
5573 * Clear the screen
5574 */
5575 static void
5576clear_screen(void)
5577{
5578 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005579
5580 if (!USE_VTP)
5581 clear_chars(g_coord, Rows * Columns);
5582 else
5583 {
5584 set_console_color_rgb();
5585 gotoxy(1, 1);
5586 vtp_printf("\033[2J");
5587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588}
5589
5590
5591/*
5592 * Clear to end of display
5593 */
5594 static void
5595clear_to_end_of_display(void)
5596{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005597 COORD save = g_coord;
5598
5599 if (!USE_VTP)
5600 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005602 else
5603 {
5604 set_console_color_rgb();
5605 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5606 vtp_printf("\033[0J");
5607
5608 gotoxy(save.X + 1, save.Y + 1);
5609 g_coord = save;
5610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611}
5612
5613
5614/*
5615 * Clear to end of line
5616 */
5617 static void
5618clear_to_end_of_line(void)
5619{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005620 COORD save = g_coord;
5621
5622 if (!USE_VTP)
5623 clear_chars(g_coord, Columns - g_coord.X);
5624 else
5625 {
5626 set_console_color_rgb();
5627 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5628 vtp_printf("\033[0K");
5629
5630 gotoxy(save.X + 1, save.Y + 1);
5631 g_coord = save;
5632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635
5636/*
5637 * Scroll the scroll region up by `cLines' lines
5638 */
5639 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005640scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 COORD oldcoord = g_coord;
5643
5644 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5645 delete_lines(cLines);
5646
5647 g_coord = oldcoord;
5648}
5649
5650
5651/*
5652 * Set the scroll region
5653 */
5654 static void
5655set_scroll_region(
5656 unsigned left,
5657 unsigned top,
5658 unsigned right,
5659 unsigned bottom)
5660{
5661 if (left >= right
5662 || top >= bottom
5663 || right > (unsigned) Columns - 1
5664 || bottom > (unsigned) Rows - 1)
5665 return;
5666
5667 g_srScrollRegion.Left = left;
5668 g_srScrollRegion.Top = top;
5669 g_srScrollRegion.Right = right;
5670 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005671}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005672
Bram Moolenaar6982f422019-02-16 16:48:01 +01005673 static void
5674set_scroll_region_tb(
5675 unsigned top,
5676 unsigned bottom)
5677{
5678 if (top >= bottom || bottom > (unsigned)Rows - 1)
5679 return;
5680
5681 g_srScrollRegion.Top = top;
5682 g_srScrollRegion.Bottom = bottom;
5683}
5684
5685 static void
5686set_scroll_region_lr(
5687 unsigned left,
5688 unsigned right)
5689{
5690 if (left >= right || right > (unsigned)Columns - 1)
5691 return;
5692
5693 g_srScrollRegion.Left = left;
5694 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695}
5696
5697
5698/*
5699 * Insert `cLines' lines at the current cursor position
5700 */
5701 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005702insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005704 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 COORD dest;
5706 CHAR_INFO fill;
5707
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005708 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5709
Bram Moolenaar6982f422019-02-16 16:48:01 +01005710 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 dest.Y = g_coord.Y + cLines;
5712
Bram Moolenaar6982f422019-02-16 16:48:01 +01005713 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 source.Top = g_coord.Y;
5715 source.Right = g_srScrollRegion.Right;
5716 source.Bottom = g_srScrollRegion.Bottom - cLines;
5717
Bram Moolenaar6982f422019-02-16 16:48:01 +01005718 clip.Left = g_srScrollRegion.Left;
5719 clip.Top = g_coord.Y;
5720 clip.Right = g_srScrollRegion.Right;
5721 clip.Bottom = g_srScrollRegion.Bottom;
5722
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005723 fill.Char.AsciiChar = ' ';
5724 if (!USE_VTP)
5725 fill.Attributes = g_attrCurrent;
5726 else
5727 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005729 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005730
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005731 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5732
Bram Moolenaar6982f422019-02-16 16:48:01 +01005733 // Here we have to deal with a win32 console flake: If the scroll
5734 // region looks like abc and we scroll c to a and fill with d we get
5735 // cbd... if we scroll block c one line at a time to a, we get cdd...
5736 // vim expects cdd consistently... So we have to deal with that
5737 // here... (this also occurs scrolling the same way in the other
5738 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 if (source.Bottom < dest.Y)
5741 {
5742 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005743 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaar6982f422019-02-16 16:48:01 +01005745 coord.X = source.Left;
5746 for (i = clip.Top; i < dest.Y; ++i)
5747 {
5748 coord.Y = i;
5749 clear_chars(coord, source.Right - source.Left + 1);
5750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752}
5753
5754
5755/*
5756 * Delete `cLines' lines at the current cursor position
5757 */
5758 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005759delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005761 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 COORD dest;
5763 CHAR_INFO fill;
5764 int nb;
5765
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005766 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5767
Bram Moolenaar6982f422019-02-16 16:48:01 +01005768 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 dest.Y = g_coord.Y;
5770
Bram Moolenaar6982f422019-02-16 16:48:01 +01005771 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 source.Top = g_coord.Y + cLines;
5773 source.Right = g_srScrollRegion.Right;
5774 source.Bottom = g_srScrollRegion.Bottom;
5775
Bram Moolenaar6982f422019-02-16 16:48:01 +01005776 clip.Left = g_srScrollRegion.Left;
5777 clip.Top = g_coord.Y;
5778 clip.Right = g_srScrollRegion.Right;
5779 clip.Bottom = g_srScrollRegion.Bottom;
5780
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005781 fill.Char.AsciiChar = ' ';
5782 if (!USE_VTP)
5783 fill.Attributes = g_attrCurrent;
5784 else
5785 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005787 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005788
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005789 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5790
Bram Moolenaar6982f422019-02-16 16:48:01 +01005791 // Here we have to deal with a win32 console flake; See insert_lines()
5792 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 nb = dest.Y + (source.Bottom - source.Top) + 1;
5795
5796 if (nb < source.Top)
5797 {
5798 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005799 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar6982f422019-02-16 16:48:01 +01005801 coord.X = source.Left;
5802 for (i = nb; i < clip.Bottom; ++i)
5803 {
5804 coord.Y = i;
5805 clear_chars(coord, source.Right - source.Left + 1);
5806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808}
5809
5810
5811/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005812 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 */
5814 static void
5815gotoxy(
5816 unsigned x,
5817 unsigned y)
5818{
5819 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5820 return;
5821
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005822 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005823 {
5824 // external cursor coords are 1-based; internal are 0-based
5825 g_coord.X = x - 1;
5826 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005827 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005828 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005829 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005830 {
5831 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005832 // destruction. Insider build bug. Always enabled because it's cheap
5833 // and avoids mistakes with recognizing the build.
5834 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005835
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005836 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005837
5838 g_coord.X = x - 1;
5839 g_coord.Y = y - 1;
5840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842
5843
5844/*
5845 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005846 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 */
5848 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005849textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005851 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 SetConsoleTextAttribute(g_hConOut, wAttr);
5854}
5855
5856
5857 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005858textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005860 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005862 if (!USE_VTP)
5863 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5864 else
5865 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866}
5867
5868
5869 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005870textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005872 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005874 if (!USE_VTP)
5875 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5876 else
5877 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879
5880
5881/*
5882 * restore the default text attribute (whatever we started with)
5883 */
5884 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005885normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005887 if (!USE_VTP)
5888 textattr(g_attrDefault);
5889 else
5890 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892
5893
5894static WORD g_attrPreStandout = 0;
5895
5896/*
5897 * Make the text standout, by brightening it
5898 */
5899 static void
5900standout(void)
5901{
5902 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5905}
5906
5907
5908/*
5909 * Turn off standout mode
5910 */
5911 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005912standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005916
5917 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918}
5919
5920
5921/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005922 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005925mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 char_u *p;
5928 int n;
5929
5930 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5931 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005932 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005933# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005934 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005935# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005936 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {
5938 p = T_ME + 2;
5939 n = getdigits(&p);
5940 if (*p == 'm' && n > 0)
5941 {
5942 cterm_normal_fg_color = (n & 0xf) + 1;
5943 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5944 }
5945 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005946# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005947 cterm_normal_fg_gui_color = INVALCOLOR;
5948 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950}
5951
5952
5953/*
5954 * visual bell: flash the screen
5955 */
5956 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005957visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
5959 COORD coordOrigin = {0, 0};
5960 WORD attrFlash = ~g_attrCurrent & 0xff;
5961
5962 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005963 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
5965 if (oldattrs == NULL)
5966 return;
5967 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5968 coordOrigin, &dwDummy);
5969 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5970 coordOrigin, &dwDummy);
5971
Bram Moolenaar0f873732019-12-05 20:28:46 +01005972 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005973 if (!USE_VTP)
5974 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 coordOrigin, &dwDummy);
5976 vim_free(oldattrs);
5977}
5978
5979
5980/*
5981 * Make the cursor visible or invisible
5982 */
5983 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005984cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005987# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990}
5991
5992
5993/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005994 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005995 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005997 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005999 char_u *pchBuf,
6000 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006002 COORD coord = g_coord;
6003 DWORD written;
6004 DWORD n, cchwritten, cells;
6005 static WCHAR *unicodebuf = NULL;
6006 static int unibuflen = 0;
6007 int length;
6008 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006010 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6011 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006012 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006013 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006014 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006015 unibuflen = length;
6016 }
6017 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6018 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006020 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006021
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006022 if (!USE_VTP)
6023 {
6024 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6025 coord, &written);
6026 // When writing fails or didn't write a single character, pretend one
6027 // character was written, otherwise we get stuck.
6028 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6029 coord, &cchwritten) == 0
6030 || cchwritten == 0 || cchwritten == (DWORD)-1)
6031 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006032 }
6033 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006034 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006035 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6036 NULL) == 0 || cchwritten == 0)
6037 cchwritten = 1;
6038 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006039
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006040 if (cchwritten == length)
6041 {
6042 written = cbToWrite;
6043 g_coord.X += (SHORT)cells;
6044 }
6045 else
6046 {
6047 char_u *p = pchBuf;
6048 for (n = 0; n < cchwritten; n++)
6049 MB_CPTR_ADV(p);
6050 written = p - pchBuf;
6051 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 while (g_coord.X > g_srScrollRegion.Right)
6055 {
6056 g_coord.X -= (SHORT) Columns;
6057 if (g_coord.Y < g_srScrollRegion.Bottom)
6058 g_coord.Y++;
6059 }
6060
6061 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6062
6063 return written;
6064}
6065
6066
6067/*
6068 * mch_write(): write the output buffer to the screen, translating ESC
6069 * sequences into calls to console output routines.
6070 */
6071 void
6072mch_write(
6073 char_u *s,
6074 int len)
6075{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006076# ifdef VIMDLL
6077 if (gui.in_use)
6078 return;
6079# endif
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 s[len] = NUL;
6082
6083 if (!term_console)
6084 {
6085 write(1, s, (unsigned)len);
6086 return;
6087 }
6088
Bram Moolenaar0f873732019-12-05 20:28:46 +01006089 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 while (len--)
6091 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006092 // optimization: use one single write_chars for runs of text,
6093 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006094 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 if (p_wd)
6097 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006098 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (prefix != 0)
6100 prefix = 1;
6101 }
6102
6103 if (prefix != 0)
6104 {
6105 DWORD nWritten;
6106
6107 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006108# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (fdDump)
6110 {
6111 fputc('>', fdDump);
6112 fwrite(s, sizeof(char_u), nWritten, fdDump);
6113 fputs("<\n", fdDump);
6114 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 len -= (nWritten - 1);
6117 s += nWritten;
6118 }
6119 else if (s[0] == '\n')
6120 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006121 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (g_coord.Y == g_srScrollRegion.Bottom)
6123 {
6124 scroll(1);
6125 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6126 }
6127 else
6128 {
6129 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6130 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006131# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (fdDump)
6133 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 s++;
6136 }
6137 else if (s[0] == '\r')
6138 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006139 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006141# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (fdDump)
6143 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 s++;
6146 }
6147 else if (s[0] == '\b')
6148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006149 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (g_coord.X > g_srScrollRegion.Left)
6151 g_coord.X--;
6152 else if (g_coord.Y > g_srScrollRegion.Top)
6153 {
6154 g_coord.X = g_srScrollRegion.Right;
6155 g_coord.Y--;
6156 }
6157 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006158# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (fdDump)
6160 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 s++;
6163 }
6164 else if (s[0] == '\a')
6165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006166 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006168# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (fdDump)
6170 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 s++;
6173 }
6174 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6175 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006176# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006177 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006178# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006179 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006180 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 switch (s[2])
6183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case '0': case '1': case '2': case '3': case '4':
6185 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006186 p = s + 1;
6187 do
6188 {
6189 ++p;
6190 args[argc] = getdigits(&p);
6191 argc += (argc < 15) ? 1 : 0;
6192 if (p > s + len)
6193 break;
6194 } while (*p == ';');
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (p > s + len)
6197 break;
6198
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006199 arg1 = args[0];
6200 arg2 = args[1];
6201 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006203 if (argc == 1 && args[0] == 0)
6204 normvideo();
6205 else if (argc == 1)
6206 {
6207 if (USE_VTP)
6208 textcolor((WORD) arg1);
6209 else
6210 textattr((WORD) arg1);
6211 }
6212 else if (USE_VTP)
6213 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006215 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006217 gotoxy(arg2, arg1);
6218 }
6219 else if (argc == 2 && *p == 'r')
6220 {
6221 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6222 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006223 else if (argc == 2 && *p == 'R')
6224 {
6225 set_scroll_region_tb(arg1, arg2);
6226 }
6227 else if (argc == 2 && *p == 'V')
6228 {
6229 set_scroll_region_lr(arg1, arg2);
6230 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006231 else if (argc == 1 && *p == 'A')
6232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 gotoxy(g_coord.X + 1,
6234 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6235 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006236 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 textbackground((WORD) arg1);
6239 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006240 else if (argc == 1 && *p == 'C')
6241 {
6242 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6243 g_coord.Y + 1);
6244 }
6245 else if (argc == 1 && *p == 'f')
6246 {
6247 textcolor((WORD) arg1);
6248 }
6249 else if (argc == 1 && *p == 'H')
6250 {
6251 gotoxy(1, arg1);
6252 }
6253 else if (argc == 1 && *p == 'L')
6254 {
6255 insert_lines(arg1);
6256 }
6257 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259 delete_lines(arg1);
6260 }
6261
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006262 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 s = p + 1;
6264 break;
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gotoxy(g_coord.X + 1,
6268 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6269 goto got3;
6270
6271 case 'B':
6272 visual_bell();
6273 goto got3;
6274
6275 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6277 g_coord.Y + 1);
6278 goto got3;
6279
6280 case 'E':
6281 termcap_mode_end();
6282 goto got3;
6283
6284 case 'F':
6285 standout();
6286 goto got3;
6287
6288 case 'f':
6289 standend();
6290 goto got3;
6291
6292 case 'H':
6293 gotoxy(1, 1);
6294 goto got3;
6295
6296 case 'j':
6297 clear_to_end_of_display();
6298 goto got3;
6299
6300 case 'J':
6301 clear_screen();
6302 goto got3;
6303
6304 case 'K':
6305 clear_to_end_of_line();
6306 goto got3;
6307
6308 case 'L':
6309 insert_lines(1);
6310 goto got3;
6311
6312 case 'M':
6313 delete_lines(1);
6314 goto got3;
6315
6316 case 'S':
6317 termcap_mode_start();
6318 goto got3;
6319
6320 case 'V':
6321 cursor_visible(TRUE);
6322 goto got3;
6323
6324 case 'v':
6325 cursor_visible(FALSE);
6326 goto got3;
6327
6328 got3:
6329 s += 3;
6330 len -= 2;
6331 }
6332
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006333# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (fdDump)
6335 {
6336 fputs("ESC | ", fdDump);
6337 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6338 fputc('\n', fdDump);
6339 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 else
6343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006344 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 DWORD nWritten;
6346
6347 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006348# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 if (fdDump)
6350 {
6351 fputc('>', fdDump);
6352 fwrite(s, sizeof(char_u), nWritten, fdDump);
6353 fputs("<\n", fdDump);
6354 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 len -= (nWritten - 1);
6358 s += nWritten;
6359 }
6360 }
6361
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006362# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (fdDump)
6364 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366}
6367
Bram Moolenaar0f873732019-12-05 20:28:46 +01006368#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370
6371/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006372 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
6374 void
6375mch_delay(
6376 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006377 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006379#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006380 Sleep((int)msec); // never wait for input
6381#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006382# ifdef VIMDLL
6383 if (gui.in_use)
6384 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006385 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006386 return;
6387 }
6388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006390# ifdef FEAT_MZSCHEME
6391 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6392 {
6393 int towait = p_mzq;
6394
Bram Moolenaar0f873732019-12-05 20:28:46 +01006395 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006396 while (msec > 0)
6397 {
6398 mzvim_check_threads();
6399 if (msec < towait)
6400 towait = msec;
6401 Sleep(towait);
6402 msec -= towait;
6403 }
6404 }
6405 else
6406# endif
6407 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006409 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410#endif
6411}
6412
6413
6414/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006415 * This version of remove is not scared by a readonly (backup) file.
6416 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 * Return 0 for success, -1 for failure.
6418 */
6419 int
6420mch_remove(char_u *name)
6421{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006422 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 int n;
6424
Bram Moolenaar203258c2016-01-17 22:15:16 +01006425 /*
6426 * On Windows, deleting a directory's symbolic link is done by
6427 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6428 */
6429 if (mch_isdir(name) && mch_is_symbolic_link(name))
6430 return mch_rmdir(name);
6431
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006432 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6433
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006434 wn = enc_to_utf16(name, NULL);
6435 if (wn == NULL)
6436 return -1;
6437
6438 n = DeleteFileW(wn) ? 0 : -1;
6439 vim_free(wn);
6440 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442
6443
6444/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006445 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 */
6447 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006448mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006450#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6451# ifdef VIMDLL
6452 if (!gui.in_use)
6453# endif
6454 if (g_fCtrlCPressed || g_fCBrkPressed)
6455 {
6456 ctrl_break_was_pressed = g_fCBrkPressed;
6457 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6458 got_int = TRUE;
6459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460#endif
6461}
6462
Bram Moolenaar0f873732019-12-05 20:28:46 +01006463// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006464#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006465
6466/*
6467 * How much main memory in KiB that can be used by VIM.
6468 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006469 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006470mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006471{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006472 MEMORYSTATUSEX ms;
6473
Bram Moolenaar0f873732019-12-05 20:28:46 +01006474 // Need to use GlobalMemoryStatusEx() when there is more memory than
6475 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006476 ms.dwLength = sizeof(MEMORYSTATUSEX);
6477 GlobalMemoryStatusEx(&ms);
6478 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006479 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006481 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006482 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006483 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006484 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006485 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006486 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006487 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006488 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006489 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006493 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6495 * file whose short file name is "FOO.BAR" (its long file name will
6496 * be correct: "foo.bar~"). Because a file can be accessed by
6497 * either its SFN or its LFN, "foo.bar" has effectively been
6498 * renamed to "foo.bar", which is not at all what was wanted. This
6499 * seems to happen only when renaming files with three-character
6500 * extensions by appending a suffix that does not include ".".
6501 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6502 *
6503 * There is another problem, which isn't really a bug but isn't right either:
6504 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6505 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6506 * service pack 6. Doesn't seem to happen on Windows 98.
6507 *
6508 * Like rename(), returns 0 upon success, non-zero upon failure.
6509 * Should probably set errno appropriately when errors occur.
6510 */
6511 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006512mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006514 WCHAR *p;
6515 int i;
6516 WCHAR szTempFile[_MAX_PATH + 1];
6517 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006520 // No need to play tricks unless the file name contains a "~" as the
6521 // seventh character.
6522 p = wold;
6523 for (i = 0; wold[i] != NUL; ++i)
6524 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6525 && wold[i + 1] != 0)
6526 p = wold + i + 1;
6527 if ((int)(wold + i - p) < 8 || p[6] != '~')
6528 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006530 // Get base path of new file name. Undocumented feature: If pszNewFile is
6531 // a directory, no error is returned and pszFilePart will be NULL.
6532 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 // Get (and create) a unique temporary file name in directory of new file
6537 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return -2;
6539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 // blow the temp file away
6541 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 return -3;
6543
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006544 // rename old file to the temp file
6545 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 return -4;
6547
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006548 // now create an empty file called pszOldFile; this prevents the operating
6549 // system using pszOldFile as an alias (SFN) if we're renaming within the
6550 // same directory. For example, we're editing a file called
6551 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6552 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6553 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6554 // cause all sorts of problems later in buf_write(). So, we create an
6555 // empty file called filena~1.txt and the system will have to find some
6556 // other SFN for filena~1.txt~, such as filena~2.txt
6557 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6559 return -5;
6560 if (!CloseHandle(hf))
6561 return -6;
6562
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 // rename the temp file to the new file
6564 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006566 // Renaming failed. Rename the file back to its old name, so that it
6567 // looks like nothing happened.
6568 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 return -7;
6570 }
6571
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006572 // Seems to be left around on Novell filesystems
6573 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006575 // finally, remove the empty old file
6576 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 return -8;
6578
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006579 return 0;
6580}
6581
6582
6583/*
6584 * Converts the filenames to UTF-16, then call mch_wrename().
6585 * Like rename(), returns 0 upon success, non-zero upon failure.
6586 */
6587 int
6588mch_rename(
6589 const char *pszOldFile,
6590 const char *pszNewFile)
6591{
6592 WCHAR *wold = NULL;
6593 WCHAR *wnew = NULL;
6594 int retval = -1;
6595
6596 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6597 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6598 if (wold != NULL && wnew != NULL)
6599 retval = mch_wrename(wold, wnew);
6600 vim_free(wold);
6601 vim_free(wnew);
6602 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605/*
6606 * Get the default shell for the current hardware platform
6607 */
6608 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006609default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006611 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
6613
6614/*
6615 * mch_access() extends access() to do more detailed check on network drives.
6616 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6617 */
6618 int
6619mch_access(char *n, int p)
6620{
6621 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006622 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006623 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006625 wn = enc_to_utf16((char_u *)n, NULL);
6626 if (wn == NULL)
6627 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006629 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 if (p & R_OK)
6634 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006635 // Read check is performed by seeing if we can do a find file on
6636 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006637 int i;
6638 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006640 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6641 TempNameW[i] = wn[i];
6642 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6643 TempNameW[i++] = '\\';
6644 TempNameW[i++] = '*';
6645 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006647 hFile = FindFirstFileW(TempNameW, &d);
6648 if (hFile == INVALID_HANDLE_VALUE)
6649 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006650 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653
6654 if (p & W_OK)
6655 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006656 // Trying to create a temporary file in the directory should catch
6657 // directories on read-only network shares. However, in
6658 // directories whose ACL allows writes but denies deletes will end
6659 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006660 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6661 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006662 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006663 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665 }
6666 else
6667 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006668 // Don't consider a file read-only if another process has opened it.
6669 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6670
Bram Moolenaar0f873732019-12-05 20:28:46 +01006671 // Trying to open the file for the required access does ACL, read-only
6672 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006673 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6674 | ((p & R_OK) ? GENERIC_READ : 0);
6675
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006676 hFile = CreateFileW(wn, access_mode, share_mode,
6677 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if (hFile == INVALID_HANDLE_VALUE)
6679 goto getout;
6680 CloseHandle(hFile);
6681 }
6682
Bram Moolenaar0f873732019-12-05 20:28:46 +01006683 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 return retval;
6687}
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006690 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 */
6692 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006693mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 WCHAR *wn;
6696 int f;
6697
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006698 wn = enc_to_utf16((char_u *)name, NULL);
6699 if (wn == NULL)
6700 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006702 f = _wopen(wn, flags, mode);
6703 vim_free(wn);
6704 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706
6707/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006708 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 */
6710 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006711mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 WCHAR *wn, *wm;
6714 FILE *f = NULL;
6715
Bram Moolenaara12a1612019-01-24 16:39:02 +01006716#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 // Work around an annoying assertion in the Microsoft debug CRT
6718 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006719 char newMode = mode[strlen(mode) - 1];
6720 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006721
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006722 _get_fmode(&oldMode);
6723 if (newMode == 't')
6724 _set_fmode(_O_TEXT);
6725 else if (newMode == 'b')
6726 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006727#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006728 wn = enc_to_utf16((char_u *)name, NULL);
6729 wm = enc_to_utf16((char_u *)mode, NULL);
6730 if (wn != NULL && wm != NULL)
6731 f = _wfopen(wn, wm);
6732 vim_free(wn);
6733 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006734
Bram Moolenaara12a1612019-01-24 16:39:02 +01006735#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006736 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006737#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006738 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741/*
6742 * SUB STREAM (aka info stream) handling:
6743 *
6744 * NTFS can have sub streams for each file. Normal contents of file is
6745 * stored in the main stream, and extra contents (author information and
6746 * title and so on) can be stored in sub stream. After Windows 2000, user
6747 * can access and store those informations in sub streams via explorer's
6748 * property menuitem in right click menu. Those informations in sub streams
6749 * were lost when copying only the main stream. So we have to copy sub
6750 * streams.
6751 *
6752 * Incomplete explanation:
6753 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6754 * More useful info and an example:
6755 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6756 */
6757
6758/*
6759 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6760 * and write to stream "substream" of file "to".
6761 * Errors are ignored.
6762 */
6763 static void
6764copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6765{
6766 HANDLE hTo;
6767 WCHAR *to_name;
6768
6769 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6770 wcscpy(to_name, to);
6771 wcscat(to_name, substream);
6772
6773 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6774 FILE_ATTRIBUTE_NORMAL, NULL);
6775 if (hTo != INVALID_HANDLE_VALUE)
6776 {
6777 long done;
6778 DWORD todo;
6779 DWORD readcnt, written;
6780 char buf[4096];
6781
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 for (done = 0; done < len; done += written)
6784 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006785 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006786 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6787 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6789 FALSE, FALSE, context)
6790 || readcnt != todo
6791 || !WriteFile(hTo, buf, todo, &written, NULL)
6792 || written != todo)
6793 break;
6794 }
6795 CloseHandle(hTo);
6796 }
6797
6798 free(to_name);
6799}
6800
6801/*
6802 * Copy info streams from file "from" to file "to".
6803 */
6804 static void
6805copy_infostreams(char_u *from, char_u *to)
6806{
6807 WCHAR *fromw;
6808 WCHAR *tow;
6809 HANDLE sh;
6810 WIN32_STREAM_ID sid;
6811 int headersize;
6812 WCHAR streamname[_MAX_PATH];
6813 DWORD readcount;
6814 void *context = NULL;
6815 DWORD lo, hi;
6816 int len;
6817
Bram Moolenaar0f873732019-12-05 20:28:46 +01006818 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006819 fromw = enc_to_utf16(from, NULL);
6820 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (fromw != NULL && tow != NULL)
6822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6825 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6826 if (sh != INVALID_HANDLE_VALUE)
6827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 // Use BackupRead() to find the info streams. Repeat until we
6829 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 for (;;)
6831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006832 // Get the header to find the length of the stream name. If
6833 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006835 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6837 &readcount, FALSE, FALSE, &context)
6838 || readcount == 0)
6839 break;
6840
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 // We only deal with streams that have a name. The normal
6842 // file data appears to be without a name, even though docs
6843 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (sid.dwStreamNameSize > 0)
6845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006846 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 if (!BackupRead(sh, (LPBYTE)streamname,
6848 sid.dwStreamNameSize,
6849 &readcount, FALSE, FALSE, &context))
6850 break;
6851
Bram Moolenaar0f873732019-12-05 20:28:46 +01006852 // Copy an info stream with a name ":anything:$DATA".
6853 // Skip "::$DATA", it has no stream name (examples suggest
6854 // it might be used for the normal file contents).
6855 // Note that BackupRead() counts bytes, but the name is in
6856 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 len = readcount / sizeof(WCHAR);
6858 streamname[len] = 0;
6859 if (len > 7 && wcsicmp(streamname + len - 6,
6860 L":$DATA") == 0)
6861 {
6862 streamname[len - 6] = 0;
6863 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006864 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866 }
6867
Bram Moolenaar0f873732019-12-05 20:28:46 +01006868 // Advance to the next stream. We might try seeking too far,
6869 // but BackupSeek() doesn't skip over stream borders, thus
6870 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 &lo, &hi, &context);
6873 }
6874
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6877
6878 CloseHandle(sh);
6879 }
6880 }
6881 vim_free(fromw);
6882 vim_free(tow);
6883}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
6885/*
6886 * Copy file attributes from file "from" to file "to".
6887 * For Windows NT and later we copy info streams.
6888 * Always returns zero, errors are ignored.
6889 */
6890 int
6891mch_copy_file_attribute(char_u *from, char_u *to)
6892{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006893 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006894 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 return 0;
6896}
6897
6898#if defined(MYRESETSTKOFLW) || defined(PROTO)
6899/*
6900 * Recreate a destroyed stack guard page in win32.
6901 * Written by Benjamin Peterson.
6902 */
6903
Bram Moolenaar0f873732019-12-05 20:28:46 +01006904// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006905# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
6907/*
6908 * This function does the same thing as _resetstkoflw(), which is only
6909 * available in DevStudio .net and later.
6910 * Returns 0 for failure, 1 for success.
6911 */
6912 int
6913myresetstkoflw(void)
6914{
6915 BYTE *pStackPtr;
6916 BYTE *pGuardPage;
6917 BYTE *pStackBase;
6918 BYTE *pLowestPossiblePage;
6919 MEMORY_BASIC_INFORMATION mbi;
6920 SYSTEM_INFO si;
6921 DWORD nPageSize;
6922 DWORD dummy;
6923
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 GetSystemInfo(&si);
6926 nPageSize = si.dwPageSize;
6927
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 pStackPtr = (BYTE*)_alloca(1);
6930
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6933 return 0;
6934 pStackBase = (BYTE*)mbi.AllocationBase;
6935
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006936 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006937 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006938 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006941 // We want the first committed page in the stack Start at the stack
6942 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 BYTE *pBlock = pStackBase;
6944
Bram Moolenaara466c992005-07-09 21:03:22 +00006945 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
6947 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6948 return 0;
6949
6950 pBlock += mbi.RegionSize;
6951
6952 if (mbi.State & MEM_COMMIT)
6953 break;
6954 }
6955
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 if (mbi.Protect & PAGE_GUARD)
6958 return 1;
6959
Bram Moolenaar0f873732019-12-05 20:28:46 +01006960 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6962 pGuardPage = pLowestPossiblePage;
6963 else
6964 pGuardPage = (BYTE*)mbi.BaseAddress;
6965
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6968 return 0;
6969
Bram Moolenaar0f873732019-12-05 20:28:46 +01006970 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6972 &dummy))
6973 return 0;
6974 }
6975
6976 return 1;
6977}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006980
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006981/*
6982 * The command line arguments in UCS2
6983 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006984static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006985static LPWSTR *ArglistW = NULL;
6986static int global_argc = 0;
6987static char **global_argv;
6988
Bram Moolenaar0f873732019-12-05 20:28:46 +01006989static int used_file_argc = 0; // last argument in global_argv[] used
6990 // for the argument list.
6991static int *used_file_indexes = NULL; // indexes in global_argv[] for
6992 // command line arguments added to
6993 // the argument list
6994static int used_file_count = 0; // nr of entries in used_file_indexes
6995static int used_file_literal = FALSE; // take file names literally
6996static int used_file_full_path = FALSE; // file name was full path
6997static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006998static int used_alist_count = 0;
6999
7000
7001/*
7002 * Get the command line arguments. Unicode version.
7003 * Returns argc. Zero when something fails.
7004 */
7005 int
7006get_cmd_argsW(char ***argvp)
7007{
7008 char **argv = NULL;
7009 int argc = 0;
7010 int i;
7011
Bram Moolenaar14993322014-09-09 12:25:33 +02007012 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007013 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7014 if (ArglistW != NULL)
7015 {
7016 argv = malloc((nArgsW + 1) * sizeof(char *));
7017 if (argv != NULL)
7018 {
7019 argc = nArgsW;
7020 argv[argc] = NULL;
7021 for (i = 0; i < argc; ++i)
7022 {
7023 int len;
7024
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007026 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007027 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007028 (LPSTR *)&argv[i], &len, 0, 0);
7029 if (argv[i] == NULL)
7030 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007031 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007032 while (i > 0)
7033 free(argv[--i]);
7034 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007035 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007036 argc = 0;
7037 }
7038 }
7039 }
7040 }
7041
7042 global_argc = argc;
7043 global_argv = argv;
7044 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007045 {
7046 if (used_file_indexes != NULL)
7047 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007048 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007049 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007050
7051 if (argvp != NULL)
7052 *argvp = argv;
7053 return argc;
7054}
7055
7056 void
7057free_cmd_argsW(void)
7058{
7059 if (ArglistW != NULL)
7060 {
7061 GlobalFree(ArglistW);
7062 ArglistW = NULL;
7063 }
7064}
7065
7066/*
7067 * Remember "name" is an argument that was added to the argument list.
7068 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7069 * is called.
7070 */
7071 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007072used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007073{
7074 int i;
7075
7076 if (used_file_indexes == NULL)
7077 return;
7078 for (i = used_file_argc + 1; i < global_argc; ++i)
7079 if (STRCMP(global_argv[i], name) == 0)
7080 {
7081 used_file_argc = i;
7082 used_file_indexes[used_file_count++] = i;
7083 break;
7084 }
7085 used_file_literal = literal;
7086 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007087 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007088}
7089
7090/*
7091 * Remember the length of the argument list as it was. If it changes then we
7092 * leave it alone when 'encoding' is set.
7093 */
7094 void
7095set_alist_count(void)
7096{
7097 used_alist_count = GARGCOUNT;
7098}
7099
7100/*
7101 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7102 * has been changed while starting up. Use the UCS-2 command line arguments
7103 * and convert them to 'encoding'.
7104 */
7105 void
7106fix_arg_enc(void)
7107{
7108 int i;
7109 int idx;
7110 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007111 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007112
Bram Moolenaar0f873732019-12-05 20:28:46 +01007113 // Safety checks:
7114 // - if argument count differs between the wide and non-wide argument
7115 // list, something must be wrong.
7116 // - the file name arguments must have been located.
7117 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007118 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007119 || ArglistW == NULL
7120 || used_file_indexes == NULL
7121 || used_file_count == 0
7122 || used_alist_count != GARGCOUNT)
7123 return;
7124
Bram Moolenaar0f873732019-12-05 20:28:46 +01007125 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007126 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007127 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007128 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007129 for (i = 0; i < GARGCOUNT; ++i)
7130 fnum_list[i] = GARGLIST[i].ae_fnum;
7131
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007133 alist_clear(&global_alist);
7134 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007135 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007136
7137 for (i = 0; i < used_file_count; ++i)
7138 {
7139 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007140 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007141 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007142 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007143 int literal = used_file_literal;
7144
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007145#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007146 // When using diff mode may need to concatenate file name to
7147 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007148 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7149 && !mch_isdir(alist_name(&GARGLIST[0])))
7150 {
7151 char_u *r;
7152
7153 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7154 if (r != NULL)
7155 {
7156 vim_free(str);
7157 str = r;
7158 }
7159 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007160#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007161 // Re-use the old buffer by renaming it. When not using literal
7162 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007163 if (used_file_literal)
7164 buf_set_name(fnum_list[i], str);
7165
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // Check backtick literal. backtick literal is already expanded in
7167 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007168 if (literal == FALSE)
7169 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007170 size_t len = STRLEN(str);
7171
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007172 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7173 literal = TRUE;
7174 }
7175 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007176 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007177 }
7178
7179 if (!used_file_literal)
7180 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 // Now expand wildcards in the arguments.
7182 // Temporarily add '(' and ')' to 'isfname'. These are valid
7183 // filename characters but are excluded from 'isfname' to make
7184 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7185 // Also, unset wildignore to not be influenced by this option.
7186 // The arguments specified in command-line should be kept even if
7187 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007188 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007189 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007190 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007191 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007192 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007193 }
7194
Bram Moolenaar0f873732019-12-05 20:28:46 +01007195 // If wildcard expansion failed, we are editing the first file of the
7196 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007197 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7198 {
7199 do_cmdline_cmd((char_u *)":rewind");
7200 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007201 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007202 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007203
7204 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007205}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007206
7207 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007208mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007209{
7210 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007211 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007212
Bram Moolenaar964b3742019-05-24 18:54:09 +02007213 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007214 if (envbuf == NULL)
7215 return -1;
7216
7217 sprintf((char *)envbuf, "%s=%s", var, value);
7218
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007219 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007220
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007221 vim_free(envbuf);
7222 if (p == NULL)
7223 return -1;
7224 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007225#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007226 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007227#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007228 // Unlike Un*x systems, we can free the string for _wputenv().
7229 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007230
7231 return 0;
7232}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007233
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007234/*
7235 * Support for 256 colors and 24-bit colors was added in Windows 10
7236 * version 1703 (Creators update).
7237 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007238#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7239
7240/*
7241 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007242 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007243 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007244#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007245
7246/*
7247 * ConPTY differences between versions, need different logic.
7248 * version 1903 (May 2019 update).
7249 */
7250#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7251
7252/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007253 * version 1909 (November 2019 update).
7254 */
7255#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7256
7257/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007258 * Confirm until this version. Also the logic changes.
7259 * insider preview.
7260 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007261#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007262
7263/*
7264 * Not stable now.
7265 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007266#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007267
7268 static void
7269vtp_flag_init(void)
7270{
7271 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007272#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007273 DWORD mode;
7274 HANDLE out;
7275
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007276# ifdef VIMDLL
7277 if (!gui.in_use)
7278# endif
7279 {
7280 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007281
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007282 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7283 GetConsoleMode(out, &mode);
7284 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7285 if (SetConsoleMode(out, mode) == 0)
7286 vtp_working = 0;
7287 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007288#endif
7289
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007290 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007291 conpty_working = 1;
7292 if (ver >= CONPTY_STABLE_BUILD)
7293 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007294
Bram Moolenaar57da6982019-09-13 22:30:11 +02007295 if (ver <= CONPTY_INSIDER_BUILD)
7296 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007297 if (ver <= CONPTY_1909_BUILD)
7298 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007299 if (ver <= CONPTY_1903_BUILD)
7300 conpty_type = 2;
7301 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7302 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007303}
7304
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007305#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007306
7307 static void
7308vtp_init(void)
7309{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007310 HMODULE hKerneldll;
7311 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007312# ifdef FEAT_TERMGUICOLORS
7313 COLORREF fg, bg;
7314# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007315
Bram Moolenaar0f873732019-12-05 20:28:46 +01007316 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007317 hKerneldll = GetModuleHandle("kernel32.dll");
7318 if (hKerneldll != NULL)
7319 {
7320 pGetConsoleScreenBufferInfoEx =
7321 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7322 hKerneldll, "GetConsoleScreenBufferInfoEx");
7323 pSetConsoleScreenBufferInfoEx =
7324 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7325 hKerneldll, "SetConsoleScreenBufferInfoEx");
7326 if (pGetConsoleScreenBufferInfoEx != NULL
7327 && pSetConsoleScreenBufferInfoEx != NULL)
7328 has_csbiex = TRUE;
7329 }
7330
7331 csbi.cbSize = sizeof(csbi);
7332 if (has_csbiex)
7333 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007334 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7335 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007336 store_console_bg_rgb = save_console_bg_rgb;
7337 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007338
7339# ifdef FEAT_TERMGUICOLORS
7340 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7341 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7342 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7343 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7344 default_console_color_bg = bg;
7345 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007346# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007347
7348 set_console_color_rgb();
7349}
7350
7351 static void
7352vtp_exit(void)
7353{
Bram Moolenaardf543822020-01-30 11:53:59 +01007354 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007355}
7356
7357 static int
7358vtp_printf(
7359 char *format,
7360 ...)
7361{
7362 char_u buf[100];
7363 va_list list;
7364 DWORD result;
7365
7366 va_start(list, format);
7367 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7368 va_end(list);
7369 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7370 return (int)result;
7371}
7372
7373 static void
7374vtp_sgr_bulk(
7375 int arg)
7376{
7377 int args[1];
7378
7379 args[0] = arg;
7380 vtp_sgr_bulks(1, args);
7381}
7382
7383 static void
7384vtp_sgr_bulks(
7385 int argc,
7386 int *args
7387)
7388{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007389 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007390 char_u buf[2 + (4 * 16) + 1];
7391 char_u *p;
7392 int i;
7393
7394 p = buf;
7395 *p++ = '\033';
7396 *p++ = '[';
7397
7398 for (i = 0; i < argc; ++i)
7399 {
7400 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7401 *p++ = ';';
7402 }
7403 p--;
7404 *p++ = 'm';
7405 *p = NUL;
7406 vtp_printf((char *)buf);
7407}
7408
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007409# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007410 static int
7411ctermtoxterm(
7412 int cterm)
7413{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007414 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007415
7416 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7417 return (((int)r << 16) | ((int)g << 8) | (int)b);
7418}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007419# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007420
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007421 static void
7422set_console_color_rgb(void)
7423{
7424# ifdef FEAT_TERMGUICOLORS
7425 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007426 guicolor_T fg, bg;
7427 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007428
7429 if (!USE_VTP)
7430 return;
7431
Bram Moolenaara050b942019-12-02 21:35:31 +01007432 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7433
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007434 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7435 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7436
7437 csbi.cbSize = sizeof(csbi);
7438 if (has_csbiex)
7439 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7440
7441 csbi.cbSize = sizeof(csbi);
7442 csbi.srWindow.Right += 1;
7443 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007444 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7445 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007446 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7447 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007448 if (has_csbiex)
7449 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7450# endif
7451}
7452
Bram Moolenaara050b942019-12-02 21:35:31 +01007453# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7454 void
7455get_default_console_color(
7456 int *cterm_fg,
7457 int *cterm_bg,
7458 guicolor_T *gui_fg,
7459 guicolor_T *gui_bg)
7460{
7461 int id;
7462 guicolor_T guifg = INVALCOLOR;
7463 guicolor_T guibg = INVALCOLOR;
7464 int ctermfg = 0;
7465 int ctermbg = 0;
7466
7467 id = syn_name2id((char_u *)"Normal");
7468 if (id > 0 && p_tgc)
7469 syn_id2colors(id, &guifg, &guibg);
7470 if (guifg == INVALCOLOR)
7471 {
7472 ctermfg = -1;
7473 if (id > 0)
7474 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7475 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7476 : default_console_color_fg;
7477 cterm_normal_fg_gui_color = guifg;
7478 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7479 }
7480 if (guibg == INVALCOLOR)
7481 {
7482 ctermbg = -1;
7483 if (id > 0)
7484 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7485 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7486 : default_console_color_bg;
7487 cterm_normal_bg_gui_color = guibg;
7488 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7489 }
7490
7491 *cterm_fg = ctermfg;
7492 *cterm_bg = ctermbg;
7493 *gui_fg = guifg;
7494 *gui_bg = guibg;
7495}
7496# endif
7497
Bram Moolenaardf543822020-01-30 11:53:59 +01007498/*
7499 * Set the console colors to the original colors or the last set colors.
7500 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007501 static void
7502reset_console_color_rgb(void)
7503{
7504# ifdef FEAT_TERMGUICOLORS
7505 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7506
7507 csbi.cbSize = sizeof(csbi);
7508 if (has_csbiex)
7509 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7510
7511 csbi.cbSize = sizeof(csbi);
7512 csbi.srWindow.Right += 1;
7513 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007514 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7515 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7516 if (has_csbiex)
7517 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7518# endif
7519}
7520
7521/*
7522 * Set the console colors to the original colors.
7523 */
7524 static void
7525restore_console_color_rgb(void)
7526{
7527# ifdef FEAT_TERMGUICOLORS
7528 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7529
7530 csbi.cbSize = sizeof(csbi);
7531 if (has_csbiex)
7532 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7533
7534 csbi.cbSize = sizeof(csbi);
7535 csbi.srWindow.Right += 1;
7536 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007537 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7538 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007539 if (has_csbiex)
7540 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7541# endif
7542}
7543
7544 void
7545control_console_color_rgb(void)
7546{
7547 if (USE_VTP)
7548 set_console_color_rgb();
7549 else
7550 reset_console_color_rgb();
7551}
7552
7553 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007554use_vtp(void)
7555{
7556 return USE_VTP;
7557}
7558
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007559 int
7560is_term_win32(void)
7561{
7562 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7563}
7564
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007565 int
7566has_vtp_working(void)
7567{
7568 return vtp_working;
7569}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007570
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007571#endif
7572
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007573 int
7574has_conpty_working(void)
7575{
7576 return conpty_working;
7577}
7578
7579 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007580get_conpty_type(void)
7581{
7582 return conpty_type;
7583}
7584
7585 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007586is_conpty_stable(void)
7587{
7588 return conpty_stable;
7589}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007590
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007591#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007592 void
7593resize_console_buf(void)
7594{
7595 CONSOLE_SCREEN_BUFFER_INFO csbi;
7596 COORD coord;
7597 SMALL_RECT newsize;
7598
7599 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7600 {
7601 coord.X = SRWIDTH(csbi.srWindow);
7602 coord.Y = SRHEIGHT(csbi.srWindow);
7603 SetConsoleScreenBufferSize(g_hConOut, coord);
7604
7605 newsize.Left = 0;
7606 newsize.Top = 0;
7607 newsize.Right = coord.X - 1;
7608 newsize.Bottom = coord.Y - 1;
7609 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7610
7611 SetConsoleScreenBufferSize(g_hConOut, coord);
7612 }
7613}
7614#endif