blob: c5670afd300a7bf9e93a9812f60ed61ca0993e90 [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;
203
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200204static int g_color_index_bg = 0;
205static int g_color_index_fg = 7;
206
207# ifdef FEAT_TERMGUICOLORS
208static int default_console_color_bg = 0x000000; // black
209static int default_console_color_fg = 0xc0c0c0; // white
210# endif
211
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100212# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200213# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100214# else
215# define USE_VTP 0
216# endif
217
218static void set_console_color_rgb(void);
219static void reset_console_color_rgb(void);
220#endif
221
Bram Moolenaar0f873732019-12-05 20:28:46 +0100222// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100223#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
224# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
225#endif
226
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200227#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100228static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
230
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200231static char_u *exe_path = NULL;
232
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100233static BOOL win8_or_later = FALSE;
234
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200235#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100236// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100237typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
238{
239 ULONG cbSize;
240 COORD dwSize;
241 COORD dwCursorPosition;
242 WORD wAttributes;
243 SMALL_RECT srWindow;
244 COORD dwMaximumWindowSize;
245 WORD wPopupAttributes;
246 BOOL bFullscreenSupported;
247 COLORREF ColorTable[16];
248} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
249typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
250static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
251typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
252static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
253static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100254#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100255
256/*
257 * Get version number including build number
258 */
259typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100260#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100261 (((major) << 24) | ((minor) << 16) | (build))
262
263 static DWORD
264get_build_number(void)
265{
266 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
267 HMODULE hNtdll;
268 PfnRtlGetVersion pRtlGetVersion;
269 DWORD ver = MAKE_VER(0, 0, 0);
270
271 hNtdll = GetModuleHandle("ntdll.dll");
272 if (hNtdll != NULL)
273 {
274 pRtlGetVersion =
275 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
276 pRtlGetVersion(&osver);
277 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
278 min(osver.dwMinorVersion, 255),
279 min(osver.dwBuildNumber, 32767));
280 }
281 return ver;
282}
283
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200284#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100285/*
286 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100287 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100288 */
289 static BOOL
290read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100291 HANDLE hInput,
292 INPUT_RECORD *lpBuffer,
293 DWORD nLength,
294 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100295{
296 enum
297 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100298 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100299 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100300 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100301 static DWORD s_dwIndex = 0;
302 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100303 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100304 int head;
305 int tail;
306 int i;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100307
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200308 if (nLength == -2)
309 return (s_dwMax > 0) ? TRUE : FALSE;
310
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100311 if (!win8_or_later)
312 {
313 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200314 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
315 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100316 }
317
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100318 if (s_dwMax == 0)
319 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100320 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200321 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
322 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100324 s_dwIndex = 0;
325 s_dwMax = dwEvents;
326 if (dwEvents == 0)
327 {
328 *lpEvents = 0;
329 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100330 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100331
332 if (s_dwMax > 1)
333 {
334 head = 0;
335 tail = s_dwMax - 1;
336 while (head != tail)
337 {
338 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
339 && s_irCache[head + 1].EventType
340 == WINDOW_BUFFER_SIZE_EVENT)
341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100342 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100343 for (i = head; i < tail; ++i)
344 s_irCache[i] = s_irCache[i + 1];
345 --tail;
346 continue;
347 }
348 head++;
349 }
350 s_dwMax = tail + 1;
351 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100352 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100353
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100354 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200355 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100356 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100357 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100358 return TRUE;
359}
360
361/*
362 * Version of PeekConsoleInput() that works with IME.
363 */
364 static BOOL
365peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100366 HANDLE hInput,
367 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200368 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100369 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100370{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100371 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100372}
373
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100374# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200375 static DWORD
376msg_wait_for_multiple_objects(
377 DWORD nCount,
378 LPHANDLE pHandles,
379 BOOL fWaitAll,
380 DWORD dwMilliseconds,
381 DWORD dwWakeMask)
382{
383 if (read_console_input(NULL, NULL, -2, NULL))
384 return WAIT_OBJECT_0;
385 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
386 dwMilliseconds, dwWakeMask);
387}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100388# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200389
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100390# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200391 static DWORD
392wait_for_single_object(
393 HANDLE hHandle,
394 DWORD dwMilliseconds)
395{
396 if (read_console_input(NULL, NULL, -2, NULL))
397 return WAIT_OBJECT_0;
398 return WaitForSingleObject(hHandle, dwMilliseconds);
399}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100400# endif
401#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 static void
404get_exe_name(void)
405{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100406 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
407 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100408#define MAX_ENV_PATH_LEN 8192
409 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200410 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 if (exe_name == NULL)
413 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100414 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100415 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (*temp != NUL)
417 exe_name = FullName_save((char_u *)temp, FALSE);
418 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000419
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200420 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000421 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200422 exe_path = vim_strnsave(exe_name,
423 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200424 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000425 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100426 // Append our starting directory to $PATH, so that when doing
427 // "!xxd" it's found in our starting directory. Needed because
428 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200429 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100430 if (p == NULL
431 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200432 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 if (p == NULL || *p == NUL)
434 temp[0] = NUL;
435 else
436 {
437 STRCPY(temp, p);
438 STRCAT(temp, ";");
439 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200440 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100441 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200442 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000443 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445}
446
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200447/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100448 * Unescape characters in "p" that appear in "escaped".
449 */
450 static void
451unescape_shellxquote(char_u *p, char_u *escaped)
452{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100453 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100454 int n;
455
456 while (*p != NUL)
457 {
458 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
459 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100460 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100461 p += n;
462 l -= n;
463 }
464}
465
466/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200467 * Load library "name".
468 */
469 HINSTANCE
470vimLoadLib(char *name)
471{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200472 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200473
Bram Moolenaar0f873732019-12-05 20:28:46 +0100474 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
475 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200476 if (exe_path == NULL)
477 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200478 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200480 WCHAR old_dirw[MAXPATHL];
481
482 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // Change directory to where the executable is, both to make
485 // sure we find a .dll there and to avoid looking for a .dll
486 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100487 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200488 dll = LoadLibrary(name);
489 SetCurrentDirectoryW(old_dirw);
490 return dll;
491 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200492 }
493 return dll;
494}
495
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200496#if defined(VIMDLL) || defined(PROTO)
497/*
498 * Check if the current executable file is for the GUI subsystem.
499 */
500 int
501mch_is_gui_executable(void)
502{
503 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
504 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
505 PIMAGE_NT_HEADERS pPE;
506
507 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
508 return FALSE;
509 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
510 if (pPE->Signature != IMAGE_NT_SIGNATURE)
511 return FALSE;
512 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
513 return TRUE;
514 return FALSE;
515}
516#endif
517
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100518#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
519/*
520 * Get related information about 'funcname' which is imported by 'hInst'.
521 * If 'info' is 0, return the function address.
522 * If 'info' is 1, return the module name which the function is imported from.
523 */
524 static void *
525get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
526{
527 PBYTE pImage = (PBYTE)hInst;
528 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
529 PIMAGE_NT_HEADERS pPE;
530 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100531 PIMAGE_THUNK_DATA pIAT; // Import Address Table
532 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100533 PIMAGE_IMPORT_BY_NAME pImpName;
534
535 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
536 return NULL;
537 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
538 if (pPE->Signature != IMAGE_NT_SIGNATURE)
539 return NULL;
540 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
541 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
542 .VirtualAddress);
543 for (; pImpDesc->FirstThunk; ++pImpDesc)
544 {
545 if (!pImpDesc->OriginalFirstThunk)
546 continue;
547 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
548 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
549 for (; pIAT->u1.Function; ++pIAT, ++pINT)
550 {
551 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
552 continue;
553 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
554 + (UINT_PTR)(pINT->u1.AddressOfData));
555 if (strcmp((char *)pImpName->Name, funcname) == 0)
556 {
557 switch (info)
558 {
559 case 0:
560 return (void *)pIAT->u1.Function;
561 case 1:
562 return (void *)(pImage + pImpDesc->Name);
563 default:
564 return NULL;
565 }
566 }
567 }
568 }
569 return NULL;
570}
571
572/*
573 * Get the module handle which 'funcname' in 'hInst' is imported from.
574 */
575 HINSTANCE
576find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
577{
578 char *modulename;
579
580 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
581 if (modulename != NULL)
582 return GetModuleHandleA(modulename);
583 return NULL;
584}
585
586/*
587 * Get the address of 'funcname' which is imported by 'hInst' DLL.
588 */
589 void *
590get_dll_import_func(HINSTANCE hInst, const char *funcname)
591{
592 return get_imported_func_info(hInst, funcname, 0);
593}
594#endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
597# ifndef GETTEXT_DLL
598# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200599# define GETTEXT_DLL_ALT1 "libintl-8.dll"
600# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000603static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200604static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000605static char *null_libintl_textdomain(const char *);
606static char *null_libintl_bindtextdomain(const char *, const char *);
607static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100608static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200610static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000611char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200612char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
613 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000614char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
615char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000617char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
618 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100619int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100622dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int i;
625 static struct
626 {
627 char *name;
628 FARPROC *ptr;
629 } libintl_entry[] =
630 {
631 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200632 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
634 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
635 {NULL, NULL}
636 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100637 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar7554c542018-10-06 15:03:15 +0200639 // No need to initialize twice.
640 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200642 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100643 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100644# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100645 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200646 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100647# endif
648# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200649 if (!hLibintlDLL)
650 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100651# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100652 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200654 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200656 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100657 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200658 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200660 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 for (i = 0; libintl_entry[i].name != NULL
663 && libintl_entry[i].ptr != NULL; ++i)
664 {
665 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
666 libintl_entry[i].name)) == NULL)
667 {
668 dyn_libintl_end();
669 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000670 {
671 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000673 verbose_leave();
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return 0;
676 }
677 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000678
Bram Moolenaar0f873732019-12-05 20:28:46 +0100679 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000681 "bind_textdomain_codeset");
682 if (dyn_libintl_bind_textdomain_codeset == NULL)
683 dyn_libintl_bind_textdomain_codeset =
684 null_libintl_bind_textdomain_codeset;
685
Bram Moolenaar0f873732019-12-05 20:28:46 +0100686 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100687 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
688 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100689 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100690 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
691 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return 1;
694}
695
696 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100697dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 if (hLibintlDLL)
700 FreeLibrary(hLibintlDLL);
701 hLibintlDLL = NULL;
702 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200703 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 dyn_libintl_textdomain = null_libintl_textdomain;
705 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000706 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000711null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 return (char*)msgid;
714}
715
716 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200717null_libintl_ngettext(
718 const char *msgid,
719 const char *msgid_plural,
720 unsigned long n)
721{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200722 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200723}
724
Bram Moolenaaree695f72016-08-03 22:08:45 +0200725 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100726null_libintl_bindtextdomain(
727 const char *domainname UNUSED,
728 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 return NULL;
731}
732
733 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100734null_libintl_bind_textdomain_codeset(
735 const char *domainname UNUSED,
736 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000737{
738 return NULL;
739}
740
741 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100742null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 return NULL;
745}
746
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200747 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100748null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100749{
750 return 0;
751}
752
Bram Moolenaar0f873732019-12-05 20:28:46 +0100753#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar0f873732019-12-05 20:28:46 +0100755// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757#ifndef VER_PLATFORM_WIN32_WINDOWS
758# define VER_PLATFORM_WIN32_WINDOWS 1
759#endif
760
761DWORD g_PlatformId;
762
763#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100764# ifndef PROTO
765# include <aclapi.h>
766# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200767# ifndef PROTECTED_DACL_SECURITY_INFORMATION
768# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771
Bram Moolenaar27515922013-06-29 15:36:26 +0200772#ifdef HAVE_ACL
773/*
774 * Enables or disables the specified privilege.
775 */
776 static BOOL
777win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
778{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100779 BOOL bResult;
780 LUID luid;
781 HANDLE hToken;
782 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200783
784 if (!OpenProcessToken(GetCurrentProcess(),
785 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
786 return FALSE;
787
788 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
789 {
790 CloseHandle(hToken);
791 return FALSE;
792 }
793
Bram Moolenaar45500912014-07-09 20:51:07 +0200794 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200795 tokenPrivileges.Privileges[0].Luid = luid;
796 tokenPrivileges.Privileges[0].Attributes = bEnable ?
797 SE_PRIVILEGE_ENABLED : 0;
798
799 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
800 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
801
802 CloseHandle(hToken);
803
804 return bResult && GetLastError() == ERROR_SUCCESS;
805}
806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or
810 * VER_PLATFORM_WIN32_WINDOWS (Win95).
811 */
812 void
813PlatformId(void)
814{
815 static int done = FALSE;
816
817 if (!done)
818 {
819 OSVERSIONINFO ovi;
820
821 ovi.dwOSVersionInfoSize = sizeof(ovi);
822 GetVersionEx(&ovi);
823
824 g_PlatformId = ovi.dwPlatformId;
825
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100826 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
827 || ovi.dwMajorVersion > 6)
828 win8_or_later = TRUE;
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100831 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200832 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#endif
834 done = TRUE;
835 }
836}
837
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200838#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100840# define SHIFT (SHIFT_PRESSED)
841# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
842# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
843# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845
Bram Moolenaar0f873732019-12-05 20:28:46 +0100846// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
847// We map function keys to their ANSI terminal equivalents, as produced
848// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
849// ANSI key with a value >= '\300' is nonstandard, but provided anyway
850// so that the user can have access to all SHIFT-, CTRL-, and ALT-
851// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000853static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
855 WORD wVirtKey;
856 BOOL fAnsiKey;
857 int chAlone;
858 int chShift;
859 int chCtrl;
860 int chAlt;
861} VirtKeyMap[] =
862{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200863// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
865
866 { VK_F1, TRUE, ';', 'T', '^', 'h', },
867 { VK_F2, TRUE, '<', 'U', '_', 'i', },
868 { VK_F3, TRUE, '=', 'V', '`', 'j', },
869 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
870 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
871 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
872 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
873 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
874 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
875 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200876 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
877 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200879 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
880 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
881 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
882 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
883 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
884 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
885 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
886 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
887 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
888 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100889 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200891 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100893# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200894 // Most people don't have F13-F20, but what the hell...
895 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
896 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
897 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
898 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
899 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
900 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
901 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
902 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100903# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200904 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
905 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
906 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
907 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200909 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
910 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
911 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
912 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
913 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
914 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
915 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
916 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
917 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
918 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100919 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920};
921
922
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100923# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
924# define UChar UnicodeChar
925# else
926# define UChar uChar.UnicodeChar
927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaar0f873732019-12-05 20:28:46 +0100929/*
930 * The return code indicates key code size.
931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000934 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 UINT uMods = pker->dwControlKeyState;
937 static int s_iIsDead = 0;
938 static WORD awAnsiCode[2];
939 static BYTE abKeystate[256];
940
941
942 if (s_iIsDead == 2)
943 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200944 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 s_iIsDead = 0;
946 return 1;
947 }
948
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200949 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 return 1;
951
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200952 vim_memset(abKeystate, 0, sizeof (abKeystate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Bram Moolenaar0f873732019-12-05 20:28:46 +0100954 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200955 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 if (uMods & SHIFT_PRESSED)
958 abKeystate[VK_SHIFT] = 0x80;
959 if (uMods & CAPSLOCK_ON)
960 abKeystate[VK_CAPITAL] = 1;
961
962 if ((uMods & ALT_GR) == ALT_GR)
963 {
964 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
965 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
966 }
967
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200968 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
969 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200972 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 return s_iIsDead;
975}
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977static BOOL g_fJustGotFocus = FALSE;
978
979/*
980 * Decode a KEY_EVENT into one or two keystrokes
981 */
982 static BOOL
983decode_key_event(
984 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200985 WCHAR *pch,
986 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200988 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 int i;
991 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
992
993 *pch = *pch2 = NUL;
994 g_fJustGotFocus = FALSE;
995
Bram Moolenaar0f873732019-12-05 20:28:46 +0100996 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (!pker->bKeyDown)
998 return FALSE;
999
Bram Moolenaar0f873732019-12-05 20:28:46 +01001000 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 switch (pker->wVirtualKeyCode)
1002 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001003 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 case VK_SHIFT:
1005 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001006 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return FALSE;
1008
1009 default:
1010 break;
1011 }
1012
Bram Moolenaar0f873732019-12-05 20:28:46 +01001013 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001014 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001016 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (pker->wVirtualKeyCode == '6')
1018 {
1019 *pch = Ctrl_HAT;
1020 return TRUE;
1021 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 else if (pker->wVirtualKeyCode == '2')
1024 {
1025 *pch = NUL;
1026 return TRUE;
1027 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001028 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 else if (pker->wVirtualKeyCode == 0xBD)
1030 {
1031 *pch = Ctrl__;
1032 return TRUE;
1033 }
1034 }
1035
Bram Moolenaar0f873732019-12-05 20:28:46 +01001036 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1038 {
1039 *pch = K_NUL;
1040 *pch2 = '\017';
1041 return TRUE;
1042 }
1043
1044 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1045 {
1046 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1047 {
1048 if (nModifs == 0)
1049 *pch = VirtKeyMap[i].chAlone;
1050 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1051 *pch = VirtKeyMap[i].chShift;
1052 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1053 *pch = VirtKeyMap[i].chCtrl;
1054 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1055 *pch = VirtKeyMap[i].chAlt;
1056
1057 if (*pch != 0)
1058 {
1059 if (VirtKeyMap[i].fAnsiKey)
1060 {
1061 *pch2 = *pch;
1062 *pch = K_NUL;
1063 }
1064
1065 return TRUE;
1066 }
1067 }
1068 }
1069
1070 i = win32_kbd_patch_key(pker);
1071
1072 if (i < 0)
1073 *pch = NUL;
1074 else
1075 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001076 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 if (pmodifiers != NULL)
1079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001080 // Pass on the ALT key as a modifier, but only when not combined
1081 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1083 *pmodifiers |= MOD_MASK_ALT;
1084
Bram Moolenaar0f873732019-12-05 20:28:46 +01001085 // Pass on SHIFT only for special keys, because we don't know when
1086 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1088 *pmodifiers |= MOD_MASK_SHIFT;
1089
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 // Pass on CTRL only for non-special keys, because we don't know
1091 // when it's already included with the character. And not when
1092 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1094 && *pch >= 0x20 && *pch < 0x80)
1095 *pmodifiers |= MOD_MASK_CTRL;
1096 }
1097 }
1098
1099 return (*pch != NUL);
1100}
1101
Bram Moolenaar0f873732019-12-05 20:28:46 +01001102#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105/*
1106 * For the GUI the mouse handling is in gui_w32.c.
1107 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001108#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001110mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001113#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001114static int g_fMouseAvail = FALSE; // mouse present
1115static int g_fMouseActive = FALSE; // mouse enabled
1116static int g_nMouseClick = -1; // mouse status
1117static int g_xMouse; // mouse x coordinate
1118static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120/*
1121 * Enable or disable mouse input
1122 */
1123 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001124mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 DWORD cmodein;
1127
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001128# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001129 if (gui.in_use)
1130 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (!g_fMouseAvail)
1133 return;
1134
1135 g_fMouseActive = on;
1136 GetConsoleMode(g_hConIn, &cmodein);
1137
1138 if (g_fMouseActive)
1139 cmodein |= ENABLE_MOUSE_INPUT;
1140 else
1141 cmodein &= ~ENABLE_MOUSE_INPUT;
1142
1143 SetConsoleMode(g_hConIn, cmodein);
1144}
1145
Bram Moolenaar157d8132018-03-06 17:09:20 +01001146
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001147# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001148/*
1149 * Called when 'balloonevalterm' changed.
1150 */
1151 void
1152mch_bevalterm_changed(void)
1153{
1154 mch_setmouse(g_fMouseActive);
1155}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001156# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158/*
1159 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1160 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1161 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1162 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1163 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1164 * and we return the mouse position in g_xMouse and g_yMouse.
1165 *
1166 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1167 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1168 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1169 *
1170 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1171 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1172 *
1173 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1174 * moves, even if it stays within the same character cell. We ignore
1175 * all MOUSE_MOVED messages if the position hasn't really changed, and
1176 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1177 * we're only interested in MOUSE_DRAG).
1178 *
1179 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1180 * 2-button mouses by pressing the left & right buttons simultaneously.
1181 * In practice, it's almost impossible to click both at the same time,
1182 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1183 * in such cases, if the user is clicking quickly.
1184 */
1185 static BOOL
1186decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001187 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 static int s_nOldButton = -1;
1190 static int s_nOldMouseClick = -1;
1191 static int s_xOldMouse = -1;
1192 static int s_yOldMouse = -1;
1193 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001194# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 static int s_cClicks = 1;
1198 static BOOL s_fReleased = TRUE;
1199 static DWORD s_dwLastClickTime = 0;
1200 static BOOL s_fNextIsMiddle = FALSE;
1201
Bram Moolenaar0f873732019-12-05 20:28:46 +01001202 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
1204 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1205 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1206 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1207 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1208
1209 int nButton;
1210
1211 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1212 cButtons = 2;
1213
1214 if (!g_fMouseAvail || !g_fMouseActive)
1215 {
1216 g_nMouseClick = -1;
1217 return FALSE;
1218 }
1219
Bram Moolenaar0f873732019-12-05 20:28:46 +01001220 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (g_fJustGotFocus)
1222 {
1223 g_fJustGotFocus = FALSE;
1224 return FALSE;
1225 }
1226
Bram Moolenaar0f873732019-12-05 20:28:46 +01001227 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (g_nMouseClick != -1)
1229 return TRUE;
1230
1231 nButton = -1;
1232 g_xMouse = pmer->dwMousePosition.X;
1233 g_yMouse = pmer->dwMousePosition.Y;
1234
1235 if (pmer->dwEventFlags == MOUSE_MOVED)
1236 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001237 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1238 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1240 return FALSE;
1241 }
1242
Bram Moolenaar0f873732019-12-05 20:28:46 +01001243 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1245 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001246 nButton = MOUSE_RELEASE;
1247
Bram Moolenaar0f873732019-12-05 20:28:46 +01001248 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001250 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001251# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001252 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001253 if (p_bevalterm)
1254 nButton = MOUSE_DRAG;
1255 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001256# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001257 return FALSE;
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 s_fReleased = TRUE;
1261 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001262 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001264 // on a 2-button mouse, hold down left and right buttons
1265 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1268 {
1269 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1270
Bram Moolenaar0f873732019-12-05 20:28:46 +01001271 // if either left or right button only is pressed, see if the
1272 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (dwLR == LEFT || dwLR == RIGHT)
1274 {
1275 for (;;)
1276 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001277 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1279 != WAIT_OBJECT_0)
1280 break;
1281 else
1282 {
1283 DWORD cRecords = 0;
1284 INPUT_RECORD ir;
1285 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1286
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001287 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1290 || !(pmer2->dwButtonState & LEFT_RIGHT))
1291 break;
1292 else
1293 {
1294 if (pmer2->dwEventFlags != MOUSE_MOVED)
1295 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001296 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 return decode_mouse_event(pmer2);
1299 }
1300 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1301 s_yOldMouse == pmer2->dwMousePosition.Y)
1302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001303 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001304 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
Bram Moolenaar0f873732019-12-05 20:28:46 +01001306 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001307 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1310 break;
1311 }
1312 else
1313 break;
1314 }
1315 }
1316 }
1317 }
1318 }
1319
1320 if (s_fNextIsMiddle)
1321 {
1322 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1323 ? MOUSE_DRAG : MOUSE_MIDDLE;
1324 s_fNextIsMiddle = FALSE;
1325 }
1326 else if (cButtons == 2 &&
1327 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1328 {
1329 nButton = MOUSE_MIDDLE;
1330
1331 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1332 {
1333 s_fNextIsMiddle = TRUE;
1334 nButton = MOUSE_RELEASE;
1335 }
1336 }
1337 else if ((pmer->dwButtonState & LEFT) == LEFT)
1338 nButton = MOUSE_LEFT;
1339 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1340 nButton = MOUSE_MIDDLE;
1341 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1342 nButton = MOUSE_RIGHT;
1343
1344 if (! s_fReleased && ! s_fNextIsMiddle
1345 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1346 return FALSE;
1347
1348 s_fReleased = s_fNextIsMiddle;
1349 }
1350
1351 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1352 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001353 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (nButton != -1 && nButton != MOUSE_RELEASE)
1355 {
1356 DWORD dwCurrentTime = GetTickCount();
1357
1358 if (s_xOldMouse != g_xMouse
1359 || s_yOldMouse != g_yMouse
1360 || s_nOldButton != nButton
1361 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001362# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1366 {
1367 s_cClicks = 1;
1368 }
1369 else if (++s_cClicks > 4)
1370 {
1371 s_cClicks = 1;
1372 }
1373
1374 s_dwLastClickTime = dwCurrentTime;
1375 }
1376 }
1377 else if (pmer->dwEventFlags == MOUSE_MOVED)
1378 {
1379 if (nButton != -1 && nButton != MOUSE_RELEASE)
1380 nButton = MOUSE_DRAG;
1381
1382 s_cClicks = 1;
1383 }
1384
1385 if (nButton == -1)
1386 return FALSE;
1387
1388 if (nButton != MOUSE_RELEASE)
1389 s_nOldButton = nButton;
1390
1391 g_nMouseClick = nButton;
1392
1393 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1394 g_nMouseClick |= MOUSE_SHIFT;
1395 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1396 g_nMouseClick |= MOUSE_CTRL;
1397 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1398 g_nMouseClick |= MOUSE_ALT;
1399
1400 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1401 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1402
Bram Moolenaar0f873732019-12-05 20:28:46 +01001403 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (s_xOldMouse == g_xMouse
1405 && s_yOldMouse == g_yMouse
1406 && s_nOldMouseClick == g_nMouseClick)
1407 {
1408 g_nMouseClick = -1;
1409 return FALSE;
1410 }
1411
1412 s_xOldMouse = g_xMouse;
1413 s_yOldMouse = g_yMouse;
1414 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001415# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 s_nOldMouseClick = g_nMouseClick;
1419
1420 return TRUE;
1421}
1422
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001423#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425
1426#ifdef MCH_CURSOR_SHAPE
1427/*
1428 * Set the shape of the cursor.
1429 * 'thickness' can be from 1 (thin) to 99 (block)
1430 */
1431 static void
1432mch_set_cursor_shape(int thickness)
1433{
1434 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1435 ConsoleCursorInfo.dwSize = thickness;
1436 ConsoleCursorInfo.bVisible = s_cursor_visible;
1437
1438 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1439 if (s_cursor_visible)
1440 SetConsoleCursorPosition(g_hConOut, g_coord);
1441}
1442
1443 void
1444mch_update_cursor(void)
1445{
1446 int idx;
1447 int thickness;
1448
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001449# ifdef VIMDLL
1450 if (gui.in_use)
1451 return;
1452# endif
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /*
1455 * How the cursor is drawn depends on the current mode.
1456 */
1457 idx = get_shape_idx(FALSE);
1458
1459 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001460 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 else
1462 thickness = shape_table[idx].percentage;
1463 mch_set_cursor_shape(thickness);
1464}
1465#endif
1466
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001467#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468/*
1469 * Handle FOCUS_EVENT.
1470 */
1471 static void
1472handle_focus_event(INPUT_RECORD ir)
1473{
1474 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1475 ui_focus_change((int)g_fJustGotFocus);
1476}
1477
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001478static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480/*
1481 * Wait until console input from keyboard or mouse is available,
1482 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001483 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 * Return TRUE if something is available FALSE if not.
1485 */
1486 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001487WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 DWORD dwNow = 0, dwEndTime = 0;
1490 INPUT_RECORD ir;
1491 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001492 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001493# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001494 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001498 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 dwEndTime = GetTickCount() + msec;
1500 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001501 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 dwEndTime = INFINITE;
1503
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001504 // We need to loop until the end of the time period, because
1505 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 for (;;)
1507 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001508 // Only process messages when waiting.
1509 if (msec != 0)
1510 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001511# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001512 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001513# endif
1514# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001515 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001516# endif
1517# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001518 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001519# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001520 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001521
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001522 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001523# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001524 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 )
1527 return TRUE;
1528
1529 if (msec > 0)
1530 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001531 // If the specified wait time has passed, return. Beware that
1532 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001534 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 break;
1536 }
1537 if (msec != 0)
1538 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001539 DWORD dwWaitTime = dwEndTime - dwNow;
1540
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001541# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001542 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001543 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001544 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001545 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001546 // If there is readahead then parse_queued_messages() timed out
1547 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001548 if (channel_any_readahead())
1549 dwWaitTime = 10;
1550 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001551# endif
1552# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001553 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001554 // The 'balloonexpr' may indirectly invoke a callback while
1555 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001556 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001557# endif
1558# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001559 if (mzthreads_allowed() && p_mzq > 0
1560 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001561 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001562# endif
1563# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001564 // When waiting very briefly don't trigger timers.
1565 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001566 {
1567 long due_time;
1568
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001569 // Trigger timers and then get the time in msec until the next
1570 // one is due. Wait up to that time.
1571 due_time = check_due_timer();
1572 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001573 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001574 // timer may have used feedkeys().
1575 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001576 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001577 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1578 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001579 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001580# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001581 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001582# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001583 // Wait for either an event on the console input or a
1584 // message in the client-server window.
1585 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1586 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001587# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001588 wait_for_single_object(g_hConIn, dwWaitTime)
1589 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001590# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001591 )
1592 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594
1595 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001596 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001598# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (State & CMDLINE && msg_row == Rows - 1)
1600 {
1601 CONSOLE_SCREEN_BUFFER_INFO csbi;
1602
1603 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1604 {
1605 if (csbi.dwCursorPosition.Y != msg_row)
1606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001607 // The screen is now messed up, must redraw the
1608 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 redraw_all_later(CLEAR);
1610 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1611 redrawcmd();
1612 }
1613 }
1614 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 if (cRecords > 0)
1618 {
1619 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1620 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001621# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001622 // Windows IME sends two '\n's with only one 'ENTER'. First:
1623 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001624 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1626 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001627 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 continue;
1629 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1632 NULL, FALSE))
1633 return TRUE;
1634 }
1635
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001636 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 if (ir.EventType == FOCUS_EVENT)
1639 handle_focus_event(ir);
1640 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001641 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001642 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1643
Bram Moolenaar36698e32019-12-11 22:57:40 +01001644 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001645 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001646 if (dwSize.X != Columns || dwSize.Y != Rows)
1647 {
1648 CONSOLE_SCREEN_BUFFER_INFO csbi;
1649 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001650 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001651 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001652 if (dwSize.X != Columns || dwSize.Y != Rows)
1653 {
1654 ResizeConBuf(g_hConOut, dwSize);
1655 shell_resized();
1656 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001657 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else if (ir.EventType == MOUSE_EVENT
1660 && decode_mouse_event(&ir.Event.MouseEvent))
1661 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else if (msec == 0)
1664 break;
1665 }
1666
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001667# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001668 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (input_available())
1670 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001671# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 return FALSE;
1674}
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * return non-zero if a character is available
1678 */
1679 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001680mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001682# ifdef VIMDLL
1683 if (gui.in_use)
1684 return TRUE;
1685# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001686 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001688
1689# if defined(FEAT_TERMINAL) || defined(PROTO)
1690/*
1691 * Check for any pending input or messages.
1692 */
1693 int
1694mch_check_messages(void)
1695{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001696# ifdef VIMDLL
1697 if (gui.in_use)
1698 return TRUE;
1699# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001700 return WaitForChar(0L, TRUE);
1701}
1702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704/*
1705 * Create the console input. Used when reading stdin doesn't work.
1706 */
1707 static void
1708create_conin(void)
1709{
1710 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1711 FILE_SHARE_READ|FILE_SHARE_WRITE,
1712 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001713 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 did_create_conin = TRUE;
1715}
1716
1717/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001718 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001720 static WCHAR
1721tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001723 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725 for (;;)
1726 {
1727 INPUT_RECORD ir;
1728 DWORD cRecords = 0;
1729
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001730# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001731 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (input_available())
1733 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (g_nMouseClick != -1)
1735 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001736# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001737 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
1739 if (did_create_conin)
1740 read_error_exit();
1741 create_conin();
1742 continue;
1743 }
1744
1745 if (ir.EventType == KEY_EVENT)
1746 {
1747 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1748 pmodifiers, TRUE))
1749 return ch;
1750 }
1751 else if (ir.EventType == FOCUS_EVENT)
1752 handle_focus_event(ir);
1753 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1754 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 else if (ir.EventType == MOUSE_EVENT)
1756 {
1757 if (decode_mouse_event(&ir.Event.MouseEvent))
1758 return 0;
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001762#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764
1765/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001766 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * Get one or more characters from the keyboard or the mouse.
1768 * If time == 0, do not wait for characters.
1769 * If time == n, wait a short time for characters.
1770 * If time == -1, wait forever for characters.
1771 * Returns the number of characters read into buf.
1772 */
1773 int
1774mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001775 char_u *buf UNUSED,
1776 int maxlen UNUSED,
1777 long time UNUSED,
1778 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001780#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 int len;
1783 int c;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001784# define TYPEAHEADLEN 20
Bram Moolenaar0f873732019-12-05 20:28:46 +01001785 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 static int typeaheadlen = 0;
1787
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001788# ifdef VIMDLL
1789 if (gui.in_use)
1790 return 0;
1791# endif
1792
Bram Moolenaar0f873732019-12-05 20:28:46 +01001793 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (typeaheadlen > 0)
1795 goto theend;
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (time >= 0)
1798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001799 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001802 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001804 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar3918c952005-03-15 22:34:55 +00001806 /*
1807 * If there is no character available within 2 seconds (default)
1808 * write the autoscript file to disk. Or cause the CursorHold event
1809 * to be triggered.
1810 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001811 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001813 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001815 buf[0] = K_SPECIAL;
1816 buf[1] = KS_EXTRA;
1817 buf[2] = (int)KE_CURSORHOLD;
1818 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001820 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
1822 }
1823
1824 /*
1825 * Try to read as many characters as there are, until the buffer is full.
1826 */
1827
Bram Moolenaar0f873732019-12-05 20:28:46 +01001828 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 g_fCBrkPressed = FALSE;
1830
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001831# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (fdDump)
1833 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar0f873732019-12-05 20:28:46 +01001836 // Keep looping until there is something in the typeahead buffer and more
1837 // to get and still room in the buffer (up to two bytes for a char and
1838 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001839 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 && typeaheadlen + 5 <= TYPEAHEADLEN)
1841 {
1842 if (typebuf_changed(tb_change_cnt))
1843 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001844 // "buf" may be invalid now if a client put something in the
1845 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 typeaheadlen = 0;
1847 break;
1848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (g_nMouseClick != -1)
1850 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001851# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (fdDump)
1853 fprintf(fdDump, "{%02x @ %d, %d}",
1854 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 typeahead[typeaheadlen++] = ESC + 128;
1857 typeahead[typeaheadlen++] = 'M';
1858 typeahead[typeaheadlen++] = g_nMouseClick;
1859 typeahead[typeaheadlen++] = g_xMouse + '!';
1860 typeahead[typeaheadlen++] = g_yMouse + '!';
1861 g_nMouseClick = -1;
1862 }
1863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001865 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 int modifiers = 0;
1867
1868 c = tgetch(&modifiers, &ch2);
1869
1870 if (typebuf_changed(tb_change_cnt))
1871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001872 // "buf" may be invalid now if a client put something in the
1873 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 typeaheadlen = 0;
1875 break;
1876 }
1877
1878 if (c == Ctrl_C && ctrl_c_interrupts)
1879 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001880# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 got_int = TRUE;
1884 }
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 int n = 1;
1889
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001890 if (ch2 == NUL)
1891 {
1892 int i;
1893 char_u *p;
1894 WCHAR ch[2];
1895
1896 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001898 {
1899 ch[1] = tgetch(&modifiers, &ch2);
1900 n++;
1901 }
1902 p = utf16_to_enc(ch, &n);
1903 if (p != NULL)
1904 {
1905 for (i = 0; i < n; i++)
1906 typeahead[typeaheadlen + i] = p[i];
1907 vim_free(p);
1908 }
1909 }
1910 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001911 typeahead[typeaheadlen] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (ch2 != NUL)
1913 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001914 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001915 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001916 switch (ch2)
1917 {
1918 case (WCHAR)'\324': // SHIFT+Insert
1919 case (WCHAR)'\325': // CTRL+Insert
1920 case (WCHAR)'\327': // SHIFT+Delete
1921 case (WCHAR)'\330': // CTRL+Delete
1922 typeahead[typeaheadlen + n] = (char_u)ch2;
1923 n++;
1924 break;
1925
1926 default:
1927 typeahead[typeaheadlen + n] = 3;
1928 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1929 n += 2;
1930 break;
1931 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001932 }
1933 else
1934 {
1935 typeahead[typeaheadlen + n] = 3;
1936 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1937 n += 2;
1938 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001939 }
1940
Bram Moolenaar0f873732019-12-05 20:28:46 +01001941 // Use the ALT key to set the 8th bit of the character
1942 // when it's one byte, the 8th bit isn't set yet and not
1943 // using a double-byte encoding (would become a lead
1944 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if ((modifiers & MOD_MASK_ALT)
1946 && n == 1
1947 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 )
1950 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001951 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1952 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 modifiers &= ~MOD_MASK_ALT;
1954 }
1955
1956 if (modifiers != 0)
1957 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001958 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 mch_memmove(typeahead + typeaheadlen + 3,
1960 typeahead + typeaheadlen, n);
1961 typeahead[typeaheadlen++] = K_SPECIAL;
1962 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1963 typeahead[typeaheadlen++] = modifiers;
1964 }
1965
1966 typeaheadlen += n;
1967
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001968# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (fdDump)
1970 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973 }
1974 }
1975
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001976# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (fdDump)
1978 {
1979 fputs("]\n", fdDump);
1980 fflush(fdDump);
1981 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001985 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 len = 0;
1987 while (len < maxlen && typeaheadlen > 0)
1988 {
1989 buf[len++] = typeahead[0];
1990 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
1991 }
1992 return len;
1993
Bram Moolenaar0f873732019-12-05 20:28:46 +01001994#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001996#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997}
1998
Bram Moolenaar82881492012-11-20 16:53:39 +01001999#ifndef PROTO
2000# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002001# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
2004
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002005/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002006 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2007 * If "use_path" is FALSE: Return TRUE if "name" exists.
2008 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2009 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002010 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002013executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002015 WCHAR *p;
2016 WCHAR fnamew[_MAX_PATH];
2017 WCHAR *dumw;
2018 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002019 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaar43663192017-03-05 14:29:12 +01002021 if (!use_path)
2022 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002023 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002024 {
2025 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002026 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002027 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002028 *path = vim_strsave((char_u *)name);
2029 else
2030 *path = FullName_save((char_u *)name, FALSE);
2031 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002032 return TRUE;
2033 }
2034 return FALSE;
2035 }
2036
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002037 p = enc_to_utf16((char_u *)name, NULL);
2038 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002039 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002040
2041 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002042 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002043 if (wnewpath == NULL)
2044 return FALSE;
2045 wcscpy(wnewpath, L".;");
2046 wcscat(wnewpath, wcurpath);
2047 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2048 vim_free(wnewpath);
2049 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002050 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002051 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002052 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002053 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002054 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002055 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002056 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057}
2058
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002059#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2060 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002061/*
2062 * Bad parameter handler.
2063 *
2064 * Certain MS CRT functions will intentionally crash when passed invalid
2065 * parameters to highlight possible security holes. Setting this function as
2066 * the bad parameter handler will prevent the crash.
2067 *
2068 * In debug builds the parameters contain CRT information that might help track
2069 * down the source of a problem, but in non-debug builds the arguments are all
2070 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2071 * worth allowing these to make debugging of issues easier.
2072 */
2073 static void
2074bad_param_handler(const wchar_t *expression,
2075 const wchar_t *function,
2076 const wchar_t *file,
2077 unsigned int line,
2078 uintptr_t pReserved)
2079{
2080}
2081
2082# define SET_INVALID_PARAM_HANDLER \
2083 ((void)_set_invalid_parameter_handler(bad_param_handler))
2084#else
2085# define SET_INVALID_PARAM_HANDLER
2086#endif
2087
Bram Moolenaar4f974752019-02-17 17:44:42 +01002088#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090/*
2091 * GUI version of mch_init().
2092 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002093 static void
2094mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002096# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
Bram Moolenaar0f873732019-12-05 20:28:46 +01002100 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002101 SET_INVALID_PARAM_HANDLER;
2102
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103 // Let critical errors result in a failure, not in a dialog box. Required
2104 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 SetErrorMode(SEM_FAILCRITICALERRORS);
2106
Bram Moolenaar0f873732019-12-05 20:28:46 +01002107 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar0f873732019-12-05 20:28:46 +01002109 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 Rows = 25;
2111 Columns = 80;
2112
Bram Moolenaar0f873732019-12-05 20:28:46 +01002113 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
2115 char_u vimrun_location[_MAX_PATH + 4];
2116
Bram Moolenaar0f873732019-12-05 20:28:46 +01002117 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 STRCPY(vimrun_location, exe_name);
2119 STRCPY(gettail(vimrun_location), "vimrun.exe");
2120 if (mch_getperm(vimrun_location) >= 0)
2121 {
2122 if (*skiptowhite(vimrun_location) != NUL)
2123 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002124 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 mch_memmove(vimrun_location + 1, vimrun_location,
2126 STRLEN(vimrun_location) + 1);
2127 *vimrun_location = '"';
2128 STRCPY(gettail(vimrun_location), "vimrun\" ");
2129 }
2130 else
2131 STRCPY(gettail(vimrun_location), "vimrun ");
2132
2133 vimrun_path = (char *)vim_strsave(vimrun_location);
2134 s_dont_use_vimrun = FALSE;
2135 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002136 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 s_dont_use_vimrun = FALSE;
2138
Bram Moolenaar0f873732019-12-05 20:28:46 +01002139 // Don't give the warning for a missing vimrun.exe right now, but only
2140 // when vimrun was supposed to be used. Don't bother people that do
2141 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (s_dont_use_vimrun)
2143 need_vimrun_warning = TRUE;
2144 }
2145
2146 /*
2147 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2148 * Otherwise the default "findstr /n" is used.
2149 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002150 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2152
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002153# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002154 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002156
2157 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158}
2159
2160
Bram Moolenaar0f873732019-12-05 20:28:46 +01002161#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002162
2163#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002165# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2166# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168/*
2169 * ClearConsoleBuffer()
2170 * Description:
2171 * Clears the entire contents of the console screen buffer, using the
2172 * specified attribute.
2173 * Returns:
2174 * TRUE on success
2175 */
2176 static BOOL
2177ClearConsoleBuffer(WORD wAttribute)
2178{
2179 CONSOLE_SCREEN_BUFFER_INFO csbi;
2180 COORD coord;
2181 DWORD NumCells, dummy;
2182
2183 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2184 return FALSE;
2185
2186 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2187 coord.X = 0;
2188 coord.Y = 0;
2189 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2190 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2193 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 return TRUE;
2197}
2198
2199/*
2200 * FitConsoleWindow()
2201 * Description:
2202 * Checks if the console window will fit within given buffer dimensions.
2203 * Also, if requested, will shrink the window to fit.
2204 * Returns:
2205 * TRUE on success
2206 */
2207 static BOOL
2208FitConsoleWindow(
2209 COORD dwBufferSize,
2210 BOOL WantAdjust)
2211{
2212 CONSOLE_SCREEN_BUFFER_INFO csbi;
2213 COORD dwWindowSize;
2214 BOOL NeedAdjust = FALSE;
2215
2216 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2217 {
2218 /*
2219 * A buffer resize will fail if the current console window does
2220 * not lie completely within that buffer. To avoid this, we might
2221 * have to move and possibly shrink the window.
2222 */
2223 if (csbi.srWindow.Right >= dwBufferSize.X)
2224 {
2225 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2226 if (dwWindowSize.X > dwBufferSize.X)
2227 dwWindowSize.X = dwBufferSize.X;
2228 csbi.srWindow.Right = dwBufferSize.X - 1;
2229 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2230 NeedAdjust = TRUE;
2231 }
2232 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2233 {
2234 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2235 if (dwWindowSize.Y > dwBufferSize.Y)
2236 dwWindowSize.Y = dwBufferSize.Y;
2237 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2238 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2239 NeedAdjust = TRUE;
2240 }
2241 if (NeedAdjust && WantAdjust)
2242 {
2243 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2244 return FALSE;
2245 }
2246 return TRUE;
2247 }
2248
2249 return FALSE;
2250}
2251
2252typedef struct ConsoleBufferStruct
2253{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002254 BOOL IsValid;
2255 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002256 PCHAR_INFO Buffer;
2257 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002258 PSMALL_RECT Regions;
2259 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260} ConsoleBuffer;
2261
2262/*
2263 * SaveConsoleBuffer()
2264 * Description:
2265 * Saves important information about the console buffer, including the
2266 * actual buffer contents. The saved information is suitable for later
2267 * restoration by RestoreConsoleBuffer().
2268 * Returns:
2269 * TRUE if all information was saved; FALSE otherwise
2270 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2271 */
2272 static BOOL
2273SaveConsoleBuffer(
2274 ConsoleBuffer *cb)
2275{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002276 DWORD NumCells;
2277 COORD BufferCoord;
2278 SMALL_RECT ReadRegion;
2279 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002280 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (cb == NULL)
2283 return FALSE;
2284
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002285 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 cb->IsValid = FALSE;
2288 return FALSE;
2289 }
2290 cb->IsValid = TRUE;
2291
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002292 /*
2293 * Allocate a buffer large enough to hold the entire console screen
2294 * buffer. If this ConsoleBuffer structure has already been initialized
2295 * with a buffer of the correct size, then just use that one.
2296 */
2297 if (!cb->IsValid || cb->Buffer == NULL ||
2298 cb->BufferSize.X != cb->Info.dwSize.X ||
2299 cb->BufferSize.Y != cb->Info.dwSize.Y)
2300 {
2301 cb->BufferSize.X = cb->Info.dwSize.X;
2302 cb->BufferSize.Y = cb->Info.dwSize.Y;
2303 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2304 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002305 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002306 if (cb->Buffer == NULL)
2307 return FALSE;
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002311 * We will now copy the console screen buffer into our buffer.
2312 * ReadConsoleOutput() seems to be limited as far as how much you
2313 * can read at a time. Empirically, this number seems to be about
2314 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2315 * in chunks until it is all copied. The chunks will all have the
2316 * same horizontal characteristics, so initialize them now. The
2317 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002319 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002320 ReadRegion.Left = 0;
2321 ReadRegion.Right = cb->Info.dwSize.X - 1;
2322 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002323
2324 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2325 if (cb->Regions == NULL || numregions != cb->NumRegions)
2326 {
2327 cb->NumRegions = numregions;
2328 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002329 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002330 if (cb->Regions == NULL)
2331 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002332 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002333 return FALSE;
2334 }
2335 }
2336
2337 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002339 /*
2340 * Read into position (0, Y) in our buffer.
2341 */
2342 BufferCoord.Y = Y;
2343 /*
2344 * Read the region whose top left corner is (0, Y) and whose bottom
2345 * right corner is (width - 1, Y + Y_incr - 1). This should define
2346 * a region of size width by Y_incr. Don't worry if this region is
2347 * too large for the remaining buffer; it will be cropped.
2348 */
2349 ReadRegion.Top = Y;
2350 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002351 if (!ReadConsoleOutputW(g_hConOut, // output handle
2352 cb->Buffer, // our buffer
2353 cb->BufferSize, // dimensions of our buffer
2354 BufferCoord, // offset in our buffer
2355 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002356 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002357 VIM_CLEAR(cb->Buffer);
2358 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002359 return FALSE;
2360 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002361 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363
2364 return TRUE;
2365}
2366
2367/*
2368 * RestoreConsoleBuffer()
2369 * Description:
2370 * Restores important information about the console buffer, including the
2371 * actual buffer contents, if desired. The information to restore is in
2372 * the same format used by SaveConsoleBuffer().
2373 * Returns:
2374 * TRUE on success
2375 */
2376 static BOOL
2377RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002378 ConsoleBuffer *cb,
2379 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002381 COORD BufferCoord;
2382 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002383 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (cb == NULL || !cb->IsValid)
2386 return FALSE;
2387
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002388 /*
2389 * Before restoring the buffer contents, clear the current buffer, and
2390 * restore the cursor position and window information. Doing this now
2391 * prevents old buffer contents from "flashing" onto the screen.
2392 */
2393 if (RestoreScreen)
2394 ClearConsoleBuffer(cb->Info.wAttributes);
2395
2396 FitConsoleWindow(cb->Info.dwSize, TRUE);
2397 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2398 return FALSE;
2399 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2400 return FALSE;
2401
2402 if (!RestoreScreen)
2403 {
2404 /*
2405 * No need to restore the screen buffer contents, so we're done.
2406 */
2407 return TRUE;
2408 }
2409
2410 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2411 return FALSE;
2412 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2413 return FALSE;
2414
2415 /*
2416 * Restore the screen buffer contents.
2417 */
2418 if (cb->Buffer != NULL)
2419 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002420 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002421 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002422 BufferCoord.X = cb->Regions[i].Left;
2423 BufferCoord.Y = cb->Regions[i].Top;
2424 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002425 if (!WriteConsoleOutputW(g_hConOut, // output handle
2426 cb->Buffer, // our buffer
2427 cb->BufferSize, // dimensions of our buffer
2428 BufferCoord, // offset in our buffer
2429 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002430 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002431 }
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 return TRUE;
2435}
2436
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002437# define FEAT_RESTORE_ORIG_SCREEN
2438# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002439static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441static ConsoleBuffer g_cbNonTermcap = { 0 };
2442static ConsoleBuffer g_cbTermcap = { 0 };
2443
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002444# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002446HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447static HICON g_hOrigIconSmall = NULL;
2448static HICON g_hOrigIcon = NULL;
2449static HICON g_hVimIcon = NULL;
2450static BOOL g_fCanChangeIcon = FALSE;
2451
Bram Moolenaar0f873732019-12-05 20:28:46 +01002452// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002453# ifndef ICON_SMALL
2454# define ICON_SMALL 0
2455# endif
2456# ifndef ICON_BIG
2457# define ICON_BIG 1
2458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459/*
2460 * GetConsoleIcon()
2461 * Description:
2462 * Attempts to retrieve the small icon and/or the big icon currently in
2463 * use by a given window.
2464 * Returns:
2465 * TRUE on success
2466 */
2467 static BOOL
2468GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002469 HWND hWnd,
2470 HICON *phIconSmall,
2471 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 if (hWnd == NULL)
2474 return FALSE;
2475
2476 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002477 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2478 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002480 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2481 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 return TRUE;
2483}
2484
2485/*
2486 * SetConsoleIcon()
2487 * Description:
2488 * Attempts to change the small icon and/or the big icon currently in
2489 * use by a given window.
2490 * Returns:
2491 * TRUE on success
2492 */
2493 static BOOL
2494SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002495 HWND hWnd,
2496 HICON hIconSmall,
2497 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (hWnd == NULL)
2500 return FALSE;
2501
2502 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002503 SendMessage(hWnd, WM_SETICON,
2504 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002506 SendMessage(hWnd, WM_SETICON,
2507 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 return TRUE;
2509}
2510
2511/*
2512 * SaveConsoleTitleAndIcon()
2513 * Description:
2514 * Saves the current console window title in g_szOrigTitle, for later
2515 * restoration. Also, attempts to obtain a handle to the console window,
2516 * and use it to save the small and big icons currently in use by the
2517 * console window. This is not always possible on some versions of Windows;
2518 * nor is it possible when running Vim remotely using Telnet (since the
2519 * console window the user sees is owned by a remote process).
2520 */
2521 static void
2522SaveConsoleTitleAndIcon(void)
2523{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002524 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2526 return;
2527
2528 /*
2529 * Obtain a handle to the console window using GetConsoleWindow() from
2530 * KERNEL32.DLL; we need to handle in order to change the window icon.
2531 * This function only exists on NT-based Windows, starting with Windows
2532 * 2000. On older operating systems, we can't change the window icon
2533 * anyway.
2534 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002535 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (g_hWnd == NULL)
2537 return;
2538
Bram Moolenaar0f873732019-12-05 20:28:46 +01002539 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2541 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2542 return;
2543
Bram Moolenaar0f873732019-12-05 20:28:46 +01002544 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002545 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002546 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (g_hVimIcon != NULL)
2548 g_fCanChangeIcon = TRUE;
2549}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552static int g_fWindInitCalled = FALSE;
2553static int g_fTermcapMode = FALSE;
2554static CONSOLE_CURSOR_INFO g_cci;
2555static DWORD g_cmodein = 0;
2556static DWORD g_cmodeout = 0;
2557
2558/*
2559 * non-GUI version of mch_init().
2560 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002561 static void
2562mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002564# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002565 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002566# endif
2567# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002572 SET_INVALID_PARAM_HANDLER;
2573
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574 // Let critical errors result in a failure, not in a dialog box. Required
2575 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 SetErrorMode(SEM_FAILCRITICALERRORS);
2577
Bram Moolenaar0f873732019-12-05 20:28:46 +01002578 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 out_flush();
2580
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (read_cmd_fd == 0)
2583 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2584 else
2585 create_conin();
2586 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2587
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002588# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002589 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002590 SaveConsoleBuffer(&g_cbOrig);
2591 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002592# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002593 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002594 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2595 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (cterm_normal_fg_color == 0)
2598 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2599 if (cterm_normal_bg_color == 0)
2600 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2601
Bram Moolenaarbdace832019-03-02 10:13:42 +01002602 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002603 g_color_index_fg = g_attrDefault & 0xf;
2604 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2605
Bram Moolenaar0f873732019-12-05 20:28:46 +01002606 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 update_tcap(g_attrCurrent);
2608
2609 GetConsoleCursorInfo(g_hConOut, &g_cci);
2610 GetConsoleMode(g_hConIn, &g_cmodein);
2611 GetConsoleMode(g_hConOut, &g_cmodeout);
2612
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002613# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 SaveConsoleTitleAndIcon();
2615 /*
2616 * Set both the small and big icons of the console window to Vim's icon.
2617 * Note that Vim presently only has one size of icon (32x32), but it
2618 * automatically gets scaled down to 16x16 when setting the small icon.
2619 */
2620 if (g_fCanChangeIcon)
2621 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 ui_get_shellsize();
2625
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002626# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 fdDump = fopen("dump", "wt");
2628
2629 if (fdDump)
2630 {
2631 time_t t;
2632
2633 time(&t);
2634 fputs(ctime(&t), fdDump);
2635 fflush(fdDump);
2636 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 g_fWindInitCalled = TRUE;
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002643# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002644 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002645# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002646
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002647 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002648 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649}
2650
2651/*
2652 * non-GUI version of mch_exit().
2653 * Shut down and exit with status `r'
2654 * Careful: mch_exit() may be called before mch_init()!
2655 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002656 static void
2657mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002659 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002661 vtp_exit();
2662
Bram Moolenaar955f1982017-02-05 15:10:51 +01002663 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (g_fWindInitCalled)
2665 settmode(TMODE_COOK);
2666
Bram Moolenaar0f873732019-12-05 20:28:46 +01002667 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
2669 if (g_fWindInitCalled)
2670 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002671# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002672 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /*
2674 * Restore both the small and big icons of the console window to
2675 * what they were at startup. Don't do this when the window is
2676 * closed, Vim would hang here.
2677 */
2678 if (g_fCanChangeIcon && !g_fForceExit)
2679 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002682# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (fdDump)
2684 {
2685 time_t t;
2686
2687 time(&t);
2688 fputs(ctime(&t), fdDump);
2689 fclose(fdDump);
2690 }
2691 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694
2695 SetConsoleCursorInfo(g_hConOut, &g_cci);
2696 SetConsoleMode(g_hConIn, g_cmodein);
2697 SetConsoleMode(g_hConOut, g_cmodeout);
2698
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002699# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 exit(r);
2704}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002705#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002707 void
2708mch_init(void)
2709{
2710#ifdef VIMDLL
2711 if (gui.starting)
2712 mch_init_g();
2713 else
2714 mch_init_c();
2715#elif defined(FEAT_GUI_MSWIN)
2716 mch_init_g();
2717#else
2718 mch_init_c();
2719#endif
2720}
2721
2722 void
2723mch_exit(int r)
2724{
2725#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002726 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002727 mch_exit_g(r);
2728 else
2729 mch_exit_c(r);
2730#elif defined(FEAT_GUI_MSWIN)
2731 mch_exit_g(r);
2732#else
2733 mch_exit_c(r);
2734#endif
2735}
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737/*
2738 * Do we have an interactive window?
2739 */
2740 int
2741mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002742 int argc UNUSED,
2743 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 get_exe_name();
2746
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002747#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002750# ifdef VIMDLL
2751 if (gui.in_use)
2752 return OK;
2753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (isatty(1))
2755 return OK;
2756 return FAIL;
2757#endif
2758}
2759
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002760/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002761 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 * When "len" is > 0, also expand short to long filenames.
2763 */
2764 void
2765fname_case(
2766 char_u *name,
2767 int len)
2768{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002769 int flen;
2770 WCHAR *p;
2771 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002773 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002774 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 return;
2776
2777 slash_adjust(name);
2778
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002779 p = enc_to_utf16(name, NULL);
2780 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002781 return;
2782
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002783 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002785 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002787 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002789 if (len > 0 || flen >= (int)STRLEN(q))
2790 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2791 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002794 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797
2798/*
2799 * Insert user name in s[len].
2800 */
2801 int
2802mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002803 char_u *s,
2804 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002806 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002807 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002809 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002810 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002811 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002812
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002813 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002814 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002815 vim_strncpy(s, p, len - 1);
2816 vim_free(p);
2817 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002818 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 s[0] = NUL;
2821 return FAIL;
2822}
2823
2824
2825/*
2826 * Insert host name in s[len].
2827 */
2828 void
2829mch_get_host_name(
2830 char_u *s,
2831 int len)
2832{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002833 WCHAR wszHostName[256 + 1];
2834 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002836 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002837 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002838 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002839
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002840 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002841 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002842 vim_strncpy(s, p, len - 1);
2843 vim_free(p);
2844 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002845 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847}
2848
2849
2850/*
2851 * return process ID
2852 */
2853 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002854mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 return (long)GetCurrentProcessId();
2857}
2858
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002859/*
2860 * return TRUE if process "pid" is still running
2861 */
2862 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002863mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002864{
2865 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2866 DWORD status = 0;
2867 int ret = FALSE;
2868
2869 if (hProcess == NULL)
2870 return FALSE; // might not have access
2871 if (GetExitCodeProcess(hProcess, &status) )
2872 ret = status == STILL_ACTIVE;
2873 CloseHandle(hProcess);
2874 return ret;
2875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
2877/*
2878 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2879 * Return OK for success, FAIL for failure.
2880 */
2881 int
2882mch_dirname(
2883 char_u *buf,
2884 int len)
2885{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002886 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 /*
2889 * Originally this was:
2890 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2891 * But the Win32s known bug list says that getcwd() doesn't work
2892 * so use the Win32 system call instead. <Negri>
2893 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002894 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002896 WCHAR wcbuf[_MAX_PATH + 1];
2897 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002899 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002901 p = utf16_to_enc(wcbuf, NULL);
2902 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002903 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002904 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002906 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002909 if (p == NULL)
2910 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002911
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002912 if (p != NULL)
2913 {
2914 vim_strncpy(buf, p, len - 1);
2915 vim_free(p);
2916 return OK;
2917 }
2918 }
2919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002923 * Get file permissions for "name".
2924 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 */
2926 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002927mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002929 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002930 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002932 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002933 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934}
2935
2936
2937/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002938 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002939 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002940 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002943mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002945 long n;
2946 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002947
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002948 p = enc_to_utf16(name, NULL);
2949 if (p == NULL)
2950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002952 n = _wchmod(p, perm);
2953 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002954 if (n == -1)
2955 return FAIL;
2956
2957 win32_set_archive(name);
2958
2959 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962/*
2963 * Set hidden flag for "name".
2964 */
2965 void
2966mch_hide(char_u *name)
2967{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002968 int attrs = win32_getattrs(name);
2969 if (attrs == -1)
2970 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002972 attrs |= FILE_ATTRIBUTE_HIDDEN;
2973 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002977 * Return TRUE if file "name" exists and is hidden.
2978 */
2979 int
2980mch_ishidden(char_u *name)
2981{
2982 int f = win32_getattrs(name);
2983
2984 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002985 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002986
2987 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
2988}
2989
2990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 * return TRUE if "name" is a directory
2992 * return FALSE if "name" is not a directory or upon error
2993 */
2994 int
2995mch_isdir(char_u *name)
2996{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002997 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003000 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3003}
3004
3005/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003006 * return TRUE if "name" is a directory, NOT a symlink to a directory
3007 * return FALSE if "name" is not a directory
3008 * return FALSE for error
3009 */
3010 int
3011mch_isrealdir(char_u *name)
3012{
3013 return mch_isdir(name) && !mch_is_symbolic_link(name);
3014}
3015
3016/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003017 * Create directory "name".
3018 * Return 0 on success, -1 on error.
3019 */
3020 int
3021mch_mkdir(char_u *name)
3022{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003023 WCHAR *p;
3024 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003025
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003026 p = enc_to_utf16(name, NULL);
3027 if (p == NULL)
3028 return -1;
3029 retval = _wmkdir(p);
3030 vim_free(p);
3031 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003032}
3033
3034/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003035 * Delete directory "name".
3036 * Return 0 on success, -1 on error.
3037 */
3038 int
3039mch_rmdir(char_u *name)
3040{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003041 WCHAR *p;
3042 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003043
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003044 p = enc_to_utf16(name, NULL);
3045 if (p == NULL)
3046 return -1;
3047 retval = _wrmdir(p);
3048 vim_free(p);
3049 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003050}
3051
3052/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003053 * Return TRUE if file "fname" has more than one link.
3054 */
3055 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003056mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003057{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003058 BY_HANDLE_FILE_INFORMATION info;
3059
3060 return win32_fileinfo(fname, &info) == FILEINFO_OK
3061 && info.nNumberOfLinks > 1;
3062}
3063
3064/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003065 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003066 */
3067 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003068mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003069{
3070 HANDLE hFind;
3071 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003072 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003073 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003074 WIN32_FIND_DATAW findDataW;
3075
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003076 wn = enc_to_utf16(name, NULL);
3077 if (wn == NULL)
3078 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003079
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003080 hFind = FindFirstFileW(wn, &findDataW);
3081 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003082 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003083 {
3084 fileFlags = findDataW.dwFileAttributes;
3085 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003086 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003087 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003088
3089 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003090 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3091 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003092 res = TRUE;
3093
3094 return res;
3095}
3096
3097/*
3098 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3099 * link.
3100 */
3101 int
3102mch_is_linked(char_u *fname)
3103{
3104 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3105 return TRUE;
3106 return FALSE;
3107}
3108
3109/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003110 * Get the by-handle-file-information for "fname".
3111 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003112 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003113 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3114 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3115 */
3116 int
3117win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3118{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003119 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003120 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003121 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003122
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003123 wn = enc_to_utf16(fname, NULL);
3124 if (wn == NULL)
3125 return FILEINFO_ENC_FAIL;
3126
3127 hFile = CreateFileW(wn, // file name
3128 GENERIC_READ, // access mode
3129 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3130 NULL, // security descriptor
3131 OPEN_EXISTING, // creation disposition
3132 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3133 NULL); // handle to template file
3134 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003135
3136 if (hFile != INVALID_HANDLE_VALUE)
3137 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003138 if (GetFileInformationByHandle(hFile, info) != 0)
3139 res = FILEINFO_OK;
3140 else
3141 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003142 CloseHandle(hFile);
3143 }
3144
Bram Moolenaar03f48552006-02-28 23:52:23 +00003145 return res;
3146}
3147
3148/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003149 * get file attributes for `name'
3150 * -1 : error
3151 * else FILE_ATTRIBUTE_* defined in winnt.h
3152 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003153 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003154win32_getattrs(char_u *name)
3155{
3156 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003157 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003158
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003159 p = enc_to_utf16(name, NULL);
3160 if (p == NULL)
3161 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003162
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003163 attr = GetFileAttributesW(p);
3164 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003165
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003166 return attr;
3167}
3168
3169/*
3170 * set file attributes for `name' to `attrs'
3171 *
3172 * return -1 for failure, 0 otherwise
3173 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003174 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003175win32_setattrs(char_u *name, int attrs)
3176{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003177 int res;
3178 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003179
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003180 p = enc_to_utf16(name, NULL);
3181 if (p == NULL)
3182 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003183
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003184 res = SetFileAttributesW(p, attrs);
3185 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003186
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003187 return res ? 0 : -1;
3188}
3189
3190/*
3191 * Set archive flag for "name".
3192 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003193 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003194win32_set_archive(char_u *name)
3195{
3196 int attrs = win32_getattrs(name);
3197 if (attrs == -1)
3198 return -1;
3199
3200 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3201 return win32_setattrs(name, attrs);
3202}
3203
3204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 * Return TRUE if file or directory "name" is writable (not readonly).
3206 * Strange semantics of Win32: a readonly directory is writable, but you can't
3207 * delete a file. Let's say this means it is writable.
3208 */
3209 int
3210mch_writable(char_u *name)
3211{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003212 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003214 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3215 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216}
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003219 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003220 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003221 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3222 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 */
3224 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003225mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
Bram Moolenaar86621892019-03-30 21:51:28 +01003227 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3228 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3229 // UTF-8.
3230 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003231 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003232 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003233
Bram Moolenaar86621892019-03-30 21:51:28 +01003234 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003235 return FALSE;
3236
Bram Moolenaar86621892019-03-30 21:51:28 +01003237 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003238 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003239 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003240 return TRUE;
3241
3242 /*
3243 * Loop over all extensions in $PATHEXT.
3244 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003245 p = mch_getenv("PATHEXT");
3246 if (p == NULL)
3247 p = (char_u *)".com;.exe;.bat;.cmd";
3248 saved = vim_strsave(p);
3249 if (saved == NULL)
3250 return FALSE;
3251 p = saved;
3252 while (*p)
3253 {
3254 char_u *tmp = vim_strchr(p, ';');
3255
3256 if (tmp != NULL)
3257 *tmp = NUL;
3258 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3259 && executable_exists((char *)name, path, use_path))
3260 {
3261 vim_free(saved);
3262 return TRUE;
3263 }
3264 if (tmp == NULL)
3265 break;
3266 p = tmp + 1;
3267 }
3268 vim_free(saved);
3269
Bram Moolenaar86621892019-03-30 21:51:28 +01003270 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003271 p = mch_getenv("PATHEXT");
3272 if (p == NULL)
3273 p = (char_u *)".com;.exe;.bat;.cmd";
3274 while (*p)
3275 {
3276 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3277 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003278 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003279 buf[len] = NUL;
3280 ++p;
3281 if (*p)
3282 ++p;
3283 }
3284 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003285 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003286 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003287 return TRUE;
3288 }
3289 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292/*
3293 * Check what "name" is:
3294 * NODE_NORMAL: file or directory (or doesn't exist)
3295 * NODE_WRITABLE: writable device, socket, fifo, etc.
3296 * NODE_OTHER: non-writable things
3297 */
3298 int
3299mch_nodetype(char_u *name)
3300{
3301 HANDLE hFile;
3302 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003303 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar0f873732019-12-05 20:28:46 +01003305 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3306 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3307 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003308 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3309 return NODE_WRITABLE;
3310
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003311 wn = enc_to_utf16(name, NULL);
3312 if (wn == NULL)
3313 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003314
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003315 hFile = CreateFileW(wn, // file name
3316 GENERIC_WRITE, // access mode
3317 0, // share mode
3318 NULL, // security descriptor
3319 OPEN_EXISTING, // creation disposition
3320 0, // file attributes
3321 NULL); // handle to template file
3322 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (hFile == INVALID_HANDLE_VALUE)
3324 return NODE_NORMAL;
3325
3326 type = GetFileType(hFile);
3327 CloseHandle(hFile);
3328 if (type == FILE_TYPE_CHAR)
3329 return NODE_WRITABLE;
3330 if (type == FILE_TYPE_DISK)
3331 return NODE_NORMAL;
3332 return NODE_OTHER;
3333}
3334
3335#ifdef HAVE_ACL
3336struct my_acl
3337{
3338 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3339 PSID pSidOwner;
3340 PSID pSidGroup;
3341 PACL pDacl;
3342 PACL pSacl;
3343};
3344#endif
3345
3346/*
3347 * Return a pointer to the ACL of file "fname" in allocated memory.
3348 * Return NULL if the ACL is not available for whatever reason.
3349 */
3350 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003351mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353#ifndef HAVE_ACL
3354 return (vim_acl_T)NULL;
3355#else
3356 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003357 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003359 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003360 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003362 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003363
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003364 wn = enc_to_utf16(fname, NULL);
3365 if (wn == NULL)
3366 return NULL;
3367
3368 // Try to retrieve the entire security descriptor.
3369 err = GetNamedSecurityInfoW(
3370 wn, // Abstract filename
3371 SE_FILE_OBJECT, // File Object
3372 OWNER_SECURITY_INFORMATION |
3373 GROUP_SECURITY_INFORMATION |
3374 DACL_SECURITY_INFORMATION |
3375 SACL_SECURITY_INFORMATION,
3376 &p->pSidOwner, // Ownership information.
3377 &p->pSidGroup, // Group membership.
3378 &p->pDacl, // Discretionary information.
3379 &p->pSacl, // For auditing purposes.
3380 &p->pSecurityDescriptor);
3381 if (err == ERROR_ACCESS_DENIED ||
3382 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003383 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003384 // Retrieve only DACL.
3385 (void)GetNamedSecurityInfoW(
3386 wn,
3387 SE_FILE_OBJECT,
3388 DACL_SECURITY_INFORMATION,
3389 NULL,
3390 NULL,
3391 &p->pDacl,
3392 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003393 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003394 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003395 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003396 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003397 mch_free_acl((vim_acl_T)p);
3398 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003400 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402
3403 return (vim_acl_T)p;
3404#endif
3405}
3406
Bram Moolenaar27515922013-06-29 15:36:26 +02003407#ifdef HAVE_ACL
3408/*
3409 * Check if "acl" contains inherited ACE.
3410 */
3411 static BOOL
3412is_acl_inherited(PACL acl)
3413{
3414 DWORD i;
3415 ACL_SIZE_INFORMATION acl_info;
3416 PACCESS_ALLOWED_ACE ace;
3417
3418 acl_info.AceCount = 0;
3419 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3420 for (i = 0; i < acl_info.AceCount; i++)
3421 {
3422 GetAce(acl, i, (LPVOID *)&ace);
3423 if (ace->Header.AceFlags & INHERITED_ACE)
3424 return TRUE;
3425 }
3426 return FALSE;
3427}
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430/*
3431 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3432 * Errors are ignored.
3433 * This must only be called with "acl" equal to what mch_get_acl() returned.
3434 */
3435 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003436mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438#ifdef HAVE_ACL
3439 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003440 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003441 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003443 if (p == NULL)
3444 return;
3445
3446 wn = enc_to_utf16(fname, NULL);
3447 if (wn == NULL)
3448 return;
3449
3450 // Set security flags
3451 if (p->pSidOwner)
3452 sec_info |= OWNER_SECURITY_INFORMATION;
3453 if (p->pSidGroup)
3454 sec_info |= GROUP_SECURITY_INFORMATION;
3455 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003456 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003457 sec_info |= DACL_SECURITY_INFORMATION;
3458 // Do not inherit its parent's DACL.
3459 // If the DACL is inherited, Cygwin permissions would be changed.
3460 if (!is_acl_inherited(p->pDacl))
3461 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003462 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003463 if (p->pSacl)
3464 sec_info |= SACL_SECURITY_INFORMATION;
3465
3466 (void)SetNamedSecurityInfoW(
3467 wn, // Abstract filename
3468 SE_FILE_OBJECT, // File Object
3469 sec_info,
3470 p->pSidOwner, // Ownership information.
3471 p->pSidGroup, // Group membership.
3472 p->pDacl, // Discretionary information.
3473 p->pSacl // For auditing purposes.
3474 );
3475 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
3477}
3478
3479 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003480mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482#ifdef HAVE_ACL
3483 struct my_acl *p = (struct my_acl *)acl;
3484
3485 if (p != NULL)
3486 {
3487 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3488 vim_free(p);
3489 }
3490#endif
3491}
3492
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003493#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495/*
3496 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3497 */
3498 static BOOL WINAPI
3499handler_routine(
3500 DWORD dwCtrlType)
3501{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003502 INPUT_RECORD ir;
3503 DWORD out;
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 switch (dwCtrlType)
3506 {
3507 case CTRL_C_EVENT:
3508 if (ctrl_c_interrupts)
3509 g_fCtrlCPressed = TRUE;
3510 return TRUE;
3511
3512 case CTRL_BREAK_EVENT:
3513 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003514 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003515 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003516 ir.EventType = KEY_EVENT;
3517 ir.Event.KeyEvent.bKeyDown = TRUE;
3518 ir.Event.KeyEvent.wRepeatCount = 1;
3519 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3520 ir.Event.KeyEvent.wVirtualScanCode = 0;
3521 ir.Event.KeyEvent.dwControlKeyState = 0;
3522 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3523 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return TRUE;
3525
Bram Moolenaar0f873732019-12-05 20:28:46 +01003526 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 case CTRL_CLOSE_EVENT:
3528 case CTRL_LOGOFF_EVENT:
3529 case CTRL_SHUTDOWN_EVENT:
3530 windgoto((int)Rows - 1, 0);
3531 g_fForceExit = TRUE;
3532
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003533 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 (dwCtrlType == CTRL_CLOSE_EVENT
3535 ? _("close")
3536 : dwCtrlType == CTRL_LOGOFF_EVENT
3537 ? _("logoff")
3538 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003539# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar0f873732019-12-05 20:28:46 +01003543 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar0f873732019-12-05 20:28:46 +01003545 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 default:
3548 return FALSE;
3549 }
3550}
3551
3552
3553/*
3554 * set the tty in (raw) ? "raw" : "cooked" mode
3555 */
3556 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003557mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 DWORD cmodein;
3560 DWORD cmodeout;
3561 BOOL bEnableHandler;
3562
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003563# ifdef VIMDLL
3564 if (gui.in_use)
3565 return;
3566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 GetConsoleMode(g_hConIn, &cmodein);
3568 GetConsoleMode(g_hConOut, &cmodeout);
3569 if (tmode == TMODE_RAW)
3570 {
3571 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3572 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (g_fMouseActive)
3574 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003575 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003576# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003577 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3578 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003579 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003580# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003581 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003582# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003583 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 bEnableHandler = TRUE;
3585 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003586 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3589 ENABLE_ECHO_INPUT);
3590 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3591 bEnableHandler = FALSE;
3592 }
3593 SetConsoleMode(g_hConIn, cmodein);
3594 SetConsoleMode(g_hConOut, cmodeout);
3595 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3596
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003597# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (fdDump)
3599 {
3600 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3601 tmode == TMODE_RAW ? "raw" :
3602 tmode == TMODE_COOK ? "cooked" : "normal",
3603 cmodein, cmodeout);
3604 fflush(fdDump);
3605 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607}
3608
3609
3610/*
3611 * Get the size of the current window in `Rows' and `Columns'
3612 * Return OK when size could be determined, FAIL otherwise.
3613 */
3614 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003615mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 CONSOLE_SCREEN_BUFFER_INFO csbi;
3618
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003619# ifdef VIMDLL
3620 if (gui.in_use)
3621 return OK;
3622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3624 {
3625 /*
3626 * For some reason, we are trying to get the screen dimensions
3627 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3628 * variables are really intended to mean the size of Vim screen
3629 * while in termcap mode.
3630 */
3631 Rows = g_cbTermcap.Info.dwSize.Y;
3632 Columns = g_cbTermcap.Info.dwSize.X;
3633 }
3634 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3635 {
3636 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3637 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3638 }
3639 else
3640 {
3641 Rows = 25;
3642 Columns = 80;
3643 }
3644 return OK;
3645}
3646
3647/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003648 * Resize console buffer to 'COORD'
3649 */
3650 static void
3651ResizeConBuf(
3652 HANDLE hConsole,
3653 COORD coordScreen)
3654{
3655 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3656 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003657# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003658 if (fdDump)
3659 {
3660 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3661 GetLastError());
3662 fflush(fdDump);
3663 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003664# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003665 }
3666}
3667
3668/*
3669 * Resize console window size to 'srWindowRect'
3670 */
3671 static void
3672ResizeWindow(
3673 HANDLE hConsole,
3674 SMALL_RECT srWindowRect)
3675{
3676 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3677 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003678# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003679 if (fdDump)
3680 {
3681 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3682 GetLastError());
3683 fflush(fdDump);
3684 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003685# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003686 }
3687}
3688
3689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 * Set a console window to `xSize' * `ySize'
3691 */
3692 static void
3693ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003694 HANDLE hConsole,
3695 int xSize,
3696 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003698 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3699 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003701 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003702 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003704# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (fdDump)
3706 {
3707 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3708 fflush(fdDump);
3709 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar0f873732019-12-05 20:28:46 +01003712 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 coordScreen = GetLargestConsoleWindowSize(hConsole);
3714
Bram Moolenaar0f873732019-12-05 20:28:46 +01003715 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3717 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3718 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3719
3720 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3721 {
3722 int sx, sy;
3723
3724 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3725 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3726 if (sy < ySize || sx < xSize)
3727 {
3728 /*
3729 * Increasing number of lines/columns, do buffer first.
3730 * Use the maximal size in x and y direction.
3731 */
3732 if (sy < ySize)
3733 coordScreen.Y = ySize;
3734 else
3735 coordScreen.Y = sy;
3736 if (sx < xSize)
3737 coordScreen.X = xSize;
3738 else
3739 coordScreen.X = sx;
3740 SetConsoleScreenBufferSize(hConsole, coordScreen);
3741 }
3742 }
3743
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003744 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 coordScreen.X = xSize;
3746 coordScreen.Y = ySize;
3747
Bram Moolenaar2551c032018-08-23 22:38:31 +02003748 // In the new console call API, only the first time in reverse order
3749 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003751 ResizeWindow(hConsole, srWindowRect);
3752 ResizeConBuf(hConsole, coordScreen);
3753 }
3754 else
3755 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003756 // Workaround for a Windows 10 bug
3757 cursor.X = srWindowRect.Left;
3758 cursor.Y = srWindowRect.Top;
3759 SetConsoleCursorPosition(hConsole, cursor);
3760
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003761 ResizeConBuf(hConsole, coordScreen);
3762 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003763 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765}
3766
3767
3768/*
3769 * Set the console window to `Rows' * `Columns'
3770 */
3771 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003772mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 COORD coordScreen;
3775
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003776# ifdef VIMDLL
3777 if (gui.in_use)
3778 return;
3779# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003780 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (suppress_winsize != 0)
3782 {
3783 suppress_winsize = 2;
3784 return;
3785 }
3786
3787 if (term_console)
3788 {
3789 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3790
Bram Moolenaar0f873732019-12-05 20:28:46 +01003791 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (Rows > coordScreen.Y)
3793 Rows = coordScreen.Y;
3794 if (Columns > coordScreen.X)
3795 Columns = coordScreen.X;
3796
3797 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3798 }
3799}
3800
3801/*
3802 * Rows and/or Columns has changed.
3803 */
3804 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003805mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003807# ifdef VIMDLL
3808 if (gui.in_use)
3809 return;
3810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3812}
3813
3814
3815/*
3816 * Called when started up, to set the winsize that was delayed.
3817 */
3818 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003819mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 if (suppress_winsize == 2)
3822 {
3823 suppress_winsize = 0;
3824 mch_set_shellsize();
3825 shell_resized();
3826 }
3827 suppress_winsize = 0;
3828}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003829#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003831 static BOOL
3832vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003833 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003834 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003835 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003836 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003837 PROCESS_INFORMATION *pi,
3838 LPVOID *env,
3839 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003840{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003841 BOOL ret = FALSE;
3842 WCHAR *wcmd, *wcwd = NULL;
3843
3844 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3845 if (wcmd == NULL)
3846 return FALSE;
3847 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003848 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003849 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3850 if (wcwd == NULL)
3851 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003852 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003853
3854 ret = CreateProcessW(
3855 NULL, // Executable name
3856 wcmd, // Command to execute
3857 NULL, // Process security attributes
3858 NULL, // Thread security attributes
3859 inherit_handles, // Inherit handles
3860 flags, // Creation flags
3861 env, // Environment
3862 wcwd, // Current directory
3863 (LPSTARTUPINFOW)si, // Startup information
3864 pi); // Process information
3865theend:
3866 vim_free(wcmd);
3867 vim_free(wcwd);
3868 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003872 static HINSTANCE
3873vim_shell_execute(
3874 char *cmd,
3875 INT n_show_cmd)
3876{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003877 HINSTANCE ret;
3878 WCHAR *wcmd;
3879
3880 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3881 if (wcmd == NULL)
3882 return (HINSTANCE) 0;
3883
3884 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3885 vim_free(wcmd);
3886 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003887}
3888
3889
Bram Moolenaar4f974752019-02-17 17:44:42 +01003890#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892/*
3893 * Specialised version of system() for Win32 GUI mode.
3894 * This version proceeds as follows:
3895 * 1. Create a console window for use by the subprocess
3896 * 2. Run the subprocess (it gets the allocated console by default)
3897 * 3. Wait for the subprocess to terminate and get its exit code
3898 * 4. Prompt the user to press a key to close the console window
3899 */
3900 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003901mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 STARTUPINFO si;
3904 PROCESS_INFORMATION pi;
3905 DWORD ret = 0;
3906 HWND hwnd = GetFocus();
3907
3908 si.cb = sizeof(si);
3909 si.lpReserved = NULL;
3910 si.lpDesktop = NULL;
3911 si.lpTitle = NULL;
3912 si.dwFlags = STARTF_USESHOWWINDOW;
3913 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003914 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003915 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003917 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003918 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 si.wShowWindow = SW_SHOWNORMAL;
3921 si.cbReserved2 = 0;
3922 si.lpReserved2 = NULL;
3923
Bram Moolenaar0f873732019-12-05 20:28:46 +01003924 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003925 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003926 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3927 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar0f873732019-12-05 20:28:46 +01003929 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003931# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 int delay = 1;
3933
Bram Moolenaar0f873732019-12-05 20:28:46 +01003934 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 for (;;)
3936 {
3937 MSG msg;
3938
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003939 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
3941 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003942 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003943 delay = 1;
3944 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3947 break;
3948
Bram Moolenaar0f873732019-12-05 20:28:46 +01003949 // We start waiting for a very short time and then increase it, so
3950 // that we respond quickly when the process is quick, and don't
3951 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (delay < 50)
3953 delay += 10;
3954 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003955# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar0f873732019-12-05 20:28:46 +01003959 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 GetExitCodeProcess(pi.hProcess, &ret);
3961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
Bram Moolenaar0f873732019-12-05 20:28:46 +01003963 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 CloseHandle(pi.hThread);
3965 CloseHandle(pi.hProcess);
3966
Bram Moolenaar0f873732019-12-05 20:28:46 +01003967 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3969
3970 return ret;
3971}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003972
3973/*
3974 * Thread launched by the gui to send the current buffer data to the
3975 * process. This way avoid to hang up vim totally if the children
3976 * process take a long time to process the lines.
3977 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02003978 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003979sub_process_writer(LPVOID param)
3980{
3981 HANDLE g_hChildStd_IN_Wr = param;
3982 linenr_T lnum = curbuf->b_op_start.lnum;
3983 DWORD len = 0;
3984 DWORD l;
3985 char_u *lp = ml_get(lnum);
3986 char_u *s;
3987 int written = 0;
3988
3989 for (;;)
3990 {
3991 l = (DWORD)STRLEN(lp + written);
3992 if (l == 0)
3993 len = 0;
3994 else if (lp[written] == NL)
3995 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003996 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003997 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
3998 }
3999 else
4000 {
4001 s = vim_strchr(lp + written, NL);
4002 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4003 s == NULL ? l : (DWORD)(s - (lp + written)),
4004 &len, NULL);
4005 }
4006 if (len == (int)l)
4007 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004008 // Finished a line, add a NL, unless this line should not have
4009 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004010 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004011 || (!curbuf->b_p_bin
4012 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004013 || (lnum != curbuf->b_no_eol_lnum
4014 && (lnum != curbuf->b_ml.ml_line_count
4015 || curbuf->b_p_eol)))
4016 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004017 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4018 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004019 }
4020
4021 ++lnum;
4022 if (lnum > curbuf->b_op_end.lnum)
4023 break;
4024
4025 lp = ml_get(lnum);
4026 written = 0;
4027 }
4028 else if (len > 0)
4029 written += len;
4030 }
4031
Bram Moolenaar0f873732019-12-05 20:28:46 +01004032 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004033 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004034 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004035}
4036
4037
Bram Moolenaar0f873732019-12-05 20:28:46 +01004038# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004039
4040/*
4041 * This function read from the children's stdout and write the
4042 * data on screen or in the buffer accordingly.
4043 */
4044 static void
4045dump_pipe(int options,
4046 HANDLE g_hChildStd_OUT_Rd,
4047 garray_T *ga,
4048 char_u buffer[],
4049 DWORD *buffer_off)
4050{
4051 DWORD availableBytes = 0;
4052 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004053 int ret;
4054 DWORD len;
4055 DWORD toRead;
4056 int repeatCount;
4057
Bram Moolenaar0f873732019-12-05 20:28:46 +01004058 // we query the pipe to see if there is any data to read
4059 // to avoid to perform a blocking read
4060 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4061 NULL, // optional buffer
4062 0, // buffer size
4063 NULL, // number of read bytes
4064 &availableBytes, // available bytes total
4065 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004066
4067 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004068 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004069 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004070 {
4071 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004072 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004073 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004074 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004075
Bram Moolenaar0f873732019-12-05 20:28:46 +01004076 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004077 if (len == 0)
4078 break;
4079
4080 availableBytes -= len;
4081
4082 if (options & SHELL_READ)
4083 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084 // Do NUL -> NL translation, append NL separated
4085 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004086 for (i = 0; i < len; ++i)
4087 {
4088 if (buffer[i] == NL)
4089 append_ga_line(ga);
4090 else if (buffer[i] == NUL)
4091 ga_append(ga, NL);
4092 else
4093 ga_append(ga, buffer[i]);
4094 }
4095 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004096 else if (has_mbyte)
4097 {
4098 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004099 int c;
4100 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004101
4102 len += *buffer_off;
4103 buffer[len] = NUL;
4104
Bram Moolenaar0f873732019-12-05 20:28:46 +01004105 // Check if the last character in buffer[] is
4106 // incomplete, keep these bytes for the next
4107 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004108 for (p = buffer; p < buffer + len; p += l)
4109 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004110 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004111 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004113 else if (MB_BYTE2LEN(*p) != l)
4114 break;
4115 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004116 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004117 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004119 if (len >= 12)
4120 ++p;
4121 else
4122 {
4123 *buffer_off = len;
4124 return;
4125 }
4126 }
4127 c = *p;
4128 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004129 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004130 if (p < buffer + len)
4131 {
4132 *p = c;
4133 *buffer_off = (DWORD)((buffer + len) - p);
4134 mch_memmove(buffer, p, *buffer_off);
4135 return;
4136 }
4137 *buffer_off = 0;
4138 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004139 else
4140 {
4141 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004142 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004143 }
4144
4145 windgoto(msg_row, msg_col);
4146 cursor_on();
4147 out_flush();
4148 }
4149}
4150
4151/*
4152 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4153 * for communication and doesn't open any new window.
4154 */
4155 static int
4156mch_system_piped(char *cmd, int options)
4157{
4158 STARTUPINFO si;
4159 PROCESS_INFORMATION pi;
4160 DWORD ret = 0;
4161
4162 HANDLE g_hChildStd_IN_Rd = NULL;
4163 HANDLE g_hChildStd_IN_Wr = NULL;
4164 HANDLE g_hChildStd_OUT_Rd = NULL;
4165 HANDLE g_hChildStd_OUT_Wr = NULL;
4166
Bram Moolenaar0f873732019-12-05 20:28:46 +01004167 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004168 DWORD len;
4169
Bram Moolenaar0f873732019-12-05 20:28:46 +01004170 // buffer used to receive keys
4171 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4172 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004173
4174 DWORD i;
4175 int c;
4176 int noread_cnt = 0;
4177 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004178 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004179 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004180 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004181
4182 SECURITY_ATTRIBUTES saAttr;
4183
Bram Moolenaar0f873732019-12-05 20:28:46 +01004184 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004185 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4186 saAttr.bInheritHandle = TRUE;
4187 saAttr.lpSecurityDescriptor = NULL;
4188
4189 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004190 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004191 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004192 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004193 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004194 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004195 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004196 {
4197 CloseHandle(g_hChildStd_IN_Rd);
4198 CloseHandle(g_hChildStd_IN_Wr);
4199 CloseHandle(g_hChildStd_OUT_Rd);
4200 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004202 }
4203
4204 si.cb = sizeof(si);
4205 si.lpReserved = NULL;
4206 si.lpDesktop = NULL;
4207 si.lpTitle = NULL;
4208 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4209
Bram Moolenaar0f873732019-12-05 20:28:46 +01004210 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004211 si.hStdError = g_hChildStd_OUT_Wr;
4212 si.hStdOutput = g_hChildStd_OUT_Wr;
4213 si.hStdInput = g_hChildStd_IN_Rd;
4214 si.wShowWindow = SW_HIDE;
4215 si.cbReserved2 = 0;
4216 si.lpReserved2 = NULL;
4217
4218 if (options & SHELL_READ)
4219 ga_init2(&ga, 1, BUFLEN);
4220
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004221 if (cmd != NULL)
4222 {
4223 p = (char *)vim_strsave((char_u *)cmd);
4224 if (p != NULL)
4225 unescape_shellxquote((char_u *)p, p_sxe);
4226 else
4227 p = cmd;
4228 }
4229
Bram Moolenaar0f873732019-12-05 20:28:46 +01004230 // Now, run the command.
4231 // About "Inherit handles" being TRUE: this command can be litigious,
4232 // handle inheritance was deactivated for pending temp file, but, if we
4233 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004234 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4235 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004236
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004237 if (p != cmd)
4238 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004239
Bram Moolenaar0f873732019-12-05 20:28:46 +01004240 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004241 CloseHandle(g_hChildStd_IN_Rd);
4242 CloseHandle(g_hChildStd_OUT_Wr);
4243
4244 if (options & SHELL_WRITE)
4245 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004246 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004247 _beginthreadex(NULL, // security attributes
4248 0, // default stack size
4249 sub_process_writer, // function to be executed
4250 g_hChildStd_IN_Wr, // parameter
4251 0, // creation flag, start immediately
4252 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004253 CloseHandle(thread);
4254 g_hChildStd_IN_Wr = NULL;
4255 }
4256
Bram Moolenaar0f873732019-12-05 20:28:46 +01004257 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004258 for (;;)
4259 {
4260 MSG msg;
4261
Bram Moolenaar175d0702013-12-11 18:36:33 +01004262 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004263 {
4264 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004265 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004266 }
4267
Bram Moolenaar0f873732019-12-05 20:28:46 +01004268 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004269 if ((options & (SHELL_READ|SHELL_WRITE))
4270# ifdef FEAT_GUI
4271 || gui.in_use
4272# endif
4273 )
4274 {
4275 len = 0;
4276 if (!(options & SHELL_EXPAND)
4277 && ((options &
4278 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4279 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4280# ifdef FEAT_GUI
4281 || gui.in_use
4282# endif
4283 )
4284 && (ta_len > 0 || noread_cnt > 4))
4285 {
4286 if (ta_len == 0)
4287 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 // Get extra characters when we don't have any. Reset the
4289 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004290 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004291 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4292 }
4293 if (ta_len > 0 || len > 0)
4294 {
4295 /*
4296 * For pipes: Check for CTRL-C: send interrupt signal to
4297 * child. Check for CTRL-D: EOF, close pipe to child.
4298 */
4299 if (len == 1 && cmd != NULL)
4300 {
4301 if (ta_buf[ta_len] == Ctrl_C)
4302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004303 // Learn what exit code is expected, for
4304 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004305 TerminateProcess(pi.hProcess, 9);
4306 }
4307 if (ta_buf[ta_len] == Ctrl_D)
4308 {
4309 CloseHandle(g_hChildStd_IN_Wr);
4310 g_hChildStd_IN_Wr = NULL;
4311 }
4312 }
4313
Bram Moolenaar0f873732019-12-05 20:28:46 +01004314 // replace K_BS by <BS> and K_DEL by <DEL>
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004315 for (i = ta_len; i < ta_len + len; ++i)
4316 {
4317 if (ta_buf[i] == CSI && len - i > 2)
4318 {
4319 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4320 if (c == K_DEL || c == K_KDEL || c == K_BS)
4321 {
4322 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4323 (size_t)(len - i - 2));
4324 if (c == K_DEL || c == K_KDEL)
4325 ta_buf[i] = DEL;
4326 else
4327 ta_buf[i] = Ctrl_H;
4328 len -= 2;
4329 }
4330 }
4331 else if (ta_buf[i] == '\r')
4332 ta_buf[i] = '\n';
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004333 if (has_mbyte)
4334 i += (*mb_ptr2len_len)(ta_buf + i,
4335 ta_len + len - i) - 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004336 }
4337
4338 /*
4339 * For pipes: echo the typed characters. For a pty this
4340 * does not seem to work.
4341 */
4342 for (i = ta_len; i < ta_len + len; ++i)
4343 {
4344 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4345 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004346 else if (has_mbyte)
4347 {
4348 int l = (*mb_ptr2len)(ta_buf + i);
4349
4350 msg_outtrans_len(ta_buf + i, l);
4351 i += l - 1;
4352 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004353 else
4354 msg_outtrans_len(ta_buf + i, 1);
4355 }
4356 windgoto(msg_row, msg_col);
4357 out_flush();
4358
4359 ta_len += len;
4360
4361 /*
4362 * Write the characters to the child, unless EOF has been
4363 * typed for pipes. Write one character at a time, to
4364 * avoid losing too much typeahead. When writing buffer
4365 * lines, drop the typed characters (only check for
4366 * CTRL-C).
4367 */
4368 if (options & SHELL_WRITE)
4369 ta_len = 0;
4370 else if (g_hChildStd_IN_Wr != NULL)
4371 {
4372 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4373 1, &len, NULL);
4374 // if we are typing in, we want to keep things reactive
4375 delay = 1;
4376 if (len > 0)
4377 {
4378 ta_len -= len;
4379 mch_memmove(ta_buf, ta_buf + len, ta_len);
4380 }
4381 }
4382 }
4383 }
4384 }
4385
4386 if (ta_len)
4387 ui_inchar_undo(ta_buf, ta_len);
4388
4389 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4390 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004391 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004392 break;
4393 }
4394
4395 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004396 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004397
Bram Moolenaar0f873732019-12-05 20:28:46 +01004398 // We start waiting for a very short time and then increase it, so
4399 // that we respond quickly when the process is quick, and don't
4400 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004401 if (delay < 50)
4402 delay += 10;
4403 }
4404
Bram Moolenaar0f873732019-12-05 20:28:46 +01004405 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004406 CloseHandle(g_hChildStd_OUT_Rd);
4407 if (g_hChildStd_IN_Wr != NULL)
4408 CloseHandle(g_hChildStd_IN_Wr);
4409
4410 WaitForSingleObject(pi.hProcess, INFINITE);
4411
Bram Moolenaar0f873732019-12-05 20:28:46 +01004412 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004413 GetExitCodeProcess(pi.hProcess, &ret);
4414
4415 if (options & SHELL_READ)
4416 {
4417 if (ga.ga_len > 0)
4418 {
4419 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004420 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004421 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4422 }
4423 else
4424 curbuf->b_no_eol_lnum = 0;
4425 ga_clear(&ga);
4426 }
4427
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004429 CloseHandle(pi.hThread);
4430 CloseHandle(pi.hProcess);
4431
4432 return ret;
4433}
4434
4435 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004436mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004437{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004438 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004439 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004440 return mch_system_piped(cmd, options);
4441 else
4442 return mch_system_classic(cmd, options);
4443}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004446#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004447 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004448mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004449{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004450 int ret;
4451 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004452 char_u *buf;
4453 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004454
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004455 // If the command starts and ends with double quotes, enclose the command
4456 // in parentheses.
4457 len = STRLEN(cmd);
4458 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4459 {
4460 len += 3;
4461 buf = alloc(len);
4462 if (buf == NULL)
4463 return -1;
4464 vim_snprintf((char *)buf, len, "(%s)", cmd);
4465 wcmd = enc_to_utf16(buf, NULL);
4466 free(buf);
4467 }
4468 else
4469 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4470
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004471 if (wcmd == NULL)
4472 return -1;
4473
4474 ret = _wsystem(wcmd);
4475 vim_free(wcmd);
4476 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479#endif
4480
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004481 static int
4482mch_system(char *cmd, int options)
4483{
4484#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004485 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004486 return mch_system_g(cmd, options);
4487 else
4488 return mch_system_c(cmd, options);
4489#elif defined(FEAT_GUI_MSWIN)
4490 return mch_system_g(cmd, options);
4491#else
4492 return mch_system_c(cmd, options);
4493#endif
4494}
4495
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004496#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4497/*
4498 * Use a terminal window to run a shell command in.
4499 */
4500 static int
4501mch_call_shell_terminal(
4502 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004503 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004504{
4505 jobopt_T opt;
4506 char_u *newcmd = NULL;
4507 typval_T argvar[2];
4508 long_u cmdlen;
4509 int retval = -1;
4510 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004511 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004512 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004513 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004514
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004515 if (cmd == NULL)
4516 cmdlen = STRLEN(p_sh) + 1;
4517 else
4518 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004519 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004520 if (newcmd == NULL)
4521 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004522 if (cmd == NULL)
4523 {
4524 STRCPY(newcmd, p_sh);
4525 ch_log(NULL, "starting terminal to run a shell");
4526 }
4527 else
4528 {
4529 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4530 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4531 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004532
4533 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004534
4535 argvar[0].v_type = VAR_STRING;
4536 argvar[0].vval.v_string = newcmd;
4537 argvar[1].v_type = VAR_UNKNOWN;
4538 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004539 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004540 {
4541 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004542 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004543 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004544
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004545 job = term_getjob(buf->b_term);
4546 ++job->jv_refcount;
4547
Bram Moolenaar0f873732019-12-05 20:28:46 +01004548 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004549 aucmd_prepbuf(&aco, buf);
4550
4551 clear_oparg(&oa);
4552 while (term_use_loop())
4553 {
4554 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4555 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556 // If terminal_loop() returns OK we got a key that is handled
4557 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004558 if (terminal_loop(TRUE) == OK)
4559 normal_cmd(&oa, TRUE);
4560 }
4561 else
4562 normal_cmd(&oa, TRUE);
4563 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004564 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004565 ch_log(NULL, "system command finished");
4566
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004567 job_unref(job);
4568
Bram Moolenaar0f873732019-12-05 20:28:46 +01004569 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004570 aucmd_restbuf(&aco);
4571
4572 wait_return(TRUE);
4573 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4574
4575 vim_free(newcmd);
4576 return retval;
4577}
4578#endif
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580/*
4581 * Either execute a command by calling the shell or start a new shell
4582 */
4583 int
4584mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004585 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004586 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587{
4588 int x = 0;
4589 int tmode = cur_tmode;
4590#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004591 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004592
Bram Moolenaar0f873732019-12-05 20:28:46 +01004593 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004594 if (GetConsoleTitleW(szShellTitle,
4595 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004596 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004597 if (cmd == NULL)
4598 wcscat(szShellTitle, L" :sh");
4599 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004600 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004601 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004602
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004603 if (wn != NULL)
4604 {
4605 wcscat(szShellTitle, L" - !");
4606 if ((wcslen(szShellTitle) + wcslen(wn) <
4607 sizeof(szShellTitle)/sizeof(WCHAR)))
4608 wcscat(szShellTitle, wn);
4609 SetConsoleTitleW(szShellTitle);
4610 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004611 }
4612 }
4613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
4615
4616 out_flush();
4617
4618#ifdef MCH_WRITE_DUMP
4619 if (fdDump)
4620 {
4621 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4622 fflush(fdDump);
4623 }
4624#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004625#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004626 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004627 if (
4628# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004629 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004630# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004631 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004632 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4633 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004634 char_u *cmdbase = cmd;
4635
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004636 if (cmdbase != NULL)
4637 // Skip a leading quote and (.
4638 while (*cmdbase == '"' || *cmdbase == '(')
4639 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004640
4641 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004642 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4643 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004644 {
4645 // Use a terminal window to run the command in.
4646 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004647# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004648 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004649# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004650 return x;
4651 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004652 }
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 /*
4656 * Catch all deadly signals while running the external command, because a
4657 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4658 */
4659 signal(SIGINT, SIG_IGN);
4660#if defined(__GNUC__) && !defined(__MINGW32__)
4661 signal(SIGKILL, SIG_IGN);
4662#else
4663 signal(SIGBREAK, SIG_IGN);
4664#endif
4665 signal(SIGILL, SIG_IGN);
4666 signal(SIGFPE, SIG_IGN);
4667 signal(SIGSEGV, SIG_IGN);
4668 signal(SIGTERM, SIG_IGN);
4669 signal(SIGABRT, SIG_IGN);
4670
4671 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 if (cmd == NULL)
4675 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004676 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678 else
4679 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004680 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004681 char_u *newcmd = NULL;
4682 char_u *cmdbase = cmd;
4683 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004684
Bram Moolenaar0f873732019-12-05 20:28:46 +01004685 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004686 if (*cmdbase == '"' )
4687 ++cmdbase;
4688 if (*cmdbase == '(')
4689 ++cmdbase;
4690
Bram Moolenaar1c465442017-03-12 20:10:05 +01004691 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004692 {
4693 STARTUPINFO si;
4694 PROCESS_INFORMATION pi;
4695 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004696 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004697 char_u *p;
4698
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004699 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004700 si.cb = sizeof(si);
4701 si.lpReserved = NULL;
4702 si.lpDesktop = NULL;
4703 si.lpTitle = NULL;
4704 si.dwFlags = 0;
4705 si.cbReserved2 = 0;
4706 si.lpReserved2 = NULL;
4707
4708 cmdbase = skipwhite(cmdbase + 5);
4709 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004710 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004711 {
4712 cmdbase = skipwhite(cmdbase + 4);
4713 si.dwFlags = STARTF_USESHOWWINDOW;
4714 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004715 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004716 }
4717 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004718 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004719 {
4720 cmdbase = skipwhite(cmdbase + 2);
4721 flags = CREATE_NO_WINDOW;
4722 si.dwFlags = STARTF_USESTDHANDLES;
4723 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004724 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004725 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004726 NULL, // Security att.
4727 OPEN_EXISTING, // Open flags
4728 FILE_ATTRIBUTE_NORMAL, // File att.
4729 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004730 si.hStdOutput = si.hStdInput;
4731 si.hStdError = si.hStdInput;
4732 }
4733
Bram Moolenaar0f873732019-12-05 20:28:46 +01004734 // Remove a trailing ", ) and )" if they have a match
4735 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004736 if (cmdbase > cmd)
4737 {
4738 p = cmdbase + STRLEN(cmdbase);
4739 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4740 *--p = NUL;
4741 if (p > cmdbase && p[-1] == ')'
4742 && (*cmd =='(' || cmd[1] == '('))
4743 *--p = NUL;
4744 }
4745
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004746 newcmd = cmdbase;
4747 unescape_shellxquote(cmdbase, p_sxe);
4748
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004749 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004750 * If creating new console, arguments are passed to the
4751 * 'cmd.exe' as-is. If it's not, arguments are not treated
4752 * correctly for current 'cmd.exe'. So unescape characters in
4753 * shellxescape except '|' for avoiding to be treated as
4754 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004755 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004756 if (flags != CREATE_NEW_CONSOLE)
4757 {
4758 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004759 char_u *cmd_shell = mch_getenv("COMSPEC");
4760
4761 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004762 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004763
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004764 subcmd = vim_strsave_escaped_ext(cmdbase,
4765 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004766 if (subcmd != NULL)
4767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004768 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004769 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004770 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004771 if (newcmd != NULL)
4772 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004773 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004774 else
4775 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004776 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004777 }
4778 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004779
4780 /*
4781 * Now, start the command as a process, so that it doesn't
4782 * inherit our handles which causes unpleasant dangling swap
4783 * files if we exit before the spawned process
4784 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004785 if (vim_create_process((char *)newcmd, FALSE, flags,
4786 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004787 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004788 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4789 > (HINSTANCE)32)
4790 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004791 else
4792 {
4793 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004794#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004795# ifdef VIMDLL
4796 if (gui.in_use)
4797# endif
4798 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004799#endif
4800 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004801
4802 if (newcmd != cmdbase)
4803 vim_free(newcmd);
4804
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004805 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004806 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004807 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004808 CloseHandle(si.hStdInput);
4809 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004810 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004811 CloseHandle(pi.hThread);
4812 CloseHandle(pi.hProcess);
4813 }
4814 else
4815 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004816 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004817#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004818 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004819 (!s_dont_use_vimrun && p_stmp ?
4820 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4821 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004823 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004824
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004825 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004826 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004828#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004829 if (
4830# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004831 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004832# endif
4833 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004835 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4836 "External commands will not pause after completion.\n"
4837 "See :help win32-vimrun for more information.");
4838 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004839 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4840 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004841
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004842 if (wmsg != NULL && wtitle != NULL)
4843 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4844 vim_free(wmsg);
4845 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 need_vimrun_warning = FALSE;
4847 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004848 if (
4849# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004850 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004851# endif
4852 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004853 // Use vimrun to execute the command. It opens a console
4854 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004855 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 vimrun_path,
4857 (msg_silent != 0 || (options & SHELL_DOOUT))
4858 ? "-s " : "",
4859 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004860 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004861# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004862 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004863# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004864 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004865 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004866 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4867 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004870 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004871 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004873 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 }
4877
4878 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004879 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
Bram Moolenaar0f873732019-12-05 20:28:46 +01004881 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004883#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004884 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004885 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
4887 )
4888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004889 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 msg_putchar('\n');
4891 }
4892#ifdef FEAT_TITLE
4893 resettitle();
4894#endif
4895
4896 signal(SIGINT, SIG_DFL);
4897#if defined(__GNUC__) && !defined(__MINGW32__)
4898 signal(SIGKILL, SIG_DFL);
4899#else
4900 signal(SIGBREAK, SIG_DFL);
4901#endif
4902 signal(SIGILL, SIG_DFL);
4903 signal(SIGFPE, SIG_DFL);
4904 signal(SIGSEGV, SIG_DFL);
4905 signal(SIGTERM, SIG_DFL);
4906 signal(SIGABRT, SIG_DFL);
4907
4908 return x;
4909}
4910
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004911#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004912 static HANDLE
4913job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004914 char_u *fname,
4915 DWORD dwDesiredAccess,
4916 DWORD dwShareMode,
4917 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4918 DWORD dwCreationDisposition,
4919 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004920{
4921 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004922 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004923
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004924 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004925 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004926 return INVALID_HANDLE_VALUE;
4927
4928 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4929 lpSecurityAttributes, dwCreationDisposition,
4930 dwFlagsAndAttributes, NULL);
4931 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004932 return h;
4933}
4934
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004935/*
4936 * Turn the dictionary "env" into a NUL separated list that can be used as the
4937 * environment argument of vim_create_process().
4938 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004939 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004940win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004941{
4942 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004943 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004944 LPVOID base = GetEnvironmentStringsW();
4945
Bram Moolenaar0f873732019-12-05 20:28:46 +01004946 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004947 if (ga_grow(gap, 1) == FAIL)
4948 return;
4949
4950 if (base)
4951 {
4952 WCHAR *p = (WCHAR*) base;
4953
Bram Moolenaar0f873732019-12-05 20:28:46 +01004954 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004955 if (ga_grow(gap, 1) == FAIL)
4956 return;
4957
4958 while (*p != 0 || *(p + 1) != 0)
4959 {
4960 if (ga_grow(gap, 1) == OK)
4961 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4962 p++;
4963 }
4964 FreeEnvironmentStrings(base);
4965 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4966 }
4967
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004968 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004969 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004970 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004971 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004972 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004973 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004974 typval_T *item = &dict_lookup(hi)->di_tv;
4975 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004976 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004977 --todo;
4978 if (wkey != NULL && wval != NULL)
4979 {
4980 size_t n;
4981 size_t lkey = wcslen(wkey);
4982 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004983
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004984 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4985 continue;
4986 for (n = 0; n < lkey; n++)
4987 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4988 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4989 for (n = 0; n < lval; n++)
4990 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4991 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4992 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004993 vim_free(wkey);
4994 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004995 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004996 }
4997 }
4998
Bram Moolenaar493359e2018-06-12 20:25:52 +02004999# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005000 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005001# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005002 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005003 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005004# endif
5005# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005006 char_u *version = get_vim_var_str(VV_VERSION);
5007 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005008# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005009 // size of "VIM_SERVERNAME=" and value,
5010 // plus "VIM_TERMINAL=" and value,
5011 // plus two terminating NULs
5012 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005013# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005014 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005015# endif
5016# ifdef FEAT_TERMINAL
5017 + 13 + version_len + 2
5018# endif
5019 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005020
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005021 if (ga_grow(gap, (int)n) == OK)
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 for (n = 0; n < 15; n++)
5025 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5026 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005027 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005028 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5029 (WCHAR)servername[n];
5030 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005031# endif
5032# ifdef FEAT_TERMINAL
5033 if (is_terminal)
5034 {
5035 for (n = 0; n < 13; n++)
5036 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5037 (WCHAR)"VIM_TERMINAL="[n];
5038 for (n = 0; n < version_len; n++)
5039 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5040 (WCHAR)version[n];
5041 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5042 }
5043# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005044 }
5045 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005046# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005047}
5048
Bram Moolenaarb091f302019-01-19 14:37:00 +01005049/*
5050 * Create a pair of pipes.
5051 * Return TRUE for success, FALSE for failure.
5052 */
5053 static BOOL
5054create_pipe_pair(HANDLE handles[2])
5055{
5056 static LONG s;
5057 char name[64];
5058 SECURITY_ATTRIBUTES sa;
5059
5060 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5061 GetCurrentProcessId(),
5062 InterlockedIncrement(&s));
5063
5064 // Create named pipe. Max size of named pipe is 65535.
5065 handles[1] = CreateNamedPipe(
5066 name,
5067 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5068 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005069 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005070
5071 if (handles[1] == INVALID_HANDLE_VALUE)
5072 return FALSE;
5073
5074 sa.nLength = sizeof(sa);
5075 sa.bInheritHandle = TRUE;
5076 sa.lpSecurityDescriptor = NULL;
5077
5078 handles[0] = CreateFile(name,
5079 FILE_GENERIC_READ,
5080 FILE_SHARE_READ, &sa,
5081 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5082
5083 if (handles[0] == INVALID_HANDLE_VALUE)
5084 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005085 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005086 return FALSE;
5087 }
5088
5089 return TRUE;
5090}
5091
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005092 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005093mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005094{
5095 STARTUPINFO si;
5096 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005097 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005098 SECURITY_ATTRIBUTES saAttr;
5099 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005100 HANDLE ifd[2];
5101 HANDLE ofd[2];
5102 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005103 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005104
5105 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5106 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5107 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5108 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5109 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5110 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5111 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5112
5113 if (use_out_for_err && use_null_for_out)
5114 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005115
5116 ifd[0] = INVALID_HANDLE_VALUE;
5117 ifd[1] = INVALID_HANDLE_VALUE;
5118 ofd[0] = INVALID_HANDLE_VALUE;
5119 ofd[1] = INVALID_HANDLE_VALUE;
5120 efd[0] = INVALID_HANDLE_VALUE;
5121 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005122 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005123
Bram Moolenaar14207f42016-10-27 21:13:10 +02005124 jo = CreateJobObject(NULL, NULL);
5125 if (jo == NULL)
5126 {
5127 job->jv_status = JOB_FAILED;
5128 goto failed;
5129 }
5130
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005131 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005132 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005133
Bram Moolenaar76467df2016-02-12 19:30:26 +01005134 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005135 ZeroMemory(&si, sizeof(si));
5136 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005137 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005138 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005139
Bram Moolenaard8070362016-02-15 21:56:54 +01005140 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5141 saAttr.bInheritHandle = TRUE;
5142 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005143
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005144 if (use_file_for_in)
5145 {
5146 char_u *fname = options->jo_io_name[PART_IN];
5147
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005148 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5149 FILE_SHARE_READ | FILE_SHARE_WRITE,
5150 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5151 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005153 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005154 goto failed;
5155 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005156 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005157 else if (!use_null_for_in
5158 && (!create_pipe_pair(ifd)
5159 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005160 goto failed;
5161
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005162 if (use_file_for_out)
5163 {
5164 char_u *fname = options->jo_io_name[PART_OUT];
5165
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005166 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5167 FILE_SHARE_READ | FILE_SHARE_WRITE,
5168 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5169 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005171 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005172 goto failed;
5173 }
5174 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005175 else if (!use_null_for_out &&
5176 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005177 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005178 goto failed;
5179
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005180 if (use_file_for_err)
5181 {
5182 char_u *fname = options->jo_io_name[PART_ERR];
5183
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005184 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5185 FILE_SHARE_READ | FILE_SHARE_WRITE,
5186 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5187 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005189 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005190 goto failed;
5191 }
5192 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005193 else if (!use_out_for_err && !use_null_for_err &&
5194 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005195 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005196 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005197
Bram Moolenaard8070362016-02-15 21:56:54 +01005198 si.dwFlags |= STARTF_USESTDHANDLES;
5199 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005200 si.hStdOutput = ofd[1];
5201 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5202
5203 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5204 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005205 if (options->jo_set & JO_CHANNEL)
5206 {
5207 channel = options->jo_channel;
5208 if (channel != NULL)
5209 ++channel->ch_refcount;
5210 }
5211 else
5212 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005213 if (channel == NULL)
5214 goto failed;
5215 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005216
5217 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005218 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005219 CREATE_DEFAULT_ERROR_MODE |
5220 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005221 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005222 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005223 &si, &pi,
5224 ga.ga_data,
5225 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005226 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005227 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005228 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005229 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005230 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005231
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005232 ga_clear(&ga);
5233
Bram Moolenaar14207f42016-10-27 21:13:10 +02005234 if (!AssignProcessToJobObject(jo, pi.hProcess))
5235 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005236 // if failing, switch the way to terminate
5237 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005238 CloseHandle(jo);
5239 jo = NULL;
5240 }
5241 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005242 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005243 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005244 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005245 job->jv_status = JOB_STARTED;
5246
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005247 CloseHandle(ifd[0]);
5248 CloseHandle(ofd[1]);
5249 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005250 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005251
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005252 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005253 if (channel != NULL)
5254 {
5255 channel_set_pipes(channel,
5256 use_file_for_in || use_null_for_in
5257 ? INVALID_FD : (sock_T)ifd[1],
5258 use_file_for_out || use_null_for_out
5259 ? INVALID_FD : (sock_T)ofd[0],
5260 use_out_for_err || use_file_for_err || use_null_for_err
5261 ? INVALID_FD : (sock_T)efd[0]);
5262 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005263 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005264 return;
5265
5266failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005267 CloseHandle(ifd[0]);
5268 CloseHandle(ofd[0]);
5269 CloseHandle(efd[0]);
5270 CloseHandle(ifd[1]);
5271 CloseHandle(ofd[1]);
5272 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005273 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005274 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005275}
5276
5277 char *
5278mch_job_status(job_T *job)
5279{
5280 DWORD dwExitCode = 0;
5281
Bram Moolenaar76467df2016-02-12 19:30:26 +01005282 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5283 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005284 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005285 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005286 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005287 {
5288 ch_log(job->jv_channel, "Job ended");
5289 job->jv_status = JOB_ENDED;
5290 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005291 return "dead";
5292 }
5293 return "run";
5294}
5295
Bram Moolenaar97792de2016-10-15 18:36:49 +02005296 job_T *
5297mch_detect_ended_job(job_T *job_list)
5298{
5299 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5300 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5301 job_T *job = job_list;
5302
5303 while (job != NULL)
5304 {
5305 DWORD n;
5306 DWORD result;
5307
5308 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5309 && job != NULL; job = job->jv_next)
5310 {
5311 if (job->jv_status == JOB_STARTED)
5312 {
5313 jobHandles[n] = job->jv_proc_info.hProcess;
5314 jobArray[n] = job;
5315 ++n;
5316 }
5317 }
5318 if (n == 0)
5319 continue;
5320 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5321 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5322 {
5323 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5324
5325 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5326 return wait_job;
5327 }
5328 }
5329 return NULL;
5330}
5331
Bram Moolenaarfb630902016-10-29 14:55:00 +02005332 static BOOL
5333terminate_all(HANDLE process, int code)
5334{
5335 PROCESSENTRY32 pe;
5336 HANDLE h = INVALID_HANDLE_VALUE;
5337 DWORD pid = GetProcessId(process);
5338
5339 if (pid != 0)
5340 {
5341 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5342 if (h != INVALID_HANDLE_VALUE)
5343 {
5344 pe.dwSize = sizeof(PROCESSENTRY32);
5345 if (!Process32First(h, &pe))
5346 goto theend;
5347
5348 do
5349 {
5350 if (pe.th32ParentProcessID == pid)
5351 {
5352 HANDLE ph = OpenProcess(
5353 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5354 if (ph != NULL)
5355 {
5356 terminate_all(ph, code);
5357 CloseHandle(ph);
5358 }
5359 }
5360 } while (Process32Next(h, &pe));
5361
5362 CloseHandle(h);
5363 }
5364 }
5365
5366theend:
5367 return TerminateProcess(process, code);
5368}
5369
5370/*
5371 * Send a (deadly) signal to "job".
5372 * Return FAIL if it didn't work.
5373 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005374 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005375mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005376{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005377 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005378
Bram Moolenaar923d9262016-02-25 20:56:01 +01005379 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005380 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005381 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005382 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005383 {
5384 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5385 job->jv_channel->ch_killing = TRUE;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005386 return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005387 }
Bram Moolenaarfb630902016-10-29 14:55:00 +02005388 return terminate_all(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005389 }
5390
5391 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5392 return FAIL;
5393 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005394 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5395 job->jv_proc_info.dwProcessId)
5396 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005397 FreeConsole();
5398 return ret;
5399}
5400
5401/*
5402 * Clear the data related to "job".
5403 */
5404 void
5405mch_clear_job(job_T *job)
5406{
5407 if (job->jv_status != JOB_FAILED)
5408 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005409 if (job->jv_job_object != NULL)
5410 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005411 CloseHandle(job->jv_proc_info.hProcess);
5412 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005413}
5414#endif
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005417#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
5419/*
5420 * Start termcap mode
5421 */
5422 static void
5423termcap_mode_start(void)
5424{
5425 DWORD cmodein;
5426
5427 if (g_fTermcapMode)
5428 return;
5429
5430 SaveConsoleBuffer(&g_cbNonTermcap);
5431
5432 if (g_cbTermcap.IsValid)
5433 {
5434 /*
5435 * We've been in termcap mode before. Restore certain screen
5436 * characteristics, including the buffer size and the window
5437 * size. Since we will be redrawing the screen, we don't need
5438 * to restore the actual contents of the buffer.
5439 */
5440 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005441 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5443 Rows = g_cbTermcap.Info.dwSize.Y;
5444 Columns = g_cbTermcap.Info.dwSize.X;
5445 }
5446 else
5447 {
5448 /*
5449 * This is our first time entering termcap mode. Clear the console
5450 * screen buffer, and resize the buffer to match the current window
5451 * size. We will use this as the size of our editing environment.
5452 */
5453 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005454 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5456 }
5457
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005458# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (g_fMouseActive)
5464 cmodein |= ENABLE_MOUSE_INPUT;
5465 else
5466 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 cmodein |= ENABLE_WINDOW_INPUT;
5468 SetConsoleMode(g_hConIn, cmodein);
5469
5470 redraw_later_clear();
5471 g_fTermcapMode = TRUE;
5472}
5473
5474
5475/*
5476 * End termcap mode
5477 */
5478 static void
5479termcap_mode_end(void)
5480{
5481 DWORD cmodein;
5482 ConsoleBuffer *cb;
5483 COORD coord;
5484 DWORD dwDummy;
5485
5486 if (!g_fTermcapMode)
5487 return;
5488
5489 SaveConsoleBuffer(&g_cbTermcap);
5490
5491 GetConsoleMode(g_hConIn, &cmodein);
5492 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5493 SetConsoleMode(g_hConIn, cmodein);
5494
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005495# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005496 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005501 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 SetConsoleCursorInfo(g_hConOut, &g_cci);
5503
5504 if (p_rs || exiting)
5505 {
5506 /*
5507 * Clear anything that happens to be on the current line.
5508 */
5509 coord.X = 0;
5510 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5511 FillConsoleOutputCharacter(g_hConOut, ' ',
5512 cb->Info.dwSize.X, coord, &dwDummy);
5513 /*
5514 * The following is just for aesthetics. If we are exiting without
5515 * restoring the screen, then we want to have a prompt string
5516 * appear at the bottom line. However, the command interpreter
5517 * seems to always advance the cursor one line before displaying
5518 * the prompt string, which causes the screen to scroll. To
5519 * counter this, move the cursor up one line before exiting.
5520 */
5521 if (exiting && !p_rs)
5522 coord.Y--;
5523 /*
5524 * Position the cursor at the leftmost column of the desired row.
5525 */
5526 SetConsoleCursorPosition(g_hConOut, coord);
5527 }
5528
5529 g_fTermcapMode = FALSE;
5530}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005531#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005534#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 void
5536mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005537 char_u *s UNUSED,
5538 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005540 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541}
5542
5543#else
5544
5545/*
5546 * clear `n' chars, starting from `coord'
5547 */
5548 static void
5549clear_chars(
5550 COORD coord,
5551 DWORD n)
5552{
5553 DWORD dwDummy;
5554
5555 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005556
5557 if (!USE_VTP)
5558 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5559 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005560 {
5561 set_console_color_rgb();
5562 gotoxy(coord.X + 1, coord.Y + 1);
5563 vtp_printf("\033[%dX", n);
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565}
5566
5567
5568/*
5569 * Clear the screen
5570 */
5571 static void
5572clear_screen(void)
5573{
5574 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005575
5576 if (!USE_VTP)
5577 clear_chars(g_coord, Rows * Columns);
5578 else
5579 {
5580 set_console_color_rgb();
5581 gotoxy(1, 1);
5582 vtp_printf("\033[2J");
5583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
5585
5586
5587/*
5588 * Clear to end of display
5589 */
5590 static void
5591clear_to_end_of_display(void)
5592{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005593 COORD save = g_coord;
5594
5595 if (!USE_VTP)
5596 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005598 else
5599 {
5600 set_console_color_rgb();
5601 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5602 vtp_printf("\033[0J");
5603
5604 gotoxy(save.X + 1, save.Y + 1);
5605 g_coord = save;
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609
5610/*
5611 * Clear to end of line
5612 */
5613 static void
5614clear_to_end_of_line(void)
5615{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005616 COORD save = g_coord;
5617
5618 if (!USE_VTP)
5619 clear_chars(g_coord, Columns - g_coord.X);
5620 else
5621 {
5622 set_console_color_rgb();
5623 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5624 vtp_printf("\033[0K");
5625
5626 gotoxy(save.X + 1, save.Y + 1);
5627 g_coord = save;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630
5631
5632/*
5633 * Scroll the scroll region up by `cLines' lines
5634 */
5635 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005636scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 COORD oldcoord = g_coord;
5639
5640 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5641 delete_lines(cLines);
5642
5643 g_coord = oldcoord;
5644}
5645
5646
5647/*
5648 * Set the scroll region
5649 */
5650 static void
5651set_scroll_region(
5652 unsigned left,
5653 unsigned top,
5654 unsigned right,
5655 unsigned bottom)
5656{
5657 if (left >= right
5658 || top >= bottom
5659 || right > (unsigned) Columns - 1
5660 || bottom > (unsigned) Rows - 1)
5661 return;
5662
5663 g_srScrollRegion.Left = left;
5664 g_srScrollRegion.Top = top;
5665 g_srScrollRegion.Right = right;
5666 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005667}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005668
Bram Moolenaar6982f422019-02-16 16:48:01 +01005669 static void
5670set_scroll_region_tb(
5671 unsigned top,
5672 unsigned bottom)
5673{
5674 if (top >= bottom || bottom > (unsigned)Rows - 1)
5675 return;
5676
5677 g_srScrollRegion.Top = top;
5678 g_srScrollRegion.Bottom = bottom;
5679}
5680
5681 static void
5682set_scroll_region_lr(
5683 unsigned left,
5684 unsigned right)
5685{
5686 if (left >= right || right > (unsigned)Columns - 1)
5687 return;
5688
5689 g_srScrollRegion.Left = left;
5690 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691}
5692
5693
5694/*
5695 * Insert `cLines' lines at the current cursor position
5696 */
5697 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005698insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005700 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 COORD dest;
5702 CHAR_INFO fill;
5703
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005704 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5705
Bram Moolenaar6982f422019-02-16 16:48:01 +01005706 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 dest.Y = g_coord.Y + cLines;
5708
Bram Moolenaar6982f422019-02-16 16:48:01 +01005709 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 source.Top = g_coord.Y;
5711 source.Right = g_srScrollRegion.Right;
5712 source.Bottom = g_srScrollRegion.Bottom - cLines;
5713
Bram Moolenaar6982f422019-02-16 16:48:01 +01005714 clip.Left = g_srScrollRegion.Left;
5715 clip.Top = g_coord.Y;
5716 clip.Right = g_srScrollRegion.Right;
5717 clip.Bottom = g_srScrollRegion.Bottom;
5718
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005719 fill.Char.AsciiChar = ' ';
5720 if (!USE_VTP)
5721 fill.Attributes = g_attrCurrent;
5722 else
5723 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005725 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005726
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005727 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5728
Bram Moolenaar6982f422019-02-16 16:48:01 +01005729 // Here we have to deal with a win32 console flake: If the scroll
5730 // region looks like abc and we scroll c to a and fill with d we get
5731 // cbd... if we scroll block c one line at a time to a, we get cdd...
5732 // vim expects cdd consistently... So we have to deal with that
5733 // here... (this also occurs scrolling the same way in the other
5734 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 if (source.Bottom < dest.Y)
5737 {
5738 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005739 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar6982f422019-02-16 16:48:01 +01005741 coord.X = source.Left;
5742 for (i = clip.Top; i < dest.Y; ++i)
5743 {
5744 coord.Y = i;
5745 clear_chars(coord, source.Right - source.Left + 1);
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748}
5749
5750
5751/*
5752 * Delete `cLines' lines at the current cursor position
5753 */
5754 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005755delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005757 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 COORD dest;
5759 CHAR_INFO fill;
5760 int nb;
5761
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005762 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5763
Bram Moolenaar6982f422019-02-16 16:48:01 +01005764 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 dest.Y = g_coord.Y;
5766
Bram Moolenaar6982f422019-02-16 16:48:01 +01005767 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 source.Top = g_coord.Y + cLines;
5769 source.Right = g_srScrollRegion.Right;
5770 source.Bottom = g_srScrollRegion.Bottom;
5771
Bram Moolenaar6982f422019-02-16 16:48:01 +01005772 clip.Left = g_srScrollRegion.Left;
5773 clip.Top = g_coord.Y;
5774 clip.Right = g_srScrollRegion.Right;
5775 clip.Bottom = g_srScrollRegion.Bottom;
5776
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005777 fill.Char.AsciiChar = ' ';
5778 if (!USE_VTP)
5779 fill.Attributes = g_attrCurrent;
5780 else
5781 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005783 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005784
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005785 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5786
Bram Moolenaar6982f422019-02-16 16:48:01 +01005787 // Here we have to deal with a win32 console flake; See insert_lines()
5788 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790 nb = dest.Y + (source.Bottom - source.Top) + 1;
5791
5792 if (nb < source.Top)
5793 {
5794 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005795 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar6982f422019-02-16 16:48:01 +01005797 coord.X = source.Left;
5798 for (i = nb; i < clip.Bottom; ++i)
5799 {
5800 coord.Y = i;
5801 clear_chars(coord, source.Right - source.Left + 1);
5802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
5804}
5805
5806
5807/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005808 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 */
5810 static void
5811gotoxy(
5812 unsigned x,
5813 unsigned y)
5814{
5815 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5816 return;
5817
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005818 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005819 {
5820 // external cursor coords are 1-based; internal are 0-based
5821 g_coord.X = x - 1;
5822 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005823 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005824 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005825 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005826 {
5827 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005828 // destruction. Insider build bug. Always enabled because it's cheap
5829 // and avoids mistakes with recognizing the build.
5830 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005831
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005832 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005833
5834 g_coord.X = x - 1;
5835 g_coord.Y = y - 1;
5836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837}
5838
5839
5840/*
5841 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005842 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005845textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005847 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 SetConsoleTextAttribute(g_hConOut, wAttr);
5850}
5851
5852
5853 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005854textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005856 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005858 if (!USE_VTP)
5859 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5860 else
5861 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862}
5863
5864
5865 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005866textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005868 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005870 if (!USE_VTP)
5871 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5872 else
5873 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874}
5875
5876
5877/*
5878 * restore the default text attribute (whatever we started with)
5879 */
5880 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005881normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005883 if (!USE_VTP)
5884 textattr(g_attrDefault);
5885 else
5886 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887}
5888
5889
5890static WORD g_attrPreStandout = 0;
5891
5892/*
5893 * Make the text standout, by brightening it
5894 */
5895 static void
5896standout(void)
5897{
5898 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5901}
5902
5903
5904/*
5905 * Turn off standout mode
5906 */
5907 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005908standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005912
5913 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914}
5915
5916
5917/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005918 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 */
5920 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005921mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 char_u *p;
5924 int n;
5925
5926 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5927 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005928 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005929# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005930 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005931# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005932 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {
5934 p = T_ME + 2;
5935 n = getdigits(&p);
5936 if (*p == 'm' && n > 0)
5937 {
5938 cterm_normal_fg_color = (n & 0xf) + 1;
5939 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5940 }
5941 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005942# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005943 cterm_normal_fg_gui_color = INVALCOLOR;
5944 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946}
5947
5948
5949/*
5950 * visual bell: flash the screen
5951 */
5952 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005953visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 COORD coordOrigin = {0, 0};
5956 WORD attrFlash = ~g_attrCurrent & 0xff;
5957
5958 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005959 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
5961 if (oldattrs == NULL)
5962 return;
5963 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5964 coordOrigin, &dwDummy);
5965 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5966 coordOrigin, &dwDummy);
5967
Bram Moolenaar0f873732019-12-05 20:28:46 +01005968 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005969 if (!USE_VTP)
5970 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 coordOrigin, &dwDummy);
5972 vim_free(oldattrs);
5973}
5974
5975
5976/*
5977 * Make the cursor visible or invisible
5978 */
5979 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005980cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005983# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986}
5987
5988
5989/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005990 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005991 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005993 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005995 char_u *pchBuf,
5996 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005998 COORD coord = g_coord;
5999 DWORD written;
6000 DWORD n, cchwritten, cells;
6001 static WCHAR *unicodebuf = NULL;
6002 static int unibuflen = 0;
6003 int length;
6004 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006006 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6007 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006008 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006009 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006010 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006011 unibuflen = length;
6012 }
6013 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6014 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006016 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006017
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006018 if (!USE_VTP)
6019 {
6020 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6021 coord, &written);
6022 // When writing fails or didn't write a single character, pretend one
6023 // character was written, otherwise we get stuck.
6024 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6025 coord, &cchwritten) == 0
6026 || cchwritten == 0 || cchwritten == (DWORD)-1)
6027 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006028 }
6029 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006030 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006031 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6032 NULL) == 0 || cchwritten == 0)
6033 cchwritten = 1;
6034 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006035
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006036 if (cchwritten == length)
6037 {
6038 written = cbToWrite;
6039 g_coord.X += (SHORT)cells;
6040 }
6041 else
6042 {
6043 char_u *p = pchBuf;
6044 for (n = 0; n < cchwritten; n++)
6045 MB_CPTR_ADV(p);
6046 written = p - pchBuf;
6047 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 while (g_coord.X > g_srScrollRegion.Right)
6051 {
6052 g_coord.X -= (SHORT) Columns;
6053 if (g_coord.Y < g_srScrollRegion.Bottom)
6054 g_coord.Y++;
6055 }
6056
6057 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6058
6059 return written;
6060}
6061
6062
6063/*
6064 * mch_write(): write the output buffer to the screen, translating ESC
6065 * sequences into calls to console output routines.
6066 */
6067 void
6068mch_write(
6069 char_u *s,
6070 int len)
6071{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006072# ifdef VIMDLL
6073 if (gui.in_use)
6074 return;
6075# endif
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 s[len] = NUL;
6078
6079 if (!term_console)
6080 {
6081 write(1, s, (unsigned)len);
6082 return;
6083 }
6084
Bram Moolenaar0f873732019-12-05 20:28:46 +01006085 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 while (len--)
6087 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006088 // optimization: use one single write_chars for runs of text,
6089 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006090 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 if (p_wd)
6093 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006094 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (prefix != 0)
6096 prefix = 1;
6097 }
6098
6099 if (prefix != 0)
6100 {
6101 DWORD nWritten;
6102
6103 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006104# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (fdDump)
6106 {
6107 fputc('>', fdDump);
6108 fwrite(s, sizeof(char_u), nWritten, fdDump);
6109 fputs("<\n", fdDump);
6110 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 len -= (nWritten - 1);
6113 s += nWritten;
6114 }
6115 else if (s[0] == '\n')
6116 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006117 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 if (g_coord.Y == g_srScrollRegion.Bottom)
6119 {
6120 scroll(1);
6121 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6122 }
6123 else
6124 {
6125 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6126 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006127# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (fdDump)
6129 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 s++;
6132 }
6133 else if (s[0] == '\r')
6134 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006135 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006137# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (fdDump)
6139 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 s++;
6142 }
6143 else if (s[0] == '\b')
6144 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006145 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (g_coord.X > g_srScrollRegion.Left)
6147 g_coord.X--;
6148 else if (g_coord.Y > g_srScrollRegion.Top)
6149 {
6150 g_coord.X = g_srScrollRegion.Right;
6151 g_coord.Y--;
6152 }
6153 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006154# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (fdDump)
6156 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 s++;
6159 }
6160 else if (s[0] == '\a')
6161 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006162 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006164# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (fdDump)
6166 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 s++;
6169 }
6170 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6171 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006172# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006173 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006174# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006175 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006176 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 switch (s[2])
6179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 case '0': case '1': case '2': case '3': case '4':
6181 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006182 p = s + 1;
6183 do
6184 {
6185 ++p;
6186 args[argc] = getdigits(&p);
6187 argc += (argc < 15) ? 1 : 0;
6188 if (p > s + len)
6189 break;
6190 } while (*p == ';');
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (p > s + len)
6193 break;
6194
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006195 arg1 = args[0];
6196 arg2 = args[1];
6197 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006199 if (argc == 1 && args[0] == 0)
6200 normvideo();
6201 else if (argc == 1)
6202 {
6203 if (USE_VTP)
6204 textcolor((WORD) arg1);
6205 else
6206 textattr((WORD) arg1);
6207 }
6208 else if (USE_VTP)
6209 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006211 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006213 gotoxy(arg2, arg1);
6214 }
6215 else if (argc == 2 && *p == 'r')
6216 {
6217 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6218 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006219 else if (argc == 2 && *p == 'R')
6220 {
6221 set_scroll_region_tb(arg1, arg2);
6222 }
6223 else if (argc == 2 && *p == 'V')
6224 {
6225 set_scroll_region_lr(arg1, arg2);
6226 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006227 else if (argc == 1 && *p == 'A')
6228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 gotoxy(g_coord.X + 1,
6230 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6231 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006232 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 textbackground((WORD) arg1);
6235 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006236 else if (argc == 1 && *p == 'C')
6237 {
6238 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6239 g_coord.Y + 1);
6240 }
6241 else if (argc == 1 && *p == 'f')
6242 {
6243 textcolor((WORD) arg1);
6244 }
6245 else if (argc == 1 && *p == 'H')
6246 {
6247 gotoxy(1, arg1);
6248 }
6249 else if (argc == 1 && *p == 'L')
6250 {
6251 insert_lines(arg1);
6252 }
6253 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 {
6255 delete_lines(arg1);
6256 }
6257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006258 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 s = p + 1;
6260 break;
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 gotoxy(g_coord.X + 1,
6264 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6265 goto got3;
6266
6267 case 'B':
6268 visual_bell();
6269 goto got3;
6270
6271 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6273 g_coord.Y + 1);
6274 goto got3;
6275
6276 case 'E':
6277 termcap_mode_end();
6278 goto got3;
6279
6280 case 'F':
6281 standout();
6282 goto got3;
6283
6284 case 'f':
6285 standend();
6286 goto got3;
6287
6288 case 'H':
6289 gotoxy(1, 1);
6290 goto got3;
6291
6292 case 'j':
6293 clear_to_end_of_display();
6294 goto got3;
6295
6296 case 'J':
6297 clear_screen();
6298 goto got3;
6299
6300 case 'K':
6301 clear_to_end_of_line();
6302 goto got3;
6303
6304 case 'L':
6305 insert_lines(1);
6306 goto got3;
6307
6308 case 'M':
6309 delete_lines(1);
6310 goto got3;
6311
6312 case 'S':
6313 termcap_mode_start();
6314 goto got3;
6315
6316 case 'V':
6317 cursor_visible(TRUE);
6318 goto got3;
6319
6320 case 'v':
6321 cursor_visible(FALSE);
6322 goto got3;
6323
6324 got3:
6325 s += 3;
6326 len -= 2;
6327 }
6328
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006329# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (fdDump)
6331 {
6332 fputs("ESC | ", fdDump);
6333 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6334 fputc('\n', fdDump);
6335 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338 else
6339 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006340 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 DWORD nWritten;
6342
6343 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006344# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (fdDump)
6346 {
6347 fputc('>', fdDump);
6348 fwrite(s, sizeof(char_u), nWritten, fdDump);
6349 fputs("<\n", fdDump);
6350 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353 len -= (nWritten - 1);
6354 s += nWritten;
6355 }
6356 }
6357
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006358# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (fdDump)
6360 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362}
6363
Bram Moolenaar0f873732019-12-05 20:28:46 +01006364#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366
6367/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006368 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 */
6370 void
6371mch_delay(
6372 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006373 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006375#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006376 Sleep((int)msec); // never wait for input
6377#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006378# ifdef VIMDLL
6379 if (gui.in_use)
6380 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006381 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006382 return;
6383 }
6384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006386# ifdef FEAT_MZSCHEME
6387 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6388 {
6389 int towait = p_mzq;
6390
Bram Moolenaar0f873732019-12-05 20:28:46 +01006391 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006392 while (msec > 0)
6393 {
6394 mzvim_check_threads();
6395 if (msec < towait)
6396 towait = msec;
6397 Sleep(towait);
6398 msec -= towait;
6399 }
6400 }
6401 else
6402# endif
6403 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006405 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406#endif
6407}
6408
6409
6410/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006411 * This version of remove is not scared by a readonly (backup) file.
6412 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 * Return 0 for success, -1 for failure.
6414 */
6415 int
6416mch_remove(char_u *name)
6417{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006418 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 int n;
6420
Bram Moolenaar203258c2016-01-17 22:15:16 +01006421 /*
6422 * On Windows, deleting a directory's symbolic link is done by
6423 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6424 */
6425 if (mch_isdir(name) && mch_is_symbolic_link(name))
6426 return mch_rmdir(name);
6427
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006428 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6429
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006430 wn = enc_to_utf16(name, NULL);
6431 if (wn == NULL)
6432 return -1;
6433
6434 n = DeleteFileW(wn) ? 0 : -1;
6435 vim_free(wn);
6436 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
6439
6440/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006441 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 */
6443 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006444mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006446#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6447# ifdef VIMDLL
6448 if (!gui.in_use)
6449# endif
6450 if (g_fCtrlCPressed || g_fCBrkPressed)
6451 {
6452 ctrl_break_was_pressed = g_fCBrkPressed;
6453 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6454 got_int = TRUE;
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456#endif
6457}
6458
Bram Moolenaar0f873732019-12-05 20:28:46 +01006459// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006460#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006461
6462/*
6463 * How much main memory in KiB that can be used by VIM.
6464 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006465 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006466mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006467{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006468 MEMORYSTATUSEX ms;
6469
Bram Moolenaar0f873732019-12-05 20:28:46 +01006470 // Need to use GlobalMemoryStatusEx() when there is more memory than
6471 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006472 ms.dwLength = sizeof(MEMORYSTATUSEX);
6473 GlobalMemoryStatusEx(&ms);
6474 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006475 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006476 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006477 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006478 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006479 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006480 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006481 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006482 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006483 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006484 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006485 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006489 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6491 * file whose short file name is "FOO.BAR" (its long file name will
6492 * be correct: "foo.bar~"). Because a file can be accessed by
6493 * either its SFN or its LFN, "foo.bar" has effectively been
6494 * renamed to "foo.bar", which is not at all what was wanted. This
6495 * seems to happen only when renaming files with three-character
6496 * extensions by appending a suffix that does not include ".".
6497 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6498 *
6499 * There is another problem, which isn't really a bug but isn't right either:
6500 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6501 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6502 * service pack 6. Doesn't seem to happen on Windows 98.
6503 *
6504 * Like rename(), returns 0 upon success, non-zero upon failure.
6505 * Should probably set errno appropriately when errors occur.
6506 */
6507 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006508mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006510 WCHAR *p;
6511 int i;
6512 WCHAR szTempFile[_MAX_PATH + 1];
6513 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006516 // No need to play tricks unless the file name contains a "~" as the
6517 // seventh character.
6518 p = wold;
6519 for (i = 0; wold[i] != NUL; ++i)
6520 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6521 && wold[i + 1] != 0)
6522 p = wold + i + 1;
6523 if ((int)(wold + i - p) < 8 || p[6] != '~')
6524 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006526 // Get base path of new file name. Undocumented feature: If pszNewFile is
6527 // a directory, no error is returned and pszFilePart will be NULL.
6528 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006530 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006532 // Get (and create) a unique temporary file name in directory of new file
6533 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 return -2;
6535
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 // blow the temp file away
6537 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return -3;
6539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 // rename old file to the temp file
6541 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 return -4;
6543
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006544 // now create an empty file called pszOldFile; this prevents the operating
6545 // system using pszOldFile as an alias (SFN) if we're renaming within the
6546 // same directory. For example, we're editing a file called
6547 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6548 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6549 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6550 // cause all sorts of problems later in buf_write(). So, we create an
6551 // empty file called filena~1.txt and the system will have to find some
6552 // other SFN for filena~1.txt~, such as filena~2.txt
6553 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6555 return -5;
6556 if (!CloseHandle(hf))
6557 return -6;
6558
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006559 // rename the temp file to the new file
6560 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006562 // Renaming failed. Rename the file back to its old name, so that it
6563 // looks like nothing happened.
6564 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return -7;
6566 }
6567
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006568 // Seems to be left around on Novell filesystems
6569 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006571 // finally, remove the empty old file
6572 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return -8;
6574
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006575 return 0;
6576}
6577
6578
6579/*
6580 * Converts the filenames to UTF-16, then call mch_wrename().
6581 * Like rename(), returns 0 upon success, non-zero upon failure.
6582 */
6583 int
6584mch_rename(
6585 const char *pszOldFile,
6586 const char *pszNewFile)
6587{
6588 WCHAR *wold = NULL;
6589 WCHAR *wnew = NULL;
6590 int retval = -1;
6591
6592 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6593 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6594 if (wold != NULL && wnew != NULL)
6595 retval = mch_wrename(wold, wnew);
6596 vim_free(wold);
6597 vim_free(wnew);
6598 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599}
6600
6601/*
6602 * Get the default shell for the current hardware platform
6603 */
6604 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006605default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006607 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
6611 * mch_access() extends access() to do more detailed check on network drives.
6612 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6613 */
6614 int
6615mch_access(char *n, int p)
6616{
6617 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006618 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006619 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006621 wn = enc_to_utf16((char_u *)n, NULL);
6622 if (wn == NULL)
6623 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006625 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 if (p & R_OK)
6630 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006631 // Read check is performed by seeing if we can do a find file on
6632 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006633 int i;
6634 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006636 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6637 TempNameW[i] = wn[i];
6638 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6639 TempNameW[i++] = '\\';
6640 TempNameW[i++] = '*';
6641 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006643 hFile = FindFirstFileW(TempNameW, &d);
6644 if (hFile == INVALID_HANDLE_VALUE)
6645 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
6649
6650 if (p & W_OK)
6651 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006652 // Trying to create a temporary file in the directory should catch
6653 // directories on read-only network shares. However, in
6654 // directories whose ACL allows writes but denies deletes will end
6655 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006656 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6657 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006658 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006659 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
6661 }
6662 else
6663 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006664 // Don't consider a file read-only if another process has opened it.
6665 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6666
Bram Moolenaar0f873732019-12-05 20:28:46 +01006667 // Trying to open the file for the required access does ACL, read-only
6668 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006669 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6670 | ((p & R_OK) ? GENERIC_READ : 0);
6671
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006672 hFile = CreateFileW(wn, access_mode, share_mode,
6673 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if (hFile == INVALID_HANDLE_VALUE)
6675 goto getout;
6676 CloseHandle(hFile);
6677 }
6678
Bram Moolenaar0f873732019-12-05 20:28:46 +01006679 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 return retval;
6683}
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006686 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 */
6688 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006689mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 WCHAR *wn;
6692 int f;
6693
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006694 wn = enc_to_utf16((char_u *)name, NULL);
6695 if (wn == NULL)
6696 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006698 f = _wopen(wn, flags, mode);
6699 vim_free(wn);
6700 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701}
6702
6703/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006704 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 */
6706 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006707mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 WCHAR *wn, *wm;
6710 FILE *f = NULL;
6711
Bram Moolenaara12a1612019-01-24 16:39:02 +01006712#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006713 // Work around an annoying assertion in the Microsoft debug CRT
6714 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006715 char newMode = mode[strlen(mode) - 1];
6716 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006717
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006718 _get_fmode(&oldMode);
6719 if (newMode == 't')
6720 _set_fmode(_O_TEXT);
6721 else if (newMode == 'b')
6722 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006723#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006724 wn = enc_to_utf16((char_u *)name, NULL);
6725 wm = enc_to_utf16((char_u *)mode, NULL);
6726 if (wn != NULL && wm != NULL)
6727 f = _wfopen(wn, wm);
6728 vim_free(wn);
6729 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006730
Bram Moolenaara12a1612019-01-24 16:39:02 +01006731#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006732 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006733#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006734 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737/*
6738 * SUB STREAM (aka info stream) handling:
6739 *
6740 * NTFS can have sub streams for each file. Normal contents of file is
6741 * stored in the main stream, and extra contents (author information and
6742 * title and so on) can be stored in sub stream. After Windows 2000, user
6743 * can access and store those informations in sub streams via explorer's
6744 * property menuitem in right click menu. Those informations in sub streams
6745 * were lost when copying only the main stream. So we have to copy sub
6746 * streams.
6747 *
6748 * Incomplete explanation:
6749 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6750 * More useful info and an example:
6751 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6752 */
6753
6754/*
6755 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6756 * and write to stream "substream" of file "to".
6757 * Errors are ignored.
6758 */
6759 static void
6760copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6761{
6762 HANDLE hTo;
6763 WCHAR *to_name;
6764
6765 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6766 wcscpy(to_name, to);
6767 wcscat(to_name, substream);
6768
6769 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6770 FILE_ATTRIBUTE_NORMAL, NULL);
6771 if (hTo != INVALID_HANDLE_VALUE)
6772 {
6773 long done;
6774 DWORD todo;
6775 DWORD readcnt, written;
6776 char buf[4096];
6777
Bram Moolenaar0f873732019-12-05 20:28:46 +01006778 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 for (done = 0; done < len; done += written)
6780 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006781 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006782 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6783 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6785 FALSE, FALSE, context)
6786 || readcnt != todo
6787 || !WriteFile(hTo, buf, todo, &written, NULL)
6788 || written != todo)
6789 break;
6790 }
6791 CloseHandle(hTo);
6792 }
6793
6794 free(to_name);
6795}
6796
6797/*
6798 * Copy info streams from file "from" to file "to".
6799 */
6800 static void
6801copy_infostreams(char_u *from, char_u *to)
6802{
6803 WCHAR *fromw;
6804 WCHAR *tow;
6805 HANDLE sh;
6806 WIN32_STREAM_ID sid;
6807 int headersize;
6808 WCHAR streamname[_MAX_PATH];
6809 DWORD readcount;
6810 void *context = NULL;
6811 DWORD lo, hi;
6812 int len;
6813
Bram Moolenaar0f873732019-12-05 20:28:46 +01006814 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006815 fromw = enc_to_utf16(from, NULL);
6816 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (fromw != NULL && tow != NULL)
6818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6821 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6822 if (sh != INVALID_HANDLE_VALUE)
6823 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006824 // Use BackupRead() to find the info streams. Repeat until we
6825 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 for (;;)
6827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 // Get the header to find the length of the stream name. If
6829 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006831 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6833 &readcount, FALSE, FALSE, &context)
6834 || readcount == 0)
6835 break;
6836
Bram Moolenaar0f873732019-12-05 20:28:46 +01006837 // We only deal with streams that have a name. The normal
6838 // file data appears to be without a name, even though docs
6839 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (sid.dwStreamNameSize > 0)
6841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006842 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (!BackupRead(sh, (LPBYTE)streamname,
6844 sid.dwStreamNameSize,
6845 &readcount, FALSE, FALSE, &context))
6846 break;
6847
Bram Moolenaar0f873732019-12-05 20:28:46 +01006848 // Copy an info stream with a name ":anything:$DATA".
6849 // Skip "::$DATA", it has no stream name (examples suggest
6850 // it might be used for the normal file contents).
6851 // Note that BackupRead() counts bytes, but the name is in
6852 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 len = readcount / sizeof(WCHAR);
6854 streamname[len] = 0;
6855 if (len > 7 && wcsicmp(streamname + len - 6,
6856 L":$DATA") == 0)
6857 {
6858 streamname[len - 6] = 0;
6859 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006860 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862 }
6863
Bram Moolenaar0f873732019-12-05 20:28:46 +01006864 // Advance to the next stream. We might try seeking too far,
6865 // but BackupSeek() doesn't skip over stream borders, thus
6866 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006867 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 &lo, &hi, &context);
6869 }
6870
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6873
6874 CloseHandle(sh);
6875 }
6876 }
6877 vim_free(fromw);
6878 vim_free(tow);
6879}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
6881/*
6882 * Copy file attributes from file "from" to file "to".
6883 * For Windows NT and later we copy info streams.
6884 * Always returns zero, errors are ignored.
6885 */
6886 int
6887mch_copy_file_attribute(char_u *from, char_u *to)
6888{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006890 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 return 0;
6892}
6893
6894#if defined(MYRESETSTKOFLW) || defined(PROTO)
6895/*
6896 * Recreate a destroyed stack guard page in win32.
6897 * Written by Benjamin Peterson.
6898 */
6899
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006901# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903/*
6904 * This function does the same thing as _resetstkoflw(), which is only
6905 * available in DevStudio .net and later.
6906 * Returns 0 for failure, 1 for success.
6907 */
6908 int
6909myresetstkoflw(void)
6910{
6911 BYTE *pStackPtr;
6912 BYTE *pGuardPage;
6913 BYTE *pStackBase;
6914 BYTE *pLowestPossiblePage;
6915 MEMORY_BASIC_INFORMATION mbi;
6916 SYSTEM_INFO si;
6917 DWORD nPageSize;
6918 DWORD dummy;
6919
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 GetSystemInfo(&si);
6922 nPageSize = si.dwPageSize;
6923
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 pStackPtr = (BYTE*)_alloca(1);
6926
Bram Moolenaar0f873732019-12-05 20:28:46 +01006927 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6929 return 0;
6930 pStackBase = (BYTE*)mbi.AllocationBase;
6931
Bram Moolenaar0f873732019-12-05 20:28:46 +01006932 // ...and the page thats min_stack_req pages away from stack base; this is
6933 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006934 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006937 // We want the first committed page in the stack Start at the stack
6938 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 BYTE *pBlock = pStackBase;
6940
Bram Moolenaara466c992005-07-09 21:03:22 +00006941 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
6943 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6944 return 0;
6945
6946 pBlock += mbi.RegionSize;
6947
6948 if (mbi.State & MEM_COMMIT)
6949 break;
6950 }
6951
Bram Moolenaar0f873732019-12-05 20:28:46 +01006952 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (mbi.Protect & PAGE_GUARD)
6954 return 1;
6955
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6958 pGuardPage = pLowestPossiblePage;
6959 else
6960 pGuardPage = (BYTE*)mbi.BaseAddress;
6961
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6964 return 0;
6965
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6968 &dummy))
6969 return 0;
6970 }
6971
6972 return 1;
6973}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006976
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006977/*
6978 * The command line arguments in UCS2
6979 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006980static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006981static LPWSTR *ArglistW = NULL;
6982static int global_argc = 0;
6983static char **global_argv;
6984
Bram Moolenaar0f873732019-12-05 20:28:46 +01006985static int used_file_argc = 0; // last argument in global_argv[] used
6986 // for the argument list.
6987static int *used_file_indexes = NULL; // indexes in global_argv[] for
6988 // command line arguments added to
6989 // the argument list
6990static int used_file_count = 0; // nr of entries in used_file_indexes
6991static int used_file_literal = FALSE; // take file names literally
6992static int used_file_full_path = FALSE; // file name was full path
6993static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006994static int used_alist_count = 0;
6995
6996
6997/*
6998 * Get the command line arguments. Unicode version.
6999 * Returns argc. Zero when something fails.
7000 */
7001 int
7002get_cmd_argsW(char ***argvp)
7003{
7004 char **argv = NULL;
7005 int argc = 0;
7006 int i;
7007
Bram Moolenaar14993322014-09-09 12:25:33 +02007008 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007009 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7010 if (ArglistW != NULL)
7011 {
7012 argv = malloc((nArgsW + 1) * sizeof(char *));
7013 if (argv != NULL)
7014 {
7015 argc = nArgsW;
7016 argv[argc] = NULL;
7017 for (i = 0; i < argc; ++i)
7018 {
7019 int len;
7020
Bram Moolenaar0f873732019-12-05 20:28:46 +01007021 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007022 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007023 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007024 (LPSTR *)&argv[i], &len, 0, 0);
7025 if (argv[i] == NULL)
7026 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007027 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007028 while (i > 0)
7029 free(argv[--i]);
7030 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007031 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007032 argc = 0;
7033 }
7034 }
7035 }
7036 }
7037
7038 global_argc = argc;
7039 global_argv = argv;
7040 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007041 {
7042 if (used_file_indexes != NULL)
7043 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007044 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007045 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007046
7047 if (argvp != NULL)
7048 *argvp = argv;
7049 return argc;
7050}
7051
7052 void
7053free_cmd_argsW(void)
7054{
7055 if (ArglistW != NULL)
7056 {
7057 GlobalFree(ArglistW);
7058 ArglistW = NULL;
7059 }
7060}
7061
7062/*
7063 * Remember "name" is an argument that was added to the argument list.
7064 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7065 * is called.
7066 */
7067 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007068used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007069{
7070 int i;
7071
7072 if (used_file_indexes == NULL)
7073 return;
7074 for (i = used_file_argc + 1; i < global_argc; ++i)
7075 if (STRCMP(global_argv[i], name) == 0)
7076 {
7077 used_file_argc = i;
7078 used_file_indexes[used_file_count++] = i;
7079 break;
7080 }
7081 used_file_literal = literal;
7082 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007083 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007084}
7085
7086/*
7087 * Remember the length of the argument list as it was. If it changes then we
7088 * leave it alone when 'encoding' is set.
7089 */
7090 void
7091set_alist_count(void)
7092{
7093 used_alist_count = GARGCOUNT;
7094}
7095
7096/*
7097 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7098 * has been changed while starting up. Use the UCS-2 command line arguments
7099 * and convert them to 'encoding'.
7100 */
7101 void
7102fix_arg_enc(void)
7103{
7104 int i;
7105 int idx;
7106 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007107 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007108
Bram Moolenaar0f873732019-12-05 20:28:46 +01007109 // Safety checks:
7110 // - if argument count differs between the wide and non-wide argument
7111 // list, something must be wrong.
7112 // - the file name arguments must have been located.
7113 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007114 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007115 || ArglistW == NULL
7116 || used_file_indexes == NULL
7117 || used_file_count == 0
7118 || used_alist_count != GARGCOUNT)
7119 return;
7120
Bram Moolenaar0f873732019-12-05 20:28:46 +01007121 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007122 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007123 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007124 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007125 for (i = 0; i < GARGCOUNT; ++i)
7126 fnum_list[i] = GARGLIST[i].ae_fnum;
7127
Bram Moolenaar0f873732019-12-05 20:28:46 +01007128 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007129 alist_clear(&global_alist);
7130 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007131 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007132
7133 for (i = 0; i < used_file_count; ++i)
7134 {
7135 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007136 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007137 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007138 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007139 int literal = used_file_literal;
7140
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007141#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007142 // When using diff mode may need to concatenate file name to
7143 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007144 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7145 && !mch_isdir(alist_name(&GARGLIST[0])))
7146 {
7147 char_u *r;
7148
7149 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7150 if (r != NULL)
7151 {
7152 vim_free(str);
7153 str = r;
7154 }
7155 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007156#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007157 // Re-use the old buffer by renaming it. When not using literal
7158 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007159 if (used_file_literal)
7160 buf_set_name(fnum_list[i], str);
7161
Bram Moolenaar0f873732019-12-05 20:28:46 +01007162 // Check backtick literal. backtick literal is already expanded in
7163 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007164 if (literal == FALSE)
7165 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007166 size_t len = STRLEN(str);
7167
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007168 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7169 literal = TRUE;
7170 }
7171 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007172 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007173 }
7174
7175 if (!used_file_literal)
7176 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007177 // Now expand wildcards in the arguments.
7178 // Temporarily add '(' and ')' to 'isfname'. These are valid
7179 // filename characters but are excluded from 'isfname' to make
7180 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7181 // Also, unset wildignore to not be influenced by this option.
7182 // The arguments specified in command-line should be kept even if
7183 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007184 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007185 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007186 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007187 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007188 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007189 }
7190
Bram Moolenaar0f873732019-12-05 20:28:46 +01007191 // If wildcard expansion failed, we are editing the first file of the
7192 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007193 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7194 {
7195 do_cmdline_cmd((char_u *)":rewind");
7196 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007197 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007198 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007199
7200 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007201}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007202
7203 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007204mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007205{
7206 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007207 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007208
Bram Moolenaar964b3742019-05-24 18:54:09 +02007209 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007210 if (envbuf == NULL)
7211 return -1;
7212
7213 sprintf((char *)envbuf, "%s=%s", var, value);
7214
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007215 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007216
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007217 vim_free(envbuf);
7218 if (p == NULL)
7219 return -1;
7220 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007221#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007222 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007223#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007224 // Unlike Un*x systems, we can free the string for _wputenv().
7225 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007226
7227 return 0;
7228}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007229
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007230/*
7231 * Support for 256 colors and 24-bit colors was added in Windows 10
7232 * version 1703 (Creators update).
7233 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007234#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7235
7236/*
7237 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007238 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007239 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007240#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007241
7242/*
7243 * ConPTY differences between versions, need different logic.
7244 * version 1903 (May 2019 update).
7245 */
7246#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7247
7248/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007249 * version 1909 (November 2019 update).
7250 */
7251#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7252
7253/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007254 * Confirm until this version. Also the logic changes.
7255 * insider preview.
7256 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007257#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007258
7259/*
7260 * Not stable now.
7261 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007262#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007263
7264 static void
7265vtp_flag_init(void)
7266{
7267 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007268#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007269 DWORD mode;
7270 HANDLE out;
7271
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007272# ifdef VIMDLL
7273 if (!gui.in_use)
7274# endif
7275 {
7276 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007277
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007278 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7279 GetConsoleMode(out, &mode);
7280 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7281 if (SetConsoleMode(out, mode) == 0)
7282 vtp_working = 0;
7283 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007284#endif
7285
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007286 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007287 conpty_working = 1;
7288 if (ver >= CONPTY_STABLE_BUILD)
7289 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007290
Bram Moolenaar57da6982019-09-13 22:30:11 +02007291 if (ver <= CONPTY_INSIDER_BUILD)
7292 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007293 if (ver <= CONPTY_1909_BUILD)
7294 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007295 if (ver <= CONPTY_1903_BUILD)
7296 conpty_type = 2;
7297 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7298 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007299}
7300
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007301#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007302
7303 static void
7304vtp_init(void)
7305{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007306 HMODULE hKerneldll;
7307 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007308# ifdef FEAT_TERMGUICOLORS
7309 COLORREF fg, bg;
7310# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007311
Bram Moolenaar0f873732019-12-05 20:28:46 +01007312 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007313 hKerneldll = GetModuleHandle("kernel32.dll");
7314 if (hKerneldll != NULL)
7315 {
7316 pGetConsoleScreenBufferInfoEx =
7317 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7318 hKerneldll, "GetConsoleScreenBufferInfoEx");
7319 pSetConsoleScreenBufferInfoEx =
7320 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7321 hKerneldll, "SetConsoleScreenBufferInfoEx");
7322 if (pGetConsoleScreenBufferInfoEx != NULL
7323 && pSetConsoleScreenBufferInfoEx != NULL)
7324 has_csbiex = TRUE;
7325 }
7326
7327 csbi.cbSize = sizeof(csbi);
7328 if (has_csbiex)
7329 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007330 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7331 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
7332
7333# ifdef FEAT_TERMGUICOLORS
7334 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7335 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7336 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7337 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7338 default_console_color_bg = bg;
7339 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007340# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007341
7342 set_console_color_rgb();
7343}
7344
7345 static void
7346vtp_exit(void)
7347{
7348 reset_console_color_rgb();
7349}
7350
7351 static int
7352vtp_printf(
7353 char *format,
7354 ...)
7355{
7356 char_u buf[100];
7357 va_list list;
7358 DWORD result;
7359
7360 va_start(list, format);
7361 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7362 va_end(list);
7363 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7364 return (int)result;
7365}
7366
7367 static void
7368vtp_sgr_bulk(
7369 int arg)
7370{
7371 int args[1];
7372
7373 args[0] = arg;
7374 vtp_sgr_bulks(1, args);
7375}
7376
7377 static void
7378vtp_sgr_bulks(
7379 int argc,
7380 int *args
7381)
7382{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007383 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007384 char_u buf[2 + (4 * 16) + 1];
7385 char_u *p;
7386 int i;
7387
7388 p = buf;
7389 *p++ = '\033';
7390 *p++ = '[';
7391
7392 for (i = 0; i < argc; ++i)
7393 {
7394 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7395 *p++ = ';';
7396 }
7397 p--;
7398 *p++ = 'm';
7399 *p = NUL;
7400 vtp_printf((char *)buf);
7401}
7402
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007403# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007404 static int
7405ctermtoxterm(
7406 int cterm)
7407{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007408 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007409
7410 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7411 return (((int)r << 16) | ((int)g << 8) | (int)b);
7412}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007413# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007414
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007415 static void
7416set_console_color_rgb(void)
7417{
7418# ifdef FEAT_TERMGUICOLORS
7419 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007420 guicolor_T fg, bg;
7421 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007422
7423 if (!USE_VTP)
7424 return;
7425
Bram Moolenaara050b942019-12-02 21:35:31 +01007426 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7427
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007428 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7429 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7430
7431 csbi.cbSize = sizeof(csbi);
7432 if (has_csbiex)
7433 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7434
7435 csbi.cbSize = sizeof(csbi);
7436 csbi.srWindow.Right += 1;
7437 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007438 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7439 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007440 if (has_csbiex)
7441 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7442# endif
7443}
7444
Bram Moolenaara050b942019-12-02 21:35:31 +01007445# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7446 void
7447get_default_console_color(
7448 int *cterm_fg,
7449 int *cterm_bg,
7450 guicolor_T *gui_fg,
7451 guicolor_T *gui_bg)
7452{
7453 int id;
7454 guicolor_T guifg = INVALCOLOR;
7455 guicolor_T guibg = INVALCOLOR;
7456 int ctermfg = 0;
7457 int ctermbg = 0;
7458
7459 id = syn_name2id((char_u *)"Normal");
7460 if (id > 0 && p_tgc)
7461 syn_id2colors(id, &guifg, &guibg);
7462 if (guifg == INVALCOLOR)
7463 {
7464 ctermfg = -1;
7465 if (id > 0)
7466 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7467 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7468 : default_console_color_fg;
7469 cterm_normal_fg_gui_color = guifg;
7470 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7471 }
7472 if (guibg == INVALCOLOR)
7473 {
7474 ctermbg = -1;
7475 if (id > 0)
7476 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7477 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7478 : default_console_color_bg;
7479 cterm_normal_bg_gui_color = guibg;
7480 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7481 }
7482
7483 *cterm_fg = ctermfg;
7484 *cterm_bg = ctermbg;
7485 *gui_fg = guifg;
7486 *gui_bg = guibg;
7487}
7488# endif
7489
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007490 static void
7491reset_console_color_rgb(void)
7492{
7493# ifdef FEAT_TERMGUICOLORS
7494 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7495
7496 csbi.cbSize = sizeof(csbi);
7497 if (has_csbiex)
7498 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7499
7500 csbi.cbSize = sizeof(csbi);
7501 csbi.srWindow.Right += 1;
7502 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007503 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7504 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007505 if (has_csbiex)
7506 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7507# endif
7508}
7509
7510 void
7511control_console_color_rgb(void)
7512{
7513 if (USE_VTP)
7514 set_console_color_rgb();
7515 else
7516 reset_console_color_rgb();
7517}
7518
7519 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007520use_vtp(void)
7521{
7522 return USE_VTP;
7523}
7524
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007525 int
7526is_term_win32(void)
7527{
7528 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7529}
7530
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007531 int
7532has_vtp_working(void)
7533{
7534 return vtp_working;
7535}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007536
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007537#endif
7538
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007539 int
7540has_conpty_working(void)
7541{
7542 return conpty_working;
7543}
7544
7545 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007546get_conpty_type(void)
7547{
7548 return conpty_type;
7549}
7550
7551 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007552is_conpty_stable(void)
7553{
7554 return conpty_stable;
7555}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007556
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007557#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007558 void
7559resize_console_buf(void)
7560{
7561 CONSOLE_SCREEN_BUFFER_INFO csbi;
7562 COORD coord;
7563 SMALL_RECT newsize;
7564
7565 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7566 {
7567 coord.X = SRWIDTH(csbi.srWindow);
7568 coord.Y = SRHEIGHT(csbi.srWindow);
7569 SetConsoleScreenBufferSize(g_hConOut, coord);
7570
7571 newsize.Left = 0;
7572 newsize.Top = 0;
7573 newsize.Right = coord.X - 1;
7574 newsize.Bottom = coord.Y - 1;
7575 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7576
7577 SetConsoleScreenBufferSize(g_hConOut, coord);
7578 }
7579}
7580#endif