blob: b56033cac72335a61c4c756a570341be25abbd7c [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
1644 // Only call shell_resized() when the size actually change 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);
1650 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1651 ResizeConBuf(g_hConOut, dwSize);
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001652 shell_resized();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001653 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 else if (ir.EventType == MOUSE_EVENT
1656 && decode_mouse_event(&ir.Event.MouseEvent))
1657 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else if (msec == 0)
1660 break;
1661 }
1662
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001663# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001664 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (input_available())
1666 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001667# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 return FALSE;
1670}
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672/*
1673 * return non-zero if a character is available
1674 */
1675 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001676mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001678# ifdef VIMDLL
1679 if (gui.in_use)
1680 return TRUE;
1681# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001682 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001684
1685# if defined(FEAT_TERMINAL) || defined(PROTO)
1686/*
1687 * Check for any pending input or messages.
1688 */
1689 int
1690mch_check_messages(void)
1691{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001692# ifdef VIMDLL
1693 if (gui.in_use)
1694 return TRUE;
1695# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001696 return WaitForChar(0L, TRUE);
1697}
1698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700/*
1701 * Create the console input. Used when reading stdin doesn't work.
1702 */
1703 static void
1704create_conin(void)
1705{
1706 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1707 FILE_SHARE_READ|FILE_SHARE_WRITE,
1708 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001709 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 did_create_conin = TRUE;
1711}
1712
1713/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001714 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001716 static WCHAR
1717tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001719 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 for (;;)
1722 {
1723 INPUT_RECORD ir;
1724 DWORD cRecords = 0;
1725
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001726# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001727 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (input_available())
1729 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (g_nMouseClick != -1)
1731 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001732# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001733 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
1735 if (did_create_conin)
1736 read_error_exit();
1737 create_conin();
1738 continue;
1739 }
1740
1741 if (ir.EventType == KEY_EVENT)
1742 {
1743 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1744 pmodifiers, TRUE))
1745 return ch;
1746 }
1747 else if (ir.EventType == FOCUS_EVENT)
1748 handle_focus_event(ir);
1749 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1750 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 else if (ir.EventType == MOUSE_EVENT)
1752 {
1753 if (decode_mouse_event(&ir.Event.MouseEvent))
1754 return 0;
1755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001758#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760
1761/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001762 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 * Get one or more characters from the keyboard or the mouse.
1764 * If time == 0, do not wait for characters.
1765 * If time == n, wait a short time for characters.
1766 * If time == -1, wait forever for characters.
1767 * Returns the number of characters read into buf.
1768 */
1769 int
1770mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001771 char_u *buf UNUSED,
1772 int maxlen UNUSED,
1773 long time UNUSED,
1774 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001776#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 int len;
1779 int c;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001780# define TYPEAHEADLEN 20
Bram Moolenaar0f873732019-12-05 20:28:46 +01001781 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 static int typeaheadlen = 0;
1783
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001784# ifdef VIMDLL
1785 if (gui.in_use)
1786 return 0;
1787# endif
1788
Bram Moolenaar0f873732019-12-05 20:28:46 +01001789 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (typeaheadlen > 0)
1791 goto theend;
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (time >= 0)
1794 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001795 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001798 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar3918c952005-03-15 22:34:55 +00001802 /*
1803 * If there is no character available within 2 seconds (default)
1804 * write the autoscript file to disk. Or cause the CursorHold event
1805 * to be triggered.
1806 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001807 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001809 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001811 buf[0] = K_SPECIAL;
1812 buf[1] = KS_EXTRA;
1813 buf[2] = (int)KE_CURSORHOLD;
1814 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001816 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818 }
1819
1820 /*
1821 * Try to read as many characters as there are, until the buffer is full.
1822 */
1823
Bram Moolenaar0f873732019-12-05 20:28:46 +01001824 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 g_fCBrkPressed = FALSE;
1826
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001827# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (fdDump)
1829 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar0f873732019-12-05 20:28:46 +01001832 // Keep looping until there is something in the typeahead buffer and more
1833 // to get and still room in the buffer (up to two bytes for a char and
1834 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001835 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 && typeaheadlen + 5 <= TYPEAHEADLEN)
1837 {
1838 if (typebuf_changed(tb_change_cnt))
1839 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001840 // "buf" may be invalid now if a client put something in the
1841 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 typeaheadlen = 0;
1843 break;
1844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (g_nMouseClick != -1)
1846 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001847# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (fdDump)
1849 fprintf(fdDump, "{%02x @ %d, %d}",
1850 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 typeahead[typeaheadlen++] = ESC + 128;
1853 typeahead[typeaheadlen++] = 'M';
1854 typeahead[typeaheadlen++] = g_nMouseClick;
1855 typeahead[typeaheadlen++] = g_xMouse + '!';
1856 typeahead[typeaheadlen++] = g_yMouse + '!';
1857 g_nMouseClick = -1;
1858 }
1859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001861 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 int modifiers = 0;
1863
1864 c = tgetch(&modifiers, &ch2);
1865
1866 if (typebuf_changed(tb_change_cnt))
1867 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001868 // "buf" may be invalid now if a client put something in the
1869 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 typeaheadlen = 0;
1871 break;
1872 }
1873
1874 if (c == Ctrl_C && ctrl_c_interrupts)
1875 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001876# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 got_int = TRUE;
1880 }
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 int n = 1;
1885
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001886 if (ch2 == NUL)
1887 {
1888 int i;
1889 char_u *p;
1890 WCHAR ch[2];
1891
1892 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001893 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001894 {
1895 ch[1] = tgetch(&modifiers, &ch2);
1896 n++;
1897 }
1898 p = utf16_to_enc(ch, &n);
1899 if (p != NULL)
1900 {
1901 for (i = 0; i < n; i++)
1902 typeahead[typeaheadlen + i] = p[i];
1903 vim_free(p);
1904 }
1905 }
1906 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001907 typeahead[typeaheadlen] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (ch2 != NUL)
1909 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001910 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001911 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001912 switch (ch2)
1913 {
1914 case (WCHAR)'\324': // SHIFT+Insert
1915 case (WCHAR)'\325': // CTRL+Insert
1916 case (WCHAR)'\327': // SHIFT+Delete
1917 case (WCHAR)'\330': // CTRL+Delete
1918 typeahead[typeaheadlen + n] = (char_u)ch2;
1919 n++;
1920 break;
1921
1922 default:
1923 typeahead[typeaheadlen + n] = 3;
1924 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1925 n += 2;
1926 break;
1927 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001928 }
1929 else
1930 {
1931 typeahead[typeaheadlen + n] = 3;
1932 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1933 n += 2;
1934 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001935 }
1936
Bram Moolenaar0f873732019-12-05 20:28:46 +01001937 // Use the ALT key to set the 8th bit of the character
1938 // when it's one byte, the 8th bit isn't set yet and not
1939 // using a double-byte encoding (would become a lead
1940 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if ((modifiers & MOD_MASK_ALT)
1942 && n == 1
1943 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 )
1946 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001947 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1948 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 modifiers &= ~MOD_MASK_ALT;
1950 }
1951
1952 if (modifiers != 0)
1953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001954 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 mch_memmove(typeahead + typeaheadlen + 3,
1956 typeahead + typeaheadlen, n);
1957 typeahead[typeaheadlen++] = K_SPECIAL;
1958 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1959 typeahead[typeaheadlen++] = modifiers;
1960 }
1961
1962 typeaheadlen += n;
1963
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001964# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (fdDump)
1966 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 }
1970 }
1971
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001972# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (fdDump)
1974 {
1975 fputs("]\n", fdDump);
1976 fflush(fdDump);
1977 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001981 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 len = 0;
1983 while (len < maxlen && typeaheadlen > 0)
1984 {
1985 buf[len++] = typeahead[0];
1986 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
1987 }
1988 return len;
1989
Bram Moolenaar0f873732019-12-05 20:28:46 +01001990#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001992#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993}
1994
Bram Moolenaar82881492012-11-20 16:53:39 +01001995#ifndef PROTO
1996# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001997# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01001998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#endif
2000
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002001/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002002 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2003 * If "use_path" is FALSE: Return TRUE if "name" exists.
2004 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2005 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002006 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002009executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002011 WCHAR *p;
2012 WCHAR fnamew[_MAX_PATH];
2013 WCHAR *dumw;
2014 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002015 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
Bram Moolenaar43663192017-03-05 14:29:12 +01002017 if (!use_path)
2018 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002019 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002020 {
2021 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002022 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002023 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002024 *path = vim_strsave((char_u *)name);
2025 else
2026 *path = FullName_save((char_u *)name, FALSE);
2027 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002028 return TRUE;
2029 }
2030 return FALSE;
2031 }
2032
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002033 p = enc_to_utf16((char_u *)name, NULL);
2034 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002035 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002036
2037 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002038 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002039 if (wnewpath == NULL)
2040 return FALSE;
2041 wcscpy(wnewpath, L".;");
2042 wcscat(wnewpath, wcurpath);
2043 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2044 vim_free(wnewpath);
2045 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002046 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002047 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002048 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002049 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002050 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002051 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002052 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053}
2054
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002055#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2056 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002057/*
2058 * Bad parameter handler.
2059 *
2060 * Certain MS CRT functions will intentionally crash when passed invalid
2061 * parameters to highlight possible security holes. Setting this function as
2062 * the bad parameter handler will prevent the crash.
2063 *
2064 * In debug builds the parameters contain CRT information that might help track
2065 * down the source of a problem, but in non-debug builds the arguments are all
2066 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2067 * worth allowing these to make debugging of issues easier.
2068 */
2069 static void
2070bad_param_handler(const wchar_t *expression,
2071 const wchar_t *function,
2072 const wchar_t *file,
2073 unsigned int line,
2074 uintptr_t pReserved)
2075{
2076}
2077
2078# define SET_INVALID_PARAM_HANDLER \
2079 ((void)_set_invalid_parameter_handler(bad_param_handler))
2080#else
2081# define SET_INVALID_PARAM_HANDLER
2082#endif
2083
Bram Moolenaar4f974752019-02-17 17:44:42 +01002084#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086/*
2087 * GUI version of mch_init().
2088 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002089 static void
2090mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002092# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
Bram Moolenaar0f873732019-12-05 20:28:46 +01002096 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002097 SET_INVALID_PARAM_HANDLER;
2098
Bram Moolenaar0f873732019-12-05 20:28:46 +01002099 // Let critical errors result in a failure, not in a dialog box. Required
2100 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 SetErrorMode(SEM_FAILCRITICALERRORS);
2102
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
Bram Moolenaar0f873732019-12-05 20:28:46 +01002105 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 Rows = 25;
2107 Columns = 80;
2108
Bram Moolenaar0f873732019-12-05 20:28:46 +01002109 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
2111 char_u vimrun_location[_MAX_PATH + 4];
2112
Bram Moolenaar0f873732019-12-05 20:28:46 +01002113 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 STRCPY(vimrun_location, exe_name);
2115 STRCPY(gettail(vimrun_location), "vimrun.exe");
2116 if (mch_getperm(vimrun_location) >= 0)
2117 {
2118 if (*skiptowhite(vimrun_location) != NUL)
2119 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002120 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 mch_memmove(vimrun_location + 1, vimrun_location,
2122 STRLEN(vimrun_location) + 1);
2123 *vimrun_location = '"';
2124 STRCPY(gettail(vimrun_location), "vimrun\" ");
2125 }
2126 else
2127 STRCPY(gettail(vimrun_location), "vimrun ");
2128
2129 vimrun_path = (char *)vim_strsave(vimrun_location);
2130 s_dont_use_vimrun = FALSE;
2131 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002132 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 s_dont_use_vimrun = FALSE;
2134
Bram Moolenaar0f873732019-12-05 20:28:46 +01002135 // Don't give the warning for a missing vimrun.exe right now, but only
2136 // when vimrun was supposed to be used. Don't bother people that do
2137 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (s_dont_use_vimrun)
2139 need_vimrun_warning = TRUE;
2140 }
2141
2142 /*
2143 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2144 * Otherwise the default "findstr /n" is used.
2145 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002146 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2148
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002149# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002150 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002151# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002152
2153 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154}
2155
2156
Bram Moolenaar0f873732019-12-05 20:28:46 +01002157#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002158
2159#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002161# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2162# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164/*
2165 * ClearConsoleBuffer()
2166 * Description:
2167 * Clears the entire contents of the console screen buffer, using the
2168 * specified attribute.
2169 * Returns:
2170 * TRUE on success
2171 */
2172 static BOOL
2173ClearConsoleBuffer(WORD wAttribute)
2174{
2175 CONSOLE_SCREEN_BUFFER_INFO csbi;
2176 COORD coord;
2177 DWORD NumCells, dummy;
2178
2179 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2180 return FALSE;
2181
2182 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2183 coord.X = 0;
2184 coord.Y = 0;
2185 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2186 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2189 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 return TRUE;
2193}
2194
2195/*
2196 * FitConsoleWindow()
2197 * Description:
2198 * Checks if the console window will fit within given buffer dimensions.
2199 * Also, if requested, will shrink the window to fit.
2200 * Returns:
2201 * TRUE on success
2202 */
2203 static BOOL
2204FitConsoleWindow(
2205 COORD dwBufferSize,
2206 BOOL WantAdjust)
2207{
2208 CONSOLE_SCREEN_BUFFER_INFO csbi;
2209 COORD dwWindowSize;
2210 BOOL NeedAdjust = FALSE;
2211
2212 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2213 {
2214 /*
2215 * A buffer resize will fail if the current console window does
2216 * not lie completely within that buffer. To avoid this, we might
2217 * have to move and possibly shrink the window.
2218 */
2219 if (csbi.srWindow.Right >= dwBufferSize.X)
2220 {
2221 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2222 if (dwWindowSize.X > dwBufferSize.X)
2223 dwWindowSize.X = dwBufferSize.X;
2224 csbi.srWindow.Right = dwBufferSize.X - 1;
2225 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2226 NeedAdjust = TRUE;
2227 }
2228 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2229 {
2230 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2231 if (dwWindowSize.Y > dwBufferSize.Y)
2232 dwWindowSize.Y = dwBufferSize.Y;
2233 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2234 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2235 NeedAdjust = TRUE;
2236 }
2237 if (NeedAdjust && WantAdjust)
2238 {
2239 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2240 return FALSE;
2241 }
2242 return TRUE;
2243 }
2244
2245 return FALSE;
2246}
2247
2248typedef struct ConsoleBufferStruct
2249{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002250 BOOL IsValid;
2251 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002252 PCHAR_INFO Buffer;
2253 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002254 PSMALL_RECT Regions;
2255 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256} ConsoleBuffer;
2257
2258/*
2259 * SaveConsoleBuffer()
2260 * Description:
2261 * Saves important information about the console buffer, including the
2262 * actual buffer contents. The saved information is suitable for later
2263 * restoration by RestoreConsoleBuffer().
2264 * Returns:
2265 * TRUE if all information was saved; FALSE otherwise
2266 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2267 */
2268 static BOOL
2269SaveConsoleBuffer(
2270 ConsoleBuffer *cb)
2271{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002272 DWORD NumCells;
2273 COORD BufferCoord;
2274 SMALL_RECT ReadRegion;
2275 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002276 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (cb == NULL)
2279 return FALSE;
2280
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002281 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 cb->IsValid = FALSE;
2284 return FALSE;
2285 }
2286 cb->IsValid = TRUE;
2287
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002288 /*
2289 * Allocate a buffer large enough to hold the entire console screen
2290 * buffer. If this ConsoleBuffer structure has already been initialized
2291 * with a buffer of the correct size, then just use that one.
2292 */
2293 if (!cb->IsValid || cb->Buffer == NULL ||
2294 cb->BufferSize.X != cb->Info.dwSize.X ||
2295 cb->BufferSize.Y != cb->Info.dwSize.Y)
2296 {
2297 cb->BufferSize.X = cb->Info.dwSize.X;
2298 cb->BufferSize.Y = cb->Info.dwSize.Y;
2299 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2300 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002301 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002302 if (cb->Buffer == NULL)
2303 return FALSE;
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002307 * We will now copy the console screen buffer into our buffer.
2308 * ReadConsoleOutput() seems to be limited as far as how much you
2309 * can read at a time. Empirically, this number seems to be about
2310 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2311 * in chunks until it is all copied. The chunks will all have the
2312 * same horizontal characteristics, so initialize them now. The
2313 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002315 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002316 ReadRegion.Left = 0;
2317 ReadRegion.Right = cb->Info.dwSize.X - 1;
2318 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002319
2320 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2321 if (cb->Regions == NULL || numregions != cb->NumRegions)
2322 {
2323 cb->NumRegions = numregions;
2324 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002325 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002326 if (cb->Regions == NULL)
2327 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002328 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002329 return FALSE;
2330 }
2331 }
2332
2333 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002335 /*
2336 * Read into position (0, Y) in our buffer.
2337 */
2338 BufferCoord.Y = Y;
2339 /*
2340 * Read the region whose top left corner is (0, Y) and whose bottom
2341 * right corner is (width - 1, Y + Y_incr - 1). This should define
2342 * a region of size width by Y_incr. Don't worry if this region is
2343 * too large for the remaining buffer; it will be cropped.
2344 */
2345 ReadRegion.Top = Y;
2346 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 if (!ReadConsoleOutputW(g_hConOut, // output handle
2348 cb->Buffer, // our buffer
2349 cb->BufferSize, // dimensions of our buffer
2350 BufferCoord, // offset in our buffer
2351 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002352 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002353 VIM_CLEAR(cb->Buffer);
2354 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002355 return FALSE;
2356 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002357 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359
2360 return TRUE;
2361}
2362
2363/*
2364 * RestoreConsoleBuffer()
2365 * Description:
2366 * Restores important information about the console buffer, including the
2367 * actual buffer contents, if desired. The information to restore is in
2368 * the same format used by SaveConsoleBuffer().
2369 * Returns:
2370 * TRUE on success
2371 */
2372 static BOOL
2373RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002374 ConsoleBuffer *cb,
2375 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002377 COORD BufferCoord;
2378 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002379 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 if (cb == NULL || !cb->IsValid)
2382 return FALSE;
2383
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002384 /*
2385 * Before restoring the buffer contents, clear the current buffer, and
2386 * restore the cursor position and window information. Doing this now
2387 * prevents old buffer contents from "flashing" onto the screen.
2388 */
2389 if (RestoreScreen)
2390 ClearConsoleBuffer(cb->Info.wAttributes);
2391
2392 FitConsoleWindow(cb->Info.dwSize, TRUE);
2393 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2394 return FALSE;
2395 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2396 return FALSE;
2397
2398 if (!RestoreScreen)
2399 {
2400 /*
2401 * No need to restore the screen buffer contents, so we're done.
2402 */
2403 return TRUE;
2404 }
2405
2406 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2407 return FALSE;
2408 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2409 return FALSE;
2410
2411 /*
2412 * Restore the screen buffer contents.
2413 */
2414 if (cb->Buffer != NULL)
2415 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002416 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002417 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002418 BufferCoord.X = cb->Regions[i].Left;
2419 BufferCoord.Y = cb->Regions[i].Top;
2420 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002421 if (!WriteConsoleOutputW(g_hConOut, // output handle
2422 cb->Buffer, // our buffer
2423 cb->BufferSize, // dimensions of our buffer
2424 BufferCoord, // offset in our buffer
2425 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002426 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002427 }
2428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 return TRUE;
2431}
2432
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002433# define FEAT_RESTORE_ORIG_SCREEN
2434# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002435static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437static ConsoleBuffer g_cbNonTermcap = { 0 };
2438static ConsoleBuffer g_cbTermcap = { 0 };
2439
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002440# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002442HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443static HICON g_hOrigIconSmall = NULL;
2444static HICON g_hOrigIcon = NULL;
2445static HICON g_hVimIcon = NULL;
2446static BOOL g_fCanChangeIcon = FALSE;
2447
Bram Moolenaar0f873732019-12-05 20:28:46 +01002448// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002449# ifndef ICON_SMALL
2450# define ICON_SMALL 0
2451# endif
2452# ifndef ICON_BIG
2453# define ICON_BIG 1
2454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455/*
2456 * GetConsoleIcon()
2457 * Description:
2458 * Attempts to retrieve the small icon and/or the big icon currently in
2459 * use by a given window.
2460 * Returns:
2461 * TRUE on success
2462 */
2463 static BOOL
2464GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002465 HWND hWnd,
2466 HICON *phIconSmall,
2467 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 if (hWnd == NULL)
2470 return FALSE;
2471
2472 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002473 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2474 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002476 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2477 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return TRUE;
2479}
2480
2481/*
2482 * SetConsoleIcon()
2483 * Description:
2484 * Attempts to change the small icon and/or the big icon currently in
2485 * use by a given window.
2486 * Returns:
2487 * TRUE on success
2488 */
2489 static BOOL
2490SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002491 HWND hWnd,
2492 HICON hIconSmall,
2493 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (hWnd == NULL)
2496 return FALSE;
2497
2498 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002499 SendMessage(hWnd, WM_SETICON,
2500 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002502 SendMessage(hWnd, WM_SETICON,
2503 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 return TRUE;
2505}
2506
2507/*
2508 * SaveConsoleTitleAndIcon()
2509 * Description:
2510 * Saves the current console window title in g_szOrigTitle, for later
2511 * restoration. Also, attempts to obtain a handle to the console window,
2512 * and use it to save the small and big icons currently in use by the
2513 * console window. This is not always possible on some versions of Windows;
2514 * nor is it possible when running Vim remotely using Telnet (since the
2515 * console window the user sees is owned by a remote process).
2516 */
2517 static void
2518SaveConsoleTitleAndIcon(void)
2519{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002520 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2522 return;
2523
2524 /*
2525 * Obtain a handle to the console window using GetConsoleWindow() from
2526 * KERNEL32.DLL; we need to handle in order to change the window icon.
2527 * This function only exists on NT-based Windows, starting with Windows
2528 * 2000. On older operating systems, we can't change the window icon
2529 * anyway.
2530 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002531 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (g_hWnd == NULL)
2533 return;
2534
Bram Moolenaar0f873732019-12-05 20:28:46 +01002535 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2537 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2538 return;
2539
Bram Moolenaar0f873732019-12-05 20:28:46 +01002540 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002541 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002542 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (g_hVimIcon != NULL)
2544 g_fCanChangeIcon = TRUE;
2545}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548static int g_fWindInitCalled = FALSE;
2549static int g_fTermcapMode = FALSE;
2550static CONSOLE_CURSOR_INFO g_cci;
2551static DWORD g_cmodein = 0;
2552static DWORD g_cmodeout = 0;
2553
2554/*
2555 * non-GUI version of mch_init().
2556 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002557 static void
2558mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002560# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002561 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002562# endif
2563# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
Bram Moolenaar0f873732019-12-05 20:28:46 +01002567 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002568 SET_INVALID_PARAM_HANDLER;
2569
Bram Moolenaar0f873732019-12-05 20:28:46 +01002570 // Let critical errors result in a failure, not in a dialog box. Required
2571 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 SetErrorMode(SEM_FAILCRITICALERRORS);
2573
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 out_flush();
2576
Bram Moolenaar0f873732019-12-05 20:28:46 +01002577 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (read_cmd_fd == 0)
2579 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2580 else
2581 create_conin();
2582 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2583
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002584# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002586 SaveConsoleBuffer(&g_cbOrig);
2587 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002588# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002589 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002590 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2591 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (cterm_normal_fg_color == 0)
2594 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2595 if (cterm_normal_bg_color == 0)
2596 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2597
Bram Moolenaarbdace832019-03-02 10:13:42 +01002598 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002599 g_color_index_fg = g_attrDefault & 0xf;
2600 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2601
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 update_tcap(g_attrCurrent);
2604
2605 GetConsoleCursorInfo(g_hConOut, &g_cci);
2606 GetConsoleMode(g_hConIn, &g_cmodein);
2607 GetConsoleMode(g_hConOut, &g_cmodeout);
2608
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002609# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 SaveConsoleTitleAndIcon();
2611 /*
2612 * Set both the small and big icons of the console window to Vim's icon.
2613 * Note that Vim presently only has one size of icon (32x32), but it
2614 * automatically gets scaled down to 16x16 when setting the small icon.
2615 */
2616 if (g_fCanChangeIcon)
2617 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 ui_get_shellsize();
2621
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002622# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 fdDump = fopen("dump", "wt");
2624
2625 if (fdDump)
2626 {
2627 time_t t;
2628
2629 time(&t);
2630 fputs(ctime(&t), fdDump);
2631 fflush(fdDump);
2632 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 g_fWindInitCalled = TRUE;
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002639# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002640 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002641# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002642
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002643 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002644 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645}
2646
2647/*
2648 * non-GUI version of mch_exit().
2649 * Shut down and exit with status `r'
2650 * Careful: mch_exit() may be called before mch_init()!
2651 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002652 static void
2653mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002655 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002657 vtp_exit();
2658
Bram Moolenaar955f1982017-02-05 15:10:51 +01002659 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (g_fWindInitCalled)
2661 settmode(TMODE_COOK);
2662
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 if (g_fWindInitCalled)
2666 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002667# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002668 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /*
2670 * Restore both the small and big icons of the console window to
2671 * what they were at startup. Don't do this when the window is
2672 * closed, Vim would hang here.
2673 */
2674 if (g_fCanChangeIcon && !g_fForceExit)
2675 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002678# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (fdDump)
2680 {
2681 time_t t;
2682
2683 time(&t);
2684 fputs(ctime(&t), fdDump);
2685 fclose(fdDump);
2686 }
2687 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690
2691 SetConsoleCursorInfo(g_hConOut, &g_cci);
2692 SetConsoleMode(g_hConIn, g_cmodein);
2693 SetConsoleMode(g_hConOut, g_cmodeout);
2694
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002695# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 exit(r);
2700}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002701#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002703 void
2704mch_init(void)
2705{
2706#ifdef VIMDLL
2707 if (gui.starting)
2708 mch_init_g();
2709 else
2710 mch_init_c();
2711#elif defined(FEAT_GUI_MSWIN)
2712 mch_init_g();
2713#else
2714 mch_init_c();
2715#endif
2716}
2717
2718 void
2719mch_exit(int r)
2720{
2721#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002722 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002723 mch_exit_g(r);
2724 else
2725 mch_exit_c(r);
2726#elif defined(FEAT_GUI_MSWIN)
2727 mch_exit_g(r);
2728#else
2729 mch_exit_c(r);
2730#endif
2731}
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733/*
2734 * Do we have an interactive window?
2735 */
2736 int
2737mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002738 int argc UNUSED,
2739 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 get_exe_name();
2742
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002743#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002744 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002746# ifdef VIMDLL
2747 if (gui.in_use)
2748 return OK;
2749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (isatty(1))
2751 return OK;
2752 return FAIL;
2753#endif
2754}
2755
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002756/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002757 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * When "len" is > 0, also expand short to long filenames.
2759 */
2760 void
2761fname_case(
2762 char_u *name,
2763 int len)
2764{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002765 int flen;
2766 WCHAR *p;
2767 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002769 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002770 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 return;
2772
2773 slash_adjust(name);
2774
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002775 p = enc_to_utf16(name, NULL);
2776 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002777 return;
2778
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002779 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002781 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002783 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002785 if (len > 0 || flen >= (int)STRLEN(q))
2786 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2787 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002790 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791}
2792
2793
2794/*
2795 * Insert user name in s[len].
2796 */
2797 int
2798mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002799 char_u *s,
2800 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002803 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002805 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002806 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002807 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002808
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002809 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002810 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002811 vim_strncpy(s, p, len - 1);
2812 vim_free(p);
2813 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002814 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 s[0] = NUL;
2817 return FAIL;
2818}
2819
2820
2821/*
2822 * Insert host name in s[len].
2823 */
2824 void
2825mch_get_host_name(
2826 char_u *s,
2827 int len)
2828{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002829 WCHAR wszHostName[256 + 1];
2830 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002832 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002833 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002834 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002835
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002836 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002837 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002838 vim_strncpy(s, p, len - 1);
2839 vim_free(p);
2840 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002841 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843}
2844
2845
2846/*
2847 * return process ID
2848 */
2849 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002850mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 return (long)GetCurrentProcessId();
2853}
2854
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002855/*
2856 * return TRUE if process "pid" is still running
2857 */
2858 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002859mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002860{
2861 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2862 DWORD status = 0;
2863 int ret = FALSE;
2864
2865 if (hProcess == NULL)
2866 return FALSE; // might not have access
2867 if (GetExitCodeProcess(hProcess, &status) )
2868 ret = status == STILL_ACTIVE;
2869 CloseHandle(hProcess);
2870 return ret;
2871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873/*
2874 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2875 * Return OK for success, FAIL for failure.
2876 */
2877 int
2878mch_dirname(
2879 char_u *buf,
2880 int len)
2881{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002882 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 /*
2885 * Originally this was:
2886 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2887 * But the Win32s known bug list says that getcwd() doesn't work
2888 * so use the Win32 system call instead. <Negri>
2889 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002890 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002892 WCHAR wcbuf[_MAX_PATH + 1];
2893 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002895 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002897 p = utf16_to_enc(wcbuf, NULL);
2898 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002899 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002900 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002902 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002905 if (p == NULL)
2906 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002907
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002908 if (p != NULL)
2909 {
2910 vim_strncpy(buf, p, len - 1);
2911 vim_free(p);
2912 return OK;
2913 }
2914 }
2915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916}
2917
2918/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002919 * Get file permissions for "name".
2920 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002923mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002925 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002926 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002928 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002929 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
2932
2933/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002934 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002935 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002936 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002939mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002941 long n;
2942 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002943
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002944 p = enc_to_utf16(name, NULL);
2945 if (p == NULL)
2946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002948 n = _wchmod(p, perm);
2949 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002950 if (n == -1)
2951 return FAIL;
2952
2953 win32_set_archive(name);
2954
2955 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
2958/*
2959 * Set hidden flag for "name".
2960 */
2961 void
2962mch_hide(char_u *name)
2963{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002964 int attrs = win32_getattrs(name);
2965 if (attrs == -1)
2966 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002968 attrs |= FILE_ATTRIBUTE_HIDDEN;
2969 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970}
2971
2972/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002973 * Return TRUE if file "name" exists and is hidden.
2974 */
2975 int
2976mch_ishidden(char_u *name)
2977{
2978 int f = win32_getattrs(name);
2979
2980 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002981 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002982
2983 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
2984}
2985
2986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 * return TRUE if "name" is a directory
2988 * return FALSE if "name" is not a directory or upon error
2989 */
2990 int
2991mch_isdir(char_u *name)
2992{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002993 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002996 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
2999}
3000
3001/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003002 * return TRUE if "name" is a directory, NOT a symlink to a directory
3003 * return FALSE if "name" is not a directory
3004 * return FALSE for error
3005 */
3006 int
3007mch_isrealdir(char_u *name)
3008{
3009 return mch_isdir(name) && !mch_is_symbolic_link(name);
3010}
3011
3012/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003013 * Create directory "name".
3014 * Return 0 on success, -1 on error.
3015 */
3016 int
3017mch_mkdir(char_u *name)
3018{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003019 WCHAR *p;
3020 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003021
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003022 p = enc_to_utf16(name, NULL);
3023 if (p == NULL)
3024 return -1;
3025 retval = _wmkdir(p);
3026 vim_free(p);
3027 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003028}
3029
3030/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003031 * Delete directory "name".
3032 * Return 0 on success, -1 on error.
3033 */
3034 int
3035mch_rmdir(char_u *name)
3036{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003037 WCHAR *p;
3038 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003039
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003040 p = enc_to_utf16(name, NULL);
3041 if (p == NULL)
3042 return -1;
3043 retval = _wrmdir(p);
3044 vim_free(p);
3045 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003046}
3047
3048/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003049 * Return TRUE if file "fname" has more than one link.
3050 */
3051 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003052mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003053{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003054 BY_HANDLE_FILE_INFORMATION info;
3055
3056 return win32_fileinfo(fname, &info) == FILEINFO_OK
3057 && info.nNumberOfLinks > 1;
3058}
3059
3060/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003061 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003062 */
3063 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003064mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003065{
3066 HANDLE hFind;
3067 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003068 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003069 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003070 WIN32_FIND_DATAW findDataW;
3071
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003072 wn = enc_to_utf16(name, NULL);
3073 if (wn == NULL)
3074 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003075
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003076 hFind = FindFirstFileW(wn, &findDataW);
3077 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003078 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003079 {
3080 fileFlags = findDataW.dwFileAttributes;
3081 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003082 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003083 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003084
3085 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003086 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3087 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003088 res = TRUE;
3089
3090 return res;
3091}
3092
3093/*
3094 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3095 * link.
3096 */
3097 int
3098mch_is_linked(char_u *fname)
3099{
3100 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3101 return TRUE;
3102 return FALSE;
3103}
3104
3105/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003106 * Get the by-handle-file-information for "fname".
3107 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003108 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003109 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3110 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3111 */
3112 int
3113win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3114{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003115 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003116 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003117 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003118
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003119 wn = enc_to_utf16(fname, NULL);
3120 if (wn == NULL)
3121 return FILEINFO_ENC_FAIL;
3122
3123 hFile = CreateFileW(wn, // file name
3124 GENERIC_READ, // access mode
3125 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3126 NULL, // security descriptor
3127 OPEN_EXISTING, // creation disposition
3128 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3129 NULL); // handle to template file
3130 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003131
3132 if (hFile != INVALID_HANDLE_VALUE)
3133 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003134 if (GetFileInformationByHandle(hFile, info) != 0)
3135 res = FILEINFO_OK;
3136 else
3137 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003138 CloseHandle(hFile);
3139 }
3140
Bram Moolenaar03f48552006-02-28 23:52:23 +00003141 return res;
3142}
3143
3144/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003145 * get file attributes for `name'
3146 * -1 : error
3147 * else FILE_ATTRIBUTE_* defined in winnt.h
3148 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003149 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003150win32_getattrs(char_u *name)
3151{
3152 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003153 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003154
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003155 p = enc_to_utf16(name, NULL);
3156 if (p == NULL)
3157 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003158
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003159 attr = GetFileAttributesW(p);
3160 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003161
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003162 return attr;
3163}
3164
3165/*
3166 * set file attributes for `name' to `attrs'
3167 *
3168 * return -1 for failure, 0 otherwise
3169 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003170 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003171win32_setattrs(char_u *name, int attrs)
3172{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003173 int res;
3174 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003175
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003176 p = enc_to_utf16(name, NULL);
3177 if (p == NULL)
3178 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003179
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003180 res = SetFileAttributesW(p, attrs);
3181 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003182
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003183 return res ? 0 : -1;
3184}
3185
3186/*
3187 * Set archive flag for "name".
3188 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003189 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003190win32_set_archive(char_u *name)
3191{
3192 int attrs = win32_getattrs(name);
3193 if (attrs == -1)
3194 return -1;
3195
3196 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3197 return win32_setattrs(name, attrs);
3198}
3199
3200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 * Return TRUE if file or directory "name" is writable (not readonly).
3202 * Strange semantics of Win32: a readonly directory is writable, but you can't
3203 * delete a file. Let's say this means it is writable.
3204 */
3205 int
3206mch_writable(char_u *name)
3207{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003208 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003210 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3211 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003215 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003216 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003217 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3218 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
3220 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003221mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar86621892019-03-30 21:51:28 +01003223 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3224 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3225 // UTF-8.
3226 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003227 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003228 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003229
Bram Moolenaar86621892019-03-30 21:51:28 +01003230 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003231 return FALSE;
3232
Bram Moolenaar86621892019-03-30 21:51:28 +01003233 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003234 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003235 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003236 return TRUE;
3237
3238 /*
3239 * Loop over all extensions in $PATHEXT.
3240 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003241 p = mch_getenv("PATHEXT");
3242 if (p == NULL)
3243 p = (char_u *)".com;.exe;.bat;.cmd";
3244 saved = vim_strsave(p);
3245 if (saved == NULL)
3246 return FALSE;
3247 p = saved;
3248 while (*p)
3249 {
3250 char_u *tmp = vim_strchr(p, ';');
3251
3252 if (tmp != NULL)
3253 *tmp = NUL;
3254 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3255 && executable_exists((char *)name, path, use_path))
3256 {
3257 vim_free(saved);
3258 return TRUE;
3259 }
3260 if (tmp == NULL)
3261 break;
3262 p = tmp + 1;
3263 }
3264 vim_free(saved);
3265
Bram Moolenaar86621892019-03-30 21:51:28 +01003266 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003267 p = mch_getenv("PATHEXT");
3268 if (p == NULL)
3269 p = (char_u *)".com;.exe;.bat;.cmd";
3270 while (*p)
3271 {
3272 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3273 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003274 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003275 buf[len] = NUL;
3276 ++p;
3277 if (*p)
3278 ++p;
3279 }
3280 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003281 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003282 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003283 return TRUE;
3284 }
3285 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288/*
3289 * Check what "name" is:
3290 * NODE_NORMAL: file or directory (or doesn't exist)
3291 * NODE_WRITABLE: writable device, socket, fifo, etc.
3292 * NODE_OTHER: non-writable things
3293 */
3294 int
3295mch_nodetype(char_u *name)
3296{
3297 HANDLE hFile;
3298 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003299 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar0f873732019-12-05 20:28:46 +01003301 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3302 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3303 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003304 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3305 return NODE_WRITABLE;
3306
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003307 wn = enc_to_utf16(name, NULL);
3308 if (wn == NULL)
3309 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003310
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003311 hFile = CreateFileW(wn, // file name
3312 GENERIC_WRITE, // access mode
3313 0, // share mode
3314 NULL, // security descriptor
3315 OPEN_EXISTING, // creation disposition
3316 0, // file attributes
3317 NULL); // handle to template file
3318 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (hFile == INVALID_HANDLE_VALUE)
3320 return NODE_NORMAL;
3321
3322 type = GetFileType(hFile);
3323 CloseHandle(hFile);
3324 if (type == FILE_TYPE_CHAR)
3325 return NODE_WRITABLE;
3326 if (type == FILE_TYPE_DISK)
3327 return NODE_NORMAL;
3328 return NODE_OTHER;
3329}
3330
3331#ifdef HAVE_ACL
3332struct my_acl
3333{
3334 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3335 PSID pSidOwner;
3336 PSID pSidGroup;
3337 PACL pDacl;
3338 PACL pSacl;
3339};
3340#endif
3341
3342/*
3343 * Return a pointer to the ACL of file "fname" in allocated memory.
3344 * Return NULL if the ACL is not available for whatever reason.
3345 */
3346 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003347mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349#ifndef HAVE_ACL
3350 return (vim_acl_T)NULL;
3351#else
3352 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003353 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003355 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003356 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003358 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003359
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003360 wn = enc_to_utf16(fname, NULL);
3361 if (wn == NULL)
3362 return NULL;
3363
3364 // Try to retrieve the entire security descriptor.
3365 err = GetNamedSecurityInfoW(
3366 wn, // Abstract filename
3367 SE_FILE_OBJECT, // File Object
3368 OWNER_SECURITY_INFORMATION |
3369 GROUP_SECURITY_INFORMATION |
3370 DACL_SECURITY_INFORMATION |
3371 SACL_SECURITY_INFORMATION,
3372 &p->pSidOwner, // Ownership information.
3373 &p->pSidGroup, // Group membership.
3374 &p->pDacl, // Discretionary information.
3375 &p->pSacl, // For auditing purposes.
3376 &p->pSecurityDescriptor);
3377 if (err == ERROR_ACCESS_DENIED ||
3378 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003379 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003380 // Retrieve only DACL.
3381 (void)GetNamedSecurityInfoW(
3382 wn,
3383 SE_FILE_OBJECT,
3384 DACL_SECURITY_INFORMATION,
3385 NULL,
3386 NULL,
3387 &p->pDacl,
3388 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003389 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003390 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003391 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003392 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003393 mch_free_acl((vim_acl_T)p);
3394 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003396 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398
3399 return (vim_acl_T)p;
3400#endif
3401}
3402
Bram Moolenaar27515922013-06-29 15:36:26 +02003403#ifdef HAVE_ACL
3404/*
3405 * Check if "acl" contains inherited ACE.
3406 */
3407 static BOOL
3408is_acl_inherited(PACL acl)
3409{
3410 DWORD i;
3411 ACL_SIZE_INFORMATION acl_info;
3412 PACCESS_ALLOWED_ACE ace;
3413
3414 acl_info.AceCount = 0;
3415 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3416 for (i = 0; i < acl_info.AceCount; i++)
3417 {
3418 GetAce(acl, i, (LPVOID *)&ace);
3419 if (ace->Header.AceFlags & INHERITED_ACE)
3420 return TRUE;
3421 }
3422 return FALSE;
3423}
3424#endif
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426/*
3427 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3428 * Errors are ignored.
3429 * This must only be called with "acl" equal to what mch_get_acl() returned.
3430 */
3431 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003432mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434#ifdef HAVE_ACL
3435 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003436 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003437 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003439 if (p == NULL)
3440 return;
3441
3442 wn = enc_to_utf16(fname, NULL);
3443 if (wn == NULL)
3444 return;
3445
3446 // Set security flags
3447 if (p->pSidOwner)
3448 sec_info |= OWNER_SECURITY_INFORMATION;
3449 if (p->pSidGroup)
3450 sec_info |= GROUP_SECURITY_INFORMATION;
3451 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003452 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003453 sec_info |= DACL_SECURITY_INFORMATION;
3454 // Do not inherit its parent's DACL.
3455 // If the DACL is inherited, Cygwin permissions would be changed.
3456 if (!is_acl_inherited(p->pDacl))
3457 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003458 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003459 if (p->pSacl)
3460 sec_info |= SACL_SECURITY_INFORMATION;
3461
3462 (void)SetNamedSecurityInfoW(
3463 wn, // Abstract filename
3464 SE_FILE_OBJECT, // File Object
3465 sec_info,
3466 p->pSidOwner, // Ownership information.
3467 p->pSidGroup, // Group membership.
3468 p->pDacl, // Discretionary information.
3469 p->pSacl // For auditing purposes.
3470 );
3471 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#endif
3473}
3474
3475 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003476mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478#ifdef HAVE_ACL
3479 struct my_acl *p = (struct my_acl *)acl;
3480
3481 if (p != NULL)
3482 {
3483 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3484 vim_free(p);
3485 }
3486#endif
3487}
3488
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003489#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491/*
3492 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3493 */
3494 static BOOL WINAPI
3495handler_routine(
3496 DWORD dwCtrlType)
3497{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003498 INPUT_RECORD ir;
3499 DWORD out;
3500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 switch (dwCtrlType)
3502 {
3503 case CTRL_C_EVENT:
3504 if (ctrl_c_interrupts)
3505 g_fCtrlCPressed = TRUE;
3506 return TRUE;
3507
3508 case CTRL_BREAK_EVENT:
3509 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003510 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003511 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003512 ir.EventType = KEY_EVENT;
3513 ir.Event.KeyEvent.bKeyDown = TRUE;
3514 ir.Event.KeyEvent.wRepeatCount = 1;
3515 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3516 ir.Event.KeyEvent.wVirtualScanCode = 0;
3517 ir.Event.KeyEvent.dwControlKeyState = 0;
3518 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3519 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return TRUE;
3521
Bram Moolenaar0f873732019-12-05 20:28:46 +01003522 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 case CTRL_CLOSE_EVENT:
3524 case CTRL_LOGOFF_EVENT:
3525 case CTRL_SHUTDOWN_EVENT:
3526 windgoto((int)Rows - 1, 0);
3527 g_fForceExit = TRUE;
3528
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003529 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 (dwCtrlType == CTRL_CLOSE_EVENT
3531 ? _("close")
3532 : dwCtrlType == CTRL_LOGOFF_EVENT
3533 ? _("logoff")
3534 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003535# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar0f873732019-12-05 20:28:46 +01003539 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar0f873732019-12-05 20:28:46 +01003541 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 default:
3544 return FALSE;
3545 }
3546}
3547
3548
3549/*
3550 * set the tty in (raw) ? "raw" : "cooked" mode
3551 */
3552 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003553mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 DWORD cmodein;
3556 DWORD cmodeout;
3557 BOOL bEnableHandler;
3558
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003559# ifdef VIMDLL
3560 if (gui.in_use)
3561 return;
3562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 GetConsoleMode(g_hConIn, &cmodein);
3564 GetConsoleMode(g_hConOut, &cmodeout);
3565 if (tmode == TMODE_RAW)
3566 {
3567 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3568 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (g_fMouseActive)
3570 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003571 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003572# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003573 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3574 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003575 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003576# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003577 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003578# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003579 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 bEnableHandler = TRUE;
3581 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003582 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3585 ENABLE_ECHO_INPUT);
3586 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3587 bEnableHandler = FALSE;
3588 }
3589 SetConsoleMode(g_hConIn, cmodein);
3590 SetConsoleMode(g_hConOut, cmodeout);
3591 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3592
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003593# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (fdDump)
3595 {
3596 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3597 tmode == TMODE_RAW ? "raw" :
3598 tmode == TMODE_COOK ? "cooked" : "normal",
3599 cmodein, cmodeout);
3600 fflush(fdDump);
3601 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003602# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603}
3604
3605
3606/*
3607 * Get the size of the current window in `Rows' and `Columns'
3608 * Return OK when size could be determined, FAIL otherwise.
3609 */
3610 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003611mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
3613 CONSOLE_SCREEN_BUFFER_INFO csbi;
3614
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003615# ifdef VIMDLL
3616 if (gui.in_use)
3617 return OK;
3618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3620 {
3621 /*
3622 * For some reason, we are trying to get the screen dimensions
3623 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3624 * variables are really intended to mean the size of Vim screen
3625 * while in termcap mode.
3626 */
3627 Rows = g_cbTermcap.Info.dwSize.Y;
3628 Columns = g_cbTermcap.Info.dwSize.X;
3629 }
3630 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3631 {
3632 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3633 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3634 }
3635 else
3636 {
3637 Rows = 25;
3638 Columns = 80;
3639 }
3640 return OK;
3641}
3642
3643/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003644 * Resize console buffer to 'COORD'
3645 */
3646 static void
3647ResizeConBuf(
3648 HANDLE hConsole,
3649 COORD coordScreen)
3650{
3651 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3652 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003653# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003654 if (fdDump)
3655 {
3656 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3657 GetLastError());
3658 fflush(fdDump);
3659 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003660# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003661 }
3662}
3663
3664/*
3665 * Resize console window size to 'srWindowRect'
3666 */
3667 static void
3668ResizeWindow(
3669 HANDLE hConsole,
3670 SMALL_RECT srWindowRect)
3671{
3672 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3673 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003674# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003675 if (fdDump)
3676 {
3677 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3678 GetLastError());
3679 fflush(fdDump);
3680 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003681# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003682 }
3683}
3684
3685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 * Set a console window to `xSize' * `ySize'
3687 */
3688 static void
3689ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003690 HANDLE hConsole,
3691 int xSize,
3692 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3695 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003697 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003698 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003700# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 if (fdDump)
3702 {
3703 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3704 fflush(fdDump);
3705 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
Bram Moolenaar0f873732019-12-05 20:28:46 +01003708 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 coordScreen = GetLargestConsoleWindowSize(hConsole);
3710
Bram Moolenaar0f873732019-12-05 20:28:46 +01003711 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3713 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3714 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3715
3716 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3717 {
3718 int sx, sy;
3719
3720 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3721 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3722 if (sy < ySize || sx < xSize)
3723 {
3724 /*
3725 * Increasing number of lines/columns, do buffer first.
3726 * Use the maximal size in x and y direction.
3727 */
3728 if (sy < ySize)
3729 coordScreen.Y = ySize;
3730 else
3731 coordScreen.Y = sy;
3732 if (sx < xSize)
3733 coordScreen.X = xSize;
3734 else
3735 coordScreen.X = sx;
3736 SetConsoleScreenBufferSize(hConsole, coordScreen);
3737 }
3738 }
3739
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003740 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 coordScreen.X = xSize;
3742 coordScreen.Y = ySize;
3743
Bram Moolenaar2551c032018-08-23 22:38:31 +02003744 // In the new console call API, only the first time in reverse order
3745 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003747 ResizeWindow(hConsole, srWindowRect);
3748 ResizeConBuf(hConsole, coordScreen);
3749 }
3750 else
3751 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003752 // Workaround for a Windows 10 bug
3753 cursor.X = srWindowRect.Left;
3754 cursor.Y = srWindowRect.Top;
3755 SetConsoleCursorPosition(hConsole, cursor);
3756
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003757 ResizeConBuf(hConsole, coordScreen);
3758 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003759 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761}
3762
3763
3764/*
3765 * Set the console window to `Rows' * `Columns'
3766 */
3767 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003768mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 COORD coordScreen;
3771
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003772# ifdef VIMDLL
3773 if (gui.in_use)
3774 return;
3775# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003776 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (suppress_winsize != 0)
3778 {
3779 suppress_winsize = 2;
3780 return;
3781 }
3782
3783 if (term_console)
3784 {
3785 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3786
Bram Moolenaar0f873732019-12-05 20:28:46 +01003787 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (Rows > coordScreen.Y)
3789 Rows = coordScreen.Y;
3790 if (Columns > coordScreen.X)
3791 Columns = coordScreen.X;
3792
3793 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3794 }
3795}
3796
3797/*
3798 * Rows and/or Columns has changed.
3799 */
3800 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003801mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003803# ifdef VIMDLL
3804 if (gui.in_use)
3805 return;
3806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3808}
3809
3810
3811/*
3812 * Called when started up, to set the winsize that was delayed.
3813 */
3814 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003815mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 if (suppress_winsize == 2)
3818 {
3819 suppress_winsize = 0;
3820 mch_set_shellsize();
3821 shell_resized();
3822 }
3823 suppress_winsize = 0;
3824}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003825#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003827 static BOOL
3828vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003829 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003830 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003831 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003832 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003833 PROCESS_INFORMATION *pi,
3834 LPVOID *env,
3835 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003836{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003837 BOOL ret = FALSE;
3838 WCHAR *wcmd, *wcwd = NULL;
3839
3840 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3841 if (wcmd == NULL)
3842 return FALSE;
3843 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003844 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003845 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3846 if (wcwd == NULL)
3847 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003848 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003849
3850 ret = CreateProcessW(
3851 NULL, // Executable name
3852 wcmd, // Command to execute
3853 NULL, // Process security attributes
3854 NULL, // Thread security attributes
3855 inherit_handles, // Inherit handles
3856 flags, // Creation flags
3857 env, // Environment
3858 wcwd, // Current directory
3859 (LPSTARTUPINFOW)si, // Startup information
3860 pi); // Process information
3861theend:
3862 vim_free(wcmd);
3863 vim_free(wcwd);
3864 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003868 static HINSTANCE
3869vim_shell_execute(
3870 char *cmd,
3871 INT n_show_cmd)
3872{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003873 HINSTANCE ret;
3874 WCHAR *wcmd;
3875
3876 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3877 if (wcmd == NULL)
3878 return (HINSTANCE) 0;
3879
3880 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3881 vim_free(wcmd);
3882 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003883}
3884
3885
Bram Moolenaar4f974752019-02-17 17:44:42 +01003886#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888/*
3889 * Specialised version of system() for Win32 GUI mode.
3890 * This version proceeds as follows:
3891 * 1. Create a console window for use by the subprocess
3892 * 2. Run the subprocess (it gets the allocated console by default)
3893 * 3. Wait for the subprocess to terminate and get its exit code
3894 * 4. Prompt the user to press a key to close the console window
3895 */
3896 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003897mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
3899 STARTUPINFO si;
3900 PROCESS_INFORMATION pi;
3901 DWORD ret = 0;
3902 HWND hwnd = GetFocus();
3903
3904 si.cb = sizeof(si);
3905 si.lpReserved = NULL;
3906 si.lpDesktop = NULL;
3907 si.lpTitle = NULL;
3908 si.dwFlags = STARTF_USESHOWWINDOW;
3909 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003910 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003911 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003913 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003914 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 else
3916 si.wShowWindow = SW_SHOWNORMAL;
3917 si.cbReserved2 = 0;
3918 si.lpReserved2 = NULL;
3919
Bram Moolenaar0f873732019-12-05 20:28:46 +01003920 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003921 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003922 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3923 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar0f873732019-12-05 20:28:46 +01003925 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003927# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int delay = 1;
3929
Bram Moolenaar0f873732019-12-05 20:28:46 +01003930 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 for (;;)
3932 {
3933 MSG msg;
3934
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003935 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003938 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003939 delay = 1;
3940 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3943 break;
3944
Bram Moolenaar0f873732019-12-05 20:28:46 +01003945 // We start waiting for a very short time and then increase it, so
3946 // that we respond quickly when the process is quick, and don't
3947 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (delay < 50)
3949 delay += 10;
3950 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003951# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar0f873732019-12-05 20:28:46 +01003955 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 GetExitCodeProcess(pi.hProcess, &ret);
3957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar0f873732019-12-05 20:28:46 +01003959 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 CloseHandle(pi.hThread);
3961 CloseHandle(pi.hProcess);
3962
Bram Moolenaar0f873732019-12-05 20:28:46 +01003963 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3965
3966 return ret;
3967}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003968
3969/*
3970 * Thread launched by the gui to send the current buffer data to the
3971 * process. This way avoid to hang up vim totally if the children
3972 * process take a long time to process the lines.
3973 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02003974 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003975sub_process_writer(LPVOID param)
3976{
3977 HANDLE g_hChildStd_IN_Wr = param;
3978 linenr_T lnum = curbuf->b_op_start.lnum;
3979 DWORD len = 0;
3980 DWORD l;
3981 char_u *lp = ml_get(lnum);
3982 char_u *s;
3983 int written = 0;
3984
3985 for (;;)
3986 {
3987 l = (DWORD)STRLEN(lp + written);
3988 if (l == 0)
3989 len = 0;
3990 else if (lp[written] == NL)
3991 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003992 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003993 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
3994 }
3995 else
3996 {
3997 s = vim_strchr(lp + written, NL);
3998 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
3999 s == NULL ? l : (DWORD)(s - (lp + written)),
4000 &len, NULL);
4001 }
4002 if (len == (int)l)
4003 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 // Finished a line, add a NL, unless this line should not have
4005 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004006 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004007 || (!curbuf->b_p_bin
4008 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004009 || (lnum != curbuf->b_no_eol_lnum
4010 && (lnum != curbuf->b_ml.ml_line_count
4011 || curbuf->b_p_eol)))
4012 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004013 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4014 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004015 }
4016
4017 ++lnum;
4018 if (lnum > curbuf->b_op_end.lnum)
4019 break;
4020
4021 lp = ml_get(lnum);
4022 written = 0;
4023 }
4024 else if (len > 0)
4025 written += len;
4026 }
4027
Bram Moolenaar0f873732019-12-05 20:28:46 +01004028 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004029 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004030 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004031}
4032
4033
Bram Moolenaar0f873732019-12-05 20:28:46 +01004034# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004035
4036/*
4037 * This function read from the children's stdout and write the
4038 * data on screen or in the buffer accordingly.
4039 */
4040 static void
4041dump_pipe(int options,
4042 HANDLE g_hChildStd_OUT_Rd,
4043 garray_T *ga,
4044 char_u buffer[],
4045 DWORD *buffer_off)
4046{
4047 DWORD availableBytes = 0;
4048 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004049 int ret;
4050 DWORD len;
4051 DWORD toRead;
4052 int repeatCount;
4053
Bram Moolenaar0f873732019-12-05 20:28:46 +01004054 // we query the pipe to see if there is any data to read
4055 // to avoid to perform a blocking read
4056 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4057 NULL, // optional buffer
4058 0, // buffer size
4059 NULL, // number of read bytes
4060 &availableBytes, // available bytes total
4061 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004062
4063 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004064 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004065 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004066 {
4067 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004068 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004069 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004070 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004071
Bram Moolenaar0f873732019-12-05 20:28:46 +01004072 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004073 if (len == 0)
4074 break;
4075
4076 availableBytes -= len;
4077
4078 if (options & SHELL_READ)
4079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004080 // Do NUL -> NL translation, append NL separated
4081 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004082 for (i = 0; i < len; ++i)
4083 {
4084 if (buffer[i] == NL)
4085 append_ga_line(ga);
4086 else if (buffer[i] == NUL)
4087 ga_append(ga, NL);
4088 else
4089 ga_append(ga, buffer[i]);
4090 }
4091 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004092 else if (has_mbyte)
4093 {
4094 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004095 int c;
4096 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004097
4098 len += *buffer_off;
4099 buffer[len] = NUL;
4100
Bram Moolenaar0f873732019-12-05 20:28:46 +01004101 // Check if the last character in buffer[] is
4102 // incomplete, keep these bytes for the next
4103 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004104 for (p = buffer; p < buffer + len; p += l)
4105 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004106 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004107 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004108 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004109 else if (MB_BYTE2LEN(*p) != l)
4110 break;
4111 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004113 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004115 if (len >= 12)
4116 ++p;
4117 else
4118 {
4119 *buffer_off = len;
4120 return;
4121 }
4122 }
4123 c = *p;
4124 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004125 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004126 if (p < buffer + len)
4127 {
4128 *p = c;
4129 *buffer_off = (DWORD)((buffer + len) - p);
4130 mch_memmove(buffer, p, *buffer_off);
4131 return;
4132 }
4133 *buffer_off = 0;
4134 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004135 else
4136 {
4137 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004138 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004139 }
4140
4141 windgoto(msg_row, msg_col);
4142 cursor_on();
4143 out_flush();
4144 }
4145}
4146
4147/*
4148 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4149 * for communication and doesn't open any new window.
4150 */
4151 static int
4152mch_system_piped(char *cmd, int options)
4153{
4154 STARTUPINFO si;
4155 PROCESS_INFORMATION pi;
4156 DWORD ret = 0;
4157
4158 HANDLE g_hChildStd_IN_Rd = NULL;
4159 HANDLE g_hChildStd_IN_Wr = NULL;
4160 HANDLE g_hChildStd_OUT_Rd = NULL;
4161 HANDLE g_hChildStd_OUT_Wr = NULL;
4162
Bram Moolenaar0f873732019-12-05 20:28:46 +01004163 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004164 DWORD len;
4165
Bram Moolenaar0f873732019-12-05 20:28:46 +01004166 // buffer used to receive keys
4167 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4168 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004169
4170 DWORD i;
4171 int c;
4172 int noread_cnt = 0;
4173 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004174 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004175 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004176 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004177
4178 SECURITY_ATTRIBUTES saAttr;
4179
Bram Moolenaar0f873732019-12-05 20:28:46 +01004180 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004181 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4182 saAttr.bInheritHandle = TRUE;
4183 saAttr.lpSecurityDescriptor = NULL;
4184
4185 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004186 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004187 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004188 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004189 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004190 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004191 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004192 {
4193 CloseHandle(g_hChildStd_IN_Rd);
4194 CloseHandle(g_hChildStd_IN_Wr);
4195 CloseHandle(g_hChildStd_OUT_Rd);
4196 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004197 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004198 }
4199
4200 si.cb = sizeof(si);
4201 si.lpReserved = NULL;
4202 si.lpDesktop = NULL;
4203 si.lpTitle = NULL;
4204 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4205
Bram Moolenaar0f873732019-12-05 20:28:46 +01004206 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004207 si.hStdError = g_hChildStd_OUT_Wr;
4208 si.hStdOutput = g_hChildStd_OUT_Wr;
4209 si.hStdInput = g_hChildStd_IN_Rd;
4210 si.wShowWindow = SW_HIDE;
4211 si.cbReserved2 = 0;
4212 si.lpReserved2 = NULL;
4213
4214 if (options & SHELL_READ)
4215 ga_init2(&ga, 1, BUFLEN);
4216
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004217 if (cmd != NULL)
4218 {
4219 p = (char *)vim_strsave((char_u *)cmd);
4220 if (p != NULL)
4221 unescape_shellxquote((char_u *)p, p_sxe);
4222 else
4223 p = cmd;
4224 }
4225
Bram Moolenaar0f873732019-12-05 20:28:46 +01004226 // Now, run the command.
4227 // About "Inherit handles" being TRUE: this command can be litigious,
4228 // handle inheritance was deactivated for pending temp file, but, if we
4229 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004230 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4231 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004232
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004233 if (p != cmd)
4234 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004235
Bram Moolenaar0f873732019-12-05 20:28:46 +01004236 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004237 CloseHandle(g_hChildStd_IN_Rd);
4238 CloseHandle(g_hChildStd_OUT_Wr);
4239
4240 if (options & SHELL_WRITE)
4241 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004242 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004243 _beginthreadex(NULL, // security attributes
4244 0, // default stack size
4245 sub_process_writer, // function to be executed
4246 g_hChildStd_IN_Wr, // parameter
4247 0, // creation flag, start immediately
4248 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004249 CloseHandle(thread);
4250 g_hChildStd_IN_Wr = NULL;
4251 }
4252
Bram Moolenaar0f873732019-12-05 20:28:46 +01004253 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004254 for (;;)
4255 {
4256 MSG msg;
4257
Bram Moolenaar175d0702013-12-11 18:36:33 +01004258 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004259 {
4260 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004261 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004262 }
4263
Bram Moolenaar0f873732019-12-05 20:28:46 +01004264 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004265 if ((options & (SHELL_READ|SHELL_WRITE))
4266# ifdef FEAT_GUI
4267 || gui.in_use
4268# endif
4269 )
4270 {
4271 len = 0;
4272 if (!(options & SHELL_EXPAND)
4273 && ((options &
4274 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4275 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4276# ifdef FEAT_GUI
4277 || gui.in_use
4278# endif
4279 )
4280 && (ta_len > 0 || noread_cnt > 4))
4281 {
4282 if (ta_len == 0)
4283 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004284 // Get extra characters when we don't have any. Reset the
4285 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004286 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004287 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4288 }
4289 if (ta_len > 0 || len > 0)
4290 {
4291 /*
4292 * For pipes: Check for CTRL-C: send interrupt signal to
4293 * child. Check for CTRL-D: EOF, close pipe to child.
4294 */
4295 if (len == 1 && cmd != NULL)
4296 {
4297 if (ta_buf[ta_len] == Ctrl_C)
4298 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004299 // Learn what exit code is expected, for
4300 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004301 TerminateProcess(pi.hProcess, 9);
4302 }
4303 if (ta_buf[ta_len] == Ctrl_D)
4304 {
4305 CloseHandle(g_hChildStd_IN_Wr);
4306 g_hChildStd_IN_Wr = NULL;
4307 }
4308 }
4309
Bram Moolenaar0f873732019-12-05 20:28:46 +01004310 // replace K_BS by <BS> and K_DEL by <DEL>
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004311 for (i = ta_len; i < ta_len + len; ++i)
4312 {
4313 if (ta_buf[i] == CSI && len - i > 2)
4314 {
4315 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4316 if (c == K_DEL || c == K_KDEL || c == K_BS)
4317 {
4318 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4319 (size_t)(len - i - 2));
4320 if (c == K_DEL || c == K_KDEL)
4321 ta_buf[i] = DEL;
4322 else
4323 ta_buf[i] = Ctrl_H;
4324 len -= 2;
4325 }
4326 }
4327 else if (ta_buf[i] == '\r')
4328 ta_buf[i] = '\n';
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004329 if (has_mbyte)
4330 i += (*mb_ptr2len_len)(ta_buf + i,
4331 ta_len + len - i) - 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004332 }
4333
4334 /*
4335 * For pipes: echo the typed characters. For a pty this
4336 * does not seem to work.
4337 */
4338 for (i = ta_len; i < ta_len + len; ++i)
4339 {
4340 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4341 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004342 else if (has_mbyte)
4343 {
4344 int l = (*mb_ptr2len)(ta_buf + i);
4345
4346 msg_outtrans_len(ta_buf + i, l);
4347 i += l - 1;
4348 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004349 else
4350 msg_outtrans_len(ta_buf + i, 1);
4351 }
4352 windgoto(msg_row, msg_col);
4353 out_flush();
4354
4355 ta_len += len;
4356
4357 /*
4358 * Write the characters to the child, unless EOF has been
4359 * typed for pipes. Write one character at a time, to
4360 * avoid losing too much typeahead. When writing buffer
4361 * lines, drop the typed characters (only check for
4362 * CTRL-C).
4363 */
4364 if (options & SHELL_WRITE)
4365 ta_len = 0;
4366 else if (g_hChildStd_IN_Wr != NULL)
4367 {
4368 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4369 1, &len, NULL);
4370 // if we are typing in, we want to keep things reactive
4371 delay = 1;
4372 if (len > 0)
4373 {
4374 ta_len -= len;
4375 mch_memmove(ta_buf, ta_buf + len, ta_len);
4376 }
4377 }
4378 }
4379 }
4380 }
4381
4382 if (ta_len)
4383 ui_inchar_undo(ta_buf, ta_len);
4384
4385 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4386 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004387 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004388 break;
4389 }
4390
4391 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004392 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004393
Bram Moolenaar0f873732019-12-05 20:28:46 +01004394 // We start waiting for a very short time and then increase it, so
4395 // that we respond quickly when the process is quick, and don't
4396 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004397 if (delay < 50)
4398 delay += 10;
4399 }
4400
Bram Moolenaar0f873732019-12-05 20:28:46 +01004401 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004402 CloseHandle(g_hChildStd_OUT_Rd);
4403 if (g_hChildStd_IN_Wr != NULL)
4404 CloseHandle(g_hChildStd_IN_Wr);
4405
4406 WaitForSingleObject(pi.hProcess, INFINITE);
4407
Bram Moolenaar0f873732019-12-05 20:28:46 +01004408 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004409 GetExitCodeProcess(pi.hProcess, &ret);
4410
4411 if (options & SHELL_READ)
4412 {
4413 if (ga.ga_len > 0)
4414 {
4415 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004417 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4418 }
4419 else
4420 curbuf->b_no_eol_lnum = 0;
4421 ga_clear(&ga);
4422 }
4423
Bram Moolenaar0f873732019-12-05 20:28:46 +01004424 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004425 CloseHandle(pi.hThread);
4426 CloseHandle(pi.hProcess);
4427
4428 return ret;
4429}
4430
4431 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004432mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004433{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004434 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004435 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004436 return mch_system_piped(cmd, options);
4437 else
4438 return mch_system_classic(cmd, options);
4439}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004442#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004443 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004444mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004445{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004446 int ret;
4447 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004448 char_u *buf;
4449 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004450
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004451 // If the command starts and ends with double quotes, enclose the command
4452 // in parentheses.
4453 len = STRLEN(cmd);
4454 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4455 {
4456 len += 3;
4457 buf = alloc(len);
4458 if (buf == NULL)
4459 return -1;
4460 vim_snprintf((char *)buf, len, "(%s)", cmd);
4461 wcmd = enc_to_utf16(buf, NULL);
4462 free(buf);
4463 }
4464 else
4465 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4466
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004467 if (wcmd == NULL)
4468 return -1;
4469
4470 ret = _wsystem(wcmd);
4471 vim_free(wcmd);
4472 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475#endif
4476
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004477 static int
4478mch_system(char *cmd, int options)
4479{
4480#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004481 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004482 return mch_system_g(cmd, options);
4483 else
4484 return mch_system_c(cmd, options);
4485#elif defined(FEAT_GUI_MSWIN)
4486 return mch_system_g(cmd, options);
4487#else
4488 return mch_system_c(cmd, options);
4489#endif
4490}
4491
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004492#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4493/*
4494 * Use a terminal window to run a shell command in.
4495 */
4496 static int
4497mch_call_shell_terminal(
4498 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004499 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004500{
4501 jobopt_T opt;
4502 char_u *newcmd = NULL;
4503 typval_T argvar[2];
4504 long_u cmdlen;
4505 int retval = -1;
4506 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004507 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004508 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004509 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004510
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004511 if (cmd == NULL)
4512 cmdlen = STRLEN(p_sh) + 1;
4513 else
4514 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004515 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004516 if (newcmd == NULL)
4517 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004518 if (cmd == NULL)
4519 {
4520 STRCPY(newcmd, p_sh);
4521 ch_log(NULL, "starting terminal to run a shell");
4522 }
4523 else
4524 {
4525 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4526 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4527 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004528
4529 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004530
4531 argvar[0].v_type = VAR_STRING;
4532 argvar[0].vval.v_string = newcmd;
4533 argvar[1].v_type = VAR_UNKNOWN;
4534 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004535 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004536 {
4537 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004538 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004539 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004540
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004541 job = term_getjob(buf->b_term);
4542 ++job->jv_refcount;
4543
Bram Moolenaar0f873732019-12-05 20:28:46 +01004544 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004545 aucmd_prepbuf(&aco, buf);
4546
4547 clear_oparg(&oa);
4548 while (term_use_loop())
4549 {
4550 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4551 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 // If terminal_loop() returns OK we got a key that is handled
4553 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004554 if (terminal_loop(TRUE) == OK)
4555 normal_cmd(&oa, TRUE);
4556 }
4557 else
4558 normal_cmd(&oa, TRUE);
4559 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004560 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004561 ch_log(NULL, "system command finished");
4562
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004563 job_unref(job);
4564
Bram Moolenaar0f873732019-12-05 20:28:46 +01004565 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004566 aucmd_restbuf(&aco);
4567
4568 wait_return(TRUE);
4569 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4570
4571 vim_free(newcmd);
4572 return retval;
4573}
4574#endif
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576/*
4577 * Either execute a command by calling the shell or start a new shell
4578 */
4579 int
4580mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004581 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004582 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583{
4584 int x = 0;
4585 int tmode = cur_tmode;
4586#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004587 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004588
Bram Moolenaar0f873732019-12-05 20:28:46 +01004589 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004590 if (GetConsoleTitleW(szShellTitle,
4591 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004592 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004593 if (cmd == NULL)
4594 wcscat(szShellTitle, L" :sh");
4595 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004596 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004597 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004598
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004599 if (wn != NULL)
4600 {
4601 wcscat(szShellTitle, L" - !");
4602 if ((wcslen(szShellTitle) + wcslen(wn) <
4603 sizeof(szShellTitle)/sizeof(WCHAR)))
4604 wcscat(szShellTitle, wn);
4605 SetConsoleTitleW(szShellTitle);
4606 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004607 }
4608 }
4609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#endif
4611
4612 out_flush();
4613
4614#ifdef MCH_WRITE_DUMP
4615 if (fdDump)
4616 {
4617 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4618 fflush(fdDump);
4619 }
4620#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004621#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004622 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004623 if (
4624# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004625 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004626# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004627 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004628 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4629 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004630 char_u *cmdbase = cmd;
4631
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004632 if (cmdbase != NULL)
4633 // Skip a leading quote and (.
4634 while (*cmdbase == '"' || *cmdbase == '(')
4635 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004636
4637 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004638 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4639 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004640 {
4641 // Use a terminal window to run the command in.
4642 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004643# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004644 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004645# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004646 return x;
4647 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004648 }
4649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
4651 /*
4652 * Catch all deadly signals while running the external command, because a
4653 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4654 */
4655 signal(SIGINT, SIG_IGN);
4656#if defined(__GNUC__) && !defined(__MINGW32__)
4657 signal(SIGKILL, SIG_IGN);
4658#else
4659 signal(SIGBREAK, SIG_IGN);
4660#endif
4661 signal(SIGILL, SIG_IGN);
4662 signal(SIGFPE, SIG_IGN);
4663 signal(SIGSEGV, SIG_IGN);
4664 signal(SIGTERM, SIG_IGN);
4665 signal(SIGABRT, SIG_IGN);
4666
4667 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004668 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 if (cmd == NULL)
4671 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004672 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 else
4675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004677 char_u *newcmd = NULL;
4678 char_u *cmdbase = cmd;
4679 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004680
Bram Moolenaar0f873732019-12-05 20:28:46 +01004681 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004682 if (*cmdbase == '"' )
4683 ++cmdbase;
4684 if (*cmdbase == '(')
4685 ++cmdbase;
4686
Bram Moolenaar1c465442017-03-12 20:10:05 +01004687 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004688 {
4689 STARTUPINFO si;
4690 PROCESS_INFORMATION pi;
4691 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004692 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004693 char_u *p;
4694
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004695 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004696 si.cb = sizeof(si);
4697 si.lpReserved = NULL;
4698 si.lpDesktop = NULL;
4699 si.lpTitle = NULL;
4700 si.dwFlags = 0;
4701 si.cbReserved2 = 0;
4702 si.lpReserved2 = NULL;
4703
4704 cmdbase = skipwhite(cmdbase + 5);
4705 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004706 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004707 {
4708 cmdbase = skipwhite(cmdbase + 4);
4709 si.dwFlags = STARTF_USESHOWWINDOW;
4710 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004711 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004712 }
4713 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004714 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004715 {
4716 cmdbase = skipwhite(cmdbase + 2);
4717 flags = CREATE_NO_WINDOW;
4718 si.dwFlags = STARTF_USESTDHANDLES;
4719 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004720 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004721 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004722 NULL, // Security att.
4723 OPEN_EXISTING, // Open flags
4724 FILE_ATTRIBUTE_NORMAL, // File att.
4725 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004726 si.hStdOutput = si.hStdInput;
4727 si.hStdError = si.hStdInput;
4728 }
4729
Bram Moolenaar0f873732019-12-05 20:28:46 +01004730 // Remove a trailing ", ) and )" if they have a match
4731 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004732 if (cmdbase > cmd)
4733 {
4734 p = cmdbase + STRLEN(cmdbase);
4735 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4736 *--p = NUL;
4737 if (p > cmdbase && p[-1] == ')'
4738 && (*cmd =='(' || cmd[1] == '('))
4739 *--p = NUL;
4740 }
4741
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004742 newcmd = cmdbase;
4743 unescape_shellxquote(cmdbase, p_sxe);
4744
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004745 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004746 * If creating new console, arguments are passed to the
4747 * 'cmd.exe' as-is. If it's not, arguments are not treated
4748 * correctly for current 'cmd.exe'. So unescape characters in
4749 * shellxescape except '|' for avoiding to be treated as
4750 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004751 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004752 if (flags != CREATE_NEW_CONSOLE)
4753 {
4754 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004755 char_u *cmd_shell = mch_getenv("COMSPEC");
4756
4757 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004758 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004759
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004760 subcmd = vim_strsave_escaped_ext(cmdbase,
4761 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004762 if (subcmd != NULL)
4763 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004764 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004765 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004766 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004767 if (newcmd != NULL)
4768 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004769 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004770 else
4771 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004772 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004773 }
4774 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004775
4776 /*
4777 * Now, start the command as a process, so that it doesn't
4778 * inherit our handles which causes unpleasant dangling swap
4779 * files if we exit before the spawned process
4780 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004781 if (vim_create_process((char *)newcmd, FALSE, flags,
4782 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004783 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004784 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4785 > (HINSTANCE)32)
4786 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004787 else
4788 {
4789 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004790#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004791# ifdef VIMDLL
4792 if (gui.in_use)
4793# endif
4794 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004795#endif
4796 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004797
4798 if (newcmd != cmdbase)
4799 vim_free(newcmd);
4800
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004801 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004803 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004804 CloseHandle(si.hStdInput);
4805 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004807 CloseHandle(pi.hThread);
4808 CloseHandle(pi.hProcess);
4809 }
4810 else
4811 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004812 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004813#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004814 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004815 (!s_dont_use_vimrun && p_stmp ?
4816 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4817 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004819 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004820
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004821 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004822 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004824#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004825 if (
4826# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004827 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004828# endif
4829 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004831 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4832 "External commands will not pause after completion.\n"
4833 "See :help win32-vimrun for more information.");
4834 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004835 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4836 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004837
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004838 if (wmsg != NULL && wtitle != NULL)
4839 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4840 vim_free(wmsg);
4841 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 need_vimrun_warning = FALSE;
4843 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004844 if (
4845# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004846 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004847# endif
4848 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004849 // Use vimrun to execute the command. It opens a console
4850 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004851 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 vimrun_path,
4853 (msg_silent != 0 || (options & SHELL_DOOUT))
4854 ? "-s " : "",
4855 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004856 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004857# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004858 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004859# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004860 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004861 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004862 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4863 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004866 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004867 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004869 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873
4874 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004875 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004879#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004880 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004881 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
4883 )
4884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004885 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 msg_putchar('\n');
4887 }
4888#ifdef FEAT_TITLE
4889 resettitle();
4890#endif
4891
4892 signal(SIGINT, SIG_DFL);
4893#if defined(__GNUC__) && !defined(__MINGW32__)
4894 signal(SIGKILL, SIG_DFL);
4895#else
4896 signal(SIGBREAK, SIG_DFL);
4897#endif
4898 signal(SIGILL, SIG_DFL);
4899 signal(SIGFPE, SIG_DFL);
4900 signal(SIGSEGV, SIG_DFL);
4901 signal(SIGTERM, SIG_DFL);
4902 signal(SIGABRT, SIG_DFL);
4903
4904 return x;
4905}
4906
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004907#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004908 static HANDLE
4909job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004910 char_u *fname,
4911 DWORD dwDesiredAccess,
4912 DWORD dwShareMode,
4913 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4914 DWORD dwCreationDisposition,
4915 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004916{
4917 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004918 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004919
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004920 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004921 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004922 return INVALID_HANDLE_VALUE;
4923
4924 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4925 lpSecurityAttributes, dwCreationDisposition,
4926 dwFlagsAndAttributes, NULL);
4927 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004928 return h;
4929}
4930
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004931/*
4932 * Turn the dictionary "env" into a NUL separated list that can be used as the
4933 * environment argument of vim_create_process().
4934 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004935 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004936win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004937{
4938 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004939 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004940 LPVOID base = GetEnvironmentStringsW();
4941
Bram Moolenaar0f873732019-12-05 20:28:46 +01004942 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004943 if (ga_grow(gap, 1) == FAIL)
4944 return;
4945
4946 if (base)
4947 {
4948 WCHAR *p = (WCHAR*) base;
4949
Bram Moolenaar0f873732019-12-05 20:28:46 +01004950 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004951 if (ga_grow(gap, 1) == FAIL)
4952 return;
4953
4954 while (*p != 0 || *(p + 1) != 0)
4955 {
4956 if (ga_grow(gap, 1) == OK)
4957 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4958 p++;
4959 }
4960 FreeEnvironmentStrings(base);
4961 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4962 }
4963
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004964 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004965 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004966 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004967 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004969 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004970 typval_T *item = &dict_lookup(hi)->di_tv;
4971 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004972 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004973 --todo;
4974 if (wkey != NULL && wval != NULL)
4975 {
4976 size_t n;
4977 size_t lkey = wcslen(wkey);
4978 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004979
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004980 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4981 continue;
4982 for (n = 0; n < lkey; n++)
4983 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4984 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4985 for (n = 0; n < lval; n++)
4986 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4987 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4988 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004989 vim_free(wkey);
4990 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004991 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004992 }
4993 }
4994
Bram Moolenaar493359e2018-06-12 20:25:52 +02004995# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004996 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02004997# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004998 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004999 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005000# endif
5001# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005002 char_u *version = get_vim_var_str(VV_VERSION);
5003 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005004# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005005 // size of "VIM_SERVERNAME=" and value,
5006 // plus "VIM_TERMINAL=" and value,
5007 // plus two terminating NULs
5008 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005009# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005010 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005011# endif
5012# ifdef FEAT_TERMINAL
5013 + 13 + version_len + 2
5014# endif
5015 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005016
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005017 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005018 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005019# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005020 for (n = 0; n < 15; n++)
5021 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5022 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005023 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005024 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5025 (WCHAR)servername[n];
5026 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005027# endif
5028# ifdef FEAT_TERMINAL
5029 if (is_terminal)
5030 {
5031 for (n = 0; n < 13; n++)
5032 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5033 (WCHAR)"VIM_TERMINAL="[n];
5034 for (n = 0; n < version_len; n++)
5035 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5036 (WCHAR)version[n];
5037 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5038 }
5039# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005040 }
5041 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005042# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005043}
5044
Bram Moolenaarb091f302019-01-19 14:37:00 +01005045/*
5046 * Create a pair of pipes.
5047 * Return TRUE for success, FALSE for failure.
5048 */
5049 static BOOL
5050create_pipe_pair(HANDLE handles[2])
5051{
5052 static LONG s;
5053 char name[64];
5054 SECURITY_ATTRIBUTES sa;
5055
5056 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5057 GetCurrentProcessId(),
5058 InterlockedIncrement(&s));
5059
5060 // Create named pipe. Max size of named pipe is 65535.
5061 handles[1] = CreateNamedPipe(
5062 name,
5063 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5064 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005065 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005066
5067 if (handles[1] == INVALID_HANDLE_VALUE)
5068 return FALSE;
5069
5070 sa.nLength = sizeof(sa);
5071 sa.bInheritHandle = TRUE;
5072 sa.lpSecurityDescriptor = NULL;
5073
5074 handles[0] = CreateFile(name,
5075 FILE_GENERIC_READ,
5076 FILE_SHARE_READ, &sa,
5077 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5078
5079 if (handles[0] == INVALID_HANDLE_VALUE)
5080 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005081 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005082 return FALSE;
5083 }
5084
5085 return TRUE;
5086}
5087
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005088 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005089mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005090{
5091 STARTUPINFO si;
5092 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005093 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005094 SECURITY_ATTRIBUTES saAttr;
5095 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005096 HANDLE ifd[2];
5097 HANDLE ofd[2];
5098 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005099 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005100
5101 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5102 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5103 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5104 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5105 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5106 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5107 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5108
5109 if (use_out_for_err && use_null_for_out)
5110 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005111
5112 ifd[0] = INVALID_HANDLE_VALUE;
5113 ifd[1] = INVALID_HANDLE_VALUE;
5114 ofd[0] = INVALID_HANDLE_VALUE;
5115 ofd[1] = INVALID_HANDLE_VALUE;
5116 efd[0] = INVALID_HANDLE_VALUE;
5117 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005118 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005119
Bram Moolenaar14207f42016-10-27 21:13:10 +02005120 jo = CreateJobObject(NULL, NULL);
5121 if (jo == NULL)
5122 {
5123 job->jv_status = JOB_FAILED;
5124 goto failed;
5125 }
5126
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005127 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005128 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005129
Bram Moolenaar76467df2016-02-12 19:30:26 +01005130 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005131 ZeroMemory(&si, sizeof(si));
5132 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005133 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005134 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005135
Bram Moolenaard8070362016-02-15 21:56:54 +01005136 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5137 saAttr.bInheritHandle = TRUE;
5138 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005139
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005140 if (use_file_for_in)
5141 {
5142 char_u *fname = options->jo_io_name[PART_IN];
5143
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005144 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5145 FILE_SHARE_READ | FILE_SHARE_WRITE,
5146 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5147 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005149 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005150 goto failed;
5151 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005152 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005153 else if (!use_null_for_in
5154 && (!create_pipe_pair(ifd)
5155 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005156 goto failed;
5157
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005158 if (use_file_for_out)
5159 {
5160 char_u *fname = options->jo_io_name[PART_OUT];
5161
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005162 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5163 FILE_SHARE_READ | FILE_SHARE_WRITE,
5164 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5165 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005167 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005168 goto failed;
5169 }
5170 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005171 else if (!use_null_for_out &&
5172 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005173 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005174 goto failed;
5175
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005176 if (use_file_for_err)
5177 {
5178 char_u *fname = options->jo_io_name[PART_ERR];
5179
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005180 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5181 FILE_SHARE_READ | FILE_SHARE_WRITE,
5182 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5183 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005185 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005186 goto failed;
5187 }
5188 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005189 else if (!use_out_for_err && !use_null_for_err &&
5190 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005191 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005192 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005193
Bram Moolenaard8070362016-02-15 21:56:54 +01005194 si.dwFlags |= STARTF_USESTDHANDLES;
5195 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005196 si.hStdOutput = ofd[1];
5197 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5198
5199 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5200 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005201 if (options->jo_set & JO_CHANNEL)
5202 {
5203 channel = options->jo_channel;
5204 if (channel != NULL)
5205 ++channel->ch_refcount;
5206 }
5207 else
5208 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005209 if (channel == NULL)
5210 goto failed;
5211 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005212
5213 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005214 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005215 CREATE_DEFAULT_ERROR_MODE |
5216 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005217 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005218 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005219 &si, &pi,
5220 ga.ga_data,
5221 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005222 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005223 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005224 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005225 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005226 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005227
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005228 ga_clear(&ga);
5229
Bram Moolenaar14207f42016-10-27 21:13:10 +02005230 if (!AssignProcessToJobObject(jo, pi.hProcess))
5231 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005232 // if failing, switch the way to terminate
5233 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005234 CloseHandle(jo);
5235 jo = NULL;
5236 }
5237 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005238 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005239 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005240 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005241 job->jv_status = JOB_STARTED;
5242
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005243 CloseHandle(ifd[0]);
5244 CloseHandle(ofd[1]);
5245 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005246 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005247
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005248 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005249 if (channel != NULL)
5250 {
5251 channel_set_pipes(channel,
5252 use_file_for_in || use_null_for_in
5253 ? INVALID_FD : (sock_T)ifd[1],
5254 use_file_for_out || use_null_for_out
5255 ? INVALID_FD : (sock_T)ofd[0],
5256 use_out_for_err || use_file_for_err || use_null_for_err
5257 ? INVALID_FD : (sock_T)efd[0]);
5258 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005259 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005260 return;
5261
5262failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005263 CloseHandle(ifd[0]);
5264 CloseHandle(ofd[0]);
5265 CloseHandle(efd[0]);
5266 CloseHandle(ifd[1]);
5267 CloseHandle(ofd[1]);
5268 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005269 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005270 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005271}
5272
5273 char *
5274mch_job_status(job_T *job)
5275{
5276 DWORD dwExitCode = 0;
5277
Bram Moolenaar76467df2016-02-12 19:30:26 +01005278 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5279 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005280 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005281 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005282 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005283 {
5284 ch_log(job->jv_channel, "Job ended");
5285 job->jv_status = JOB_ENDED;
5286 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005287 return "dead";
5288 }
5289 return "run";
5290}
5291
Bram Moolenaar97792de2016-10-15 18:36:49 +02005292 job_T *
5293mch_detect_ended_job(job_T *job_list)
5294{
5295 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5296 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5297 job_T *job = job_list;
5298
5299 while (job != NULL)
5300 {
5301 DWORD n;
5302 DWORD result;
5303
5304 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5305 && job != NULL; job = job->jv_next)
5306 {
5307 if (job->jv_status == JOB_STARTED)
5308 {
5309 jobHandles[n] = job->jv_proc_info.hProcess;
5310 jobArray[n] = job;
5311 ++n;
5312 }
5313 }
5314 if (n == 0)
5315 continue;
5316 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5317 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5318 {
5319 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5320
5321 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5322 return wait_job;
5323 }
5324 }
5325 return NULL;
5326}
5327
Bram Moolenaarfb630902016-10-29 14:55:00 +02005328 static BOOL
5329terminate_all(HANDLE process, int code)
5330{
5331 PROCESSENTRY32 pe;
5332 HANDLE h = INVALID_HANDLE_VALUE;
5333 DWORD pid = GetProcessId(process);
5334
5335 if (pid != 0)
5336 {
5337 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5338 if (h != INVALID_HANDLE_VALUE)
5339 {
5340 pe.dwSize = sizeof(PROCESSENTRY32);
5341 if (!Process32First(h, &pe))
5342 goto theend;
5343
5344 do
5345 {
5346 if (pe.th32ParentProcessID == pid)
5347 {
5348 HANDLE ph = OpenProcess(
5349 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5350 if (ph != NULL)
5351 {
5352 terminate_all(ph, code);
5353 CloseHandle(ph);
5354 }
5355 }
5356 } while (Process32Next(h, &pe));
5357
5358 CloseHandle(h);
5359 }
5360 }
5361
5362theend:
5363 return TerminateProcess(process, code);
5364}
5365
5366/*
5367 * Send a (deadly) signal to "job".
5368 * Return FAIL if it didn't work.
5369 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005370 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005371mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005372{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005373 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005374
Bram Moolenaar923d9262016-02-25 20:56:01 +01005375 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005376 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005377 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005378 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005379 {
5380 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5381 job->jv_channel->ch_killing = TRUE;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005382 return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005383 }
Bram Moolenaarfb630902016-10-29 14:55:00 +02005384 return terminate_all(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005385 }
5386
5387 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5388 return FAIL;
5389 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005390 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5391 job->jv_proc_info.dwProcessId)
5392 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005393 FreeConsole();
5394 return ret;
5395}
5396
5397/*
5398 * Clear the data related to "job".
5399 */
5400 void
5401mch_clear_job(job_T *job)
5402{
5403 if (job->jv_status != JOB_FAILED)
5404 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005405 if (job->jv_job_object != NULL)
5406 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005407 CloseHandle(job->jv_proc_info.hProcess);
5408 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005409}
5410#endif
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005413#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
5416 * Start termcap mode
5417 */
5418 static void
5419termcap_mode_start(void)
5420{
5421 DWORD cmodein;
5422
5423 if (g_fTermcapMode)
5424 return;
5425
5426 SaveConsoleBuffer(&g_cbNonTermcap);
5427
5428 if (g_cbTermcap.IsValid)
5429 {
5430 /*
5431 * We've been in termcap mode before. Restore certain screen
5432 * characteristics, including the buffer size and the window
5433 * size. Since we will be redrawing the screen, we don't need
5434 * to restore the actual contents of the buffer.
5435 */
5436 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005437 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5439 Rows = g_cbTermcap.Info.dwSize.Y;
5440 Columns = g_cbTermcap.Info.dwSize.X;
5441 }
5442 else
5443 {
5444 /*
5445 * This is our first time entering termcap mode. Clear the console
5446 * screen buffer, and resize the buffer to match the current window
5447 * size. We will use this as the size of our editing environment.
5448 */
5449 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005450 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5452 }
5453
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005454# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 if (g_fMouseActive)
5460 cmodein |= ENABLE_MOUSE_INPUT;
5461 else
5462 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 cmodein |= ENABLE_WINDOW_INPUT;
5464 SetConsoleMode(g_hConIn, cmodein);
5465
5466 redraw_later_clear();
5467 g_fTermcapMode = TRUE;
5468}
5469
5470
5471/*
5472 * End termcap mode
5473 */
5474 static void
5475termcap_mode_end(void)
5476{
5477 DWORD cmodein;
5478 ConsoleBuffer *cb;
5479 COORD coord;
5480 DWORD dwDummy;
5481
5482 if (!g_fTermcapMode)
5483 return;
5484
5485 SaveConsoleBuffer(&g_cbTermcap);
5486
5487 GetConsoleMode(g_hConIn, &cmodein);
5488 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5489 SetConsoleMode(g_hConIn, cmodein);
5490
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005491# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005492 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005493# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005497 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 SetConsoleCursorInfo(g_hConOut, &g_cci);
5499
5500 if (p_rs || exiting)
5501 {
5502 /*
5503 * Clear anything that happens to be on the current line.
5504 */
5505 coord.X = 0;
5506 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5507 FillConsoleOutputCharacter(g_hConOut, ' ',
5508 cb->Info.dwSize.X, coord, &dwDummy);
5509 /*
5510 * The following is just for aesthetics. If we are exiting without
5511 * restoring the screen, then we want to have a prompt string
5512 * appear at the bottom line. However, the command interpreter
5513 * seems to always advance the cursor one line before displaying
5514 * the prompt string, which causes the screen to scroll. To
5515 * counter this, move the cursor up one line before exiting.
5516 */
5517 if (exiting && !p_rs)
5518 coord.Y--;
5519 /*
5520 * Position the cursor at the leftmost column of the desired row.
5521 */
5522 SetConsoleCursorPosition(g_hConOut, coord);
5523 }
5524
5525 g_fTermcapMode = FALSE;
5526}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005527#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005530#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 void
5532mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005533 char_u *s UNUSED,
5534 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005536 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537}
5538
5539#else
5540
5541/*
5542 * clear `n' chars, starting from `coord'
5543 */
5544 static void
5545clear_chars(
5546 COORD coord,
5547 DWORD n)
5548{
5549 DWORD dwDummy;
5550
5551 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005552
5553 if (!USE_VTP)
5554 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5555 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005556 {
5557 set_console_color_rgb();
5558 gotoxy(coord.X + 1, coord.Y + 1);
5559 vtp_printf("\033[%dX", n);
5560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562
5563
5564/*
5565 * Clear the screen
5566 */
5567 static void
5568clear_screen(void)
5569{
5570 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005571
5572 if (!USE_VTP)
5573 clear_chars(g_coord, Rows * Columns);
5574 else
5575 {
5576 set_console_color_rgb();
5577 gotoxy(1, 1);
5578 vtp_printf("\033[2J");
5579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
5581
5582
5583/*
5584 * Clear to end of display
5585 */
5586 static void
5587clear_to_end_of_display(void)
5588{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005589 COORD save = g_coord;
5590
5591 if (!USE_VTP)
5592 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005594 else
5595 {
5596 set_console_color_rgb();
5597 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5598 vtp_printf("\033[0J");
5599
5600 gotoxy(save.X + 1, save.Y + 1);
5601 g_coord = save;
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605
5606/*
5607 * Clear to end of line
5608 */
5609 static void
5610clear_to_end_of_line(void)
5611{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005612 COORD save = g_coord;
5613
5614 if (!USE_VTP)
5615 clear_chars(g_coord, Columns - g_coord.X);
5616 else
5617 {
5618 set_console_color_rgb();
5619 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5620 vtp_printf("\033[0K");
5621
5622 gotoxy(save.X + 1, save.Y + 1);
5623 g_coord = save;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626
5627
5628/*
5629 * Scroll the scroll region up by `cLines' lines
5630 */
5631 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005632scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 COORD oldcoord = g_coord;
5635
5636 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5637 delete_lines(cLines);
5638
5639 g_coord = oldcoord;
5640}
5641
5642
5643/*
5644 * Set the scroll region
5645 */
5646 static void
5647set_scroll_region(
5648 unsigned left,
5649 unsigned top,
5650 unsigned right,
5651 unsigned bottom)
5652{
5653 if (left >= right
5654 || top >= bottom
5655 || right > (unsigned) Columns - 1
5656 || bottom > (unsigned) Rows - 1)
5657 return;
5658
5659 g_srScrollRegion.Left = left;
5660 g_srScrollRegion.Top = top;
5661 g_srScrollRegion.Right = right;
5662 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005663}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005664
Bram Moolenaar6982f422019-02-16 16:48:01 +01005665 static void
5666set_scroll_region_tb(
5667 unsigned top,
5668 unsigned bottom)
5669{
5670 if (top >= bottom || bottom > (unsigned)Rows - 1)
5671 return;
5672
5673 g_srScrollRegion.Top = top;
5674 g_srScrollRegion.Bottom = bottom;
5675}
5676
5677 static void
5678set_scroll_region_lr(
5679 unsigned left,
5680 unsigned right)
5681{
5682 if (left >= right || right > (unsigned)Columns - 1)
5683 return;
5684
5685 g_srScrollRegion.Left = left;
5686 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687}
5688
5689
5690/*
5691 * Insert `cLines' lines at the current cursor position
5692 */
5693 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005694insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005696 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 COORD dest;
5698 CHAR_INFO fill;
5699
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005700 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5701
Bram Moolenaar6982f422019-02-16 16:48:01 +01005702 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 dest.Y = g_coord.Y + cLines;
5704
Bram Moolenaar6982f422019-02-16 16:48:01 +01005705 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 source.Top = g_coord.Y;
5707 source.Right = g_srScrollRegion.Right;
5708 source.Bottom = g_srScrollRegion.Bottom - cLines;
5709
Bram Moolenaar6982f422019-02-16 16:48:01 +01005710 clip.Left = g_srScrollRegion.Left;
5711 clip.Top = g_coord.Y;
5712 clip.Right = g_srScrollRegion.Right;
5713 clip.Bottom = g_srScrollRegion.Bottom;
5714
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005715 fill.Char.AsciiChar = ' ';
5716 if (!USE_VTP)
5717 fill.Attributes = g_attrCurrent;
5718 else
5719 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005721 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005722
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005723 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5724
Bram Moolenaar6982f422019-02-16 16:48:01 +01005725 // Here we have to deal with a win32 console flake: If the scroll
5726 // region looks like abc and we scroll c to a and fill with d we get
5727 // cbd... if we scroll block c one line at a time to a, we get cdd...
5728 // vim expects cdd consistently... So we have to deal with that
5729 // here... (this also occurs scrolling the same way in the other
5730 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 if (source.Bottom < dest.Y)
5733 {
5734 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005735 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar6982f422019-02-16 16:48:01 +01005737 coord.X = source.Left;
5738 for (i = clip.Top; i < dest.Y; ++i)
5739 {
5740 coord.Y = i;
5741 clear_chars(coord, source.Right - source.Left + 1);
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
5744}
5745
5746
5747/*
5748 * Delete `cLines' lines at the current cursor position
5749 */
5750 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005751delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005753 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 COORD dest;
5755 CHAR_INFO fill;
5756 int nb;
5757
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005758 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5759
Bram Moolenaar6982f422019-02-16 16:48:01 +01005760 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 dest.Y = g_coord.Y;
5762
Bram Moolenaar6982f422019-02-16 16:48:01 +01005763 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 source.Top = g_coord.Y + cLines;
5765 source.Right = g_srScrollRegion.Right;
5766 source.Bottom = g_srScrollRegion.Bottom;
5767
Bram Moolenaar6982f422019-02-16 16:48:01 +01005768 clip.Left = g_srScrollRegion.Left;
5769 clip.Top = g_coord.Y;
5770 clip.Right = g_srScrollRegion.Right;
5771 clip.Bottom = g_srScrollRegion.Bottom;
5772
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005773 fill.Char.AsciiChar = ' ';
5774 if (!USE_VTP)
5775 fill.Attributes = g_attrCurrent;
5776 else
5777 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005779 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005780
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005781 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5782
Bram Moolenaar6982f422019-02-16 16:48:01 +01005783 // Here we have to deal with a win32 console flake; See insert_lines()
5784 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
5786 nb = dest.Y + (source.Bottom - source.Top) + 1;
5787
5788 if (nb < source.Top)
5789 {
5790 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005791 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar6982f422019-02-16 16:48:01 +01005793 coord.X = source.Left;
5794 for (i = nb; i < clip.Bottom; ++i)
5795 {
5796 coord.Y = i;
5797 clear_chars(coord, source.Right - source.Left + 1);
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800}
5801
5802
5803/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005804 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
5806 static void
5807gotoxy(
5808 unsigned x,
5809 unsigned y)
5810{
5811 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5812 return;
5813
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005814 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005815 {
5816 // external cursor coords are 1-based; internal are 0-based
5817 g_coord.X = x - 1;
5818 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005819 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005820 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005821 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005822 {
5823 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005824 // destruction. Insider build bug. Always enabled because it's cheap
5825 // and avoids mistakes with recognizing the build.
5826 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005827
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005828 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005829
5830 g_coord.X = x - 1;
5831 g_coord.Y = y - 1;
5832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833}
5834
5835
5836/*
5837 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005838 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 */
5840 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005841textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005843 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
5845 SetConsoleTextAttribute(g_hConOut, wAttr);
5846}
5847
5848
5849 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005850textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005852 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005854 if (!USE_VTP)
5855 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5856 else
5857 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858}
5859
5860
5861 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005862textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005864 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005866 if (!USE_VTP)
5867 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5868 else
5869 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870}
5871
5872
5873/*
5874 * restore the default text attribute (whatever we started with)
5875 */
5876 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005877normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005879 if (!USE_VTP)
5880 textattr(g_attrDefault);
5881 else
5882 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883}
5884
5885
5886static WORD g_attrPreStandout = 0;
5887
5888/*
5889 * Make the text standout, by brightening it
5890 */
5891 static void
5892standout(void)
5893{
5894 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005895
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5897}
5898
5899
5900/*
5901 * Turn off standout mode
5902 */
5903 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005904standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005908
5909 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910}
5911
5912
5913/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005914 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 */
5916 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005917mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 char_u *p;
5920 int n;
5921
5922 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5923 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005924 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005925# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005926 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005927# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005928 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
5930 p = T_ME + 2;
5931 n = getdigits(&p);
5932 if (*p == 'm' && n > 0)
5933 {
5934 cterm_normal_fg_color = (n & 0xf) + 1;
5935 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5936 }
5937 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005938# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005939 cterm_normal_fg_gui_color = INVALCOLOR;
5940 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942}
5943
5944
5945/*
5946 * visual bell: flash the screen
5947 */
5948 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005949visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 COORD coordOrigin = {0, 0};
5952 WORD attrFlash = ~g_attrCurrent & 0xff;
5953
5954 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005955 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
5957 if (oldattrs == NULL)
5958 return;
5959 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5960 coordOrigin, &dwDummy);
5961 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5962 coordOrigin, &dwDummy);
5963
Bram Moolenaar0f873732019-12-05 20:28:46 +01005964 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005965 if (!USE_VTP)
5966 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 coordOrigin, &dwDummy);
5968 vim_free(oldattrs);
5969}
5970
5971
5972/*
5973 * Make the cursor visible or invisible
5974 */
5975 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005976cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005979# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982}
5983
5984
5985/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005986 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005987 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005989 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005991 char_u *pchBuf,
5992 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005994 COORD coord = g_coord;
5995 DWORD written;
5996 DWORD n, cchwritten, cells;
5997 static WCHAR *unicodebuf = NULL;
5998 static int unibuflen = 0;
5999 int length;
6000 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006002 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6003 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006004 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006005 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006006 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006007 unibuflen = length;
6008 }
6009 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6010 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006012 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006013
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006014 if (!USE_VTP)
6015 {
6016 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6017 coord, &written);
6018 // When writing fails or didn't write a single character, pretend one
6019 // character was written, otherwise we get stuck.
6020 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6021 coord, &cchwritten) == 0
6022 || cchwritten == 0 || cchwritten == (DWORD)-1)
6023 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006024 }
6025 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006026 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006027 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6028 NULL) == 0 || cchwritten == 0)
6029 cchwritten = 1;
6030 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006031
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006032 if (cchwritten == length)
6033 {
6034 written = cbToWrite;
6035 g_coord.X += (SHORT)cells;
6036 }
6037 else
6038 {
6039 char_u *p = pchBuf;
6040 for (n = 0; n < cchwritten; n++)
6041 MB_CPTR_ADV(p);
6042 written = p - pchBuf;
6043 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 while (g_coord.X > g_srScrollRegion.Right)
6047 {
6048 g_coord.X -= (SHORT) Columns;
6049 if (g_coord.Y < g_srScrollRegion.Bottom)
6050 g_coord.Y++;
6051 }
6052
6053 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6054
6055 return written;
6056}
6057
6058
6059/*
6060 * mch_write(): write the output buffer to the screen, translating ESC
6061 * sequences into calls to console output routines.
6062 */
6063 void
6064mch_write(
6065 char_u *s,
6066 int len)
6067{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006068# ifdef VIMDLL
6069 if (gui.in_use)
6070 return;
6071# endif
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 s[len] = NUL;
6074
6075 if (!term_console)
6076 {
6077 write(1, s, (unsigned)len);
6078 return;
6079 }
6080
Bram Moolenaar0f873732019-12-05 20:28:46 +01006081 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 while (len--)
6083 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006084 // optimization: use one single write_chars for runs of text,
6085 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006086 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 if (p_wd)
6089 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006090 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (prefix != 0)
6092 prefix = 1;
6093 }
6094
6095 if (prefix != 0)
6096 {
6097 DWORD nWritten;
6098
6099 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006100# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 if (fdDump)
6102 {
6103 fputc('>', fdDump);
6104 fwrite(s, sizeof(char_u), nWritten, fdDump);
6105 fputs("<\n", fdDump);
6106 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 len -= (nWritten - 1);
6109 s += nWritten;
6110 }
6111 else if (s[0] == '\n')
6112 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006113 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (g_coord.Y == g_srScrollRegion.Bottom)
6115 {
6116 scroll(1);
6117 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6118 }
6119 else
6120 {
6121 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6122 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006123# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (fdDump)
6125 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 s++;
6128 }
6129 else if (s[0] == '\r')
6130 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006131 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006133# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (fdDump)
6135 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 s++;
6138 }
6139 else if (s[0] == '\b')
6140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006141 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (g_coord.X > g_srScrollRegion.Left)
6143 g_coord.X--;
6144 else if (g_coord.Y > g_srScrollRegion.Top)
6145 {
6146 g_coord.X = g_srScrollRegion.Right;
6147 g_coord.Y--;
6148 }
6149 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006150# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (fdDump)
6152 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 s++;
6155 }
6156 else if (s[0] == '\a')
6157 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006158 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006160# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (fdDump)
6162 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 s++;
6165 }
6166 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6167 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006168# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006169 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006170# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006171 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006172 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 switch (s[2])
6175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 case '0': case '1': case '2': case '3': case '4':
6177 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006178 p = s + 1;
6179 do
6180 {
6181 ++p;
6182 args[argc] = getdigits(&p);
6183 argc += (argc < 15) ? 1 : 0;
6184 if (p > s + len)
6185 break;
6186 } while (*p == ';');
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (p > s + len)
6189 break;
6190
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006191 arg1 = args[0];
6192 arg2 = args[1];
6193 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006195 if (argc == 1 && args[0] == 0)
6196 normvideo();
6197 else if (argc == 1)
6198 {
6199 if (USE_VTP)
6200 textcolor((WORD) arg1);
6201 else
6202 textattr((WORD) arg1);
6203 }
6204 else if (USE_VTP)
6205 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006207 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006209 gotoxy(arg2, arg1);
6210 }
6211 else if (argc == 2 && *p == 'r')
6212 {
6213 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6214 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006215 else if (argc == 2 && *p == 'R')
6216 {
6217 set_scroll_region_tb(arg1, arg2);
6218 }
6219 else if (argc == 2 && *p == 'V')
6220 {
6221 set_scroll_region_lr(arg1, arg2);
6222 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006223 else if (argc == 1 && *p == 'A')
6224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 gotoxy(g_coord.X + 1,
6226 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6227 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006228 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
6230 textbackground((WORD) arg1);
6231 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006232 else if (argc == 1 && *p == 'C')
6233 {
6234 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6235 g_coord.Y + 1);
6236 }
6237 else if (argc == 1 && *p == 'f')
6238 {
6239 textcolor((WORD) arg1);
6240 }
6241 else if (argc == 1 && *p == 'H')
6242 {
6243 gotoxy(1, arg1);
6244 }
6245 else if (argc == 1 && *p == 'L')
6246 {
6247 insert_lines(arg1);
6248 }
6249 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 delete_lines(arg1);
6252 }
6253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006254 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 s = p + 1;
6256 break;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 gotoxy(g_coord.X + 1,
6260 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6261 goto got3;
6262
6263 case 'B':
6264 visual_bell();
6265 goto got3;
6266
6267 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6269 g_coord.Y + 1);
6270 goto got3;
6271
6272 case 'E':
6273 termcap_mode_end();
6274 goto got3;
6275
6276 case 'F':
6277 standout();
6278 goto got3;
6279
6280 case 'f':
6281 standend();
6282 goto got3;
6283
6284 case 'H':
6285 gotoxy(1, 1);
6286 goto got3;
6287
6288 case 'j':
6289 clear_to_end_of_display();
6290 goto got3;
6291
6292 case 'J':
6293 clear_screen();
6294 goto got3;
6295
6296 case 'K':
6297 clear_to_end_of_line();
6298 goto got3;
6299
6300 case 'L':
6301 insert_lines(1);
6302 goto got3;
6303
6304 case 'M':
6305 delete_lines(1);
6306 goto got3;
6307
6308 case 'S':
6309 termcap_mode_start();
6310 goto got3;
6311
6312 case 'V':
6313 cursor_visible(TRUE);
6314 goto got3;
6315
6316 case 'v':
6317 cursor_visible(FALSE);
6318 goto got3;
6319
6320 got3:
6321 s += 3;
6322 len -= 2;
6323 }
6324
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006325# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (fdDump)
6327 {
6328 fputs("ESC | ", fdDump);
6329 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6330 fputc('\n', fdDump);
6331 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
6334 else
6335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006336 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 DWORD nWritten;
6338
6339 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006340# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (fdDump)
6342 {
6343 fputc('>', fdDump);
6344 fwrite(s, sizeof(char_u), nWritten, fdDump);
6345 fputs("<\n", fdDump);
6346 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349 len -= (nWritten - 1);
6350 s += nWritten;
6351 }
6352 }
6353
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006354# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (fdDump)
6356 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
Bram Moolenaar0f873732019-12-05 20:28:46 +01006360#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362
6363/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006364 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 */
6366 void
6367mch_delay(
6368 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006369 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006371#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006372 Sleep((int)msec); // never wait for input
6373#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006374# ifdef VIMDLL
6375 if (gui.in_use)
6376 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006377 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006378 return;
6379 }
6380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006382# ifdef FEAT_MZSCHEME
6383 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6384 {
6385 int towait = p_mzq;
6386
Bram Moolenaar0f873732019-12-05 20:28:46 +01006387 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006388 while (msec > 0)
6389 {
6390 mzvim_check_threads();
6391 if (msec < towait)
6392 towait = msec;
6393 Sleep(towait);
6394 msec -= towait;
6395 }
6396 }
6397 else
6398# endif
6399 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006401 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402#endif
6403}
6404
6405
6406/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006407 * This version of remove is not scared by a readonly (backup) file.
6408 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 * Return 0 for success, -1 for failure.
6410 */
6411 int
6412mch_remove(char_u *name)
6413{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006414 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 int n;
6416
Bram Moolenaar203258c2016-01-17 22:15:16 +01006417 /*
6418 * On Windows, deleting a directory's symbolic link is done by
6419 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6420 */
6421 if (mch_isdir(name) && mch_is_symbolic_link(name))
6422 return mch_rmdir(name);
6423
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006424 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6425
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006426 wn = enc_to_utf16(name, NULL);
6427 if (wn == NULL)
6428 return -1;
6429
6430 n = DeleteFileW(wn) ? 0 : -1;
6431 vim_free(wn);
6432 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
6435
6436/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006437 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
6439 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006440mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006442#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6443# ifdef VIMDLL
6444 if (!gui.in_use)
6445# endif
6446 if (g_fCtrlCPressed || g_fCBrkPressed)
6447 {
6448 ctrl_break_was_pressed = g_fCBrkPressed;
6449 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6450 got_int = TRUE;
6451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452#endif
6453}
6454
Bram Moolenaar0f873732019-12-05 20:28:46 +01006455// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006456#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006457
6458/*
6459 * How much main memory in KiB that can be used by VIM.
6460 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006461 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006462mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006463{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006464 MEMORYSTATUSEX ms;
6465
Bram Moolenaar0f873732019-12-05 20:28:46 +01006466 // Need to use GlobalMemoryStatusEx() when there is more memory than
6467 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006468 ms.dwLength = sizeof(MEMORYSTATUSEX);
6469 GlobalMemoryStatusEx(&ms);
6470 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006471 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006472 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006473 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006474 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006475 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006478 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006479 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006481 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006485 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6487 * file whose short file name is "FOO.BAR" (its long file name will
6488 * be correct: "foo.bar~"). Because a file can be accessed by
6489 * either its SFN or its LFN, "foo.bar" has effectively been
6490 * renamed to "foo.bar", which is not at all what was wanted. This
6491 * seems to happen only when renaming files with three-character
6492 * extensions by appending a suffix that does not include ".".
6493 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6494 *
6495 * There is another problem, which isn't really a bug but isn't right either:
6496 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6497 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6498 * service pack 6. Doesn't seem to happen on Windows 98.
6499 *
6500 * Like rename(), returns 0 upon success, non-zero upon failure.
6501 * Should probably set errno appropriately when errors occur.
6502 */
6503 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006504mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006506 WCHAR *p;
6507 int i;
6508 WCHAR szTempFile[_MAX_PATH + 1];
6509 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006512 // No need to play tricks unless the file name contains a "~" as the
6513 // seventh character.
6514 p = wold;
6515 for (i = 0; wold[i] != NUL; ++i)
6516 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6517 && wold[i + 1] != 0)
6518 p = wold + i + 1;
6519 if ((int)(wold + i - p) < 8 || p[6] != '~')
6520 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006522 // Get base path of new file name. Undocumented feature: If pszNewFile is
6523 // a directory, no error is returned and pszFilePart will be NULL.
6524 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006526 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006528 // Get (and create) a unique temporary file name in directory of new file
6529 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 return -2;
6531
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006532 // blow the temp file away
6533 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 return -3;
6535
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 // rename old file to the temp file
6537 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return -4;
6539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 // now create an empty file called pszOldFile; this prevents the operating
6541 // system using pszOldFile as an alias (SFN) if we're renaming within the
6542 // same directory. For example, we're editing a file called
6543 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6544 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6545 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6546 // cause all sorts of problems later in buf_write(). So, we create an
6547 // empty file called filena~1.txt and the system will have to find some
6548 // other SFN for filena~1.txt~, such as filena~2.txt
6549 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6551 return -5;
6552 if (!CloseHandle(hf))
6553 return -6;
6554
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006555 // rename the temp file to the new file
6556 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006558 // Renaming failed. Rename the file back to its old name, so that it
6559 // looks like nothing happened.
6560 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 return -7;
6562 }
6563
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006564 // Seems to be left around on Novell filesystems
6565 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006567 // finally, remove the empty old file
6568 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 return -8;
6570
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006571 return 0;
6572}
6573
6574
6575/*
6576 * Converts the filenames to UTF-16, then call mch_wrename().
6577 * Like rename(), returns 0 upon success, non-zero upon failure.
6578 */
6579 int
6580mch_rename(
6581 const char *pszOldFile,
6582 const char *pszNewFile)
6583{
6584 WCHAR *wold = NULL;
6585 WCHAR *wnew = NULL;
6586 int retval = -1;
6587
6588 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6589 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6590 if (wold != NULL && wnew != NULL)
6591 retval = mch_wrename(wold, wnew);
6592 vim_free(wold);
6593 vim_free(wnew);
6594 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
6597/*
6598 * Get the default shell for the current hardware platform
6599 */
6600 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006601default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006603 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
6605
6606/*
6607 * mch_access() extends access() to do more detailed check on network drives.
6608 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6609 */
6610 int
6611mch_access(char *n, int p)
6612{
6613 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006614 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006615 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006617 wn = enc_to_utf16((char_u *)n, NULL);
6618 if (wn == NULL)
6619 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006621 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 if (p & R_OK)
6626 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006627 // Read check is performed by seeing if we can do a find file on
6628 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006629 int i;
6630 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006632 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6633 TempNameW[i] = wn[i];
6634 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6635 TempNameW[i++] = '\\';
6636 TempNameW[i++] = '*';
6637 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006639 hFile = FindFirstFileW(TempNameW, &d);
6640 if (hFile == INVALID_HANDLE_VALUE)
6641 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645
6646 if (p & W_OK)
6647 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006648 // Trying to create a temporary file in the directory should catch
6649 // directories on read-only network shares. However, in
6650 // directories whose ACL allows writes but denies deletes will end
6651 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006652 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6653 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006654 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006655 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657 }
6658 else
6659 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006660 // Don't consider a file read-only if another process has opened it.
6661 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6662
Bram Moolenaar0f873732019-12-05 20:28:46 +01006663 // Trying to open the file for the required access does ACL, read-only
6664 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006665 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6666 | ((p & R_OK) ? GENERIC_READ : 0);
6667
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006668 hFile = CreateFileW(wn, access_mode, share_mode,
6669 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (hFile == INVALID_HANDLE_VALUE)
6671 goto getout;
6672 CloseHandle(hFile);
6673 }
6674
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 return retval;
6679}
6680
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006682 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 */
6684 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006685mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 WCHAR *wn;
6688 int f;
6689
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006690 wn = enc_to_utf16((char_u *)name, NULL);
6691 if (wn == NULL)
6692 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006694 f = _wopen(wn, flags, mode);
6695 vim_free(wn);
6696 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
6699/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006700 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006703mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 WCHAR *wn, *wm;
6706 FILE *f = NULL;
6707
Bram Moolenaara12a1612019-01-24 16:39:02 +01006708#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006709 // Work around an annoying assertion in the Microsoft debug CRT
6710 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006711 char newMode = mode[strlen(mode) - 1];
6712 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006713
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006714 _get_fmode(&oldMode);
6715 if (newMode == 't')
6716 _set_fmode(_O_TEXT);
6717 else if (newMode == 'b')
6718 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006719#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006720 wn = enc_to_utf16((char_u *)name, NULL);
6721 wm = enc_to_utf16((char_u *)mode, NULL);
6722 if (wn != NULL && wm != NULL)
6723 f = _wfopen(wn, wm);
6724 vim_free(wn);
6725 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006726
Bram Moolenaara12a1612019-01-24 16:39:02 +01006727#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006728 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006729#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006730 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733/*
6734 * SUB STREAM (aka info stream) handling:
6735 *
6736 * NTFS can have sub streams for each file. Normal contents of file is
6737 * stored in the main stream, and extra contents (author information and
6738 * title and so on) can be stored in sub stream. After Windows 2000, user
6739 * can access and store those informations in sub streams via explorer's
6740 * property menuitem in right click menu. Those informations in sub streams
6741 * were lost when copying only the main stream. So we have to copy sub
6742 * streams.
6743 *
6744 * Incomplete explanation:
6745 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6746 * More useful info and an example:
6747 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6748 */
6749
6750/*
6751 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6752 * and write to stream "substream" of file "to".
6753 * Errors are ignored.
6754 */
6755 static void
6756copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6757{
6758 HANDLE hTo;
6759 WCHAR *to_name;
6760
6761 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6762 wcscpy(to_name, to);
6763 wcscat(to_name, substream);
6764
6765 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6766 FILE_ATTRIBUTE_NORMAL, NULL);
6767 if (hTo != INVALID_HANDLE_VALUE)
6768 {
6769 long done;
6770 DWORD todo;
6771 DWORD readcnt, written;
6772 char buf[4096];
6773
Bram Moolenaar0f873732019-12-05 20:28:46 +01006774 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 for (done = 0; done < len; done += written)
6776 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006777 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006778 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6779 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6781 FALSE, FALSE, context)
6782 || readcnt != todo
6783 || !WriteFile(hTo, buf, todo, &written, NULL)
6784 || written != todo)
6785 break;
6786 }
6787 CloseHandle(hTo);
6788 }
6789
6790 free(to_name);
6791}
6792
6793/*
6794 * Copy info streams from file "from" to file "to".
6795 */
6796 static void
6797copy_infostreams(char_u *from, char_u *to)
6798{
6799 WCHAR *fromw;
6800 WCHAR *tow;
6801 HANDLE sh;
6802 WIN32_STREAM_ID sid;
6803 int headersize;
6804 WCHAR streamname[_MAX_PATH];
6805 DWORD readcount;
6806 void *context = NULL;
6807 DWORD lo, hi;
6808 int len;
6809
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006811 fromw = enc_to_utf16(from, NULL);
6812 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (fromw != NULL && tow != NULL)
6814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6817 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6818 if (sh != INVALID_HANDLE_VALUE)
6819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 // Use BackupRead() to find the info streams. Repeat until we
6821 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 for (;;)
6823 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006824 // Get the header to find the length of the stream name. If
6825 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006827 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6829 &readcount, FALSE, FALSE, &context)
6830 || readcount == 0)
6831 break;
6832
Bram Moolenaar0f873732019-12-05 20:28:46 +01006833 // We only deal with streams that have a name. The normal
6834 // file data appears to be without a name, even though docs
6835 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (sid.dwStreamNameSize > 0)
6837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (!BackupRead(sh, (LPBYTE)streamname,
6840 sid.dwStreamNameSize,
6841 &readcount, FALSE, FALSE, &context))
6842 break;
6843
Bram Moolenaar0f873732019-12-05 20:28:46 +01006844 // Copy an info stream with a name ":anything:$DATA".
6845 // Skip "::$DATA", it has no stream name (examples suggest
6846 // it might be used for the normal file contents).
6847 // Note that BackupRead() counts bytes, but the name is in
6848 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 len = readcount / sizeof(WCHAR);
6850 streamname[len] = 0;
6851 if (len > 7 && wcsicmp(streamname + len - 6,
6852 L":$DATA") == 0)
6853 {
6854 streamname[len - 6] = 0;
6855 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006856 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
6858 }
6859
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 // Advance to the next stream. We might try seeking too far,
6861 // but BackupSeek() doesn't skip over stream borders, thus
6862 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 &lo, &hi, &context);
6865 }
6866
Bram Moolenaar0f873732019-12-05 20:28:46 +01006867 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6869
6870 CloseHandle(sh);
6871 }
6872 }
6873 vim_free(fromw);
6874 vim_free(tow);
6875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877/*
6878 * Copy file attributes from file "from" to file "to".
6879 * For Windows NT and later we copy info streams.
6880 * Always returns zero, errors are ignored.
6881 */
6882 int
6883mch_copy_file_attribute(char_u *from, char_u *to)
6884{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006885 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006886 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 return 0;
6888}
6889
6890#if defined(MYRESETSTKOFLW) || defined(PROTO)
6891/*
6892 * Recreate a destroyed stack guard page in win32.
6893 * Written by Benjamin Peterson.
6894 */
6895
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006897# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
6899/*
6900 * This function does the same thing as _resetstkoflw(), which is only
6901 * available in DevStudio .net and later.
6902 * Returns 0 for failure, 1 for success.
6903 */
6904 int
6905myresetstkoflw(void)
6906{
6907 BYTE *pStackPtr;
6908 BYTE *pGuardPage;
6909 BYTE *pStackBase;
6910 BYTE *pLowestPossiblePage;
6911 MEMORY_BASIC_INFORMATION mbi;
6912 SYSTEM_INFO si;
6913 DWORD nPageSize;
6914 DWORD dummy;
6915
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 GetSystemInfo(&si);
6918 nPageSize = si.dwPageSize;
6919
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 pStackPtr = (BYTE*)_alloca(1);
6922
Bram Moolenaar0f873732019-12-05 20:28:46 +01006923 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6925 return 0;
6926 pStackBase = (BYTE*)mbi.AllocationBase;
6927
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 // ...and the page thats min_stack_req pages away from stack base; this is
6929 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006930 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 // We want the first committed page in the stack Start at the stack
6934 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 BYTE *pBlock = pStackBase;
6936
Bram Moolenaara466c992005-07-09 21:03:22 +00006937 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
6939 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6940 return 0;
6941
6942 pBlock += mbi.RegionSize;
6943
6944 if (mbi.State & MEM_COMMIT)
6945 break;
6946 }
6947
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 if (mbi.Protect & PAGE_GUARD)
6950 return 1;
6951
Bram Moolenaar0f873732019-12-05 20:28:46 +01006952 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6954 pGuardPage = pLowestPossiblePage;
6955 else
6956 pGuardPage = (BYTE*)mbi.BaseAddress;
6957
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6960 return 0;
6961
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6964 &dummy))
6965 return 0;
6966 }
6967
6968 return 1;
6969}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006973/*
6974 * The command line arguments in UCS2
6975 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006976static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006977static LPWSTR *ArglistW = NULL;
6978static int global_argc = 0;
6979static char **global_argv;
6980
Bram Moolenaar0f873732019-12-05 20:28:46 +01006981static int used_file_argc = 0; // last argument in global_argv[] used
6982 // for the argument list.
6983static int *used_file_indexes = NULL; // indexes in global_argv[] for
6984 // command line arguments added to
6985 // the argument list
6986static int used_file_count = 0; // nr of entries in used_file_indexes
6987static int used_file_literal = FALSE; // take file names literally
6988static int used_file_full_path = FALSE; // file name was full path
6989static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006990static int used_alist_count = 0;
6991
6992
6993/*
6994 * Get the command line arguments. Unicode version.
6995 * Returns argc. Zero when something fails.
6996 */
6997 int
6998get_cmd_argsW(char ***argvp)
6999{
7000 char **argv = NULL;
7001 int argc = 0;
7002 int i;
7003
Bram Moolenaar14993322014-09-09 12:25:33 +02007004 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007005 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7006 if (ArglistW != NULL)
7007 {
7008 argv = malloc((nArgsW + 1) * sizeof(char *));
7009 if (argv != NULL)
7010 {
7011 argc = nArgsW;
7012 argv[argc] = NULL;
7013 for (i = 0; i < argc; ++i)
7014 {
7015 int len;
7016
Bram Moolenaar0f873732019-12-05 20:28:46 +01007017 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007018 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007019 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007020 (LPSTR *)&argv[i], &len, 0, 0);
7021 if (argv[i] == NULL)
7022 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007024 while (i > 0)
7025 free(argv[--i]);
7026 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007027 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007028 argc = 0;
7029 }
7030 }
7031 }
7032 }
7033
7034 global_argc = argc;
7035 global_argv = argv;
7036 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007037 {
7038 if (used_file_indexes != NULL)
7039 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007040 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007041 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007042
7043 if (argvp != NULL)
7044 *argvp = argv;
7045 return argc;
7046}
7047
7048 void
7049free_cmd_argsW(void)
7050{
7051 if (ArglistW != NULL)
7052 {
7053 GlobalFree(ArglistW);
7054 ArglistW = NULL;
7055 }
7056}
7057
7058/*
7059 * Remember "name" is an argument that was added to the argument list.
7060 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7061 * is called.
7062 */
7063 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007064used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007065{
7066 int i;
7067
7068 if (used_file_indexes == NULL)
7069 return;
7070 for (i = used_file_argc + 1; i < global_argc; ++i)
7071 if (STRCMP(global_argv[i], name) == 0)
7072 {
7073 used_file_argc = i;
7074 used_file_indexes[used_file_count++] = i;
7075 break;
7076 }
7077 used_file_literal = literal;
7078 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007079 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007080}
7081
7082/*
7083 * Remember the length of the argument list as it was. If it changes then we
7084 * leave it alone when 'encoding' is set.
7085 */
7086 void
7087set_alist_count(void)
7088{
7089 used_alist_count = GARGCOUNT;
7090}
7091
7092/*
7093 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7094 * has been changed while starting up. Use the UCS-2 command line arguments
7095 * and convert them to 'encoding'.
7096 */
7097 void
7098fix_arg_enc(void)
7099{
7100 int i;
7101 int idx;
7102 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007103 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007104
Bram Moolenaar0f873732019-12-05 20:28:46 +01007105 // Safety checks:
7106 // - if argument count differs between the wide and non-wide argument
7107 // list, something must be wrong.
7108 // - the file name arguments must have been located.
7109 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007110 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007111 || ArglistW == NULL
7112 || used_file_indexes == NULL
7113 || used_file_count == 0
7114 || used_alist_count != GARGCOUNT)
7115 return;
7116
Bram Moolenaar0f873732019-12-05 20:28:46 +01007117 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007118 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007119 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007121 for (i = 0; i < GARGCOUNT; ++i)
7122 fnum_list[i] = GARGLIST[i].ae_fnum;
7123
Bram Moolenaar0f873732019-12-05 20:28:46 +01007124 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007125 alist_clear(&global_alist);
7126 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007127 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007128
7129 for (i = 0; i < used_file_count; ++i)
7130 {
7131 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007132 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007133 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007134 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007135 int literal = used_file_literal;
7136
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007137#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 // When using diff mode may need to concatenate file name to
7139 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007140 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7141 && !mch_isdir(alist_name(&GARGLIST[0])))
7142 {
7143 char_u *r;
7144
7145 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7146 if (r != NULL)
7147 {
7148 vim_free(str);
7149 str = r;
7150 }
7151 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007152#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007153 // Re-use the old buffer by renaming it. When not using literal
7154 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007155 if (used_file_literal)
7156 buf_set_name(fnum_list[i], str);
7157
Bram Moolenaar0f873732019-12-05 20:28:46 +01007158 // Check backtick literal. backtick literal is already expanded in
7159 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007160 if (literal == FALSE)
7161 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007162 size_t len = STRLEN(str);
7163
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007164 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7165 literal = TRUE;
7166 }
7167 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007168 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007169 }
7170
7171 if (!used_file_literal)
7172 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007173 // Now expand wildcards in the arguments.
7174 // Temporarily add '(' and ')' to 'isfname'. These are valid
7175 // filename characters but are excluded from 'isfname' to make
7176 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7177 // Also, unset wildignore to not be influenced by this option.
7178 // The arguments specified in command-line should be kept even if
7179 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007180 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007181 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007182 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007183 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007184 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007185 }
7186
Bram Moolenaar0f873732019-12-05 20:28:46 +01007187 // If wildcard expansion failed, we are editing the first file of the
7188 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007189 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7190 {
7191 do_cmdline_cmd((char_u *)":rewind");
7192 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007193 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007194 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007195
7196 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007197}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007198
7199 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007200mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007201{
7202 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007203 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007204
Bram Moolenaar964b3742019-05-24 18:54:09 +02007205 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007206 if (envbuf == NULL)
7207 return -1;
7208
7209 sprintf((char *)envbuf, "%s=%s", var, value);
7210
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007211 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007212
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007213 vim_free(envbuf);
7214 if (p == NULL)
7215 return -1;
7216 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007217#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007218 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007219#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007220 // Unlike Un*x systems, we can free the string for _wputenv().
7221 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007222
7223 return 0;
7224}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007225
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007226/*
7227 * Support for 256 colors and 24-bit colors was added in Windows 10
7228 * version 1703 (Creators update).
7229 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007230#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7231
7232/*
7233 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007234 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007235 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007236#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007237
7238/*
7239 * ConPTY differences between versions, need different logic.
7240 * version 1903 (May 2019 update).
7241 */
7242#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7243
7244/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007245 * version 1909 (November 2019 update).
7246 */
7247#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7248
7249/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007250 * Confirm until this version. Also the logic changes.
7251 * insider preview.
7252 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007253#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007254
7255/*
7256 * Not stable now.
7257 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007258#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007259
7260 static void
7261vtp_flag_init(void)
7262{
7263 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007264#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007265 DWORD mode;
7266 HANDLE out;
7267
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007268# ifdef VIMDLL
7269 if (!gui.in_use)
7270# endif
7271 {
7272 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007273
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007274 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7275 GetConsoleMode(out, &mode);
7276 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7277 if (SetConsoleMode(out, mode) == 0)
7278 vtp_working = 0;
7279 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007280#endif
7281
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007282 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007283 conpty_working = 1;
7284 if (ver >= CONPTY_STABLE_BUILD)
7285 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007286
Bram Moolenaar57da6982019-09-13 22:30:11 +02007287 if (ver <= CONPTY_INSIDER_BUILD)
7288 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007289 if (ver <= CONPTY_1909_BUILD)
7290 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007291 if (ver <= CONPTY_1903_BUILD)
7292 conpty_type = 2;
7293 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7294 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007295}
7296
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007297#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007298
7299 static void
7300vtp_init(void)
7301{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007302 HMODULE hKerneldll;
7303 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007304# ifdef FEAT_TERMGUICOLORS
7305 COLORREF fg, bg;
7306# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007307
Bram Moolenaar0f873732019-12-05 20:28:46 +01007308 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007309 hKerneldll = GetModuleHandle("kernel32.dll");
7310 if (hKerneldll != NULL)
7311 {
7312 pGetConsoleScreenBufferInfoEx =
7313 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7314 hKerneldll, "GetConsoleScreenBufferInfoEx");
7315 pSetConsoleScreenBufferInfoEx =
7316 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7317 hKerneldll, "SetConsoleScreenBufferInfoEx");
7318 if (pGetConsoleScreenBufferInfoEx != NULL
7319 && pSetConsoleScreenBufferInfoEx != NULL)
7320 has_csbiex = TRUE;
7321 }
7322
7323 csbi.cbSize = sizeof(csbi);
7324 if (has_csbiex)
7325 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007326 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7327 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
7328
7329# ifdef FEAT_TERMGUICOLORS
7330 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7331 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7332 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7333 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7334 default_console_color_bg = bg;
7335 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007336# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007337
7338 set_console_color_rgb();
7339}
7340
7341 static void
7342vtp_exit(void)
7343{
7344 reset_console_color_rgb();
7345}
7346
7347 static int
7348vtp_printf(
7349 char *format,
7350 ...)
7351{
7352 char_u buf[100];
7353 va_list list;
7354 DWORD result;
7355
7356 va_start(list, format);
7357 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7358 va_end(list);
7359 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7360 return (int)result;
7361}
7362
7363 static void
7364vtp_sgr_bulk(
7365 int arg)
7366{
7367 int args[1];
7368
7369 args[0] = arg;
7370 vtp_sgr_bulks(1, args);
7371}
7372
7373 static void
7374vtp_sgr_bulks(
7375 int argc,
7376 int *args
7377)
7378{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007379 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007380 char_u buf[2 + (4 * 16) + 1];
7381 char_u *p;
7382 int i;
7383
7384 p = buf;
7385 *p++ = '\033';
7386 *p++ = '[';
7387
7388 for (i = 0; i < argc; ++i)
7389 {
7390 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7391 *p++ = ';';
7392 }
7393 p--;
7394 *p++ = 'm';
7395 *p = NUL;
7396 vtp_printf((char *)buf);
7397}
7398
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007399# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007400 static int
7401ctermtoxterm(
7402 int cterm)
7403{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007404 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007405
7406 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7407 return (((int)r << 16) | ((int)g << 8) | (int)b);
7408}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007409# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007410
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007411 static void
7412set_console_color_rgb(void)
7413{
7414# ifdef FEAT_TERMGUICOLORS
7415 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007416 guicolor_T fg, bg;
7417 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007418
7419 if (!USE_VTP)
7420 return;
7421
Bram Moolenaara050b942019-12-02 21:35:31 +01007422 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7423
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007424 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7425 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7426
7427 csbi.cbSize = sizeof(csbi);
7428 if (has_csbiex)
7429 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7430
7431 csbi.cbSize = sizeof(csbi);
7432 csbi.srWindow.Right += 1;
7433 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007434 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7435 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007436 if (has_csbiex)
7437 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7438# endif
7439}
7440
Bram Moolenaara050b942019-12-02 21:35:31 +01007441# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7442 void
7443get_default_console_color(
7444 int *cterm_fg,
7445 int *cterm_bg,
7446 guicolor_T *gui_fg,
7447 guicolor_T *gui_bg)
7448{
7449 int id;
7450 guicolor_T guifg = INVALCOLOR;
7451 guicolor_T guibg = INVALCOLOR;
7452 int ctermfg = 0;
7453 int ctermbg = 0;
7454
7455 id = syn_name2id((char_u *)"Normal");
7456 if (id > 0 && p_tgc)
7457 syn_id2colors(id, &guifg, &guibg);
7458 if (guifg == INVALCOLOR)
7459 {
7460 ctermfg = -1;
7461 if (id > 0)
7462 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7463 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7464 : default_console_color_fg;
7465 cterm_normal_fg_gui_color = guifg;
7466 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7467 }
7468 if (guibg == INVALCOLOR)
7469 {
7470 ctermbg = -1;
7471 if (id > 0)
7472 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7473 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7474 : default_console_color_bg;
7475 cterm_normal_bg_gui_color = guibg;
7476 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7477 }
7478
7479 *cterm_fg = ctermfg;
7480 *cterm_bg = ctermbg;
7481 *gui_fg = guifg;
7482 *gui_bg = guibg;
7483}
7484# endif
7485
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007486 static void
7487reset_console_color_rgb(void)
7488{
7489# ifdef FEAT_TERMGUICOLORS
7490 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7491
7492 csbi.cbSize = sizeof(csbi);
7493 if (has_csbiex)
7494 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7495
7496 csbi.cbSize = sizeof(csbi);
7497 csbi.srWindow.Right += 1;
7498 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007499 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7500 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007501 if (has_csbiex)
7502 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7503# endif
7504}
7505
7506 void
7507control_console_color_rgb(void)
7508{
7509 if (USE_VTP)
7510 set_console_color_rgb();
7511 else
7512 reset_console_color_rgb();
7513}
7514
7515 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007516use_vtp(void)
7517{
7518 return USE_VTP;
7519}
7520
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007521 int
7522is_term_win32(void)
7523{
7524 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7525}
7526
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007527 int
7528has_vtp_working(void)
7529{
7530 return vtp_working;
7531}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007532
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007533#endif
7534
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007535 int
7536has_conpty_working(void)
7537{
7538 return conpty_working;
7539}
7540
7541 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007542get_conpty_type(void)
7543{
7544 return conpty_type;
7545}
7546
7547 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007548is_conpty_stable(void)
7549{
7550 return conpty_stable;
7551}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007552
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007553#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007554 void
7555resize_console_buf(void)
7556{
7557 CONSOLE_SCREEN_BUFFER_INFO csbi;
7558 COORD coord;
7559 SMALL_RECT newsize;
7560
7561 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7562 {
7563 coord.X = SRWIDTH(csbi.srWindow);
7564 coord.Y = SRHEIGHT(csbi.srWindow);
7565 SetConsoleScreenBufferSize(g_hConOut, coord);
7566
7567 newsize.Left = 0;
7568 newsize.Top = 0;
7569 newsize.Right = coord.X - 1;
7570 newsize.Bottom = coord.Y - 1;
7571 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7572
7573 SetConsoleScreenBufferSize(g_hConOut, coord);
7574 }
7575}
7576#endif