blob: 85efc52663a9e8311e5e6234efbfb889140630da [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 Moolenaarbc970da2020-04-26 19:00:07 +0200310 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100311
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200312 if (nLength == -2)
313 return (s_dwMax > 0) ? TRUE : FALSE;
314
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100315 if (!win8_or_later)
316 {
317 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200318 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
319 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100320 }
321
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100322 if (s_dwMax == 0)
323 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100324 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200325 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
326 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100327 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100328 s_dwIndex = 0;
329 s_dwMax = dwEvents;
330 if (dwEvents == 0)
331 {
332 *lpEvents = 0;
333 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100334 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100335
336 if (s_dwMax > 1)
337 {
338 head = 0;
339 tail = s_dwMax - 1;
340 while (head != tail)
341 {
342 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
343 && s_irCache[head + 1].EventType
344 == WINDOW_BUFFER_SIZE_EVENT)
345 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100346 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100347 for (i = head; i < tail; ++i)
348 s_irCache[i] = s_irCache[i + 1];
349 --tail;
350 continue;
351 }
352 head++;
353 }
354 s_dwMax = tail + 1;
355 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100356 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100357
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200358 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
359 {
360 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
361 {
362 s_irPseudo = s_irCache[s_dwIndex];
363 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
364 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
365 *lpBuffer = s_irPseudo;
366 *lpEvents = 1;
367 return TRUE;
368 }
369 }
370
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100371 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200372 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100373 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100374 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100375 return TRUE;
376}
377
378/*
379 * Version of PeekConsoleInput() that works with IME.
380 */
381 static BOOL
382peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100383 HANDLE hInput,
384 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200385 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100386 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100387{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100388 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100389}
390
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100391# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200392 static DWORD
393msg_wait_for_multiple_objects(
394 DWORD nCount,
395 LPHANDLE pHandles,
396 BOOL fWaitAll,
397 DWORD dwMilliseconds,
398 DWORD dwWakeMask)
399{
400 if (read_console_input(NULL, NULL, -2, NULL))
401 return WAIT_OBJECT_0;
402 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
403 dwMilliseconds, dwWakeMask);
404}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100405# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200406
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100407# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200408 static DWORD
409wait_for_single_object(
410 HANDLE hHandle,
411 DWORD dwMilliseconds)
412{
413 if (read_console_input(NULL, NULL, -2, NULL))
414 return WAIT_OBJECT_0;
415 return WaitForSingleObject(hHandle, dwMilliseconds);
416}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100417# endif
418#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200419
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 static void
421get_exe_name(void)
422{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100423 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
424 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100425#define MAX_ENV_PATH_LEN 8192
426 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200427 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429 if (exe_name == NULL)
430 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100431 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100432 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 if (*temp != NUL)
434 exe_name = FullName_save((char_u *)temp, FALSE);
435 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000436
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200437 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000438 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200439 exe_path = vim_strnsave(exe_name,
440 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200441 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000442 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100443 // Append our starting directory to $PATH, so that when doing
444 // "!xxd" it's found in our starting directory. Needed because
445 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200446 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100447 if (p == NULL
448 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200449 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100450 if (p == NULL || *p == NUL)
451 temp[0] = NUL;
452 else
453 {
454 STRCPY(temp, p);
455 STRCAT(temp, ";");
456 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200457 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100458 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200459 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000460 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462}
463
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200464/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100465 * Unescape characters in "p" that appear in "escaped".
466 */
467 static void
468unescape_shellxquote(char_u *p, char_u *escaped)
469{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100470 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100471 int n;
472
473 while (*p != NUL)
474 {
475 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
476 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100477 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100478 p += n;
479 l -= n;
480 }
481}
482
483/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200484 * Load library "name".
485 */
486 HINSTANCE
487vimLoadLib(char *name)
488{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200489 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200490
Bram Moolenaar0f873732019-12-05 20:28:46 +0100491 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
492 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200493 if (exe_path == NULL)
494 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200495 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200496 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200497 WCHAR old_dirw[MAXPATHL];
498
499 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
500 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100501 // Change directory to where the executable is, both to make
502 // sure we find a .dll there and to avoid looking for a .dll
503 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100504 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200505 dll = LoadLibrary(name);
506 SetCurrentDirectoryW(old_dirw);
507 return dll;
508 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200509 }
510 return dll;
511}
512
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200513#if defined(VIMDLL) || defined(PROTO)
514/*
515 * Check if the current executable file is for the GUI subsystem.
516 */
517 int
518mch_is_gui_executable(void)
519{
520 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
521 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
522 PIMAGE_NT_HEADERS pPE;
523
524 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
525 return FALSE;
526 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
527 if (pPE->Signature != IMAGE_NT_SIGNATURE)
528 return FALSE;
529 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
530 return TRUE;
531 return FALSE;
532}
533#endif
534
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100535#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
536/*
537 * Get related information about 'funcname' which is imported by 'hInst'.
538 * If 'info' is 0, return the function address.
539 * If 'info' is 1, return the module name which the function is imported from.
540 */
541 static void *
542get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
543{
544 PBYTE pImage = (PBYTE)hInst;
545 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
546 PIMAGE_NT_HEADERS pPE;
547 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100548 PIMAGE_THUNK_DATA pIAT; // Import Address Table
549 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100550 PIMAGE_IMPORT_BY_NAME pImpName;
551
552 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
553 return NULL;
554 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
555 if (pPE->Signature != IMAGE_NT_SIGNATURE)
556 return NULL;
557 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
558 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
559 .VirtualAddress);
560 for (; pImpDesc->FirstThunk; ++pImpDesc)
561 {
562 if (!pImpDesc->OriginalFirstThunk)
563 continue;
564 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
565 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
566 for (; pIAT->u1.Function; ++pIAT, ++pINT)
567 {
568 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
569 continue;
570 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
571 + (UINT_PTR)(pINT->u1.AddressOfData));
572 if (strcmp((char *)pImpName->Name, funcname) == 0)
573 {
574 switch (info)
575 {
576 case 0:
577 return (void *)pIAT->u1.Function;
578 case 1:
579 return (void *)(pImage + pImpDesc->Name);
580 default:
581 return NULL;
582 }
583 }
584 }
585 }
586 return NULL;
587}
588
589/*
590 * Get the module handle which 'funcname' in 'hInst' is imported from.
591 */
592 HINSTANCE
593find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
594{
595 char *modulename;
596
597 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
598 if (modulename != NULL)
599 return GetModuleHandleA(modulename);
600 return NULL;
601}
602
603/*
604 * Get the address of 'funcname' which is imported by 'hInst' DLL.
605 */
606 void *
607get_dll_import_func(HINSTANCE hInst, const char *funcname)
608{
609 return get_imported_func_info(hInst, funcname, 0);
610}
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
614# ifndef GETTEXT_DLL
615# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200616# define GETTEXT_DLL_ALT1 "libintl-8.dll"
617# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100619// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000620static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200621static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000622static char *null_libintl_textdomain(const char *);
623static char *null_libintl_bindtextdomain(const char *, const char *);
624static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100625static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200627static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000628char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200629char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
630 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000631char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
632char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000634char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
635 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100636int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100639dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 int i;
642 static struct
643 {
644 char *name;
645 FARPROC *ptr;
646 } libintl_entry[] =
647 {
648 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200649 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
651 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
652 {NULL, NULL}
653 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100654 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar7554c542018-10-06 15:03:15 +0200656 // No need to initialize twice.
657 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200659 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100660 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100661# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100662 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200663 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100664# endif
665# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200666 if (!hLibintlDLL)
667 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100668# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100669 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200671 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200673 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200675 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200677 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 for (i = 0; libintl_entry[i].name != NULL
680 && libintl_entry[i].ptr != NULL; ++i)
681 {
682 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
683 libintl_entry[i].name)) == NULL)
684 {
685 dyn_libintl_end();
686 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000687 {
688 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100689 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000690 verbose_leave();
691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 return 0;
693 }
694 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000695
Bram Moolenaar0f873732019-12-05 20:28:46 +0100696 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000697 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000698 "bind_textdomain_codeset");
699 if (dyn_libintl_bind_textdomain_codeset == NULL)
700 dyn_libintl_bind_textdomain_codeset =
701 null_libintl_bind_textdomain_codeset;
702
Bram Moolenaar0f873732019-12-05 20:28:46 +0100703 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100704 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
705 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100706 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
708 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 return 1;
711}
712
713 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100714dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 if (hLibintlDLL)
717 FreeLibrary(hLibintlDLL);
718 hLibintlDLL = NULL;
719 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200720 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 dyn_libintl_textdomain = null_libintl_textdomain;
722 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000723 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100724 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000728null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 return (char*)msgid;
731}
732
733 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200734null_libintl_ngettext(
735 const char *msgid,
736 const char *msgid_plural,
737 unsigned long n)
738{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200739 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200740}
741
Bram Moolenaaree695f72016-08-03 22:08:45 +0200742 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100743null_libintl_bindtextdomain(
744 const char *domainname UNUSED,
745 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 return NULL;
748}
749
750 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100751null_libintl_bind_textdomain_codeset(
752 const char *domainname UNUSED,
753 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000754{
755 return NULL;
756}
757
758 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100759null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 return NULL;
762}
763
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200764 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100765null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100766{
767 return 0;
768}
769
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar0f873732019-12-05 20:28:46 +0100772// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774#ifndef VER_PLATFORM_WIN32_WINDOWS
775# define VER_PLATFORM_WIN32_WINDOWS 1
776#endif
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100779# ifndef PROTO
780# include <aclapi.h>
781# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200782# ifndef PROTECTED_DACL_SECURITY_INFORMATION
783# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#endif
786
Bram Moolenaar27515922013-06-29 15:36:26 +0200787#ifdef HAVE_ACL
788/*
789 * Enables or disables the specified privilege.
790 */
791 static BOOL
792win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
793{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100794 BOOL bResult;
795 LUID luid;
796 HANDLE hToken;
797 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200798
799 if (!OpenProcessToken(GetCurrentProcess(),
800 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
801 return FALSE;
802
803 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
804 {
805 CloseHandle(hToken);
806 return FALSE;
807 }
808
Bram Moolenaar45500912014-07-09 20:51:07 +0200809 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200810 tokenPrivileges.Privileges[0].Luid = luid;
811 tokenPrivileges.Privileges[0].Attributes = bEnable ?
812 SE_PRIVILEGE_ENABLED : 0;
813
814 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
815 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
816
817 CloseHandle(hToken);
818
819 return bResult && GetLastError() == ERROR_SUCCESS;
820}
821#endif
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100824 * Set "win8_or_later" and fill in "windowsVersion".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 */
826 void
827PlatformId(void)
828{
829 static int done = FALSE;
830
831 if (!done)
832 {
833 OSVERSIONINFO ovi;
834
835 ovi.dwOSVersionInfoSize = sizeof(ovi);
836 GetVersionEx(&ovi);
837
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100838 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
839 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100841 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
842 || ovi.dwMajorVersion > 6)
843 win8_or_later = TRUE;
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100846 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200847 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#endif
849 done = TRUE;
850 }
851}
852
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200853#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100855# define SHIFT (SHIFT_PRESSED)
856# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
857# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
858# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860
Bram Moolenaar0f873732019-12-05 20:28:46 +0100861// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
862// We map function keys to their ANSI terminal equivalents, as produced
863// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
864// ANSI key with a value >= '\300' is nonstandard, but provided anyway
865// so that the user can have access to all SHIFT-, CTRL-, and ALT-
866// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000868static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 WORD wVirtKey;
871 BOOL fAnsiKey;
872 int chAlone;
873 int chShift;
874 int chCtrl;
875 int chAlt;
876} VirtKeyMap[] =
877{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200878// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
880
881 { VK_F1, TRUE, ';', 'T', '^', 'h', },
882 { VK_F2, TRUE, '<', 'U', '_', 'i', },
883 { VK_F3, TRUE, '=', 'V', '`', 'j', },
884 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
885 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
886 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
887 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
888 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
889 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
890 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200891 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
892 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200894 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
895 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
896 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
897 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
898 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
899 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
900 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
901 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
902 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
903 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100904 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200906 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100908# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200909 // Most people don't have F13-F20, but what the hell...
910 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
911 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
912 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
913 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
914 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
915 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
916 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
917 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100918# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200919 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
920 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
921 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
922 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200924 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
925 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
926 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
927 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
928 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
929 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
930 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
931 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
932 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
933 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100934 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935};
936
937
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100938# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
939# define UChar UnicodeChar
940# else
941# define UChar uChar.UnicodeChar
942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Bram Moolenaar0f873732019-12-05 20:28:46 +0100944/*
945 * The return code indicates key code size.
946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000949 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 UINT uMods = pker->dwControlKeyState;
952 static int s_iIsDead = 0;
953 static WORD awAnsiCode[2];
954 static BYTE abKeystate[256];
955
956
957 if (s_iIsDead == 2)
958 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200959 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 s_iIsDead = 0;
961 return 1;
962 }
963
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200964 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 return 1;
966
Bram Moolenaara80faa82020-04-12 19:37:17 +0200967 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaar0f873732019-12-05 20:28:46 +0100969 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200970 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 if (uMods & SHIFT_PRESSED)
973 abKeystate[VK_SHIFT] = 0x80;
974 if (uMods & CAPSLOCK_ON)
975 abKeystate[VK_CAPITAL] = 1;
976
977 if ((uMods & ALT_GR) == ALT_GR)
978 {
979 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
980 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
981 }
982
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200983 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
984 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200987 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 return s_iIsDead;
990}
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992static BOOL g_fJustGotFocus = FALSE;
993
994/*
995 * Decode a KEY_EVENT into one or two keystrokes
996 */
997 static BOOL
998decode_key_event(
999 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001000 WCHAR *pch,
1001 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001003 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 int i;
1006 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1007
1008 *pch = *pch2 = NUL;
1009 g_fJustGotFocus = FALSE;
1010
Bram Moolenaar0f873732019-12-05 20:28:46 +01001011 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (!pker->bKeyDown)
1013 return FALSE;
1014
Bram Moolenaar0f873732019-12-05 20:28:46 +01001015 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 switch (pker->wVirtualKeyCode)
1017 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001018 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 case VK_SHIFT:
1020 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001021 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 return FALSE;
1023
1024 default:
1025 break;
1026 }
1027
Bram Moolenaar0f873732019-12-05 20:28:46 +01001028 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001029 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001031 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (pker->wVirtualKeyCode == '6')
1033 {
1034 *pch = Ctrl_HAT;
1035 return TRUE;
1036 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001037 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 else if (pker->wVirtualKeyCode == '2')
1039 {
1040 *pch = NUL;
1041 return TRUE;
1042 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001043 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 else if (pker->wVirtualKeyCode == 0xBD)
1045 {
1046 *pch = Ctrl__;
1047 return TRUE;
1048 }
1049 }
1050
Bram Moolenaar0f873732019-12-05 20:28:46 +01001051 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1053 {
1054 *pch = K_NUL;
1055 *pch2 = '\017';
1056 return TRUE;
1057 }
1058
1059 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1060 {
1061 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1062 {
1063 if (nModifs == 0)
1064 *pch = VirtKeyMap[i].chAlone;
1065 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1066 *pch = VirtKeyMap[i].chShift;
1067 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1068 *pch = VirtKeyMap[i].chCtrl;
1069 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1070 *pch = VirtKeyMap[i].chAlt;
1071
1072 if (*pch != 0)
1073 {
1074 if (VirtKeyMap[i].fAnsiKey)
1075 {
1076 *pch2 = *pch;
1077 *pch = K_NUL;
1078 }
1079
1080 return TRUE;
1081 }
1082 }
1083 }
1084
1085 i = win32_kbd_patch_key(pker);
1086
1087 if (i < 0)
1088 *pch = NUL;
1089 else
1090 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001091 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 if (pmodifiers != NULL)
1094 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001095 // Pass on the ALT key as a modifier, but only when not combined
1096 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1098 *pmodifiers |= MOD_MASK_ALT;
1099
Bram Moolenaar0f873732019-12-05 20:28:46 +01001100 // Pass on SHIFT only for special keys, because we don't know when
1101 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1103 *pmodifiers |= MOD_MASK_SHIFT;
1104
Bram Moolenaar0f873732019-12-05 20:28:46 +01001105 // Pass on CTRL only for non-special keys, because we don't know
1106 // when it's already included with the character. And not when
1107 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1109 && *pch >= 0x20 && *pch < 0x80)
1110 *pmodifiers |= MOD_MASK_CTRL;
1111 }
1112 }
1113
1114 return (*pch != NUL);
1115}
1116
Bram Moolenaar0f873732019-12-05 20:28:46 +01001117#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120/*
1121 * For the GUI the mouse handling is in gui_w32.c.
1122 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001123#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001125mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001128#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001129static int g_fMouseAvail = FALSE; // mouse present
1130static int g_fMouseActive = FALSE; // mouse enabled
1131static int g_nMouseClick = -1; // mouse status
1132static int g_xMouse; // mouse x coordinate
1133static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135/*
1136 * Enable or disable mouse input
1137 */
1138 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001139mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 DWORD cmodein;
1142
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001143# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001144 if (gui.in_use)
1145 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (!g_fMouseAvail)
1148 return;
1149
1150 g_fMouseActive = on;
1151 GetConsoleMode(g_hConIn, &cmodein);
1152
1153 if (g_fMouseActive)
1154 cmodein |= ENABLE_MOUSE_INPUT;
1155 else
1156 cmodein &= ~ENABLE_MOUSE_INPUT;
1157
1158 SetConsoleMode(g_hConIn, cmodein);
1159}
1160
Bram Moolenaar157d8132018-03-06 17:09:20 +01001161
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001162# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001163/*
1164 * Called when 'balloonevalterm' changed.
1165 */
1166 void
1167mch_bevalterm_changed(void)
1168{
1169 mch_setmouse(g_fMouseActive);
1170}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001171# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173/*
1174 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1175 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1176 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1177 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1178 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1179 * and we return the mouse position in g_xMouse and g_yMouse.
1180 *
1181 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1182 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1183 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1184 *
1185 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1186 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1187 *
1188 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1189 * moves, even if it stays within the same character cell. We ignore
1190 * all MOUSE_MOVED messages if the position hasn't really changed, and
1191 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1192 * we're only interested in MOUSE_DRAG).
1193 *
1194 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1195 * 2-button mouses by pressing the left & right buttons simultaneously.
1196 * In practice, it's almost impossible to click both at the same time,
1197 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1198 * in such cases, if the user is clicking quickly.
1199 */
1200 static BOOL
1201decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001202 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
1204 static int s_nOldButton = -1;
1205 static int s_nOldMouseClick = -1;
1206 static int s_xOldMouse = -1;
1207 static int s_yOldMouse = -1;
1208 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001209# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 static int s_cClicks = 1;
1213 static BOOL s_fReleased = TRUE;
1214 static DWORD s_dwLastClickTime = 0;
1215 static BOOL s_fNextIsMiddle = FALSE;
1216
Bram Moolenaar0f873732019-12-05 20:28:46 +01001217 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
1219 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1220 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1221 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1222 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1223
1224 int nButton;
1225
1226 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1227 cButtons = 2;
1228
1229 if (!g_fMouseAvail || !g_fMouseActive)
1230 {
1231 g_nMouseClick = -1;
1232 return FALSE;
1233 }
1234
Bram Moolenaar0f873732019-12-05 20:28:46 +01001235 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (g_fJustGotFocus)
1237 {
1238 g_fJustGotFocus = FALSE;
1239 return FALSE;
1240 }
1241
Bram Moolenaar0f873732019-12-05 20:28:46 +01001242 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (g_nMouseClick != -1)
1244 return TRUE;
1245
1246 nButton = -1;
1247 g_xMouse = pmer->dwMousePosition.X;
1248 g_yMouse = pmer->dwMousePosition.Y;
1249
1250 if (pmer->dwEventFlags == MOUSE_MOVED)
1251 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001252 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1253 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1255 return FALSE;
1256 }
1257
Bram Moolenaar0f873732019-12-05 20:28:46 +01001258 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1260 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001261 nButton = MOUSE_RELEASE;
1262
Bram Moolenaar0f873732019-12-05 20:28:46 +01001263 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001265 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001266# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001267 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001268 if (p_bevalterm)
1269 nButton = MOUSE_DRAG;
1270 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001271# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001272 return FALSE;
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 s_fReleased = TRUE;
1276 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001277 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001279 // on a 2-button mouse, hold down left and right buttons
1280 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1283 {
1284 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1285
Bram Moolenaar0f873732019-12-05 20:28:46 +01001286 // if either left or right button only is pressed, see if the
1287 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (dwLR == LEFT || dwLR == RIGHT)
1289 {
1290 for (;;)
1291 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001292 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1294 != WAIT_OBJECT_0)
1295 break;
1296 else
1297 {
1298 DWORD cRecords = 0;
1299 INPUT_RECORD ir;
1300 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1301
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001302 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1305 || !(pmer2->dwButtonState & LEFT_RIGHT))
1306 break;
1307 else
1308 {
1309 if (pmer2->dwEventFlags != MOUSE_MOVED)
1310 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001311 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
1313 return decode_mouse_event(pmer2);
1314 }
1315 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1316 s_yOldMouse == pmer2->dwMousePosition.Y)
1317 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001318 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001319 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaar0f873732019-12-05 20:28:46 +01001321 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001322 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1325 break;
1326 }
1327 else
1328 break;
1329 }
1330 }
1331 }
1332 }
1333 }
1334
1335 if (s_fNextIsMiddle)
1336 {
1337 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1338 ? MOUSE_DRAG : MOUSE_MIDDLE;
1339 s_fNextIsMiddle = FALSE;
1340 }
1341 else if (cButtons == 2 &&
1342 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1343 {
1344 nButton = MOUSE_MIDDLE;
1345
1346 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1347 {
1348 s_fNextIsMiddle = TRUE;
1349 nButton = MOUSE_RELEASE;
1350 }
1351 }
1352 else if ((pmer->dwButtonState & LEFT) == LEFT)
1353 nButton = MOUSE_LEFT;
1354 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1355 nButton = MOUSE_MIDDLE;
1356 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1357 nButton = MOUSE_RIGHT;
1358
1359 if (! s_fReleased && ! s_fNextIsMiddle
1360 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1361 return FALSE;
1362
1363 s_fReleased = s_fNextIsMiddle;
1364 }
1365
1366 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1367 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001368 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (nButton != -1 && nButton != MOUSE_RELEASE)
1370 {
1371 DWORD dwCurrentTime = GetTickCount();
1372
1373 if (s_xOldMouse != g_xMouse
1374 || s_yOldMouse != g_yMouse
1375 || s_nOldButton != nButton
1376 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001377# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1381 {
1382 s_cClicks = 1;
1383 }
1384 else if (++s_cClicks > 4)
1385 {
1386 s_cClicks = 1;
1387 }
1388
1389 s_dwLastClickTime = dwCurrentTime;
1390 }
1391 }
1392 else if (pmer->dwEventFlags == MOUSE_MOVED)
1393 {
1394 if (nButton != -1 && nButton != MOUSE_RELEASE)
1395 nButton = MOUSE_DRAG;
1396
1397 s_cClicks = 1;
1398 }
1399
1400 if (nButton == -1)
1401 return FALSE;
1402
1403 if (nButton != MOUSE_RELEASE)
1404 s_nOldButton = nButton;
1405
1406 g_nMouseClick = nButton;
1407
1408 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1409 g_nMouseClick |= MOUSE_SHIFT;
1410 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1411 g_nMouseClick |= MOUSE_CTRL;
1412 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1413 g_nMouseClick |= MOUSE_ALT;
1414
1415 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1416 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1417
Bram Moolenaar0f873732019-12-05 20:28:46 +01001418 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (s_xOldMouse == g_xMouse
1420 && s_yOldMouse == g_yMouse
1421 && s_nOldMouseClick == g_nMouseClick)
1422 {
1423 g_nMouseClick = -1;
1424 return FALSE;
1425 }
1426
1427 s_xOldMouse = g_xMouse;
1428 s_yOldMouse = g_yMouse;
1429 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001430# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 s_nOldMouseClick = g_nMouseClick;
1434
1435 return TRUE;
1436}
1437
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001438#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440
1441#ifdef MCH_CURSOR_SHAPE
1442/*
1443 * Set the shape of the cursor.
1444 * 'thickness' can be from 1 (thin) to 99 (block)
1445 */
1446 static void
1447mch_set_cursor_shape(int thickness)
1448{
1449 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1450 ConsoleCursorInfo.dwSize = thickness;
1451 ConsoleCursorInfo.bVisible = s_cursor_visible;
1452
1453 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1454 if (s_cursor_visible)
1455 SetConsoleCursorPosition(g_hConOut, g_coord);
1456}
1457
1458 void
1459mch_update_cursor(void)
1460{
1461 int idx;
1462 int thickness;
1463
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001464# ifdef VIMDLL
1465 if (gui.in_use)
1466 return;
1467# endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 /*
1470 * How the cursor is drawn depends on the current mode.
1471 */
1472 idx = get_shape_idx(FALSE);
1473
1474 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001475 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else
1477 thickness = shape_table[idx].percentage;
1478 mch_set_cursor_shape(thickness);
1479}
1480#endif
1481
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001482#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Handle FOCUS_EVENT.
1485 */
1486 static void
1487handle_focus_event(INPUT_RECORD ir)
1488{
1489 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1490 ui_focus_change((int)g_fJustGotFocus);
1491}
1492
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001493static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495/*
1496 * Wait until console input from keyboard or mouse is available,
1497 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001498 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 * Return TRUE if something is available FALSE if not.
1500 */
1501 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001502WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 DWORD dwNow = 0, dwEndTime = 0;
1505 INPUT_RECORD ir;
1506 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001507 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001508# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001509 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
1512 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001513 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 dwEndTime = GetTickCount() + msec;
1515 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001516 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 dwEndTime = INFINITE;
1518
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001519 // We need to loop until the end of the time period, because
1520 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 for (;;)
1522 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001523 // Only process messages when waiting.
1524 if (msec != 0)
1525 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001526# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001527 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001528# endif
1529# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001530 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001531# endif
1532# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001533 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001534# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001535 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001536
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001537 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001538# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001539 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 )
1542 return TRUE;
1543
1544 if (msec > 0)
1545 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001546 // If the specified wait time has passed, return. Beware that
1547 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001549 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 break;
1551 }
1552 if (msec != 0)
1553 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001554 DWORD dwWaitTime = dwEndTime - dwNow;
1555
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001556# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001557 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001558 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001559 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001560 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001561 // If there is readahead then parse_queued_messages() timed out
1562 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001563 if (channel_any_readahead())
1564 dwWaitTime = 10;
1565 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001566# endif
1567# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001568 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001569 // The 'balloonexpr' may indirectly invoke a callback while
1570 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001571 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001572# endif
1573# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001574 if (mzthreads_allowed() && p_mzq > 0
1575 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001576 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001577# endif
1578# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001579 // When waiting very briefly don't trigger timers.
1580 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001581 {
1582 long due_time;
1583
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001584 // Trigger timers and then get the time in msec until the next
1585 // one is due. Wait up to that time.
1586 due_time = check_due_timer();
1587 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001588 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001589 // timer may have used feedkeys().
1590 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001591 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001592 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1593 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001594 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001595# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001596 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001597# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001598 // Wait for either an event on the console input or a
1599 // message in the client-server window.
1600 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1601 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001602# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001603 wait_for_single_object(g_hConIn, dwWaitTime)
1604 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001605# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001606 )
1607 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609
1610 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001611 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001613# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (State & CMDLINE && msg_row == Rows - 1)
1615 {
1616 CONSOLE_SCREEN_BUFFER_INFO csbi;
1617
1618 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1619 {
1620 if (csbi.dwCursorPosition.Y != msg_row)
1621 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001622 // The screen is now messed up, must redraw the
1623 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 redraw_all_later(CLEAR);
1625 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1626 redrawcmd();
1627 }
1628 }
1629 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 if (cRecords > 0)
1633 {
1634 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1635 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001636# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001637 // Windows IME sends two '\n's with only one 'ENTER'. First:
1638 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001639 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1641 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001642 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 continue;
1644 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1647 NULL, FALSE))
1648 return TRUE;
1649 }
1650
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001651 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653 if (ir.EventType == FOCUS_EVENT)
1654 handle_focus_event(ir);
1655 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001656 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001657 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1658
Bram Moolenaar36698e32019-12-11 22:57:40 +01001659 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001660 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001661 if (dwSize.X != Columns || dwSize.Y != Rows)
1662 {
1663 CONSOLE_SCREEN_BUFFER_INFO csbi;
1664 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001665 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001666 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001667 if (dwSize.X != Columns || dwSize.Y != Rows)
1668 {
1669 ResizeConBuf(g_hConOut, dwSize);
1670 shell_resized();
1671 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001672 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else if (ir.EventType == MOUSE_EVENT
1675 && decode_mouse_event(&ir.Event.MouseEvent))
1676 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678 else if (msec == 0)
1679 break;
1680 }
1681
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001682# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001683 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (input_available())
1685 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001686# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 return FALSE;
1689}
1690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691/*
1692 * return non-zero if a character is available
1693 */
1694 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001695mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
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, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001703
1704# if defined(FEAT_TERMINAL) || defined(PROTO)
1705/*
1706 * Check for any pending input or messages.
1707 */
1708 int
1709mch_check_messages(void)
1710{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001711# ifdef VIMDLL
1712 if (gui.in_use)
1713 return TRUE;
1714# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001715 return WaitForChar(0L, TRUE);
1716}
1717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719/*
1720 * Create the console input. Used when reading stdin doesn't work.
1721 */
1722 static void
1723create_conin(void)
1724{
1725 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1726 FILE_SHARE_READ|FILE_SHARE_WRITE,
1727 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001728 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 did_create_conin = TRUE;
1730}
1731
1732/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001733 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001735 static WCHAR
1736tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001738 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 for (;;)
1741 {
1742 INPUT_RECORD ir;
1743 DWORD cRecords = 0;
1744
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001745# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001746 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (input_available())
1748 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (g_nMouseClick != -1)
1750 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001751# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001752 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
1754 if (did_create_conin)
1755 read_error_exit();
1756 create_conin();
1757 continue;
1758 }
1759
1760 if (ir.EventType == KEY_EVENT)
1761 {
1762 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1763 pmodifiers, TRUE))
1764 return ch;
1765 }
1766 else if (ir.EventType == FOCUS_EVENT)
1767 handle_focus_event(ir);
1768 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1769 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 else if (ir.EventType == MOUSE_EVENT)
1771 {
1772 if (decode_mouse_event(&ir.Event.MouseEvent))
1773 return 0;
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
1776}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001777#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779
1780/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001781 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 * Get one or more characters from the keyboard or the mouse.
1783 * If time == 0, do not wait for characters.
1784 * If time == n, wait a short time for characters.
1785 * If time == -1, wait forever for characters.
1786 * Returns the number of characters read into buf.
1787 */
1788 int
1789mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001790 char_u *buf UNUSED,
1791 int maxlen UNUSED,
1792 long time UNUSED,
1793 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001795#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797 int len;
1798 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001799# ifdef VIMDLL
1800// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1801# define TYPEAHEADSPACE 6
1802# else
1803# define TYPEAHEADSPACE 0
1804# endif
1805# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001806 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 static int typeaheadlen = 0;
1808
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001809# ifdef VIMDLL
1810 if (gui.in_use)
1811 return 0;
1812# endif
1813
Bram Moolenaar0f873732019-12-05 20:28:46 +01001814 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (typeaheadlen > 0)
1816 goto theend;
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (time >= 0)
1819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001820 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001823 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001825 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar3918c952005-03-15 22:34:55 +00001827 /*
1828 * If there is no character available within 2 seconds (default)
1829 * write the autoscript file to disk. Or cause the CursorHold event
1830 * to be triggered.
1831 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001832 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001834 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001836 buf[0] = K_SPECIAL;
1837 buf[1] = KS_EXTRA;
1838 buf[2] = (int)KE_CURSORHOLD;
1839 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001841 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 }
1844
1845 /*
1846 * Try to read as many characters as there are, until the buffer is full.
1847 */
1848
Bram Moolenaar0f873732019-12-05 20:28:46 +01001849 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 g_fCBrkPressed = FALSE;
1851
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001852# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (fdDump)
1854 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaar0f873732019-12-05 20:28:46 +01001857 // Keep looping until there is something in the typeahead buffer and more
1858 // to get and still room in the buffer (up to two bytes for a char and
1859 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001860 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001861 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
1863 if (typebuf_changed(tb_change_cnt))
1864 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001865 // "buf" may be invalid now if a client put something in the
1866 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 typeaheadlen = 0;
1868 break;
1869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (g_nMouseClick != -1)
1871 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001872# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (fdDump)
1874 fprintf(fdDump, "{%02x @ %d, %d}",
1875 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 typeahead[typeaheadlen++] = ESC + 128;
1878 typeahead[typeaheadlen++] = 'M';
1879 typeahead[typeaheadlen++] = g_nMouseClick;
1880 typeahead[typeaheadlen++] = g_xMouse + '!';
1881 typeahead[typeaheadlen++] = g_yMouse + '!';
1882 g_nMouseClick = -1;
1883 }
1884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001886 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 int modifiers = 0;
1888
1889 c = tgetch(&modifiers, &ch2);
1890
1891 if (typebuf_changed(tb_change_cnt))
1892 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001893 // "buf" may be invalid now if a client put something in the
1894 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 typeaheadlen = 0;
1896 break;
1897 }
1898
1899 if (c == Ctrl_C && ctrl_c_interrupts)
1900 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001901# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 got_int = TRUE;
1905 }
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 int n = 1;
1910
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001911 if (ch2 == NUL)
1912 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001913 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001914 char_u *p;
1915 WCHAR ch[2];
1916
1917 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001918 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001919 {
1920 ch[1] = tgetch(&modifiers, &ch2);
1921 n++;
1922 }
1923 p = utf16_to_enc(ch, &n);
1924 if (p != NULL)
1925 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001926 for (i = 0, j = 0; i < n; i++)
1927 {
1928 typeahead[typeaheadlen + j++] = p[i];
1929# ifdef VIMDLL
1930 if (p[i] == CSI)
1931 {
1932 typeahead[typeaheadlen + j++] = KS_EXTRA;
1933 typeahead[typeaheadlen + j++] = KE_CSI;
1934 }
1935# endif
1936 }
1937 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001938 vim_free(p);
1939 }
1940 }
1941 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001942 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001943 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001944# ifdef VIMDLL
1945 if (c == CSI)
1946 {
1947 typeahead[typeaheadlen + 1] = KS_EXTRA;
1948 typeahead[typeaheadlen + 2] = KE_CSI;
1949 n = 3;
1950 }
1951# endif
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (ch2 != NUL)
1954 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001955 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001956 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001957 switch (ch2)
1958 {
1959 case (WCHAR)'\324': // SHIFT+Insert
1960 case (WCHAR)'\325': // CTRL+Insert
1961 case (WCHAR)'\327': // SHIFT+Delete
1962 case (WCHAR)'\330': // CTRL+Delete
1963 typeahead[typeaheadlen + n] = (char_u)ch2;
1964 n++;
1965 break;
1966
1967 default:
1968 typeahead[typeaheadlen + n] = 3;
1969 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1970 n += 2;
1971 break;
1972 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001973 }
1974 else
1975 {
1976 typeahead[typeaheadlen + n] = 3;
1977 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1978 n += 2;
1979 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001980 }
1981
Bram Moolenaar0f873732019-12-05 20:28:46 +01001982 // Use the ALT key to set the 8th bit of the character
1983 // when it's one byte, the 8th bit isn't set yet and not
1984 // using a double-byte encoding (would become a lead
1985 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if ((modifiers & MOD_MASK_ALT)
1987 && n == 1
1988 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 )
1991 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001992 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1993 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 modifiers &= ~MOD_MASK_ALT;
1995 }
1996
1997 if (modifiers != 0)
1998 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001999 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 mch_memmove(typeahead + typeaheadlen + 3,
2001 typeahead + typeaheadlen, n);
2002 typeahead[typeaheadlen++] = K_SPECIAL;
2003 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2004 typeahead[typeaheadlen++] = modifiers;
2005 }
2006
2007 typeaheadlen += n;
2008
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002009# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (fdDump)
2011 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014 }
2015 }
2016
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002017# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (fdDump)
2019 {
2020 fputs("]\n", fdDump);
2021 fflush(fdDump);
2022 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002026 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 len = 0;
2028 while (len < maxlen && typeaheadlen > 0)
2029 {
2030 buf[len++] = typeahead[0];
2031 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2032 }
2033 return len;
2034
Bram Moolenaar0f873732019-12-05 20:28:46 +01002035#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002037#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038}
2039
Bram Moolenaar82881492012-11-20 16:53:39 +01002040#ifndef PROTO
2041# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002042# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#endif
2045
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002046/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002047 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2048 * If "use_path" is FALSE: Return TRUE if "name" exists.
2049 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2050 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002051 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002054executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002056 WCHAR *p;
2057 WCHAR fnamew[_MAX_PATH];
2058 WCHAR *dumw;
2059 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002060 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
Bram Moolenaar43663192017-03-05 14:29:12 +01002062 if (!use_path)
2063 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002064 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002065 {
2066 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002067 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002068 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002069 *path = vim_strsave((char_u *)name);
2070 else
2071 *path = FullName_save((char_u *)name, FALSE);
2072 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002073 return TRUE;
2074 }
2075 return FALSE;
2076 }
2077
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002078 p = enc_to_utf16((char_u *)name, NULL);
2079 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002080 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002081
2082 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002083 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002084 if (wnewpath == NULL)
2085 return FALSE;
2086 wcscpy(wnewpath, L".;");
2087 wcscat(wnewpath, wcurpath);
2088 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2089 vim_free(wnewpath);
2090 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002091 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002092 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002093 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002094 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002095 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002096 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002097 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098}
2099
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002100#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2101 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002102/*
2103 * Bad parameter handler.
2104 *
2105 * Certain MS CRT functions will intentionally crash when passed invalid
2106 * parameters to highlight possible security holes. Setting this function as
2107 * the bad parameter handler will prevent the crash.
2108 *
2109 * In debug builds the parameters contain CRT information that might help track
2110 * down the source of a problem, but in non-debug builds the arguments are all
2111 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2112 * worth allowing these to make debugging of issues easier.
2113 */
2114 static void
2115bad_param_handler(const wchar_t *expression,
2116 const wchar_t *function,
2117 const wchar_t *file,
2118 unsigned int line,
2119 uintptr_t pReserved)
2120{
2121}
2122
2123# define SET_INVALID_PARAM_HANDLER \
2124 ((void)_set_invalid_parameter_handler(bad_param_handler))
2125#else
2126# define SET_INVALID_PARAM_HANDLER
2127#endif
2128
Bram Moolenaar4f974752019-02-17 17:44:42 +01002129#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131/*
2132 * GUI version of mch_init().
2133 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002134 static void
2135mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002137# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar0f873732019-12-05 20:28:46 +01002141 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002142 SET_INVALID_PARAM_HANDLER;
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // Let critical errors result in a failure, not in a dialog box. Required
2145 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 SetErrorMode(SEM_FAILCRITICALERRORS);
2147
Bram Moolenaar0f873732019-12-05 20:28:46 +01002148 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaar0f873732019-12-05 20:28:46 +01002150 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 Rows = 25;
2152 Columns = 80;
2153
Bram Moolenaar0f873732019-12-05 20:28:46 +01002154 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 char_u vimrun_location[_MAX_PATH + 4];
2157
Bram Moolenaar0f873732019-12-05 20:28:46 +01002158 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 STRCPY(vimrun_location, exe_name);
2160 STRCPY(gettail(vimrun_location), "vimrun.exe");
2161 if (mch_getperm(vimrun_location) >= 0)
2162 {
2163 if (*skiptowhite(vimrun_location) != NUL)
2164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002165 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 mch_memmove(vimrun_location + 1, vimrun_location,
2167 STRLEN(vimrun_location) + 1);
2168 *vimrun_location = '"';
2169 STRCPY(gettail(vimrun_location), "vimrun\" ");
2170 }
2171 else
2172 STRCPY(gettail(vimrun_location), "vimrun ");
2173
2174 vimrun_path = (char *)vim_strsave(vimrun_location);
2175 s_dont_use_vimrun = FALSE;
2176 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002177 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 s_dont_use_vimrun = FALSE;
2179
Bram Moolenaar0f873732019-12-05 20:28:46 +01002180 // Don't give the warning for a missing vimrun.exe right now, but only
2181 // when vimrun was supposed to be used. Don't bother people that do
2182 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (s_dont_use_vimrun)
2184 need_vimrun_warning = TRUE;
2185 }
2186
2187 /*
2188 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2189 * Otherwise the default "findstr /n" is used.
2190 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002191 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2193
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002194# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002195 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002196# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002197
2198 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199}
2200
2201
Bram Moolenaar0f873732019-12-05 20:28:46 +01002202#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002203
2204#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002206# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2207# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209/*
2210 * ClearConsoleBuffer()
2211 * Description:
2212 * Clears the entire contents of the console screen buffer, using the
2213 * specified attribute.
2214 * Returns:
2215 * TRUE on success
2216 */
2217 static BOOL
2218ClearConsoleBuffer(WORD wAttribute)
2219{
2220 CONSOLE_SCREEN_BUFFER_INFO csbi;
2221 COORD coord;
2222 DWORD NumCells, dummy;
2223
2224 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2225 return FALSE;
2226
2227 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2228 coord.X = 0;
2229 coord.Y = 0;
2230 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2231 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2234 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 return TRUE;
2238}
2239
2240/*
2241 * FitConsoleWindow()
2242 * Description:
2243 * Checks if the console window will fit within given buffer dimensions.
2244 * Also, if requested, will shrink the window to fit.
2245 * Returns:
2246 * TRUE on success
2247 */
2248 static BOOL
2249FitConsoleWindow(
2250 COORD dwBufferSize,
2251 BOOL WantAdjust)
2252{
2253 CONSOLE_SCREEN_BUFFER_INFO csbi;
2254 COORD dwWindowSize;
2255 BOOL NeedAdjust = FALSE;
2256
2257 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2258 {
2259 /*
2260 * A buffer resize will fail if the current console window does
2261 * not lie completely within that buffer. To avoid this, we might
2262 * have to move and possibly shrink the window.
2263 */
2264 if (csbi.srWindow.Right >= dwBufferSize.X)
2265 {
2266 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2267 if (dwWindowSize.X > dwBufferSize.X)
2268 dwWindowSize.X = dwBufferSize.X;
2269 csbi.srWindow.Right = dwBufferSize.X - 1;
2270 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2271 NeedAdjust = TRUE;
2272 }
2273 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2274 {
2275 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2276 if (dwWindowSize.Y > dwBufferSize.Y)
2277 dwWindowSize.Y = dwBufferSize.Y;
2278 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2279 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2280 NeedAdjust = TRUE;
2281 }
2282 if (NeedAdjust && WantAdjust)
2283 {
2284 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2285 return FALSE;
2286 }
2287 return TRUE;
2288 }
2289
2290 return FALSE;
2291}
2292
2293typedef struct ConsoleBufferStruct
2294{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002295 BOOL IsValid;
2296 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002297 PCHAR_INFO Buffer;
2298 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002299 PSMALL_RECT Regions;
2300 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301} ConsoleBuffer;
2302
2303/*
2304 * SaveConsoleBuffer()
2305 * Description:
2306 * Saves important information about the console buffer, including the
2307 * actual buffer contents. The saved information is suitable for later
2308 * restoration by RestoreConsoleBuffer().
2309 * Returns:
2310 * TRUE if all information was saved; FALSE otherwise
2311 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2312 */
2313 static BOOL
2314SaveConsoleBuffer(
2315 ConsoleBuffer *cb)
2316{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002317 DWORD NumCells;
2318 COORD BufferCoord;
2319 SMALL_RECT ReadRegion;
2320 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002321 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (cb == NULL)
2324 return FALSE;
2325
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002326 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 cb->IsValid = FALSE;
2329 return FALSE;
2330 }
2331 cb->IsValid = TRUE;
2332
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002333 /*
2334 * Allocate a buffer large enough to hold the entire console screen
2335 * buffer. If this ConsoleBuffer structure has already been initialized
2336 * with a buffer of the correct size, then just use that one.
2337 */
2338 if (!cb->IsValid || cb->Buffer == NULL ||
2339 cb->BufferSize.X != cb->Info.dwSize.X ||
2340 cb->BufferSize.Y != cb->Info.dwSize.Y)
2341 {
2342 cb->BufferSize.X = cb->Info.dwSize.X;
2343 cb->BufferSize.Y = cb->Info.dwSize.Y;
2344 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2345 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002346 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002347 if (cb->Buffer == NULL)
2348 return FALSE;
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002352 * We will now copy the console screen buffer into our buffer.
2353 * ReadConsoleOutput() seems to be limited as far as how much you
2354 * can read at a time. Empirically, this number seems to be about
2355 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2356 * in chunks until it is all copied. The chunks will all have the
2357 * same horizontal characteristics, so initialize them now. The
2358 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002360 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002361 ReadRegion.Left = 0;
2362 ReadRegion.Right = cb->Info.dwSize.X - 1;
2363 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002364
2365 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2366 if (cb->Regions == NULL || numregions != cb->NumRegions)
2367 {
2368 cb->NumRegions = numregions;
2369 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002370 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002371 if (cb->Regions == NULL)
2372 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002373 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002374 return FALSE;
2375 }
2376 }
2377
2378 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002380 /*
2381 * Read into position (0, Y) in our buffer.
2382 */
2383 BufferCoord.Y = Y;
2384 /*
2385 * Read the region whose top left corner is (0, Y) and whose bottom
2386 * right corner is (width - 1, Y + Y_incr - 1). This should define
2387 * a region of size width by Y_incr. Don't worry if this region is
2388 * too large for the remaining buffer; it will be cropped.
2389 */
2390 ReadRegion.Top = Y;
2391 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002392 if (!ReadConsoleOutputW(g_hConOut, // output handle
2393 cb->Buffer, // our buffer
2394 cb->BufferSize, // dimensions of our buffer
2395 BufferCoord, // offset in our buffer
2396 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002397 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002398 VIM_CLEAR(cb->Buffer);
2399 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002400 return FALSE;
2401 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002402 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404
2405 return TRUE;
2406}
2407
2408/*
2409 * RestoreConsoleBuffer()
2410 * Description:
2411 * Restores important information about the console buffer, including the
2412 * actual buffer contents, if desired. The information to restore is in
2413 * the same format used by SaveConsoleBuffer().
2414 * Returns:
2415 * TRUE on success
2416 */
2417 static BOOL
2418RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002419 ConsoleBuffer *cb,
2420 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002422 COORD BufferCoord;
2423 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002424 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 if (cb == NULL || !cb->IsValid)
2427 return FALSE;
2428
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002429 /*
2430 * Before restoring the buffer contents, clear the current buffer, and
2431 * restore the cursor position and window information. Doing this now
2432 * prevents old buffer contents from "flashing" onto the screen.
2433 */
2434 if (RestoreScreen)
2435 ClearConsoleBuffer(cb->Info.wAttributes);
2436
2437 FitConsoleWindow(cb->Info.dwSize, TRUE);
2438 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2439 return FALSE;
2440 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2441 return FALSE;
2442
2443 if (!RestoreScreen)
2444 {
2445 /*
2446 * No need to restore the screen buffer contents, so we're done.
2447 */
2448 return TRUE;
2449 }
2450
2451 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2452 return FALSE;
2453 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2454 return FALSE;
2455
2456 /*
2457 * Restore the screen buffer contents.
2458 */
2459 if (cb->Buffer != NULL)
2460 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002461 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002462 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002463 BufferCoord.X = cb->Regions[i].Left;
2464 BufferCoord.Y = cb->Regions[i].Top;
2465 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002466 if (!WriteConsoleOutputW(g_hConOut, // output handle
2467 cb->Buffer, // our buffer
2468 cb->BufferSize, // dimensions of our buffer
2469 BufferCoord, // offset in our buffer
2470 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002471 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002472 }
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 return TRUE;
2476}
2477
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002478# define FEAT_RESTORE_ORIG_SCREEN
2479# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002480static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482static ConsoleBuffer g_cbNonTermcap = { 0 };
2483static ConsoleBuffer g_cbTermcap = { 0 };
2484
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002485# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002487HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488static HICON g_hOrigIconSmall = NULL;
2489static HICON g_hOrigIcon = NULL;
2490static HICON g_hVimIcon = NULL;
2491static BOOL g_fCanChangeIcon = FALSE;
2492
Bram Moolenaar0f873732019-12-05 20:28:46 +01002493// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002494# ifndef ICON_SMALL
2495# define ICON_SMALL 0
2496# endif
2497# ifndef ICON_BIG
2498# define ICON_BIG 1
2499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500/*
2501 * GetConsoleIcon()
2502 * Description:
2503 * Attempts to retrieve the small icon and/or the big icon currently in
2504 * use by a given window.
2505 * Returns:
2506 * TRUE on success
2507 */
2508 static BOOL
2509GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002510 HWND hWnd,
2511 HICON *phIconSmall,
2512 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 if (hWnd == NULL)
2515 return FALSE;
2516
2517 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002518 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2519 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002521 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2522 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 return TRUE;
2524}
2525
2526/*
2527 * SetConsoleIcon()
2528 * Description:
2529 * Attempts to change the small icon and/or the big icon currently in
2530 * use by a given window.
2531 * Returns:
2532 * TRUE on success
2533 */
2534 static BOOL
2535SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002536 HWND hWnd,
2537 HICON hIconSmall,
2538 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (hWnd == NULL)
2541 return FALSE;
2542
2543 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002544 SendMessage(hWnd, WM_SETICON,
2545 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002547 SendMessage(hWnd, WM_SETICON,
2548 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 return TRUE;
2550}
2551
2552/*
2553 * SaveConsoleTitleAndIcon()
2554 * Description:
2555 * Saves the current console window title in g_szOrigTitle, for later
2556 * restoration. Also, attempts to obtain a handle to the console window,
2557 * and use it to save the small and big icons currently in use by the
2558 * console window. This is not always possible on some versions of Windows;
2559 * nor is it possible when running Vim remotely using Telnet (since the
2560 * console window the user sees is owned by a remote process).
2561 */
2562 static void
2563SaveConsoleTitleAndIcon(void)
2564{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002565 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2567 return;
2568
2569 /*
2570 * Obtain a handle to the console window using GetConsoleWindow() from
2571 * KERNEL32.DLL; we need to handle in order to change the window icon.
2572 * This function only exists on NT-based Windows, starting with Windows
2573 * 2000. On older operating systems, we can't change the window icon
2574 * anyway.
2575 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002576 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (g_hWnd == NULL)
2578 return;
2579
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2582 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2583 return;
2584
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002586 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002587 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (g_hVimIcon != NULL)
2589 g_fCanChangeIcon = TRUE;
2590}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593static int g_fWindInitCalled = FALSE;
2594static int g_fTermcapMode = FALSE;
2595static CONSOLE_CURSOR_INFO g_cci;
2596static DWORD g_cmodein = 0;
2597static DWORD g_cmodeout = 0;
2598
2599/*
2600 * non-GUI version of mch_init().
2601 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002602 static void
2603mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002605# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002606 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002607# endif
2608# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar0f873732019-12-05 20:28:46 +01002612 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002613 SET_INVALID_PARAM_HANDLER;
2614
Bram Moolenaar0f873732019-12-05 20:28:46 +01002615 // Let critical errors result in a failure, not in a dialog box. Required
2616 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 SetErrorMode(SEM_FAILCRITICALERRORS);
2618
Bram Moolenaar0f873732019-12-05 20:28:46 +01002619 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 out_flush();
2621
Bram Moolenaar0f873732019-12-05 20:28:46 +01002622 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (read_cmd_fd == 0)
2624 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2625 else
2626 create_conin();
2627 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2628
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002629# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002630 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002631 SaveConsoleBuffer(&g_cbOrig);
2632 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002633# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002634 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002635 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2636 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (cterm_normal_fg_color == 0)
2639 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2640 if (cterm_normal_bg_color == 0)
2641 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2642
Bram Moolenaarbdace832019-03-02 10:13:42 +01002643 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002644 g_color_index_fg = g_attrDefault & 0xf;
2645 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2646
Bram Moolenaar0f873732019-12-05 20:28:46 +01002647 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 update_tcap(g_attrCurrent);
2649
2650 GetConsoleCursorInfo(g_hConOut, &g_cci);
2651 GetConsoleMode(g_hConIn, &g_cmodein);
2652 GetConsoleMode(g_hConOut, &g_cmodeout);
2653
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002654# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 SaveConsoleTitleAndIcon();
2656 /*
2657 * Set both the small and big icons of the console window to Vim's icon.
2658 * Note that Vim presently only has one size of icon (32x32), but it
2659 * automatically gets scaled down to 16x16 when setting the small icon.
2660 */
2661 if (g_fCanChangeIcon)
2662 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 ui_get_shellsize();
2666
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002667# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 fdDump = fopen("dump", "wt");
2669
2670 if (fdDump)
2671 {
2672 time_t t;
2673
2674 time(&t);
2675 fputs(ctime(&t), fdDump);
2676 fflush(fdDump);
2677 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 g_fWindInitCalled = TRUE;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002684# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002685 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002686# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002687
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002688 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002689 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * non-GUI version of mch_exit().
2694 * Shut down and exit with status `r'
2695 * Careful: mch_exit() may be called before mch_init()!
2696 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002697 static void
2698mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002700 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002702 vtp_exit();
2703
Bram Moolenaar955f1982017-02-05 15:10:51 +01002704 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (g_fWindInitCalled)
2706 settmode(TMODE_COOK);
2707
Bram Moolenaar0f873732019-12-05 20:28:46 +01002708 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 if (g_fWindInitCalled)
2711 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002712# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002713 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 /*
2715 * Restore both the small and big icons of the console window to
2716 * what they were at startup. Don't do this when the window is
2717 * closed, Vim would hang here.
2718 */
2719 if (g_fCanChangeIcon && !g_fForceExit)
2720 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002723# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (fdDump)
2725 {
2726 time_t t;
2727
2728 time(&t);
2729 fputs(ctime(&t), fdDump);
2730 fclose(fdDump);
2731 }
2732 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735
2736 SetConsoleCursorInfo(g_hConOut, &g_cci);
2737 SetConsoleMode(g_hConIn, g_cmodein);
2738 SetConsoleMode(g_hConOut, g_cmodeout);
2739
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002740# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 exit(r);
2745}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002746#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002748 void
2749mch_init(void)
2750{
2751#ifdef VIMDLL
2752 if (gui.starting)
2753 mch_init_g();
2754 else
2755 mch_init_c();
2756#elif defined(FEAT_GUI_MSWIN)
2757 mch_init_g();
2758#else
2759 mch_init_c();
2760#endif
2761}
2762
2763 void
2764mch_exit(int r)
2765{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002766#ifdef FEAT_NETBEANS_INTG
2767 netbeans_send_disconnect();
2768#endif
2769
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002770#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002771 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002772 mch_exit_g(r);
2773 else
2774 mch_exit_c(r);
2775#elif defined(FEAT_GUI_MSWIN)
2776 mch_exit_g(r);
2777#else
2778 mch_exit_c(r);
2779#endif
2780}
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782/*
2783 * Do we have an interactive window?
2784 */
2785 int
2786mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002787 int argc UNUSED,
2788 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 get_exe_name();
2791
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002792#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002793 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002795# ifdef VIMDLL
2796 if (gui.in_use)
2797 return OK;
2798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (isatty(1))
2800 return OK;
2801 return FAIL;
2802#endif
2803}
2804
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002805/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002806 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * When "len" is > 0, also expand short to long filenames.
2808 */
2809 void
2810fname_case(
2811 char_u *name,
2812 int len)
2813{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002814 int flen;
2815 WCHAR *p;
2816 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002818 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002819 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return;
2821
2822 slash_adjust(name);
2823
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002824 p = enc_to_utf16(name, NULL);
2825 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002826 return;
2827
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002828 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002830 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002832 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002834 if (len > 0 || flen >= (int)STRLEN(q))
2835 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2836 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002839 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840}
2841
2842
2843/*
2844 * Insert user name in s[len].
2845 */
2846 int
2847mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002848 char_u *s,
2849 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002851 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002852 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002854 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002855 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002856 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002857
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002858 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002859 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002860 vim_strncpy(s, p, len - 1);
2861 vim_free(p);
2862 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002863 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 s[0] = NUL;
2866 return FAIL;
2867}
2868
2869
2870/*
2871 * Insert host name in s[len].
2872 */
2873 void
2874mch_get_host_name(
2875 char_u *s,
2876 int len)
2877{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002878 WCHAR wszHostName[256 + 1];
2879 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002881 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002882 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002883 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002884
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002885 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002886 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002887 vim_strncpy(s, p, len - 1);
2888 vim_free(p);
2889 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002890 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892}
2893
2894
2895/*
2896 * return process ID
2897 */
2898 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002899mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 return (long)GetCurrentProcessId();
2902}
2903
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002904/*
2905 * return TRUE if process "pid" is still running
2906 */
2907 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002908mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002909{
2910 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2911 DWORD status = 0;
2912 int ret = FALSE;
2913
2914 if (hProcess == NULL)
2915 return FALSE; // might not have access
2916 if (GetExitCodeProcess(hProcess, &status) )
2917 ret = status == STILL_ACTIVE;
2918 CloseHandle(hProcess);
2919 return ret;
2920}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
2922/*
2923 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2924 * Return OK for success, FAIL for failure.
2925 */
2926 int
2927mch_dirname(
2928 char_u *buf,
2929 int len)
2930{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002931 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /*
2934 * Originally this was:
2935 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2936 * But the Win32s known bug list says that getcwd() doesn't work
2937 * so use the Win32 system call instead. <Negri>
2938 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002939 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002941 WCHAR wcbuf[_MAX_PATH + 1];
2942 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002944 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002946 p = utf16_to_enc(wcbuf, NULL);
2947 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002948 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002949 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002951 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002954 if (p == NULL)
2955 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002956
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002957 if (p != NULL)
2958 {
2959 vim_strncpy(buf, p, len - 1);
2960 vim_free(p);
2961 return OK;
2962 }
2963 }
2964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002968 * Get file permissions for "name".
2969 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 */
2971 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002972mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002974 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002975 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002977 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002978 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981
2982/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002983 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002984 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002985 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 */
2987 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002988mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002990 long n;
2991 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002992
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002993 p = enc_to_utf16(name, NULL);
2994 if (p == NULL)
2995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002997 n = _wchmod(p, perm);
2998 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002999 if (n == -1)
3000 return FAIL;
3001
3002 win32_set_archive(name);
3003
3004 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
3007/*
3008 * Set hidden flag for "name".
3009 */
3010 void
3011mch_hide(char_u *name)
3012{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003013 int attrs = win32_getattrs(name);
3014 if (attrs == -1)
3015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003017 attrs |= FILE_ATTRIBUTE_HIDDEN;
3018 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003022 * Return TRUE if file "name" exists and is hidden.
3023 */
3024 int
3025mch_ishidden(char_u *name)
3026{
3027 int f = win32_getattrs(name);
3028
3029 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003030 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003031
3032 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3033}
3034
3035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 * return TRUE if "name" is a directory
3037 * return FALSE if "name" is not a directory or upon error
3038 */
3039 int
3040mch_isdir(char_u *name)
3041{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003042 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003045 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
3047 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3048}
3049
3050/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003051 * return TRUE if "name" is a directory, NOT a symlink to a directory
3052 * return FALSE if "name" is not a directory
3053 * return FALSE for error
3054 */
3055 int
3056mch_isrealdir(char_u *name)
3057{
3058 return mch_isdir(name) && !mch_is_symbolic_link(name);
3059}
3060
3061/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003062 * Create directory "name".
3063 * Return 0 on success, -1 on error.
3064 */
3065 int
3066mch_mkdir(char_u *name)
3067{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003068 WCHAR *p;
3069 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003070
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003071 p = enc_to_utf16(name, NULL);
3072 if (p == NULL)
3073 return -1;
3074 retval = _wmkdir(p);
3075 vim_free(p);
3076 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003077}
3078
3079/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003080 * Delete directory "name".
3081 * Return 0 on success, -1 on error.
3082 */
3083 int
3084mch_rmdir(char_u *name)
3085{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003086 WCHAR *p;
3087 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003088
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003089 p = enc_to_utf16(name, NULL);
3090 if (p == NULL)
3091 return -1;
3092 retval = _wrmdir(p);
3093 vim_free(p);
3094 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003095}
3096
3097/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003098 * Return TRUE if file "fname" has more than one link.
3099 */
3100 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003101mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003102{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003103 BY_HANDLE_FILE_INFORMATION info;
3104
3105 return win32_fileinfo(fname, &info) == FILEINFO_OK
3106 && info.nNumberOfLinks > 1;
3107}
3108
3109/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003110 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003111 */
3112 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003113mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003114{
3115 HANDLE hFind;
3116 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003117 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003118 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003119 WIN32_FIND_DATAW findDataW;
3120
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003121 wn = enc_to_utf16(name, NULL);
3122 if (wn == NULL)
3123 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003124
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003125 hFind = FindFirstFileW(wn, &findDataW);
3126 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003127 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003128 {
3129 fileFlags = findDataW.dwFileAttributes;
3130 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003131 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003132 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003133
3134 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003135 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3136 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003137 res = TRUE;
3138
3139 return res;
3140}
3141
3142/*
3143 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3144 * link.
3145 */
3146 int
3147mch_is_linked(char_u *fname)
3148{
3149 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3150 return TRUE;
3151 return FALSE;
3152}
3153
3154/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003155 * Get the by-handle-file-information for "fname".
3156 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003157 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003158 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3159 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3160 */
3161 int
3162win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3163{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003164 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003165 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003166 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003167
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003168 wn = enc_to_utf16(fname, NULL);
3169 if (wn == NULL)
3170 return FILEINFO_ENC_FAIL;
3171
3172 hFile = CreateFileW(wn, // file name
3173 GENERIC_READ, // access mode
3174 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3175 NULL, // security descriptor
3176 OPEN_EXISTING, // creation disposition
3177 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3178 NULL); // handle to template file
3179 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003180
3181 if (hFile != INVALID_HANDLE_VALUE)
3182 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003183 if (GetFileInformationByHandle(hFile, info) != 0)
3184 res = FILEINFO_OK;
3185 else
3186 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003187 CloseHandle(hFile);
3188 }
3189
Bram Moolenaar03f48552006-02-28 23:52:23 +00003190 return res;
3191}
3192
3193/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003194 * get file attributes for `name'
3195 * -1 : error
3196 * else FILE_ATTRIBUTE_* defined in winnt.h
3197 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003198 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003199win32_getattrs(char_u *name)
3200{
3201 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003202 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003203
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003204 p = enc_to_utf16(name, NULL);
3205 if (p == NULL)
3206 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003207
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003208 attr = GetFileAttributesW(p);
3209 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003210
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003211 return attr;
3212}
3213
3214/*
3215 * set file attributes for `name' to `attrs'
3216 *
3217 * return -1 for failure, 0 otherwise
3218 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003219 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003220win32_setattrs(char_u *name, int attrs)
3221{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003222 int res;
3223 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003224
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003225 p = enc_to_utf16(name, NULL);
3226 if (p == NULL)
3227 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003228
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003229 res = SetFileAttributesW(p, attrs);
3230 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003231
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003232 return res ? 0 : -1;
3233}
3234
3235/*
3236 * Set archive flag for "name".
3237 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003238 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003239win32_set_archive(char_u *name)
3240{
3241 int attrs = win32_getattrs(name);
3242 if (attrs == -1)
3243 return -1;
3244
3245 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3246 return win32_setattrs(name, attrs);
3247}
3248
3249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Return TRUE if file or directory "name" is writable (not readonly).
3251 * Strange semantics of Win32: a readonly directory is writable, but you can't
3252 * delete a file. Let's say this means it is writable.
3253 */
3254 int
3255mch_writable(char_u *name)
3256{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003257 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003259 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3260 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003264 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003265 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003266 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3267 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003270mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaar86621892019-03-30 21:51:28 +01003272 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3273 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3274 // UTF-8.
3275 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003276 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003277 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003278
Bram Moolenaar86621892019-03-30 21:51:28 +01003279 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003280 return FALSE;
3281
Bram Moolenaar86621892019-03-30 21:51:28 +01003282 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003283 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003284 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003285 return TRUE;
3286
3287 /*
3288 * Loop over all extensions in $PATHEXT.
3289 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003290 p = mch_getenv("PATHEXT");
3291 if (p == NULL)
3292 p = (char_u *)".com;.exe;.bat;.cmd";
3293 saved = vim_strsave(p);
3294 if (saved == NULL)
3295 return FALSE;
3296 p = saved;
3297 while (*p)
3298 {
3299 char_u *tmp = vim_strchr(p, ';');
3300
3301 if (tmp != NULL)
3302 *tmp = NUL;
3303 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3304 && executable_exists((char *)name, path, use_path))
3305 {
3306 vim_free(saved);
3307 return TRUE;
3308 }
3309 if (tmp == NULL)
3310 break;
3311 p = tmp + 1;
3312 }
3313 vim_free(saved);
3314
Bram Moolenaar86621892019-03-30 21:51:28 +01003315 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003316 p = mch_getenv("PATHEXT");
3317 if (p == NULL)
3318 p = (char_u *)".com;.exe;.bat;.cmd";
3319 while (*p)
3320 {
3321 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3322 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003323 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003324 buf[len] = NUL;
3325 ++p;
3326 if (*p)
3327 ++p;
3328 }
3329 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003330 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003331 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003332 return TRUE;
3333 }
3334 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337/*
3338 * Check what "name" is:
3339 * NODE_NORMAL: file or directory (or doesn't exist)
3340 * NODE_WRITABLE: writable device, socket, fifo, etc.
3341 * NODE_OTHER: non-writable things
3342 */
3343 int
3344mch_nodetype(char_u *name)
3345{
3346 HANDLE hFile;
3347 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003348 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar0f873732019-12-05 20:28:46 +01003350 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3351 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3352 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003353 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3354 return NODE_WRITABLE;
3355
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003356 wn = enc_to_utf16(name, NULL);
3357 if (wn == NULL)
3358 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003359
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003360 hFile = CreateFileW(wn, // file name
3361 GENERIC_WRITE, // access mode
3362 0, // share mode
3363 NULL, // security descriptor
3364 OPEN_EXISTING, // creation disposition
3365 0, // file attributes
3366 NULL); // handle to template file
3367 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (hFile == INVALID_HANDLE_VALUE)
3369 return NODE_NORMAL;
3370
3371 type = GetFileType(hFile);
3372 CloseHandle(hFile);
3373 if (type == FILE_TYPE_CHAR)
3374 return NODE_WRITABLE;
3375 if (type == FILE_TYPE_DISK)
3376 return NODE_NORMAL;
3377 return NODE_OTHER;
3378}
3379
3380#ifdef HAVE_ACL
3381struct my_acl
3382{
3383 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3384 PSID pSidOwner;
3385 PSID pSidGroup;
3386 PACL pDacl;
3387 PACL pSacl;
3388};
3389#endif
3390
3391/*
3392 * Return a pointer to the ACL of file "fname" in allocated memory.
3393 * Return NULL if the ACL is not available for whatever reason.
3394 */
3395 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003396mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398#ifndef HAVE_ACL
3399 return (vim_acl_T)NULL;
3400#else
3401 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003402 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003404 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003405 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003407 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003409 wn = enc_to_utf16(fname, NULL);
3410 if (wn == NULL)
3411 return NULL;
3412
3413 // Try to retrieve the entire security descriptor.
3414 err = GetNamedSecurityInfoW(
3415 wn, // Abstract filename
3416 SE_FILE_OBJECT, // File Object
3417 OWNER_SECURITY_INFORMATION |
3418 GROUP_SECURITY_INFORMATION |
3419 DACL_SECURITY_INFORMATION |
3420 SACL_SECURITY_INFORMATION,
3421 &p->pSidOwner, // Ownership information.
3422 &p->pSidGroup, // Group membership.
3423 &p->pDacl, // Discretionary information.
3424 &p->pSacl, // For auditing purposes.
3425 &p->pSecurityDescriptor);
3426 if (err == ERROR_ACCESS_DENIED ||
3427 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003428 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003429 // Retrieve only DACL.
3430 (void)GetNamedSecurityInfoW(
3431 wn,
3432 SE_FILE_OBJECT,
3433 DACL_SECURITY_INFORMATION,
3434 NULL,
3435 NULL,
3436 &p->pDacl,
3437 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003438 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003439 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003440 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003441 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003442 mch_free_acl((vim_acl_T)p);
3443 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003445 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447
3448 return (vim_acl_T)p;
3449#endif
3450}
3451
Bram Moolenaar27515922013-06-29 15:36:26 +02003452#ifdef HAVE_ACL
3453/*
3454 * Check if "acl" contains inherited ACE.
3455 */
3456 static BOOL
3457is_acl_inherited(PACL acl)
3458{
3459 DWORD i;
3460 ACL_SIZE_INFORMATION acl_info;
3461 PACCESS_ALLOWED_ACE ace;
3462
3463 acl_info.AceCount = 0;
3464 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3465 for (i = 0; i < acl_info.AceCount; i++)
3466 {
3467 GetAce(acl, i, (LPVOID *)&ace);
3468 if (ace->Header.AceFlags & INHERITED_ACE)
3469 return TRUE;
3470 }
3471 return FALSE;
3472}
3473#endif
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475/*
3476 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3477 * Errors are ignored.
3478 * This must only be called with "acl" equal to what mch_get_acl() returned.
3479 */
3480 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003481mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483#ifdef HAVE_ACL
3484 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003485 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003486 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003488 if (p == NULL)
3489 return;
3490
3491 wn = enc_to_utf16(fname, NULL);
3492 if (wn == NULL)
3493 return;
3494
3495 // Set security flags
3496 if (p->pSidOwner)
3497 sec_info |= OWNER_SECURITY_INFORMATION;
3498 if (p->pSidGroup)
3499 sec_info |= GROUP_SECURITY_INFORMATION;
3500 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003501 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003502 sec_info |= DACL_SECURITY_INFORMATION;
3503 // Do not inherit its parent's DACL.
3504 // If the DACL is inherited, Cygwin permissions would be changed.
3505 if (!is_acl_inherited(p->pDacl))
3506 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003507 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003508 if (p->pSacl)
3509 sec_info |= SACL_SECURITY_INFORMATION;
3510
3511 (void)SetNamedSecurityInfoW(
3512 wn, // Abstract filename
3513 SE_FILE_OBJECT, // File Object
3514 sec_info,
3515 p->pSidOwner, // Ownership information.
3516 p->pSidGroup, // Group membership.
3517 p->pDacl, // Discretionary information.
3518 p->pSacl // For auditing purposes.
3519 );
3520 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#endif
3522}
3523
3524 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003525mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527#ifdef HAVE_ACL
3528 struct my_acl *p = (struct my_acl *)acl;
3529
3530 if (p != NULL)
3531 {
3532 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3533 vim_free(p);
3534 }
3535#endif
3536}
3537
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003538#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540/*
3541 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3542 */
3543 static BOOL WINAPI
3544handler_routine(
3545 DWORD dwCtrlType)
3546{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003547 INPUT_RECORD ir;
3548 DWORD out;
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 switch (dwCtrlType)
3551 {
3552 case CTRL_C_EVENT:
3553 if (ctrl_c_interrupts)
3554 g_fCtrlCPressed = TRUE;
3555 return TRUE;
3556
3557 case CTRL_BREAK_EVENT:
3558 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003559 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003560 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003561 ir.EventType = KEY_EVENT;
3562 ir.Event.KeyEvent.bKeyDown = TRUE;
3563 ir.Event.KeyEvent.wRepeatCount = 1;
3564 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3565 ir.Event.KeyEvent.wVirtualScanCode = 0;
3566 ir.Event.KeyEvent.dwControlKeyState = 0;
3567 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3568 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return TRUE;
3570
Bram Moolenaar0f873732019-12-05 20:28:46 +01003571 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 case CTRL_CLOSE_EVENT:
3573 case CTRL_LOGOFF_EVENT:
3574 case CTRL_SHUTDOWN_EVENT:
3575 windgoto((int)Rows - 1, 0);
3576 g_fForceExit = TRUE;
3577
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003578 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 (dwCtrlType == CTRL_CLOSE_EVENT
3580 ? _("close")
3581 : dwCtrlType == CTRL_LOGOFF_EVENT
3582 ? _("logoff")
3583 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003584# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaar0f873732019-12-05 20:28:46 +01003588 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar0f873732019-12-05 20:28:46 +01003590 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 default:
3593 return FALSE;
3594 }
3595}
3596
3597
3598/*
3599 * set the tty in (raw) ? "raw" : "cooked" mode
3600 */
3601 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003602mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 DWORD cmodein;
3605 DWORD cmodeout;
3606 BOOL bEnableHandler;
3607
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003608# ifdef VIMDLL
3609 if (gui.in_use)
3610 return;
3611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 GetConsoleMode(g_hConIn, &cmodein);
3613 GetConsoleMode(g_hConOut, &cmodeout);
3614 if (tmode == TMODE_RAW)
3615 {
3616 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3617 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (g_fMouseActive)
3619 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003620 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003621# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003622 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3623 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003624 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003625# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003626 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003627# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003628 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 bEnableHandler = TRUE;
3630 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003631 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3634 ENABLE_ECHO_INPUT);
3635 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3636 bEnableHandler = FALSE;
3637 }
3638 SetConsoleMode(g_hConIn, cmodein);
3639 SetConsoleMode(g_hConOut, cmodeout);
3640 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3641
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003642# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (fdDump)
3644 {
3645 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3646 tmode == TMODE_RAW ? "raw" :
3647 tmode == TMODE_COOK ? "cooked" : "normal",
3648 cmodein, cmodeout);
3649 fflush(fdDump);
3650 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652}
3653
3654
3655/*
3656 * Get the size of the current window in `Rows' and `Columns'
3657 * Return OK when size could be determined, FAIL otherwise.
3658 */
3659 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003660mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 CONSOLE_SCREEN_BUFFER_INFO csbi;
3663
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003664# ifdef VIMDLL
3665 if (gui.in_use)
3666 return OK;
3667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3669 {
3670 /*
3671 * For some reason, we are trying to get the screen dimensions
3672 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3673 * variables are really intended to mean the size of Vim screen
3674 * while in termcap mode.
3675 */
3676 Rows = g_cbTermcap.Info.dwSize.Y;
3677 Columns = g_cbTermcap.Info.dwSize.X;
3678 }
3679 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3680 {
3681 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3682 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3683 }
3684 else
3685 {
3686 Rows = 25;
3687 Columns = 80;
3688 }
3689 return OK;
3690}
3691
3692/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003693 * Resize console buffer to 'COORD'
3694 */
3695 static void
3696ResizeConBuf(
3697 HANDLE hConsole,
3698 COORD coordScreen)
3699{
3700 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3701 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003702# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003703 if (fdDump)
3704 {
3705 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3706 GetLastError());
3707 fflush(fdDump);
3708 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003709# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003710 }
3711}
3712
3713/*
3714 * Resize console window size to 'srWindowRect'
3715 */
3716 static void
3717ResizeWindow(
3718 HANDLE hConsole,
3719 SMALL_RECT srWindowRect)
3720{
3721 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3722 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003723# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003724 if (fdDump)
3725 {
3726 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3727 GetLastError());
3728 fflush(fdDump);
3729 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003730# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003731 }
3732}
3733
3734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 * Set a console window to `xSize' * `ySize'
3736 */
3737 static void
3738ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003739 HANDLE hConsole,
3740 int xSize,
3741 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003743 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3744 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003746 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003747 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003749# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (fdDump)
3751 {
3752 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3753 fflush(fdDump);
3754 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
Bram Moolenaar0f873732019-12-05 20:28:46 +01003757 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 coordScreen = GetLargestConsoleWindowSize(hConsole);
3759
Bram Moolenaar0f873732019-12-05 20:28:46 +01003760 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3762 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3763 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3764
3765 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3766 {
3767 int sx, sy;
3768
3769 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3770 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3771 if (sy < ySize || sx < xSize)
3772 {
3773 /*
3774 * Increasing number of lines/columns, do buffer first.
3775 * Use the maximal size in x and y direction.
3776 */
3777 if (sy < ySize)
3778 coordScreen.Y = ySize;
3779 else
3780 coordScreen.Y = sy;
3781 if (sx < xSize)
3782 coordScreen.X = xSize;
3783 else
3784 coordScreen.X = sx;
3785 SetConsoleScreenBufferSize(hConsole, coordScreen);
3786 }
3787 }
3788
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003789 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 coordScreen.X = xSize;
3791 coordScreen.Y = ySize;
3792
Bram Moolenaar2551c032018-08-23 22:38:31 +02003793 // In the new console call API, only the first time in reverse order
3794 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003796 ResizeWindow(hConsole, srWindowRect);
3797 ResizeConBuf(hConsole, coordScreen);
3798 }
3799 else
3800 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003801 // Workaround for a Windows 10 bug
3802 cursor.X = srWindowRect.Left;
3803 cursor.Y = srWindowRect.Top;
3804 SetConsoleCursorPosition(hConsole, cursor);
3805
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003806 ResizeConBuf(hConsole, coordScreen);
3807 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003808 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810}
3811
3812
3813/*
3814 * Set the console window to `Rows' * `Columns'
3815 */
3816 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003817mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 COORD coordScreen;
3820
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003821# ifdef VIMDLL
3822 if (gui.in_use)
3823 return;
3824# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003825 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (suppress_winsize != 0)
3827 {
3828 suppress_winsize = 2;
3829 return;
3830 }
3831
3832 if (term_console)
3833 {
3834 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3835
Bram Moolenaar0f873732019-12-05 20:28:46 +01003836 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (Rows > coordScreen.Y)
3838 Rows = coordScreen.Y;
3839 if (Columns > coordScreen.X)
3840 Columns = coordScreen.X;
3841
3842 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3843 }
3844}
3845
3846/*
3847 * Rows and/or Columns has changed.
3848 */
3849 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003850mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003852# ifdef VIMDLL
3853 if (gui.in_use)
3854 return;
3855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3857}
3858
3859
3860/*
3861 * Called when started up, to set the winsize that was delayed.
3862 */
3863 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003864mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 if (suppress_winsize == 2)
3867 {
3868 suppress_winsize = 0;
3869 mch_set_shellsize();
3870 shell_resized();
3871 }
3872 suppress_winsize = 0;
3873}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003874#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003876 static BOOL
3877vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003878 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003879 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003880 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003881 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003882 PROCESS_INFORMATION *pi,
3883 LPVOID *env,
3884 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003885{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003886 BOOL ret = FALSE;
3887 WCHAR *wcmd, *wcwd = NULL;
3888
3889 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3890 if (wcmd == NULL)
3891 return FALSE;
3892 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003893 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003894 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3895 if (wcwd == NULL)
3896 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003897 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003898
3899 ret = CreateProcessW(
3900 NULL, // Executable name
3901 wcmd, // Command to execute
3902 NULL, // Process security attributes
3903 NULL, // Thread security attributes
3904 inherit_handles, // Inherit handles
3905 flags, // Creation flags
3906 env, // Environment
3907 wcwd, // Current directory
3908 (LPSTARTUPINFOW)si, // Startup information
3909 pi); // Process information
3910theend:
3911 vim_free(wcmd);
3912 vim_free(wcwd);
3913 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003914}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003917 static HINSTANCE
3918vim_shell_execute(
3919 char *cmd,
3920 INT n_show_cmd)
3921{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003922 HINSTANCE ret;
3923 WCHAR *wcmd;
3924
3925 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3926 if (wcmd == NULL)
3927 return (HINSTANCE) 0;
3928
3929 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3930 vim_free(wcmd);
3931 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003932}
3933
3934
Bram Moolenaar4f974752019-02-17 17:44:42 +01003935#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937/*
3938 * Specialised version of system() for Win32 GUI mode.
3939 * This version proceeds as follows:
3940 * 1. Create a console window for use by the subprocess
3941 * 2. Run the subprocess (it gets the allocated console by default)
3942 * 3. Wait for the subprocess to terminate and get its exit code
3943 * 4. Prompt the user to press a key to close the console window
3944 */
3945 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003946mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 STARTUPINFO si;
3949 PROCESS_INFORMATION pi;
3950 DWORD ret = 0;
3951 HWND hwnd = GetFocus();
3952
3953 si.cb = sizeof(si);
3954 si.lpReserved = NULL;
3955 si.lpDesktop = NULL;
3956 si.lpTitle = NULL;
3957 si.dwFlags = STARTF_USESHOWWINDOW;
3958 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003959 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003960 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003962 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003963 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
3965 si.wShowWindow = SW_SHOWNORMAL;
3966 si.cbReserved2 = 0;
3967 si.lpReserved2 = NULL;
3968
Bram Moolenaar0f873732019-12-05 20:28:46 +01003969 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003970 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003971 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3972 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
Bram Moolenaar0f873732019-12-05 20:28:46 +01003974 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003976# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int delay = 1;
3978
Bram Moolenaar0f873732019-12-05 20:28:46 +01003979 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 for (;;)
3981 {
3982 MSG msg;
3983
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003984 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
3986 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003987 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003988 delay = 1;
3989 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3992 break;
3993
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // We start waiting for a very short time and then increase it, so
3995 // that we respond quickly when the process is quick, and don't
3996 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (delay < 50)
3998 delay += 10;
3999 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004000# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 GetExitCodeProcess(pi.hProcess, &ret);
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Bram Moolenaar0f873732019-12-05 20:28:46 +01004008 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 CloseHandle(pi.hThread);
4010 CloseHandle(pi.hProcess);
4011
Bram Moolenaar0f873732019-12-05 20:28:46 +01004012 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4014
4015 return ret;
4016}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004017
4018/*
4019 * Thread launched by the gui to send the current buffer data to the
4020 * process. This way avoid to hang up vim totally if the children
4021 * process take a long time to process the lines.
4022 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004023 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004024sub_process_writer(LPVOID param)
4025{
4026 HANDLE g_hChildStd_IN_Wr = param;
4027 linenr_T lnum = curbuf->b_op_start.lnum;
4028 DWORD len = 0;
4029 DWORD l;
4030 char_u *lp = ml_get(lnum);
4031 char_u *s;
4032 int written = 0;
4033
4034 for (;;)
4035 {
4036 l = (DWORD)STRLEN(lp + written);
4037 if (l == 0)
4038 len = 0;
4039 else if (lp[written] == NL)
4040 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004041 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004042 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4043 }
4044 else
4045 {
4046 s = vim_strchr(lp + written, NL);
4047 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4048 s == NULL ? l : (DWORD)(s - (lp + written)),
4049 &len, NULL);
4050 }
4051 if (len == (int)l)
4052 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004053 // Finished a line, add a NL, unless this line should not have
4054 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004055 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004056 || (!curbuf->b_p_bin
4057 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004058 || (lnum != curbuf->b_no_eol_lnum
4059 && (lnum != curbuf->b_ml.ml_line_count
4060 || curbuf->b_p_eol)))
4061 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004062 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4063 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004064 }
4065
4066 ++lnum;
4067 if (lnum > curbuf->b_op_end.lnum)
4068 break;
4069
4070 lp = ml_get(lnum);
4071 written = 0;
4072 }
4073 else if (len > 0)
4074 written += len;
4075 }
4076
Bram Moolenaar0f873732019-12-05 20:28:46 +01004077 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004078 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004079 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004080}
4081
4082
Bram Moolenaar0f873732019-12-05 20:28:46 +01004083# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004084
4085/*
4086 * This function read from the children's stdout and write the
4087 * data on screen or in the buffer accordingly.
4088 */
4089 static void
4090dump_pipe(int options,
4091 HANDLE g_hChildStd_OUT_Rd,
4092 garray_T *ga,
4093 char_u buffer[],
4094 DWORD *buffer_off)
4095{
4096 DWORD availableBytes = 0;
4097 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004098 int ret;
4099 DWORD len;
4100 DWORD toRead;
4101 int repeatCount;
4102
Bram Moolenaar0f873732019-12-05 20:28:46 +01004103 // we query the pipe to see if there is any data to read
4104 // to avoid to perform a blocking read
4105 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4106 NULL, // optional buffer
4107 0, // buffer size
4108 NULL, // number of read bytes
4109 &availableBytes, // available bytes total
4110 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004111
4112 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004113 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004114 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004115 {
4116 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004117 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004118 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004119 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004120
Bram Moolenaar0f873732019-12-05 20:28:46 +01004121 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004122 if (len == 0)
4123 break;
4124
4125 availableBytes -= len;
4126
4127 if (options & SHELL_READ)
4128 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004129 // Do NUL -> NL translation, append NL separated
4130 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004131 for (i = 0; i < len; ++i)
4132 {
4133 if (buffer[i] == NL)
4134 append_ga_line(ga);
4135 else if (buffer[i] == NUL)
4136 ga_append(ga, NL);
4137 else
4138 ga_append(ga, buffer[i]);
4139 }
4140 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004141 else if (has_mbyte)
4142 {
4143 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004144 int c;
4145 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004146
4147 len += *buffer_off;
4148 buffer[len] = NUL;
4149
Bram Moolenaar0f873732019-12-05 20:28:46 +01004150 // Check if the last character in buffer[] is
4151 // incomplete, keep these bytes for the next
4152 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004153 for (p = buffer; p < buffer + len; p += l)
4154 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004155 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004156 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004157 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004158 else if (MB_BYTE2LEN(*p) != l)
4159 break;
4160 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004161 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004162 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004163 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004164 if (len >= 12)
4165 ++p;
4166 else
4167 {
4168 *buffer_off = len;
4169 return;
4170 }
4171 }
4172 c = *p;
4173 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004174 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004175 if (p < buffer + len)
4176 {
4177 *p = c;
4178 *buffer_off = (DWORD)((buffer + len) - p);
4179 mch_memmove(buffer, p, *buffer_off);
4180 return;
4181 }
4182 *buffer_off = 0;
4183 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004184 else
4185 {
4186 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004187 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004188 }
4189
4190 windgoto(msg_row, msg_col);
4191 cursor_on();
4192 out_flush();
4193 }
4194}
4195
4196/*
4197 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4198 * for communication and doesn't open any new window.
4199 */
4200 static int
4201mch_system_piped(char *cmd, int options)
4202{
4203 STARTUPINFO si;
4204 PROCESS_INFORMATION pi;
4205 DWORD ret = 0;
4206
4207 HANDLE g_hChildStd_IN_Rd = NULL;
4208 HANDLE g_hChildStd_IN_Wr = NULL;
4209 HANDLE g_hChildStd_OUT_Rd = NULL;
4210 HANDLE g_hChildStd_OUT_Wr = NULL;
4211
Bram Moolenaar0f873732019-12-05 20:28:46 +01004212 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004213 DWORD len;
4214
Bram Moolenaar0f873732019-12-05 20:28:46 +01004215 // buffer used to receive keys
4216 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4217 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004218
4219 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004220 int noread_cnt = 0;
4221 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004222 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004223 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004224 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004225
4226 SECURITY_ATTRIBUTES saAttr;
4227
Bram Moolenaar0f873732019-12-05 20:28:46 +01004228 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004229 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4230 saAttr.bInheritHandle = TRUE;
4231 saAttr.lpSecurityDescriptor = NULL;
4232
4233 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004234 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004235 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004236 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004237 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004238 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004239 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004240 {
4241 CloseHandle(g_hChildStd_IN_Rd);
4242 CloseHandle(g_hChildStd_IN_Wr);
4243 CloseHandle(g_hChildStd_OUT_Rd);
4244 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004245 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004246 }
4247
4248 si.cb = sizeof(si);
4249 si.lpReserved = NULL;
4250 si.lpDesktop = NULL;
4251 si.lpTitle = NULL;
4252 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4253
Bram Moolenaar0f873732019-12-05 20:28:46 +01004254 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004255 si.hStdError = g_hChildStd_OUT_Wr;
4256 si.hStdOutput = g_hChildStd_OUT_Wr;
4257 si.hStdInput = g_hChildStd_IN_Rd;
4258 si.wShowWindow = SW_HIDE;
4259 si.cbReserved2 = 0;
4260 si.lpReserved2 = NULL;
4261
4262 if (options & SHELL_READ)
4263 ga_init2(&ga, 1, BUFLEN);
4264
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004265 if (cmd != NULL)
4266 {
4267 p = (char *)vim_strsave((char_u *)cmd);
4268 if (p != NULL)
4269 unescape_shellxquote((char_u *)p, p_sxe);
4270 else
4271 p = cmd;
4272 }
4273
Bram Moolenaar0f873732019-12-05 20:28:46 +01004274 // Now, run the command.
4275 // About "Inherit handles" being TRUE: this command can be litigious,
4276 // handle inheritance was deactivated for pending temp file, but, if we
4277 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004278 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4279 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004280
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004281 if (p != cmd)
4282 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004283
Bram Moolenaar0f873732019-12-05 20:28:46 +01004284 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004285 CloseHandle(g_hChildStd_IN_Rd);
4286 CloseHandle(g_hChildStd_OUT_Wr);
4287
4288 if (options & SHELL_WRITE)
4289 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004290 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004291 _beginthreadex(NULL, // security attributes
4292 0, // default stack size
4293 sub_process_writer, // function to be executed
4294 g_hChildStd_IN_Wr, // parameter
4295 0, // creation flag, start immediately
4296 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004297 CloseHandle(thread);
4298 g_hChildStd_IN_Wr = NULL;
4299 }
4300
Bram Moolenaar0f873732019-12-05 20:28:46 +01004301 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004302 for (;;)
4303 {
4304 MSG msg;
4305
Bram Moolenaar175d0702013-12-11 18:36:33 +01004306 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004307 {
4308 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004309 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004310 }
4311
Bram Moolenaar0f873732019-12-05 20:28:46 +01004312 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004313 if ((options & (SHELL_READ|SHELL_WRITE))
4314# ifdef FEAT_GUI
4315 || gui.in_use
4316# endif
4317 )
4318 {
4319 len = 0;
4320 if (!(options & SHELL_EXPAND)
4321 && ((options &
4322 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4323 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4324# ifdef FEAT_GUI
4325 || gui.in_use
4326# endif
4327 )
4328 && (ta_len > 0 || noread_cnt > 4))
4329 {
4330 if (ta_len == 0)
4331 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004332 // Get extra characters when we don't have any. Reset the
4333 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004334 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004335 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4336 }
4337 if (ta_len > 0 || len > 0)
4338 {
4339 /*
4340 * For pipes: Check for CTRL-C: send interrupt signal to
4341 * child. Check for CTRL-D: EOF, close pipe to child.
4342 */
4343 if (len == 1 && cmd != NULL)
4344 {
4345 if (ta_buf[ta_len] == Ctrl_C)
4346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004347 // Learn what exit code is expected, for
4348 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004349 TerminateProcess(pi.hProcess, 9);
4350 }
4351 if (ta_buf[ta_len] == Ctrl_D)
4352 {
4353 CloseHandle(g_hChildStd_IN_Wr);
4354 g_hChildStd_IN_Wr = NULL;
4355 }
4356 }
4357
Bram Moolenaarf4140482020-02-15 23:06:45 +01004358 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004359
4360 /*
4361 * For pipes: echo the typed characters. For a pty this
4362 * does not seem to work.
4363 */
4364 for (i = ta_len; i < ta_len + len; ++i)
4365 {
4366 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4367 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004368 else if (has_mbyte)
4369 {
4370 int l = (*mb_ptr2len)(ta_buf + i);
4371
4372 msg_outtrans_len(ta_buf + i, l);
4373 i += l - 1;
4374 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004375 else
4376 msg_outtrans_len(ta_buf + i, 1);
4377 }
4378 windgoto(msg_row, msg_col);
4379 out_flush();
4380
4381 ta_len += len;
4382
4383 /*
4384 * Write the characters to the child, unless EOF has been
4385 * typed for pipes. Write one character at a time, to
4386 * avoid losing too much typeahead. When writing buffer
4387 * lines, drop the typed characters (only check for
4388 * CTRL-C).
4389 */
4390 if (options & SHELL_WRITE)
4391 ta_len = 0;
4392 else if (g_hChildStd_IN_Wr != NULL)
4393 {
4394 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4395 1, &len, NULL);
4396 // if we are typing in, we want to keep things reactive
4397 delay = 1;
4398 if (len > 0)
4399 {
4400 ta_len -= len;
4401 mch_memmove(ta_buf, ta_buf + len, ta_len);
4402 }
4403 }
4404 }
4405 }
4406 }
4407
4408 if (ta_len)
4409 ui_inchar_undo(ta_buf, ta_len);
4410
4411 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4412 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004413 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004414 break;
4415 }
4416
4417 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004418 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004419
Bram Moolenaar0f873732019-12-05 20:28:46 +01004420 // We start waiting for a very short time and then increase it, so
4421 // that we respond quickly when the process is quick, and don't
4422 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004423 if (delay < 50)
4424 delay += 10;
4425 }
4426
Bram Moolenaar0f873732019-12-05 20:28:46 +01004427 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004428 CloseHandle(g_hChildStd_OUT_Rd);
4429 if (g_hChildStd_IN_Wr != NULL)
4430 CloseHandle(g_hChildStd_IN_Wr);
4431
4432 WaitForSingleObject(pi.hProcess, INFINITE);
4433
Bram Moolenaar0f873732019-12-05 20:28:46 +01004434 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004435 GetExitCodeProcess(pi.hProcess, &ret);
4436
4437 if (options & SHELL_READ)
4438 {
4439 if (ga.ga_len > 0)
4440 {
4441 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004442 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004443 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4444 }
4445 else
4446 curbuf->b_no_eol_lnum = 0;
4447 ga_clear(&ga);
4448 }
4449
Bram Moolenaar0f873732019-12-05 20:28:46 +01004450 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004451 CloseHandle(pi.hThread);
4452 CloseHandle(pi.hProcess);
4453
4454 return ret;
4455}
4456
4457 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004458mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004459{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004460 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004461 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004462 return mch_system_piped(cmd, options);
4463 else
4464 return mch_system_classic(cmd, options);
4465}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004468#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004469 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004470mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004471{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004472 int ret;
4473 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004474 char_u *buf;
4475 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004476
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004477 // If the command starts and ends with double quotes, enclose the command
4478 // in parentheses.
4479 len = STRLEN(cmd);
4480 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4481 {
4482 len += 3;
4483 buf = alloc(len);
4484 if (buf == NULL)
4485 return -1;
4486 vim_snprintf((char *)buf, len, "(%s)", cmd);
4487 wcmd = enc_to_utf16(buf, NULL);
4488 free(buf);
4489 }
4490 else
4491 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4492
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004493 if (wcmd == NULL)
4494 return -1;
4495
4496 ret = _wsystem(wcmd);
4497 vim_free(wcmd);
4498 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501#endif
4502
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004503 static int
4504mch_system(char *cmd, int options)
4505{
4506#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004507 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004508 return mch_system_g(cmd, options);
4509 else
4510 return mch_system_c(cmd, options);
4511#elif defined(FEAT_GUI_MSWIN)
4512 return mch_system_g(cmd, options);
4513#else
4514 return mch_system_c(cmd, options);
4515#endif
4516}
4517
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004518#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4519/*
4520 * Use a terminal window to run a shell command in.
4521 */
4522 static int
4523mch_call_shell_terminal(
4524 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004525 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004526{
4527 jobopt_T opt;
4528 char_u *newcmd = NULL;
4529 typval_T argvar[2];
4530 long_u cmdlen;
4531 int retval = -1;
4532 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004533 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004534 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004535 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004536
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004537 if (cmd == NULL)
4538 cmdlen = STRLEN(p_sh) + 1;
4539 else
4540 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004541 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004542 if (newcmd == NULL)
4543 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004544 if (cmd == NULL)
4545 {
4546 STRCPY(newcmd, p_sh);
4547 ch_log(NULL, "starting terminal to run a shell");
4548 }
4549 else
4550 {
4551 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4552 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4553 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004554
4555 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004556
4557 argvar[0].v_type = VAR_STRING;
4558 argvar[0].vval.v_string = newcmd;
4559 argvar[1].v_type = VAR_UNKNOWN;
4560 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004561 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004562 {
4563 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004564 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004565 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004566
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004567 job = term_getjob(buf->b_term);
4568 ++job->jv_refcount;
4569
Bram Moolenaar0f873732019-12-05 20:28:46 +01004570 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004571 aucmd_prepbuf(&aco, buf);
4572
4573 clear_oparg(&oa);
4574 while (term_use_loop())
4575 {
4576 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4577 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004578 // If terminal_loop() returns OK we got a key that is handled
4579 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004580 if (terminal_loop(TRUE) == OK)
4581 normal_cmd(&oa, TRUE);
4582 }
4583 else
4584 normal_cmd(&oa, TRUE);
4585 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004586 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004587 ch_log(NULL, "system command finished");
4588
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004589 job_unref(job);
4590
Bram Moolenaar0f873732019-12-05 20:28:46 +01004591 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004592 aucmd_restbuf(&aco);
4593
4594 wait_return(TRUE);
4595 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4596
4597 vim_free(newcmd);
4598 return retval;
4599}
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602/*
4603 * Either execute a command by calling the shell or start a new shell
4604 */
4605 int
4606mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004607 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004608 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 int x = 0;
4611 int tmode = cur_tmode;
4612#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004613 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004614
Bram Moolenaar0f873732019-12-05 20:28:46 +01004615 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004616 if (GetConsoleTitleW(szShellTitle,
4617 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004618 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004619 if (cmd == NULL)
4620 wcscat(szShellTitle, L" :sh");
4621 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004622 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004623 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004625 if (wn != NULL)
4626 {
4627 wcscat(szShellTitle, L" - !");
4628 if ((wcslen(szShellTitle) + wcslen(wn) <
4629 sizeof(szShellTitle)/sizeof(WCHAR)))
4630 wcscat(szShellTitle, wn);
4631 SetConsoleTitleW(szShellTitle);
4632 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004633 }
4634 }
4635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#endif
4637
4638 out_flush();
4639
4640#ifdef MCH_WRITE_DUMP
4641 if (fdDump)
4642 {
4643 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4644 fflush(fdDump);
4645 }
4646#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004647#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004648 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004649 if (
4650# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004651 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004652# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004653 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004654 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4655 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004656 char_u *cmdbase = cmd;
4657
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004658 if (cmdbase != NULL)
4659 // Skip a leading quote and (.
4660 while (*cmdbase == '"' || *cmdbase == '(')
4661 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004662
4663 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004664 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4665 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004666 {
4667 // Use a terminal window to run the command in.
4668 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004669# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004670 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004671# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004672 return x;
4673 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004674 }
4675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 /*
4678 * Catch all deadly signals while running the external command, because a
4679 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4680 */
4681 signal(SIGINT, SIG_IGN);
4682#if defined(__GNUC__) && !defined(__MINGW32__)
4683 signal(SIGKILL, SIG_IGN);
4684#else
4685 signal(SIGBREAK, SIG_IGN);
4686#endif
4687 signal(SIGILL, SIG_IGN);
4688 signal(SIGFPE, SIG_IGN);
4689 signal(SIGSEGV, SIG_IGN);
4690 signal(SIGTERM, SIG_IGN);
4691 signal(SIGABRT, SIG_IGN);
4692
4693 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004694 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 if (cmd == NULL)
4697 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004698 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 else
4701 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004702 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004703 char_u *newcmd = NULL;
4704 char_u *cmdbase = cmd;
4705 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004706
Bram Moolenaar0f873732019-12-05 20:28:46 +01004707 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004708 if (*cmdbase == '"' )
4709 ++cmdbase;
4710 if (*cmdbase == '(')
4711 ++cmdbase;
4712
Bram Moolenaar1c465442017-03-12 20:10:05 +01004713 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004714 {
4715 STARTUPINFO si;
4716 PROCESS_INFORMATION pi;
4717 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004718 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004719 char_u *p;
4720
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004721 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004722 si.cb = sizeof(si);
4723 si.lpReserved = NULL;
4724 si.lpDesktop = NULL;
4725 si.lpTitle = NULL;
4726 si.dwFlags = 0;
4727 si.cbReserved2 = 0;
4728 si.lpReserved2 = NULL;
4729
4730 cmdbase = skipwhite(cmdbase + 5);
4731 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004732 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004733 {
4734 cmdbase = skipwhite(cmdbase + 4);
4735 si.dwFlags = STARTF_USESHOWWINDOW;
4736 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004737 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004738 }
4739 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004740 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004741 {
4742 cmdbase = skipwhite(cmdbase + 2);
4743 flags = CREATE_NO_WINDOW;
4744 si.dwFlags = STARTF_USESTDHANDLES;
4745 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004746 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004747 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004748 NULL, // Security att.
4749 OPEN_EXISTING, // Open flags
4750 FILE_ATTRIBUTE_NORMAL, // File att.
4751 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004752 si.hStdOutput = si.hStdInput;
4753 si.hStdError = si.hStdInput;
4754 }
4755
Bram Moolenaar0f873732019-12-05 20:28:46 +01004756 // Remove a trailing ", ) and )" if they have a match
4757 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004758 if (cmdbase > cmd)
4759 {
4760 p = cmdbase + STRLEN(cmdbase);
4761 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4762 *--p = NUL;
4763 if (p > cmdbase && p[-1] == ')'
4764 && (*cmd =='(' || cmd[1] == '('))
4765 *--p = NUL;
4766 }
4767
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004768 newcmd = cmdbase;
4769 unescape_shellxquote(cmdbase, p_sxe);
4770
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004771 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004772 * If creating new console, arguments are passed to the
4773 * 'cmd.exe' as-is. If it's not, arguments are not treated
4774 * correctly for current 'cmd.exe'. So unescape characters in
4775 * shellxescape except '|' for avoiding to be treated as
4776 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004777 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004778 if (flags != CREATE_NEW_CONSOLE)
4779 {
4780 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004781 char_u *cmd_shell = mch_getenv("COMSPEC");
4782
4783 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004784 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004785
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004786 subcmd = vim_strsave_escaped_ext(cmdbase,
4787 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004788 if (subcmd != NULL)
4789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004790 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004791 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004792 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004793 if (newcmd != NULL)
4794 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004795 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004796 else
4797 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004798 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004799 }
4800 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004801
4802 /*
4803 * Now, start the command as a process, so that it doesn't
4804 * inherit our handles which causes unpleasant dangling swap
4805 * files if we exit before the spawned process
4806 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004807 if (vim_create_process((char *)newcmd, FALSE, flags,
4808 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004809 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004810 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4811 > (HINSTANCE)32)
4812 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004813 else
4814 {
4815 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004816#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004817# ifdef VIMDLL
4818 if (gui.in_use)
4819# endif
4820 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004821#endif
4822 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004823
4824 if (newcmd != cmdbase)
4825 vim_free(newcmd);
4826
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004827 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004828 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004829 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004830 CloseHandle(si.hStdInput);
4831 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004832 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004833 CloseHandle(pi.hThread);
4834 CloseHandle(pi.hProcess);
4835 }
4836 else
4837 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004838 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004839#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004840 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004841 (!s_dont_use_vimrun && p_stmp ?
4842 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4843 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004845 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004846
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004847 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004848 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004850#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004851 if (
4852# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004853 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004854# endif
4855 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004857 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4858 "External commands will not pause after completion.\n"
4859 "See :help win32-vimrun for more information.");
4860 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004861 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4862 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004863
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004864 if (wmsg != NULL && wtitle != NULL)
4865 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4866 vim_free(wmsg);
4867 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 need_vimrun_warning = FALSE;
4869 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004870 if (
4871# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004872 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004873# endif
4874 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004875 // Use vimrun to execute the command. It opens a console
4876 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004877 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 vimrun_path,
4879 (msg_silent != 0 || (options & SHELL_DOOUT))
4880 ? "-s " : "",
4881 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004882 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004883# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004884 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004885# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004886 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004887 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004888 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4889 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004892 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004893 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004895 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 }
4899
4900 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004901 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar0f873732019-12-05 20:28:46 +01004903 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004905#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004906 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004907 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#endif
4909 )
4910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004911 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 msg_putchar('\n');
4913 }
4914#ifdef FEAT_TITLE
4915 resettitle();
4916#endif
4917
4918 signal(SIGINT, SIG_DFL);
4919#if defined(__GNUC__) && !defined(__MINGW32__)
4920 signal(SIGKILL, SIG_DFL);
4921#else
4922 signal(SIGBREAK, SIG_DFL);
4923#endif
4924 signal(SIGILL, SIG_DFL);
4925 signal(SIGFPE, SIG_DFL);
4926 signal(SIGSEGV, SIG_DFL);
4927 signal(SIGTERM, SIG_DFL);
4928 signal(SIGABRT, SIG_DFL);
4929
4930 return x;
4931}
4932
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004933#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004934 static HANDLE
4935job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004936 char_u *fname,
4937 DWORD dwDesiredAccess,
4938 DWORD dwShareMode,
4939 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4940 DWORD dwCreationDisposition,
4941 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004942{
4943 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004944 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004945
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004946 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004947 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004948 return INVALID_HANDLE_VALUE;
4949
4950 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4951 lpSecurityAttributes, dwCreationDisposition,
4952 dwFlagsAndAttributes, NULL);
4953 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004954 return h;
4955}
4956
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004957/*
4958 * Turn the dictionary "env" into a NUL separated list that can be used as the
4959 * environment argument of vim_create_process().
4960 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004961 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004962win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004963{
4964 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004965 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004966 LPVOID base = GetEnvironmentStringsW();
4967
Bram Moolenaar0f873732019-12-05 20:28:46 +01004968 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004969 if (ga_grow(gap, 1) == FAIL)
4970 return;
4971
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004972 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004973 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004974 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004975 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004976 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004977 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004978 typval_T *item = &dict_lookup(hi)->di_tv;
4979 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004981 --todo;
4982 if (wkey != NULL && wval != NULL)
4983 {
4984 size_t n;
4985 size_t lkey = wcslen(wkey);
4986 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004987
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004988 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4989 continue;
4990 for (n = 0; n < lkey; n++)
4991 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4992 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4993 for (n = 0; n < lval; n++)
4994 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4995 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4996 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004997 vim_free(wkey);
4998 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004999 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005000 }
5001 }
5002
Bram Moolenaar355757a2020-02-10 22:06:32 +01005003 if (base)
5004 {
5005 WCHAR *p = (WCHAR*) base;
5006
5007 // for last \0
5008 if (ga_grow(gap, 1) == FAIL)
5009 return;
5010
5011 while (*p != 0 || *(p + 1) != 0)
5012 {
5013 if (ga_grow(gap, 1) == OK)
5014 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5015 p++;
5016 }
5017 FreeEnvironmentStrings(base);
5018 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5019 }
5020
Bram Moolenaar493359e2018-06-12 20:25:52 +02005021# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005022 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005023# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005024 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005025 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005026# endif
5027# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005028 char_u *version = get_vim_var_str(VV_VERSION);
5029 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005030# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005031 // size of "VIM_SERVERNAME=" and value,
5032 // plus "VIM_TERMINAL=" and value,
5033 // plus two terminating NULs
5034 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005035# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005036 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005037# endif
5038# ifdef FEAT_TERMINAL
5039 + 13 + version_len + 2
5040# endif
5041 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005042
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005043 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005044 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005045# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005046 for (n = 0; n < 15; n++)
5047 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5048 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005049 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005050 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5051 (WCHAR)servername[n];
5052 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005053# endif
5054# ifdef FEAT_TERMINAL
5055 if (is_terminal)
5056 {
5057 for (n = 0; n < 13; n++)
5058 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5059 (WCHAR)"VIM_TERMINAL="[n];
5060 for (n = 0; n < version_len; n++)
5061 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5062 (WCHAR)version[n];
5063 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5064 }
5065# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005066 }
5067 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005068# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005069}
5070
Bram Moolenaarb091f302019-01-19 14:37:00 +01005071/*
5072 * Create a pair of pipes.
5073 * Return TRUE for success, FALSE for failure.
5074 */
5075 static BOOL
5076create_pipe_pair(HANDLE handles[2])
5077{
5078 static LONG s;
5079 char name[64];
5080 SECURITY_ATTRIBUTES sa;
5081
5082 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5083 GetCurrentProcessId(),
5084 InterlockedIncrement(&s));
5085
5086 // Create named pipe. Max size of named pipe is 65535.
5087 handles[1] = CreateNamedPipe(
5088 name,
5089 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5090 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005091 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005092
5093 if (handles[1] == INVALID_HANDLE_VALUE)
5094 return FALSE;
5095
5096 sa.nLength = sizeof(sa);
5097 sa.bInheritHandle = TRUE;
5098 sa.lpSecurityDescriptor = NULL;
5099
5100 handles[0] = CreateFile(name,
5101 FILE_GENERIC_READ,
5102 FILE_SHARE_READ, &sa,
5103 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5104
5105 if (handles[0] == INVALID_HANDLE_VALUE)
5106 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005107 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005108 return FALSE;
5109 }
5110
5111 return TRUE;
5112}
5113
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005114 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005115mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005116{
5117 STARTUPINFO si;
5118 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005119 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005120 SECURITY_ATTRIBUTES saAttr;
5121 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005122 HANDLE ifd[2];
5123 HANDLE ofd[2];
5124 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005125 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005126
5127 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5128 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5129 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5130 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5131 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5132 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5133 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5134
5135 if (use_out_for_err && use_null_for_out)
5136 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005137
5138 ifd[0] = INVALID_HANDLE_VALUE;
5139 ifd[1] = INVALID_HANDLE_VALUE;
5140 ofd[0] = INVALID_HANDLE_VALUE;
5141 ofd[1] = INVALID_HANDLE_VALUE;
5142 efd[0] = INVALID_HANDLE_VALUE;
5143 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005144 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005145
Bram Moolenaar14207f42016-10-27 21:13:10 +02005146 jo = CreateJobObject(NULL, NULL);
5147 if (jo == NULL)
5148 {
5149 job->jv_status = JOB_FAILED;
5150 goto failed;
5151 }
5152
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005153 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005154 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005155
Bram Moolenaar76467df2016-02-12 19:30:26 +01005156 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005157 ZeroMemory(&si, sizeof(si));
5158 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005159 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005160 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005161
Bram Moolenaard8070362016-02-15 21:56:54 +01005162 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5163 saAttr.bInheritHandle = TRUE;
5164 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005165
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005166 if (use_file_for_in)
5167 {
5168 char_u *fname = options->jo_io_name[PART_IN];
5169
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005170 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5171 FILE_SHARE_READ | FILE_SHARE_WRITE,
5172 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5173 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005175 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005176 goto failed;
5177 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005178 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005179 else if (!use_null_for_in
5180 && (!create_pipe_pair(ifd)
5181 || !SetHandleInformation(ifd[1], 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_out)
5185 {
5186 char_u *fname = options->jo_io_name[PART_OUT];
5187
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005188 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5189 FILE_SHARE_READ | FILE_SHARE_WRITE,
5190 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5191 if (ofd[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_null_for_out &&
5198 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005199 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005200 goto failed;
5201
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005202 if (use_file_for_err)
5203 {
5204 char_u *fname = options->jo_io_name[PART_ERR];
5205
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005206 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5207 FILE_SHARE_READ | FILE_SHARE_WRITE,
5208 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5209 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005211 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005212 goto failed;
5213 }
5214 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005215 else if (!use_out_for_err && !use_null_for_err &&
5216 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005217 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005218 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005219
Bram Moolenaard8070362016-02-15 21:56:54 +01005220 si.dwFlags |= STARTF_USESTDHANDLES;
5221 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005222 si.hStdOutput = ofd[1];
5223 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5224
5225 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5226 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005227 if (options->jo_set & JO_CHANNEL)
5228 {
5229 channel = options->jo_channel;
5230 if (channel != NULL)
5231 ++channel->ch_refcount;
5232 }
5233 else
5234 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005235 if (channel == NULL)
5236 goto failed;
5237 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005238
5239 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005240 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005241 CREATE_DEFAULT_ERROR_MODE |
5242 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005243 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005244 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005245 &si, &pi,
5246 ga.ga_data,
5247 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005248 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005249 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005250 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005251 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005252 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005253
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005254 ga_clear(&ga);
5255
Bram Moolenaar14207f42016-10-27 21:13:10 +02005256 if (!AssignProcessToJobObject(jo, pi.hProcess))
5257 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005258 // if failing, switch the way to terminate
5259 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005260 CloseHandle(jo);
5261 jo = NULL;
5262 }
5263 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005264 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005265 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005266 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005267 job->jv_status = JOB_STARTED;
5268
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005269 CloseHandle(ifd[0]);
5270 CloseHandle(ofd[1]);
5271 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005272 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005273
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005274 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005275 if (channel != NULL)
5276 {
5277 channel_set_pipes(channel,
5278 use_file_for_in || use_null_for_in
5279 ? INVALID_FD : (sock_T)ifd[1],
5280 use_file_for_out || use_null_for_out
5281 ? INVALID_FD : (sock_T)ofd[0],
5282 use_out_for_err || use_file_for_err || use_null_for_err
5283 ? INVALID_FD : (sock_T)efd[0]);
5284 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005285 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005286 return;
5287
5288failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005289 CloseHandle(ifd[0]);
5290 CloseHandle(ofd[0]);
5291 CloseHandle(efd[0]);
5292 CloseHandle(ifd[1]);
5293 CloseHandle(ofd[1]);
5294 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005295 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005296 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005297}
5298
5299 char *
5300mch_job_status(job_T *job)
5301{
5302 DWORD dwExitCode = 0;
5303
Bram Moolenaar76467df2016-02-12 19:30:26 +01005304 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5305 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005306 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005307 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005308 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005309 {
5310 ch_log(job->jv_channel, "Job ended");
5311 job->jv_status = JOB_ENDED;
5312 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005313 return "dead";
5314 }
5315 return "run";
5316}
5317
Bram Moolenaar97792de2016-10-15 18:36:49 +02005318 job_T *
5319mch_detect_ended_job(job_T *job_list)
5320{
5321 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5322 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5323 job_T *job = job_list;
5324
5325 while (job != NULL)
5326 {
5327 DWORD n;
5328 DWORD result;
5329
5330 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5331 && job != NULL; job = job->jv_next)
5332 {
5333 if (job->jv_status == JOB_STARTED)
5334 {
5335 jobHandles[n] = job->jv_proc_info.hProcess;
5336 jobArray[n] = job;
5337 ++n;
5338 }
5339 }
5340 if (n == 0)
5341 continue;
5342 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5343 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5344 {
5345 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5346
5347 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5348 return wait_job;
5349 }
5350 }
5351 return NULL;
5352}
5353
Bram Moolenaarfb630902016-10-29 14:55:00 +02005354 static BOOL
5355terminate_all(HANDLE process, int code)
5356{
5357 PROCESSENTRY32 pe;
5358 HANDLE h = INVALID_HANDLE_VALUE;
5359 DWORD pid = GetProcessId(process);
5360
5361 if (pid != 0)
5362 {
5363 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5364 if (h != INVALID_HANDLE_VALUE)
5365 {
5366 pe.dwSize = sizeof(PROCESSENTRY32);
5367 if (!Process32First(h, &pe))
5368 goto theend;
5369
5370 do
5371 {
5372 if (pe.th32ParentProcessID == pid)
5373 {
5374 HANDLE ph = OpenProcess(
5375 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5376 if (ph != NULL)
5377 {
5378 terminate_all(ph, code);
5379 CloseHandle(ph);
5380 }
5381 }
5382 } while (Process32Next(h, &pe));
5383
5384 CloseHandle(h);
5385 }
5386 }
5387
5388theend:
5389 return TerminateProcess(process, code);
5390}
5391
5392/*
5393 * Send a (deadly) signal to "job".
5394 * Return FAIL if it didn't work.
5395 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005396 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005397mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005398{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005399 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005400
Bram Moolenaar923d9262016-02-25 20:56:01 +01005401 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005402 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005403 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005404 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005405 {
5406 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5407 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005408 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005409 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005410 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005411 }
5412
5413 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5414 return FAIL;
5415 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005416 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5417 job->jv_proc_info.dwProcessId)
5418 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005419 FreeConsole();
5420 return ret;
5421}
5422
5423/*
5424 * Clear the data related to "job".
5425 */
5426 void
5427mch_clear_job(job_T *job)
5428{
5429 if (job->jv_status != JOB_FAILED)
5430 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005431 if (job->jv_job_object != NULL)
5432 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005433 CloseHandle(job->jv_proc_info.hProcess);
5434 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005435}
5436#endif
5437
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005439#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441/*
5442 * Start termcap mode
5443 */
5444 static void
5445termcap_mode_start(void)
5446{
5447 DWORD cmodein;
5448
5449 if (g_fTermcapMode)
5450 return;
5451
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005452 if (!p_rs && USE_VTP)
5453 vtp_printf("\033[?1049h");
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 SaveConsoleBuffer(&g_cbNonTermcap);
5456
5457 if (g_cbTermcap.IsValid)
5458 {
5459 /*
5460 * We've been in termcap mode before. Restore certain screen
5461 * characteristics, including the buffer size and the window
5462 * size. Since we will be redrawing the screen, we don't need
5463 * to restore the actual contents of the buffer.
5464 */
5465 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005466 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5468 Rows = g_cbTermcap.Info.dwSize.Y;
5469 Columns = g_cbTermcap.Info.dwSize.X;
5470 }
5471 else
5472 {
5473 /*
5474 * This is our first time entering termcap mode. Clear the console
5475 * screen buffer, and resize the buffer to match the current window
5476 * size. We will use this as the size of our editing environment.
5477 */
5478 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005479 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5481 }
5482
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005483# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (g_fMouseActive)
5489 cmodein |= ENABLE_MOUSE_INPUT;
5490 else
5491 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 cmodein |= ENABLE_WINDOW_INPUT;
5493 SetConsoleMode(g_hConIn, cmodein);
5494
5495 redraw_later_clear();
5496 g_fTermcapMode = TRUE;
5497}
5498
5499
5500/*
5501 * End termcap mode
5502 */
5503 static void
5504termcap_mode_end(void)
5505{
5506 DWORD cmodein;
5507 ConsoleBuffer *cb;
5508 COORD coord;
5509 DWORD dwDummy;
5510
5511 if (!g_fTermcapMode)
5512 return;
5513
5514 SaveConsoleBuffer(&g_cbTermcap);
5515
5516 GetConsoleMode(g_hConIn, &cmodein);
5517 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5518 SetConsoleMode(g_hConIn, cmodein);
5519
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005520# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005521 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005522# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005524# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005525 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005526 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 SetConsoleCursorInfo(g_hConOut, &g_cci);
5528
5529 if (p_rs || exiting)
5530 {
5531 /*
5532 * Clear anything that happens to be on the current line.
5533 */
5534 coord.X = 0;
5535 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5536 FillConsoleOutputCharacter(g_hConOut, ' ',
5537 cb->Info.dwSize.X, coord, &dwDummy);
5538 /*
5539 * The following is just for aesthetics. If we are exiting without
5540 * restoring the screen, then we want to have a prompt string
5541 * appear at the bottom line. However, the command interpreter
5542 * seems to always advance the cursor one line before displaying
5543 * the prompt string, which causes the screen to scroll. To
5544 * counter this, move the cursor up one line before exiting.
5545 */
5546 if (exiting && !p_rs)
5547 coord.Y--;
5548 /*
5549 * Position the cursor at the leftmost column of the desired row.
5550 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005551 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005554 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005555 vtp_printf("\033[?1049l");
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 g_fTermcapMode = FALSE;
5558}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005559#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005562#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 void
5564mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005565 char_u *s UNUSED,
5566 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005568 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571#else
5572
5573/*
5574 * clear `n' chars, starting from `coord'
5575 */
5576 static void
5577clear_chars(
5578 COORD coord,
5579 DWORD n)
5580{
5581 DWORD dwDummy;
5582
5583 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005584
5585 if (!USE_VTP)
5586 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5587 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005588 {
5589 set_console_color_rgb();
5590 gotoxy(coord.X + 1, coord.Y + 1);
5591 vtp_printf("\033[%dX", n);
5592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593}
5594
5595
5596/*
5597 * Clear the screen
5598 */
5599 static void
5600clear_screen(void)
5601{
5602 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005603
5604 if (!USE_VTP)
5605 clear_chars(g_coord, Rows * Columns);
5606 else
5607 {
5608 set_console_color_rgb();
5609 gotoxy(1, 1);
5610 vtp_printf("\033[2J");
5611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
5614
5615/*
5616 * Clear to end of display
5617 */
5618 static void
5619clear_to_end_of_display(void)
5620{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005621 COORD save = g_coord;
5622
5623 if (!USE_VTP)
5624 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005626 else
5627 {
5628 set_console_color_rgb();
5629 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5630 vtp_printf("\033[0J");
5631
5632 gotoxy(save.X + 1, save.Y + 1);
5633 g_coord = save;
5634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635}
5636
5637
5638/*
5639 * Clear to end of line
5640 */
5641 static void
5642clear_to_end_of_line(void)
5643{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005644 COORD save = g_coord;
5645
5646 if (!USE_VTP)
5647 clear_chars(g_coord, Columns - g_coord.X);
5648 else
5649 {
5650 set_console_color_rgb();
5651 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5652 vtp_printf("\033[0K");
5653
5654 gotoxy(save.X + 1, save.Y + 1);
5655 g_coord = save;
5656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659
5660/*
5661 * Scroll the scroll region up by `cLines' lines
5662 */
5663 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005664scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 COORD oldcoord = g_coord;
5667
5668 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5669 delete_lines(cLines);
5670
5671 g_coord = oldcoord;
5672}
5673
5674
5675/*
5676 * Set the scroll region
5677 */
5678 static void
5679set_scroll_region(
5680 unsigned left,
5681 unsigned top,
5682 unsigned right,
5683 unsigned bottom)
5684{
5685 if (left >= right
5686 || top >= bottom
5687 || right > (unsigned) Columns - 1
5688 || bottom > (unsigned) Rows - 1)
5689 return;
5690
5691 g_srScrollRegion.Left = left;
5692 g_srScrollRegion.Top = top;
5693 g_srScrollRegion.Right = right;
5694 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005695}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005696
Bram Moolenaar6982f422019-02-16 16:48:01 +01005697 static void
5698set_scroll_region_tb(
5699 unsigned top,
5700 unsigned bottom)
5701{
5702 if (top >= bottom || bottom > (unsigned)Rows - 1)
5703 return;
5704
5705 g_srScrollRegion.Top = top;
5706 g_srScrollRegion.Bottom = bottom;
5707}
5708
5709 static void
5710set_scroll_region_lr(
5711 unsigned left,
5712 unsigned right)
5713{
5714 if (left >= right || right > (unsigned)Columns - 1)
5715 return;
5716
5717 g_srScrollRegion.Left = left;
5718 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
5720
5721
5722/*
5723 * Insert `cLines' lines at the current cursor position
5724 */
5725 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005726insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005728 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 COORD dest;
5730 CHAR_INFO fill;
5731
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005732 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5733
Bram Moolenaar6982f422019-02-16 16:48:01 +01005734 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 dest.Y = g_coord.Y + cLines;
5736
Bram Moolenaar6982f422019-02-16 16:48:01 +01005737 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 source.Top = g_coord.Y;
5739 source.Right = g_srScrollRegion.Right;
5740 source.Bottom = g_srScrollRegion.Bottom - cLines;
5741
Bram Moolenaar6982f422019-02-16 16:48:01 +01005742 clip.Left = g_srScrollRegion.Left;
5743 clip.Top = g_coord.Y;
5744 clip.Right = g_srScrollRegion.Right;
5745 clip.Bottom = g_srScrollRegion.Bottom;
5746
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005747 fill.Char.AsciiChar = ' ';
5748 if (!USE_VTP)
5749 fill.Attributes = g_attrCurrent;
5750 else
5751 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005753 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005754
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005755 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5756
Bram Moolenaar6982f422019-02-16 16:48:01 +01005757 // Here we have to deal with a win32 console flake: If the scroll
5758 // region looks like abc and we scroll c to a and fill with d we get
5759 // cbd... if we scroll block c one line at a time to a, we get cdd...
5760 // vim expects cdd consistently... So we have to deal with that
5761 // here... (this also occurs scrolling the same way in the other
5762 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 if (source.Bottom < dest.Y)
5765 {
5766 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005767 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
Bram Moolenaar6982f422019-02-16 16:48:01 +01005769 coord.X = source.Left;
5770 for (i = clip.Top; i < dest.Y; ++i)
5771 {
5772 coord.Y = i;
5773 clear_chars(coord, source.Right - source.Left + 1);
5774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776}
5777
5778
5779/*
5780 * Delete `cLines' lines at the current cursor position
5781 */
5782 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005783delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005785 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 COORD dest;
5787 CHAR_INFO fill;
5788 int nb;
5789
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005790 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5791
Bram Moolenaar6982f422019-02-16 16:48:01 +01005792 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 dest.Y = g_coord.Y;
5794
Bram Moolenaar6982f422019-02-16 16:48:01 +01005795 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 source.Top = g_coord.Y + cLines;
5797 source.Right = g_srScrollRegion.Right;
5798 source.Bottom = g_srScrollRegion.Bottom;
5799
Bram Moolenaar6982f422019-02-16 16:48:01 +01005800 clip.Left = g_srScrollRegion.Left;
5801 clip.Top = g_coord.Y;
5802 clip.Right = g_srScrollRegion.Right;
5803 clip.Bottom = g_srScrollRegion.Bottom;
5804
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005805 fill.Char.AsciiChar = ' ';
5806 if (!USE_VTP)
5807 fill.Attributes = g_attrCurrent;
5808 else
5809 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005811 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005812
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005813 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5814
Bram Moolenaar6982f422019-02-16 16:48:01 +01005815 // Here we have to deal with a win32 console flake; See insert_lines()
5816 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 nb = dest.Y + (source.Bottom - source.Top) + 1;
5819
5820 if (nb < source.Top)
5821 {
5822 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005823 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar6982f422019-02-16 16:48:01 +01005825 coord.X = source.Left;
5826 for (i = nb; i < clip.Bottom; ++i)
5827 {
5828 coord.Y = i;
5829 clear_chars(coord, source.Right - source.Left + 1);
5830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
5832}
5833
5834
5835/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005836 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 */
5838 static void
5839gotoxy(
5840 unsigned x,
5841 unsigned y)
5842{
5843 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5844 return;
5845
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005846 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005847 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01005848 // There are reports of double-width characters not displayed
5849 // correctly. This workaround should fix it, similar to how it's done
5850 // for VTP.
5851 g_coord.X = 0;
5852 SetConsoleCursorPosition(g_hConOut, g_coord);
5853
Bram Moolenaar2313b612019-09-27 14:14:32 +02005854 // external cursor coords are 1-based; internal are 0-based
5855 g_coord.X = x - 1;
5856 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005857 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005858 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005859 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005860 {
5861 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005862 // destruction. Insider build bug. Always enabled because it's cheap
5863 // and avoids mistakes with recognizing the build.
5864 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005865
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005866 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005867
5868 g_coord.X = x - 1;
5869 g_coord.Y = y - 1;
5870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871}
5872
5873
5874/*
5875 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005876 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 */
5878 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005879textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005881 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 SetConsoleTextAttribute(g_hConOut, wAttr);
5884}
5885
5886
5887 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005888textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005890 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005892 if (!USE_VTP)
5893 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5894 else
5895 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896}
5897
5898
5899 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005900textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005902 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005904 if (!USE_VTP)
5905 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5906 else
5907 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908}
5909
5910
5911/*
5912 * restore the default text attribute (whatever we started with)
5913 */
5914 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005915normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005917 if (!USE_VTP)
5918 textattr(g_attrDefault);
5919 else
5920 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921}
5922
5923
5924static WORD g_attrPreStandout = 0;
5925
5926/*
5927 * Make the text standout, by brightening it
5928 */
5929 static void
5930standout(void)
5931{
5932 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5935}
5936
5937
5938/*
5939 * Turn off standout mode
5940 */
5941 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005942standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005946
5947 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948}
5949
5950
5951/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005952 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 */
5954 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005955mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 char_u *p;
5958 int n;
5959
5960 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5961 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005962 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005963# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005964 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005965# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005966 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 p = T_ME + 2;
5969 n = getdigits(&p);
5970 if (*p == 'm' && n > 0)
5971 {
5972 cterm_normal_fg_color = (n & 0xf) + 1;
5973 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5974 }
5975 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005976# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005977 cterm_normal_fg_gui_color = INVALCOLOR;
5978 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
5982
5983/*
5984 * visual bell: flash the screen
5985 */
5986 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005987visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 COORD coordOrigin = {0, 0};
5990 WORD attrFlash = ~g_attrCurrent & 0xff;
5991
5992 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005993 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
5995 if (oldattrs == NULL)
5996 return;
5997 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5998 coordOrigin, &dwDummy);
5999 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6000 coordOrigin, &dwDummy);
6001
Bram Moolenaar0f873732019-12-05 20:28:46 +01006002 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006003 if (!USE_VTP)
6004 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 coordOrigin, &dwDummy);
6006 vim_free(oldattrs);
6007}
6008
6009
6010/*
6011 * Make the cursor visible or invisible
6012 */
6013 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006014cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006017
6018 if (USE_VTP)
6019 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6020
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006021# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026
6027/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006028 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006029 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006031 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006033 char_u *pchBuf,
6034 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006036 COORD coord = g_coord;
6037 DWORD written;
6038 DWORD n, cchwritten, cells;
6039 static WCHAR *unicodebuf = NULL;
6040 static int unibuflen = 0;
6041 int length;
6042 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006044 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6045 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006046 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006047 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006048 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006049 unibuflen = length;
6050 }
6051 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6052 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006054 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006055
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006056 if (!USE_VTP)
6057 {
6058 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6059 coord, &written);
6060 // When writing fails or didn't write a single character, pretend one
6061 // character was written, otherwise we get stuck.
6062 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6063 coord, &cchwritten) == 0
6064 || cchwritten == 0 || cchwritten == (DWORD)-1)
6065 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006066 }
6067 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006068 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006069 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6070 NULL) == 0 || cchwritten == 0)
6071 cchwritten = 1;
6072 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006073
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006074 if (cchwritten == length)
6075 {
6076 written = cbToWrite;
6077 g_coord.X += (SHORT)cells;
6078 }
6079 else
6080 {
6081 char_u *p = pchBuf;
6082 for (n = 0; n < cchwritten; n++)
6083 MB_CPTR_ADV(p);
6084 written = p - pchBuf;
6085 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 while (g_coord.X > g_srScrollRegion.Right)
6089 {
6090 g_coord.X -= (SHORT) Columns;
6091 if (g_coord.Y < g_srScrollRegion.Bottom)
6092 g_coord.Y++;
6093 }
6094
6095 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6096
6097 return written;
6098}
6099
6100
6101/*
6102 * mch_write(): write the output buffer to the screen, translating ESC
6103 * sequences into calls to console output routines.
6104 */
6105 void
6106mch_write(
6107 char_u *s,
6108 int len)
6109{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006110# ifdef VIMDLL
6111 if (gui.in_use)
6112 return;
6113# endif
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 s[len] = NUL;
6116
6117 if (!term_console)
6118 {
6119 write(1, s, (unsigned)len);
6120 return;
6121 }
6122
Bram Moolenaar0f873732019-12-05 20:28:46 +01006123 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 while (len--)
6125 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006126 // optimization: use one single write_chars for runs of text,
6127 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006128 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 if (p_wd)
6131 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006132 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (prefix != 0)
6134 prefix = 1;
6135 }
6136
6137 if (prefix != 0)
6138 {
6139 DWORD nWritten;
6140
6141 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006142# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (fdDump)
6144 {
6145 fputc('>', fdDump);
6146 fwrite(s, sizeof(char_u), nWritten, fdDump);
6147 fputs("<\n", fdDump);
6148 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 len -= (nWritten - 1);
6151 s += nWritten;
6152 }
6153 else if (s[0] == '\n')
6154 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006155 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (g_coord.Y == g_srScrollRegion.Bottom)
6157 {
6158 scroll(1);
6159 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6160 }
6161 else
6162 {
6163 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6164 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006165# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (fdDump)
6167 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 s++;
6170 }
6171 else if (s[0] == '\r')
6172 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006173 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006175# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (fdDump)
6177 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 s++;
6180 }
6181 else if (s[0] == '\b')
6182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (g_coord.X > g_srScrollRegion.Left)
6185 g_coord.X--;
6186 else if (g_coord.Y > g_srScrollRegion.Top)
6187 {
6188 g_coord.X = g_srScrollRegion.Right;
6189 g_coord.Y--;
6190 }
6191 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006192# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (fdDump)
6194 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 s++;
6197 }
6198 else if (s[0] == '\a')
6199 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006200 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006202# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (fdDump)
6204 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 s++;
6207 }
6208 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6209 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006210# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006211 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006212# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006213 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006214 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 switch (s[2])
6217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 case '0': case '1': case '2': case '3': case '4':
6219 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006220 p = s + 1;
6221 do
6222 {
6223 ++p;
6224 args[argc] = getdigits(&p);
6225 argc += (argc < 15) ? 1 : 0;
6226 if (p > s + len)
6227 break;
6228 } while (*p == ';');
6229
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (p > s + len)
6231 break;
6232
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006233 arg1 = args[0];
6234 arg2 = args[1];
6235 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006237 if (argc == 1 && args[0] == 0)
6238 normvideo();
6239 else if (argc == 1)
6240 {
6241 if (USE_VTP)
6242 textcolor((WORD) arg1);
6243 else
6244 textattr((WORD) arg1);
6245 }
6246 else if (USE_VTP)
6247 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006249 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006251 gotoxy(arg2, arg1);
6252 }
6253 else if (argc == 2 && *p == 'r')
6254 {
6255 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6256 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006257 else if (argc == 2 && *p == 'R')
6258 {
6259 set_scroll_region_tb(arg1, arg2);
6260 }
6261 else if (argc == 2 && *p == 'V')
6262 {
6263 set_scroll_region_lr(arg1, arg2);
6264 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006265 else if (argc == 1 && *p == 'A')
6266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gotoxy(g_coord.X + 1,
6268 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6269 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006270 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 textbackground((WORD) arg1);
6273 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006274 else if (argc == 1 && *p == 'C')
6275 {
6276 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6277 g_coord.Y + 1);
6278 }
6279 else if (argc == 1 && *p == 'f')
6280 {
6281 textcolor((WORD) arg1);
6282 }
6283 else if (argc == 1 && *p == 'H')
6284 {
6285 gotoxy(1, arg1);
6286 }
6287 else if (argc == 1 && *p == 'L')
6288 {
6289 insert_lines(arg1);
6290 }
6291 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293 delete_lines(arg1);
6294 }
6295
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006296 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 s = p + 1;
6298 break;
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 gotoxy(g_coord.X + 1,
6302 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6303 goto got3;
6304
6305 case 'B':
6306 visual_bell();
6307 goto got3;
6308
6309 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6311 g_coord.Y + 1);
6312 goto got3;
6313
6314 case 'E':
6315 termcap_mode_end();
6316 goto got3;
6317
6318 case 'F':
6319 standout();
6320 goto got3;
6321
6322 case 'f':
6323 standend();
6324 goto got3;
6325
6326 case 'H':
6327 gotoxy(1, 1);
6328 goto got3;
6329
6330 case 'j':
6331 clear_to_end_of_display();
6332 goto got3;
6333
6334 case 'J':
6335 clear_screen();
6336 goto got3;
6337
6338 case 'K':
6339 clear_to_end_of_line();
6340 goto got3;
6341
6342 case 'L':
6343 insert_lines(1);
6344 goto got3;
6345
6346 case 'M':
6347 delete_lines(1);
6348 goto got3;
6349
6350 case 'S':
6351 termcap_mode_start();
6352 goto got3;
6353
6354 case 'V':
6355 cursor_visible(TRUE);
6356 goto got3;
6357
6358 case 'v':
6359 cursor_visible(FALSE);
6360 goto got3;
6361
6362 got3:
6363 s += 3;
6364 len -= 2;
6365 }
6366
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006367# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 if (fdDump)
6369 {
6370 fputs("ESC | ", fdDump);
6371 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6372 fputc('\n', fdDump);
6373 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376 else
6377 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006378 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 DWORD nWritten;
6380
6381 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006382# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (fdDump)
6384 {
6385 fputc('>', fdDump);
6386 fwrite(s, sizeof(char_u), nWritten, fdDump);
6387 fputs("<\n", fdDump);
6388 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
6391 len -= (nWritten - 1);
6392 s += nWritten;
6393 }
6394 }
6395
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006396# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (fdDump)
6398 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400}
6401
Bram Moolenaar0f873732019-12-05 20:28:46 +01006402#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404
6405/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006406 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 */
6408 void
6409mch_delay(
6410 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006411 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006413#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006414 Sleep((int)msec); // never wait for input
6415#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006416# ifdef VIMDLL
6417 if (gui.in_use)
6418 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006419 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006420 return;
6421 }
6422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006424# ifdef FEAT_MZSCHEME
6425 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6426 {
6427 int towait = p_mzq;
6428
Bram Moolenaar0f873732019-12-05 20:28:46 +01006429 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006430 while (msec > 0)
6431 {
6432 mzvim_check_threads();
6433 if (msec < towait)
6434 towait = msec;
6435 Sleep(towait);
6436 msec -= towait;
6437 }
6438 }
6439 else
6440# endif
6441 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006443 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444#endif
6445}
6446
6447
6448/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006449 * This version of remove is not scared by a readonly (backup) file.
6450 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 * Return 0 for success, -1 for failure.
6452 */
6453 int
6454mch_remove(char_u *name)
6455{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006456 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int n;
6458
Bram Moolenaar203258c2016-01-17 22:15:16 +01006459 /*
6460 * On Windows, deleting a directory's symbolic link is done by
6461 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6462 */
6463 if (mch_isdir(name) && mch_is_symbolic_link(name))
6464 return mch_rmdir(name);
6465
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006466 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006468 wn = enc_to_utf16(name, NULL);
6469 if (wn == NULL)
6470 return -1;
6471
6472 n = DeleteFileW(wn) ? 0 : -1;
6473 vim_free(wn);
6474 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475}
6476
6477
6478/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006479 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
6481 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006482mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006484#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6485# ifdef VIMDLL
6486 if (!gui.in_use)
6487# endif
6488 if (g_fCtrlCPressed || g_fCBrkPressed)
6489 {
6490 ctrl_break_was_pressed = g_fCBrkPressed;
6491 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6492 got_int = TRUE;
6493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#endif
6495}
6496
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006498#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006499
6500/*
6501 * How much main memory in KiB that can be used by VIM.
6502 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006503 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006504mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006505{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006506 MEMORYSTATUSEX ms;
6507
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508 // Need to use GlobalMemoryStatusEx() when there is more memory than
6509 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006510 ms.dwLength = sizeof(MEMORYSTATUSEX);
6511 GlobalMemoryStatusEx(&ms);
6512 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006513 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006514 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006515 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006516 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006517 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006518 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006519 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006520 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006521 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006522 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006523 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006527 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6529 * file whose short file name is "FOO.BAR" (its long file name will
6530 * be correct: "foo.bar~"). Because a file can be accessed by
6531 * either its SFN or its LFN, "foo.bar" has effectively been
6532 * renamed to "foo.bar", which is not at all what was wanted. This
6533 * seems to happen only when renaming files with three-character
6534 * extensions by appending a suffix that does not include ".".
6535 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6536 *
6537 * There is another problem, which isn't really a bug but isn't right either:
6538 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6539 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6540 * service pack 6. Doesn't seem to happen on Windows 98.
6541 *
6542 * Like rename(), returns 0 upon success, non-zero upon failure.
6543 * Should probably set errno appropriately when errors occur.
6544 */
6545 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006546mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006548 WCHAR *p;
6549 int i;
6550 WCHAR szTempFile[_MAX_PATH + 1];
6551 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006554 // No need to play tricks unless the file name contains a "~" as the
6555 // seventh character.
6556 p = wold;
6557 for (i = 0; wold[i] != NUL; ++i)
6558 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6559 && wold[i + 1] != 0)
6560 p = wold + i + 1;
6561 if ((int)(wold + i - p) < 8 || p[6] != '~')
6562 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006564 // Get base path of new file name. Undocumented feature: If pszNewFile is
6565 // a directory, no error is returned and pszFilePart will be NULL.
6566 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006568 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006570 // Get (and create) a unique temporary file name in directory of new file
6571 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 return -2;
6573
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006574 // blow the temp file away
6575 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 return -3;
6577
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006578 // rename old file to the temp file
6579 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 return -4;
6581
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006582 // now create an empty file called pszOldFile; this prevents the operating
6583 // system using pszOldFile as an alias (SFN) if we're renaming within the
6584 // same directory. For example, we're editing a file called
6585 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6586 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6587 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6588 // cause all sorts of problems later in buf_write(). So, we create an
6589 // empty file called filena~1.txt and the system will have to find some
6590 // other SFN for filena~1.txt~, such as filena~2.txt
6591 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6593 return -5;
6594 if (!CloseHandle(hf))
6595 return -6;
6596
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006597 // rename the temp file to the new file
6598 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006600 // Renaming failed. Rename the file back to its old name, so that it
6601 // looks like nothing happened.
6602 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 return -7;
6604 }
6605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006606 // Seems to be left around on Novell filesystems
6607 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006609 // finally, remove the empty old file
6610 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 return -8;
6612
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006613 return 0;
6614}
6615
6616
6617/*
6618 * Converts the filenames to UTF-16, then call mch_wrename().
6619 * Like rename(), returns 0 upon success, non-zero upon failure.
6620 */
6621 int
6622mch_rename(
6623 const char *pszOldFile,
6624 const char *pszNewFile)
6625{
6626 WCHAR *wold = NULL;
6627 WCHAR *wnew = NULL;
6628 int retval = -1;
6629
6630 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6631 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6632 if (wold != NULL && wnew != NULL)
6633 retval = mch_wrename(wold, wnew);
6634 vim_free(wold);
6635 vim_free(wnew);
6636 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637}
6638
6639/*
6640 * Get the default shell for the current hardware platform
6641 */
6642 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006643default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006645 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
6648/*
6649 * mch_access() extends access() to do more detailed check on network drives.
6650 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6651 */
6652 int
6653mch_access(char *n, int p)
6654{
6655 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006656 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006657 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006659 wn = enc_to_utf16((char_u *)n, NULL);
6660 if (wn == NULL)
6661 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006663 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 if (p & R_OK)
6668 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 // Read check is performed by seeing if we can do a find file on
6670 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006671 int i;
6672 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006674 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6675 TempNameW[i] = wn[i];
6676 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6677 TempNameW[i++] = '\\';
6678 TempNameW[i++] = '*';
6679 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006681 hFile = FindFirstFileW(TempNameW, &d);
6682 if (hFile == INVALID_HANDLE_VALUE)
6683 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
6687
6688 if (p & W_OK)
6689 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006690 // Trying to create a temporary file in the directory should catch
6691 // directories on read-only network shares. However, in
6692 // directories whose ACL allows writes but denies deletes will end
6693 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006694 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6695 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006696 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006697 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 }
6700 else
6701 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006702 // Don't consider a file read-only if another process has opened it.
6703 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6704
Bram Moolenaar0f873732019-12-05 20:28:46 +01006705 // Trying to open the file for the required access does ACL, read-only
6706 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006707 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6708 | ((p & R_OK) ? GENERIC_READ : 0);
6709
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006710 hFile = CreateFileW(wn, access_mode, share_mode,
6711 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (hFile == INVALID_HANDLE_VALUE)
6713 goto getout;
6714 CloseHandle(hFile);
6715 }
6716
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 return retval;
6721}
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006724 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 */
6726 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006727mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 WCHAR *wn;
6730 int f;
6731
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006732 wn = enc_to_utf16((char_u *)name, NULL);
6733 if (wn == NULL)
6734 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006736 f = _wopen(wn, flags, mode);
6737 vim_free(wn);
6738 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
6741/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006742 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 */
6744 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006745mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 WCHAR *wn, *wm;
6748 FILE *f = NULL;
6749
Bram Moolenaara12a1612019-01-24 16:39:02 +01006750#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006751 // Work around an annoying assertion in the Microsoft debug CRT
6752 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006753 char newMode = mode[strlen(mode) - 1];
6754 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006755
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006756 _get_fmode(&oldMode);
6757 if (newMode == 't')
6758 _set_fmode(_O_TEXT);
6759 else if (newMode == 'b')
6760 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006761#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006762 wn = enc_to_utf16((char_u *)name, NULL);
6763 wm = enc_to_utf16((char_u *)mode, NULL);
6764 if (wn != NULL && wm != NULL)
6765 f = _wfopen(wn, wm);
6766 vim_free(wn);
6767 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006768
Bram Moolenaara12a1612019-01-24 16:39:02 +01006769#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006770 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006771#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006772 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775/*
6776 * SUB STREAM (aka info stream) handling:
6777 *
6778 * NTFS can have sub streams for each file. Normal contents of file is
6779 * stored in the main stream, and extra contents (author information and
6780 * title and so on) can be stored in sub stream. After Windows 2000, user
6781 * can access and store those informations in sub streams via explorer's
6782 * property menuitem in right click menu. Those informations in sub streams
6783 * were lost when copying only the main stream. So we have to copy sub
6784 * streams.
6785 *
6786 * Incomplete explanation:
6787 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6788 * More useful info and an example:
6789 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6790 */
6791
6792/*
6793 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6794 * and write to stream "substream" of file "to".
6795 * Errors are ignored.
6796 */
6797 static void
6798copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6799{
6800 HANDLE hTo;
6801 WCHAR *to_name;
6802
6803 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6804 wcscpy(to_name, to);
6805 wcscat(to_name, substream);
6806
6807 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6808 FILE_ATTRIBUTE_NORMAL, NULL);
6809 if (hTo != INVALID_HANDLE_VALUE)
6810 {
6811 long done;
6812 DWORD todo;
6813 DWORD readcnt, written;
6814 char buf[4096];
6815
Bram Moolenaar0f873732019-12-05 20:28:46 +01006816 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 for (done = 0; done < len; done += written)
6818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006820 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6821 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6823 FALSE, FALSE, context)
6824 || readcnt != todo
6825 || !WriteFile(hTo, buf, todo, &written, NULL)
6826 || written != todo)
6827 break;
6828 }
6829 CloseHandle(hTo);
6830 }
6831
6832 free(to_name);
6833}
6834
6835/*
6836 * Copy info streams from file "from" to file "to".
6837 */
6838 static void
6839copy_infostreams(char_u *from, char_u *to)
6840{
6841 WCHAR *fromw;
6842 WCHAR *tow;
6843 HANDLE sh;
6844 WIN32_STREAM_ID sid;
6845 int headersize;
6846 WCHAR streamname[_MAX_PATH];
6847 DWORD readcount;
6848 void *context = NULL;
6849 DWORD lo, hi;
6850 int len;
6851
Bram Moolenaar0f873732019-12-05 20:28:46 +01006852 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006853 fromw = enc_to_utf16(from, NULL);
6854 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 if (fromw != NULL && tow != NULL)
6856 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006857 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6859 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6860 if (sh != INVALID_HANDLE_VALUE)
6861 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006862 // Use BackupRead() to find the info streams. Repeat until we
6863 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 for (;;)
6865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // Get the header to find the length of the stream name. If
6867 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006869 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6871 &readcount, FALSE, FALSE, &context)
6872 || readcount == 0)
6873 break;
6874
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // We only deal with streams that have a name. The normal
6876 // file data appears to be without a name, even though docs
6877 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (sid.dwStreamNameSize > 0)
6879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006880 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 if (!BackupRead(sh, (LPBYTE)streamname,
6882 sid.dwStreamNameSize,
6883 &readcount, FALSE, FALSE, &context))
6884 break;
6885
Bram Moolenaar0f873732019-12-05 20:28:46 +01006886 // Copy an info stream with a name ":anything:$DATA".
6887 // Skip "::$DATA", it has no stream name (examples suggest
6888 // it might be used for the normal file contents).
6889 // Note that BackupRead() counts bytes, but the name is in
6890 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 len = readcount / sizeof(WCHAR);
6892 streamname[len] = 0;
6893 if (len > 7 && wcsicmp(streamname + len - 6,
6894 L":$DATA") == 0)
6895 {
6896 streamname[len - 6] = 0;
6897 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006898 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900 }
6901
Bram Moolenaar0f873732019-12-05 20:28:46 +01006902 // Advance to the next stream. We might try seeking too far,
6903 // but BackupSeek() doesn't skip over stream borders, thus
6904 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006905 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 &lo, &hi, &context);
6907 }
6908
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6911
6912 CloseHandle(sh);
6913 }
6914 }
6915 vim_free(fromw);
6916 vim_free(tow);
6917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919/*
6920 * Copy file attributes from file "from" to file "to".
6921 * For Windows NT and later we copy info streams.
6922 * Always returns zero, errors are ignored.
6923 */
6924 int
6925mch_copy_file_attribute(char_u *from, char_u *to)
6926{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006927 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006928 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 return 0;
6930}
6931
6932#if defined(MYRESETSTKOFLW) || defined(PROTO)
6933/*
6934 * Recreate a destroyed stack guard page in win32.
6935 * Written by Benjamin Peterson.
6936 */
6937
Bram Moolenaar0f873732019-12-05 20:28:46 +01006938// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006939# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941/*
6942 * This function does the same thing as _resetstkoflw(), which is only
6943 * available in DevStudio .net and later.
6944 * Returns 0 for failure, 1 for success.
6945 */
6946 int
6947myresetstkoflw(void)
6948{
6949 BYTE *pStackPtr;
6950 BYTE *pGuardPage;
6951 BYTE *pStackBase;
6952 BYTE *pLowestPossiblePage;
6953 MEMORY_BASIC_INFORMATION mbi;
6954 SYSTEM_INFO si;
6955 DWORD nPageSize;
6956 DWORD dummy;
6957
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 GetSystemInfo(&si);
6960 nPageSize = si.dwPageSize;
6961
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 pStackPtr = (BYTE*)_alloca(1);
6964
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6967 return 0;
6968 pStackBase = (BYTE*)mbi.AllocationBase;
6969
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006970 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006972 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006975 // We want the first committed page in the stack Start at the stack
6976 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 BYTE *pBlock = pStackBase;
6978
Bram Moolenaara466c992005-07-09 21:03:22 +00006979 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
6981 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6982 return 0;
6983
6984 pBlock += mbi.RegionSize;
6985
6986 if (mbi.State & MEM_COMMIT)
6987 break;
6988 }
6989
Bram Moolenaar0f873732019-12-05 20:28:46 +01006990 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (mbi.Protect & PAGE_GUARD)
6992 return 1;
6993
Bram Moolenaar0f873732019-12-05 20:28:46 +01006994 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6996 pGuardPage = pLowestPossiblePage;
6997 else
6998 pGuardPage = (BYTE*)mbi.BaseAddress;
6999
Bram Moolenaar0f873732019-12-05 20:28:46 +01007000 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7002 return 0;
7003
Bram Moolenaar0f873732019-12-05 20:28:46 +01007004 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7006 &dummy))
7007 return 0;
7008 }
7009
7010 return 1;
7011}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007014
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007015/*
7016 * The command line arguments in UCS2
7017 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007018static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007019static LPWSTR *ArglistW = NULL;
7020static int global_argc = 0;
7021static char **global_argv;
7022
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023static int used_file_argc = 0; // last argument in global_argv[] used
7024 // for the argument list.
7025static int *used_file_indexes = NULL; // indexes in global_argv[] for
7026 // command line arguments added to
7027 // the argument list
7028static int used_file_count = 0; // nr of entries in used_file_indexes
7029static int used_file_literal = FALSE; // take file names literally
7030static int used_file_full_path = FALSE; // file name was full path
7031static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007032static int used_alist_count = 0;
7033
7034
7035/*
7036 * Get the command line arguments. Unicode version.
7037 * Returns argc. Zero when something fails.
7038 */
7039 int
7040get_cmd_argsW(char ***argvp)
7041{
7042 char **argv = NULL;
7043 int argc = 0;
7044 int i;
7045
Bram Moolenaar14993322014-09-09 12:25:33 +02007046 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007047 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7048 if (ArglistW != NULL)
7049 {
7050 argv = malloc((nArgsW + 1) * sizeof(char *));
7051 if (argv != NULL)
7052 {
7053 argc = nArgsW;
7054 argv[argc] = NULL;
7055 for (i = 0; i < argc; ++i)
7056 {
7057 int len;
7058
Bram Moolenaar0f873732019-12-05 20:28:46 +01007059 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007060 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007061 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007062 (LPSTR *)&argv[i], &len, 0, 0);
7063 if (argv[i] == NULL)
7064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007065 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007066 while (i > 0)
7067 free(argv[--i]);
7068 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007069 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007070 argc = 0;
7071 }
7072 }
7073 }
7074 }
7075
7076 global_argc = argc;
7077 global_argv = argv;
7078 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007079 {
7080 if (used_file_indexes != NULL)
7081 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007082 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007083 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007084
7085 if (argvp != NULL)
7086 *argvp = argv;
7087 return argc;
7088}
7089
7090 void
7091free_cmd_argsW(void)
7092{
7093 if (ArglistW != NULL)
7094 {
7095 GlobalFree(ArglistW);
7096 ArglistW = NULL;
7097 }
7098}
7099
7100/*
7101 * Remember "name" is an argument that was added to the argument list.
7102 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7103 * is called.
7104 */
7105 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007106used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007107{
7108 int i;
7109
7110 if (used_file_indexes == NULL)
7111 return;
7112 for (i = used_file_argc + 1; i < global_argc; ++i)
7113 if (STRCMP(global_argv[i], name) == 0)
7114 {
7115 used_file_argc = i;
7116 used_file_indexes[used_file_count++] = i;
7117 break;
7118 }
7119 used_file_literal = literal;
7120 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007121 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007122}
7123
7124/*
7125 * Remember the length of the argument list as it was. If it changes then we
7126 * leave it alone when 'encoding' is set.
7127 */
7128 void
7129set_alist_count(void)
7130{
7131 used_alist_count = GARGCOUNT;
7132}
7133
7134/*
7135 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7136 * has been changed while starting up. Use the UCS-2 command line arguments
7137 * and convert them to 'encoding'.
7138 */
7139 void
7140fix_arg_enc(void)
7141{
7142 int i;
7143 int idx;
7144 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007145 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007146
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 // Safety checks:
7148 // - if argument count differs between the wide and non-wide argument
7149 // list, something must be wrong.
7150 // - the file name arguments must have been located.
7151 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007152 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007153 || ArglistW == NULL
7154 || used_file_indexes == NULL
7155 || used_file_count == 0
7156 || used_alist_count != GARGCOUNT)
7157 return;
7158
Bram Moolenaar0f873732019-12-05 20:28:46 +01007159 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007160 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007161 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007162 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007163 for (i = 0; i < GARGCOUNT; ++i)
7164 fnum_list[i] = GARGLIST[i].ae_fnum;
7165
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007167 alist_clear(&global_alist);
7168 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007169 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007170
7171 for (i = 0; i < used_file_count; ++i)
7172 {
7173 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007174 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007175 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007176 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007177 int literal = used_file_literal;
7178
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007179#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007180 // When using diff mode may need to concatenate file name to
7181 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007182 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7183 && !mch_isdir(alist_name(&GARGLIST[0])))
7184 {
7185 char_u *r;
7186
7187 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7188 if (r != NULL)
7189 {
7190 vim_free(str);
7191 str = r;
7192 }
7193 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007194#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007195 // Re-use the old buffer by renaming it. When not using literal
7196 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197 if (used_file_literal)
7198 buf_set_name(fnum_list[i], str);
7199
Bram Moolenaar0f873732019-12-05 20:28:46 +01007200 // Check backtick literal. backtick literal is already expanded in
7201 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007202 if (literal == FALSE)
7203 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007204 size_t len = STRLEN(str);
7205
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007206 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7207 literal = TRUE;
7208 }
7209 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007210 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007211 }
7212
7213 if (!used_file_literal)
7214 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007215 // Now expand wildcards in the arguments.
7216 // Temporarily add '(' and ')' to 'isfname'. These are valid
7217 // filename characters but are excluded from 'isfname' to make
7218 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7219 // Also, unset wildignore to not be influenced by this option.
7220 // The arguments specified in command-line should be kept even if
7221 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007222 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007223 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007224 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007225 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007226 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007227 }
7228
Bram Moolenaar0f873732019-12-05 20:28:46 +01007229 // If wildcard expansion failed, we are editing the first file of the
7230 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007231 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7232 {
7233 do_cmdline_cmd((char_u *)":rewind");
7234 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007235 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007236 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007237
7238 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007239}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007240
7241 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007242mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007243{
7244 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007245 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007246
Bram Moolenaar964b3742019-05-24 18:54:09 +02007247 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007248 if (envbuf == NULL)
7249 return -1;
7250
7251 sprintf((char *)envbuf, "%s=%s", var, value);
7252
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007253 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007254
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007255 vim_free(envbuf);
7256 if (p == NULL)
7257 return -1;
7258 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007259#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007260 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007261#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007262 // Unlike Un*x systems, we can free the string for _wputenv().
7263 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007264
7265 return 0;
7266}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007267
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007268/*
7269 * Support for 256 colors and 24-bit colors was added in Windows 10
7270 * version 1703 (Creators update).
7271 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007272#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7273
7274/*
7275 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007276 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007277 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007278#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007279
7280/*
7281 * ConPTY differences between versions, need different logic.
7282 * version 1903 (May 2019 update).
7283 */
7284#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7285
7286/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007287 * version 1909 (November 2019 update).
7288 */
7289#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7290
7291/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007292 * Confirm until this version. Also the logic changes.
7293 * insider preview.
7294 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007295#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007296
7297/*
7298 * Not stable now.
7299 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007300#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007301
7302 static void
7303vtp_flag_init(void)
7304{
7305 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007306#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007307 DWORD mode;
7308 HANDLE out;
7309
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007310# ifdef VIMDLL
7311 if (!gui.in_use)
7312# endif
7313 {
7314 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007315
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007316 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7317 GetConsoleMode(out, &mode);
7318 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7319 if (SetConsoleMode(out, mode) == 0)
7320 vtp_working = 0;
7321 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007322#endif
7323
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007324 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007325 conpty_working = 1;
7326 if (ver >= CONPTY_STABLE_BUILD)
7327 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007328
Bram Moolenaar57da6982019-09-13 22:30:11 +02007329 if (ver <= CONPTY_INSIDER_BUILD)
7330 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007331 if (ver <= CONPTY_1909_BUILD)
7332 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007333 if (ver <= CONPTY_1903_BUILD)
7334 conpty_type = 2;
7335 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7336 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007337}
7338
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007339#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007340
7341 static void
7342vtp_init(void)
7343{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007344 HMODULE hKerneldll;
7345 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007346# ifdef FEAT_TERMGUICOLORS
7347 COLORREF fg, bg;
7348# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007349
Bram Moolenaar0f873732019-12-05 20:28:46 +01007350 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007351 hKerneldll = GetModuleHandle("kernel32.dll");
7352 if (hKerneldll != NULL)
7353 {
7354 pGetConsoleScreenBufferInfoEx =
7355 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7356 hKerneldll, "GetConsoleScreenBufferInfoEx");
7357 pSetConsoleScreenBufferInfoEx =
7358 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7359 hKerneldll, "SetConsoleScreenBufferInfoEx");
7360 if (pGetConsoleScreenBufferInfoEx != NULL
7361 && pSetConsoleScreenBufferInfoEx != NULL)
7362 has_csbiex = TRUE;
7363 }
7364
7365 csbi.cbSize = sizeof(csbi);
7366 if (has_csbiex)
7367 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007368 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7369 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007370 store_console_bg_rgb = save_console_bg_rgb;
7371 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007372
7373# ifdef FEAT_TERMGUICOLORS
7374 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7375 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7376 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7377 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7378 default_console_color_bg = bg;
7379 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007380# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007381
7382 set_console_color_rgb();
7383}
7384
7385 static void
7386vtp_exit(void)
7387{
Bram Moolenaardf543822020-01-30 11:53:59 +01007388 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007389}
7390
7391 static int
7392vtp_printf(
7393 char *format,
7394 ...)
7395{
7396 char_u buf[100];
7397 va_list list;
7398 DWORD result;
7399
7400 va_start(list, format);
7401 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7402 va_end(list);
7403 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7404 return (int)result;
7405}
7406
7407 static void
7408vtp_sgr_bulk(
7409 int arg)
7410{
7411 int args[1];
7412
7413 args[0] = arg;
7414 vtp_sgr_bulks(1, args);
7415}
7416
7417 static void
7418vtp_sgr_bulks(
7419 int argc,
7420 int *args
7421)
7422{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007423 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007424 char_u buf[2 + (4 * 16) + 1];
7425 char_u *p;
7426 int i;
7427
7428 p = buf;
7429 *p++ = '\033';
7430 *p++ = '[';
7431
7432 for (i = 0; i < argc; ++i)
7433 {
7434 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7435 *p++ = ';';
7436 }
7437 p--;
7438 *p++ = 'm';
7439 *p = NUL;
7440 vtp_printf((char *)buf);
7441}
7442
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007443# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007444 static int
7445ctermtoxterm(
7446 int cterm)
7447{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007448 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007449
7450 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7451 return (((int)r << 16) | ((int)g << 8) | (int)b);
7452}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007453# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007454
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007455 static void
7456set_console_color_rgb(void)
7457{
7458# ifdef FEAT_TERMGUICOLORS
7459 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007460 guicolor_T fg, bg;
7461 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007462
7463 if (!USE_VTP)
7464 return;
7465
Bram Moolenaara050b942019-12-02 21:35:31 +01007466 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7467
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007468 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7469 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7470
7471 csbi.cbSize = sizeof(csbi);
7472 if (has_csbiex)
7473 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7474
7475 csbi.cbSize = sizeof(csbi);
7476 csbi.srWindow.Right += 1;
7477 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007478 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7479 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007480 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7481 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007482 if (has_csbiex)
7483 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7484# endif
7485}
7486
Bram Moolenaara050b942019-12-02 21:35:31 +01007487# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7488 void
7489get_default_console_color(
7490 int *cterm_fg,
7491 int *cterm_bg,
7492 guicolor_T *gui_fg,
7493 guicolor_T *gui_bg)
7494{
7495 int id;
7496 guicolor_T guifg = INVALCOLOR;
7497 guicolor_T guibg = INVALCOLOR;
7498 int ctermfg = 0;
7499 int ctermbg = 0;
7500
7501 id = syn_name2id((char_u *)"Normal");
7502 if (id > 0 && p_tgc)
7503 syn_id2colors(id, &guifg, &guibg);
7504 if (guifg == INVALCOLOR)
7505 {
7506 ctermfg = -1;
7507 if (id > 0)
7508 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7509 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7510 : default_console_color_fg;
7511 cterm_normal_fg_gui_color = guifg;
7512 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7513 }
7514 if (guibg == INVALCOLOR)
7515 {
7516 ctermbg = -1;
7517 if (id > 0)
7518 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7519 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7520 : default_console_color_bg;
7521 cterm_normal_bg_gui_color = guibg;
7522 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7523 }
7524
7525 *cterm_fg = ctermfg;
7526 *cterm_bg = ctermbg;
7527 *gui_fg = guifg;
7528 *gui_bg = guibg;
7529}
7530# endif
7531
Bram Moolenaardf543822020-01-30 11:53:59 +01007532/*
7533 * Set the console colors to the original colors or the last set colors.
7534 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007535 static void
7536reset_console_color_rgb(void)
7537{
7538# ifdef FEAT_TERMGUICOLORS
7539 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7540
7541 csbi.cbSize = sizeof(csbi);
7542 if (has_csbiex)
7543 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7544
7545 csbi.cbSize = sizeof(csbi);
7546 csbi.srWindow.Right += 1;
7547 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007548 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7549 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7550 if (has_csbiex)
7551 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7552# endif
7553}
7554
7555/*
7556 * Set the console colors to the original colors.
7557 */
7558 static void
7559restore_console_color_rgb(void)
7560{
7561# ifdef FEAT_TERMGUICOLORS
7562 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7563
7564 csbi.cbSize = sizeof(csbi);
7565 if (has_csbiex)
7566 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7567
7568 csbi.cbSize = sizeof(csbi);
7569 csbi.srWindow.Right += 1;
7570 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007571 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7572 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007573 if (has_csbiex)
7574 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7575# endif
7576}
7577
7578 void
7579control_console_color_rgb(void)
7580{
7581 if (USE_VTP)
7582 set_console_color_rgb();
7583 else
7584 reset_console_color_rgb();
7585}
7586
7587 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007588use_vtp(void)
7589{
7590 return USE_VTP;
7591}
7592
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007593 int
7594is_term_win32(void)
7595{
7596 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7597}
7598
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007599 int
7600has_vtp_working(void)
7601{
7602 return vtp_working;
7603}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007604
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007605#endif
7606
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007607 int
7608has_conpty_working(void)
7609{
7610 return conpty_working;
7611}
7612
7613 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007614get_conpty_type(void)
7615{
7616 return conpty_type;
7617}
7618
7619 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007620is_conpty_stable(void)
7621{
7622 return conpty_stable;
7623}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007624
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007625#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007626 void
7627resize_console_buf(void)
7628{
7629 CONSOLE_SCREEN_BUFFER_INFO csbi;
7630 COORD coord;
7631 SMALL_RECT newsize;
7632
7633 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7634 {
7635 coord.X = SRWIDTH(csbi.srWindow);
7636 coord.Y = SRHEIGHT(csbi.srWindow);
7637 SetConsoleScreenBufferSize(g_hConOut, coord);
7638
7639 newsize.Left = 0;
7640 newsize.Top = 0;
7641 newsize.Right = coord.X - 1;
7642 newsize.Bottom = coord.Y - 1;
7643 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7644
7645 SetConsoleScreenBufferSize(g_hConOut, coord);
7646 }
7647}
7648#endif