Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 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 |
| 13 | * the console version only, so there is a lot of "#ifndef FEAT_GUI_W32". |
| 14 | * |
| 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | #include "vim.h" |
| 24 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 25 | #ifdef FEAT_MZSCHEME |
| 26 | # include "if_mzsch.h" |
| 27 | #endif |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <limits.h> |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 32 | |
| 33 | /* cproto fails on missing include files */ |
| 34 | #ifndef PROTO |
| 35 | # include <process.h> |
| 36 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | |
| 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 Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 47 | #ifndef PROTO |
| 48 | # if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32) |
| 49 | # include <shellapi.h> |
| 50 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #ifdef __MINGW32__ |
| 54 | # ifndef FROM_LEFT_1ST_BUTTON_PRESSED |
| 55 | # define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001 |
| 56 | # endif |
| 57 | # ifndef RIGHTMOST_BUTTON_PRESSED |
| 58 | # define RIGHTMOST_BUTTON_PRESSED 0x0002 |
| 59 | # endif |
| 60 | # ifndef FROM_LEFT_2ND_BUTTON_PRESSED |
| 61 | # define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004 |
| 62 | # endif |
| 63 | # ifndef FROM_LEFT_3RD_BUTTON_PRESSED |
| 64 | # define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008 |
| 65 | # endif |
| 66 | # ifndef FROM_LEFT_4TH_BUTTON_PRESSED |
| 67 | # define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010 |
| 68 | # endif |
| 69 | |
| 70 | /* |
| 71 | * EventFlags |
| 72 | */ |
| 73 | # ifndef MOUSE_MOVED |
| 74 | # define MOUSE_MOVED 0x0001 |
| 75 | # endif |
| 76 | # ifndef DOUBLE_CLICK |
| 77 | # define DOUBLE_CLICK 0x0002 |
| 78 | # endif |
| 79 | #endif |
| 80 | |
| 81 | /* Record all output and all keyboard & mouse input */ |
| 82 | /* #define MCH_WRITE_DUMP */ |
| 83 | |
| 84 | #ifdef MCH_WRITE_DUMP |
| 85 | FILE* fdDump = NULL; |
| 86 | #endif |
| 87 | |
| 88 | /* |
| 89 | * When generating prototypes for Win32 on Unix, these lines make the syntax |
| 90 | * errors disappear. They do not need to be correct. |
| 91 | */ |
| 92 | #ifdef PROTO |
| 93 | #define WINAPI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | typedef char * LPCSTR; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 95 | typedef char * LPWSTR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | typedef int ACCESS_MASK; |
| 97 | typedef int BOOL; |
| 98 | typedef int COLORREF; |
| 99 | typedef int CONSOLE_CURSOR_INFO; |
| 100 | typedef int COORD; |
| 101 | typedef int DWORD; |
| 102 | typedef int HANDLE; |
Bram Moolenaar | ef26954 | 2016-01-19 13:22:12 +0100 | [diff] [blame] | 103 | typedef int LPHANDLE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | typedef int HDC; |
| 105 | typedef int HFONT; |
| 106 | typedef int HICON; |
| 107 | typedef int HINSTANCE; |
| 108 | typedef int HWND; |
| 109 | typedef int INPUT_RECORD; |
| 110 | typedef int KEY_EVENT_RECORD; |
| 111 | typedef int LOGFONT; |
| 112 | typedef int LPBOOL; |
| 113 | typedef int LPCTSTR; |
| 114 | typedef int LPDWORD; |
| 115 | typedef int LPSTR; |
| 116 | typedef int LPTSTR; |
| 117 | typedef int LPVOID; |
| 118 | typedef int MOUSE_EVENT_RECORD; |
| 119 | typedef int PACL; |
| 120 | typedef int PDWORD; |
| 121 | typedef int PHANDLE; |
| 122 | typedef int PRINTDLG; |
| 123 | typedef int PSECURITY_DESCRIPTOR; |
| 124 | typedef int PSID; |
| 125 | typedef int SECURITY_INFORMATION; |
| 126 | typedef int SHORT; |
| 127 | typedef int SMALL_RECT; |
| 128 | typedef int TEXTMETRIC; |
| 129 | typedef int TOKEN_INFORMATION_CLASS; |
| 130 | typedef int TRUSTEE; |
| 131 | typedef int WORD; |
| 132 | typedef int WCHAR; |
| 133 | typedef void VOID; |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 134 | typedef int BY_HANDLE_FILE_INFORMATION; |
Bram Moolenaar | 32ac8cd | 2013-07-03 18:49:17 +0200 | [diff] [blame] | 135 | typedef int SE_OBJECT_TYPE; |
| 136 | typedef int PSNSECINFO; |
| 137 | typedef int PSNSECINFOW; |
Bram Moolenaar | b8e0bdb | 2014-11-12 16:10:48 +0100 | [diff] [blame] | 138 | typedef int STARTUPINFO; |
| 139 | typedef int PROCESS_INFORMATION; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | #ifndef FEAT_GUI_W32 |
| 143 | /* Undocumented API in kernel32.dll needed to work around dead key bug in |
| 144 | * console-mode applications in NT 4.0. If you switch keyboard layouts |
| 145 | * in a console app to a layout that includes dead keys and then hit a |
| 146 | * dead key, a call to ToAscii will trash the stack. My thanks to Ian James |
| 147 | * and Michael Dietrich for helping me figure out this workaround. |
| 148 | */ |
| 149 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 150 | /* WINAPI BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR); */ |
| 151 | #ifndef WINAPI |
| 152 | # define WINAPI __stdcall |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | #endif |
| 154 | #if defined(__BORLANDC__) |
| 155 | typedef BOOL (__stdcall *PFNGCKLN)(LPSTR); |
| 156 | #else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 157 | typedef BOOL (WINAPI *PFNGCKLN)(LPSTR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | #endif |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 159 | static PFNGCKLN s_pfnGetConsoleKeyboardLayoutName = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | #if defined(__BORLANDC__) |
| 163 | /* Strangely Borland uses a non-standard name. */ |
| 164 | # define wcsicmp(a, b) wcscmpi((a), (b)) |
| 165 | #endif |
| 166 | |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 167 | #ifndef PROTO |
| 168 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 169 | /* Enable common dialogs input unicode from IME if possible. */ |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 170 | #ifdef FEAT_MBYTE |
Bram Moolenaar | af62ff3 | 2013-03-19 14:48:29 +0100 | [diff] [blame] | 171 | LRESULT (WINAPI *pDispatchMessage)(CONST MSG *) = DispatchMessage; |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 172 | BOOL (WINAPI *pGetMessage)(LPMSG, HWND, UINT, UINT) = GetMessage; |
| 173 | BOOL (WINAPI *pIsDialogMessage)(HWND, LPMSG) = IsDialogMessage; |
| 174 | BOOL (WINAPI *pPeekMessage)(LPMSG, HWND, UINT, UINT, UINT) = PeekMessage; |
| 175 | #endif |
| 176 | |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 177 | #endif /* PROTO */ |
| 178 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 179 | #ifndef FEAT_GUI_W32 |
| 180 | /* Win32 Console handles for input and output */ |
| 181 | static HANDLE g_hConIn = INVALID_HANDLE_VALUE; |
| 182 | static HANDLE g_hConOut = INVALID_HANDLE_VALUE; |
| 183 | |
| 184 | /* Win32 Screen buffer,coordinate,console I/O information */ |
| 185 | static SMALL_RECT g_srScrollRegion; |
| 186 | static COORD g_coord; /* 0-based, but external coords are 1-based */ |
| 187 | |
| 188 | /* The attribute of the screen when the editor was started */ |
| 189 | static WORD g_attrDefault = 7; /* lightgray text on black background */ |
| 190 | static WORD g_attrCurrent; |
| 191 | |
| 192 | static int g_fCBrkPressed = FALSE; /* set by ctrl-break interrupt */ |
| 193 | static int g_fCtrlCPressed = FALSE; /* set when ctrl-C or ctrl-break detected */ |
| 194 | static int g_fForceExit = FALSE; /* set when forcefully exiting */ |
| 195 | |
| 196 | static void termcap_mode_start(void); |
| 197 | static void termcap_mode_end(void); |
| 198 | static void clear_chars(COORD coord, DWORD n); |
| 199 | static void clear_screen(void); |
| 200 | static void clear_to_end_of_display(void); |
| 201 | static void clear_to_end_of_line(void); |
| 202 | static void scroll(unsigned cLines); |
| 203 | static void set_scroll_region(unsigned left, unsigned top, |
| 204 | unsigned right, unsigned bottom); |
| 205 | static void insert_lines(unsigned cLines); |
| 206 | static void delete_lines(unsigned cLines); |
| 207 | static void gotoxy(unsigned x, unsigned y); |
| 208 | static void normvideo(void); |
| 209 | static void textattr(WORD wAttr); |
| 210 | static void textcolor(WORD wAttr); |
| 211 | static void textbackground(WORD wAttr); |
| 212 | static void standout(void); |
| 213 | static void standend(void); |
| 214 | static void visual_bell(void); |
| 215 | static void cursor_visible(BOOL fVisible); |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 216 | static DWORD write_chars(char_u *pchBuf, DWORD cbToWrite); |
| 217 | static WCHAR tgetch(int *pmodifiers, WCHAR *pch2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | static void create_conin(void); |
| 219 | static int s_cursor_visible = TRUE; |
| 220 | static int did_create_conin = FALSE; |
| 221 | #else |
| 222 | static int s_dont_use_vimrun = TRUE; |
| 223 | static int need_vimrun_warning = FALSE; |
| 224 | static char *vimrun_path = "vimrun "; |
| 225 | #endif |
| 226 | |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 227 | static int win32_getattrs(char_u *name); |
| 228 | static int win32_setattrs(char_u *name, int attrs); |
| 229 | static int win32_set_archive(char_u *name); |
| 230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | #ifndef FEAT_GUI_W32 |
| 232 | static int suppress_winsize = 1; /* don't fiddle with console */ |
| 233 | #endif |
| 234 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 235 | static char_u *exe_path = NULL; |
| 236 | |
Bram Moolenaar | f50eb78 | 2014-02-05 13:36:54 +0100 | [diff] [blame] | 237 | static BOOL win8_or_later = FALSE; |
| 238 | |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 239 | #ifndef FEAT_GUI_W32 |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 240 | /* |
| 241 | * Version of ReadConsoleInput() that works with IME. |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 242 | * Works around problems on Windows 8. |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 243 | */ |
| 244 | static BOOL |
| 245 | read_console_input( |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 246 | HANDLE hInput, |
| 247 | INPUT_RECORD *lpBuffer, |
| 248 | DWORD nLength, |
| 249 | LPDWORD lpEvents) |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 250 | { |
| 251 | enum |
| 252 | { |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 253 | IRSIZE = 10 |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 254 | }; |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 255 | static INPUT_RECORD s_irCache[IRSIZE]; |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 256 | static DWORD s_dwIndex = 0; |
| 257 | static DWORD s_dwMax = 0; |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 258 | DWORD dwEvents; |
Bram Moolenaar | dd415a6 | 2014-02-05 14:02:27 +0100 | [diff] [blame] | 259 | int head; |
| 260 | int tail; |
| 261 | int i; |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 262 | |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 263 | if (nLength == -2) |
| 264 | return (s_dwMax > 0) ? TRUE : FALSE; |
| 265 | |
Bram Moolenaar | f50eb78 | 2014-02-05 13:36:54 +0100 | [diff] [blame] | 266 | if (!win8_or_later) |
| 267 | { |
| 268 | if (nLength == -1) |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 269 | return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents); |
| 270 | return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents); |
Bram Moolenaar | f50eb78 | 2014-02-05 13:36:54 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 273 | if (s_dwMax == 0) |
| 274 | { |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 275 | if (nLength == -1) |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 276 | return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents); |
| 277 | if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents)) |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 278 | return FALSE; |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 279 | s_dwIndex = 0; |
| 280 | s_dwMax = dwEvents; |
| 281 | if (dwEvents == 0) |
| 282 | { |
| 283 | *lpEvents = 0; |
| 284 | return TRUE; |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 285 | } |
Bram Moolenaar | dd415a6 | 2014-02-05 14:02:27 +0100 | [diff] [blame] | 286 | |
| 287 | if (s_dwMax > 1) |
| 288 | { |
| 289 | head = 0; |
| 290 | tail = s_dwMax - 1; |
| 291 | while (head != tail) |
| 292 | { |
| 293 | if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT |
| 294 | && s_irCache[head + 1].EventType |
| 295 | == WINDOW_BUFFER_SIZE_EVENT) |
| 296 | { |
| 297 | /* Remove duplicate event to avoid flicker. */ |
| 298 | for (i = head; i < tail; ++i) |
| 299 | s_irCache[i] = s_irCache[i + 1]; |
| 300 | --tail; |
| 301 | continue; |
| 302 | } |
| 303 | head++; |
| 304 | } |
| 305 | s_dwMax = tail + 1; |
| 306 | } |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 307 | } |
Bram Moolenaar | dd415a6 | 2014-02-05 14:02:27 +0100 | [diff] [blame] | 308 | |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 309 | *lpBuffer = s_irCache[s_dwIndex]; |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 310 | if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax) |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 311 | s_dwMax = 0; |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 312 | *lpEvents = 1; |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 313 | return TRUE; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Version of PeekConsoleInput() that works with IME. |
| 318 | */ |
| 319 | static BOOL |
| 320 | peek_console_input( |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 321 | HANDLE hInput, |
| 322 | INPUT_RECORD *lpBuffer, |
| 323 | DWORD nLength, |
| 324 | LPDWORD lpEvents) |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 325 | { |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 326 | return read_console_input(hInput, lpBuffer, -1, lpEvents); |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 329 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 330 | static DWORD |
| 331 | msg_wait_for_multiple_objects( |
| 332 | DWORD nCount, |
| 333 | LPHANDLE pHandles, |
| 334 | BOOL fWaitAll, |
| 335 | DWORD dwMilliseconds, |
| 336 | DWORD dwWakeMask) |
| 337 | { |
| 338 | if (read_console_input(NULL, NULL, -2, NULL)) |
| 339 | return WAIT_OBJECT_0; |
| 340 | return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll, |
| 341 | dwMilliseconds, dwWakeMask); |
| 342 | } |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 343 | # endif |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 344 | |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 345 | # ifndef FEAT_CLIENTSERVER |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 346 | static DWORD |
| 347 | wait_for_single_object( |
| 348 | HANDLE hHandle, |
| 349 | DWORD dwMilliseconds) |
| 350 | { |
| 351 | if (read_console_input(NULL, NULL, -2, NULL)) |
| 352 | return WAIT_OBJECT_0; |
| 353 | return WaitForSingleObject(hHandle, dwMilliseconds); |
| 354 | } |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 355 | # endif |
| 356 | #endif |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 357 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | static void |
| 359 | get_exe_name(void) |
| 360 | { |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 361 | /* Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned |
| 362 | * as the maximum length that works (plus a NUL byte). */ |
| 363 | #define MAX_ENV_PATH_LEN 8192 |
| 364 | char temp[MAX_ENV_PATH_LEN]; |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 365 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 366 | |
| 367 | if (exe_name == NULL) |
| 368 | { |
| 369 | /* store the name of the executable, may be used for $VIM */ |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 370 | GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | if (*temp != NUL) |
| 372 | exe_name = FullName_save((char_u *)temp, FALSE); |
| 373 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 374 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 375 | if (exe_path == NULL && exe_name != NULL) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 376 | { |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 377 | exe_path = vim_strnsave(exe_name, |
| 378 | (int)(gettail_sep(exe_name) - exe_name)); |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 379 | if (exe_path != NULL) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 380 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 381 | /* Append our starting directory to $PATH, so that when doing |
| 382 | * "!xxd" it's found in our starting directory. Needed because |
| 383 | * SearchPath() also looks there. */ |
| 384 | p = mch_getenv("PATH"); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 385 | if (p == NULL |
| 386 | || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN) |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 387 | { |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 388 | if (p == NULL || *p == NUL) |
| 389 | temp[0] = NUL; |
| 390 | else |
| 391 | { |
| 392 | STRCPY(temp, p); |
| 393 | STRCAT(temp, ";"); |
| 394 | } |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 395 | STRCAT(temp, exe_path); |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 396 | vim_setenv((char_u *)"PATH", (char_u *)temp); |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 397 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 398 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 399 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 402 | /* |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 403 | * Unescape characters in "p" that appear in "escaped". |
| 404 | */ |
| 405 | static void |
| 406 | unescape_shellxquote(char_u *p, char_u *escaped) |
| 407 | { |
Bram Moolenaar | 4336cdf | 2012-02-29 13:58:47 +0100 | [diff] [blame] | 408 | int l = (int)STRLEN(p); |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 409 | int n; |
| 410 | |
| 411 | while (*p != NUL) |
| 412 | { |
| 413 | if (*p == '^' && vim_strchr(escaped, p[1]) != NULL) |
| 414 | mch_memmove(p, p + 1, l--); |
| 415 | #ifdef FEAT_MBYTE |
| 416 | n = (*mb_ptr2len)(p); |
| 417 | #else |
| 418 | n = 1; |
| 419 | #endif |
| 420 | p += n; |
| 421 | l -= n; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 426 | * Load library "name". |
| 427 | */ |
| 428 | HINSTANCE |
| 429 | vimLoadLib(char *name) |
| 430 | { |
Bram Moolenaar | 17aa8cc | 2012-10-21 21:38:45 +0200 | [diff] [blame] | 431 | HINSTANCE dll = NULL; |
| 432 | char old_dir[MAXPATHL]; |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 433 | |
Bram Moolenaar | faca840 | 2012-10-21 02:37:10 +0200 | [diff] [blame] | 434 | /* NOTE: Do not use mch_dirname() and mch_chdir() here, they may call |
| 435 | * vimLoadLib() recursively, which causes a stack overflow. */ |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 436 | if (exe_path == NULL) |
| 437 | get_exe_name(); |
Bram Moolenaar | 17aa8cc | 2012-10-21 21:38:45 +0200 | [diff] [blame] | 438 | if (exe_path != NULL) |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 439 | { |
Bram Moolenaar | 17aa8cc | 2012-10-21 21:38:45 +0200 | [diff] [blame] | 440 | #ifdef FEAT_MBYTE |
| 441 | WCHAR old_dirw[MAXPATHL]; |
| 442 | |
| 443 | if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0) |
| 444 | { |
| 445 | /* Change directory to where the executable is, both to make |
| 446 | * sure we find a .dll there and to avoid looking for a .dll |
| 447 | * in the current directory. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 448 | SetCurrentDirectory((LPCSTR)exe_path); |
Bram Moolenaar | 17aa8cc | 2012-10-21 21:38:45 +0200 | [diff] [blame] | 449 | dll = LoadLibrary(name); |
| 450 | SetCurrentDirectoryW(old_dirw); |
| 451 | return dll; |
| 452 | } |
| 453 | /* Retry with non-wide function (for Windows 98). */ |
| 454 | if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 455 | #endif |
| 456 | if (GetCurrentDirectory(MAXPATHL, old_dir) != 0) |
| 457 | { |
| 458 | /* Change directory to where the executable is, both to make |
| 459 | * sure we find a .dll there and to avoid looking for a .dll |
| 460 | * in the current directory. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 461 | SetCurrentDirectory((LPCSTR)exe_path); |
Bram Moolenaar | 17aa8cc | 2012-10-21 21:38:45 +0200 | [diff] [blame] | 462 | dll = LoadLibrary(name); |
| 463 | SetCurrentDirectory(old_dir); |
| 464 | } |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 465 | } |
| 466 | return dll; |
| 467 | } |
| 468 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 469 | #if defined(DYNAMIC_GETTEXT) || defined(PROTO) |
| 470 | # ifndef GETTEXT_DLL |
| 471 | # define GETTEXT_DLL "libintl.dll" |
Bram Moolenaar | 286eacd | 2016-01-16 18:05:50 +0100 | [diff] [blame] | 472 | # define GETTEXT_DLL_ALT "libintl-8.dll" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 473 | # endif |
Bram Moolenaar | f6a2b08 | 2012-06-29 13:14:03 +0200 | [diff] [blame] | 474 | /* Dummy functions */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 475 | static char *null_libintl_gettext(const char *); |
| 476 | static char *null_libintl_textdomain(const char *); |
| 477 | static char *null_libintl_bindtextdomain(const char *, const char *); |
| 478 | static char *null_libintl_bind_textdomain_codeset(const char *, const char *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 480 | static HINSTANCE hLibintlDLL = NULL; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 481 | char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext; |
| 482 | char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain; |
| 483 | char *(*dyn_libintl_bindtextdomain)(const char *, const char *) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | = null_libintl_bindtextdomain; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 485 | char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *) |
| 486 | = null_libintl_bind_textdomain_codeset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | |
| 488 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 489 | dyn_libintl_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | { |
| 491 | int i; |
| 492 | static struct |
| 493 | { |
| 494 | char *name; |
| 495 | FARPROC *ptr; |
| 496 | } libintl_entry[] = |
| 497 | { |
| 498 | {"gettext", (FARPROC*)&dyn_libintl_gettext}, |
| 499 | {"textdomain", (FARPROC*)&dyn_libintl_textdomain}, |
| 500 | {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain}, |
| 501 | {NULL, NULL} |
| 502 | }; |
| 503 | |
| 504 | /* No need to initialize twice. */ |
| 505 | if (hLibintlDLL) |
| 506 | return 1; |
| 507 | /* Load gettext library (libintl.dll) */ |
Bram Moolenaar | 923e43b | 2016-01-28 15:07:38 +0100 | [diff] [blame] | 508 | hLibintlDLL = vimLoadLib(GETTEXT_DLL); |
Bram Moolenaar | 938ee83 | 2016-01-24 15:36:03 +0100 | [diff] [blame] | 509 | #ifdef GETTEXT_DLL_ALT |
Bram Moolenaar | 286eacd | 2016-01-16 18:05:50 +0100 | [diff] [blame] | 510 | if (!hLibintlDLL) |
| 511 | hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT); |
Bram Moolenaar | 938ee83 | 2016-01-24 15:36:03 +0100 | [diff] [blame] | 512 | #endif |
| 513 | if (!hLibintlDLL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 515 | if (p_verbose > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 516 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 517 | verbose_enter(); |
| 518 | EMSG2(_(e_loadlib), GETTEXT_DLL); |
| 519 | verbose_leave(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | } |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 521 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | } |
| 523 | for (i = 0; libintl_entry[i].name != NULL |
| 524 | && libintl_entry[i].ptr != NULL; ++i) |
| 525 | { |
| 526 | if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL, |
| 527 | libintl_entry[i].name)) == NULL) |
| 528 | { |
| 529 | dyn_libintl_end(); |
| 530 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 531 | { |
| 532 | verbose_enter(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 533 | EMSG2(_(e_loadfunc), libintl_entry[i].name); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 534 | verbose_leave(); |
| 535 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 539 | |
| 540 | /* The bind_textdomain_codeset() function is optional. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 541 | dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL, |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 542 | "bind_textdomain_codeset"); |
| 543 | if (dyn_libintl_bind_textdomain_codeset == NULL) |
| 544 | dyn_libintl_bind_textdomain_codeset = |
| 545 | null_libintl_bind_textdomain_codeset; |
| 546 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 551 | dyn_libintl_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 552 | { |
| 553 | if (hLibintlDLL) |
| 554 | FreeLibrary(hLibintlDLL); |
| 555 | hLibintlDLL = NULL; |
| 556 | dyn_libintl_gettext = null_libintl_gettext; |
| 557 | dyn_libintl_textdomain = null_libintl_textdomain; |
| 558 | dyn_libintl_bindtextdomain = null_libintl_bindtextdomain; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 559 | dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 562 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 563 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 564 | null_libintl_gettext(const char *msgid) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | { |
| 566 | return (char*)msgid; |
| 567 | } |
| 568 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 569 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 571 | null_libintl_bindtextdomain(const char *domainname, const char *dirname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 572 | { |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 576 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | static char * |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 578 | null_libintl_bind_textdomain_codeset(const char *domainname, |
| 579 | const char *codeset) |
| 580 | { |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 584 | /*ARGSUSED*/ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 585 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 586 | null_libintl_textdomain(const char *domainname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 587 | { |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | #endif /* DYNAMIC_GETTEXT */ |
| 592 | |
| 593 | /* This symbol is not defined in older versions of the SDK or Visual C++ */ |
| 594 | |
| 595 | #ifndef VER_PLATFORM_WIN32_WINDOWS |
| 596 | # define VER_PLATFORM_WIN32_WINDOWS 1 |
| 597 | #endif |
| 598 | |
| 599 | DWORD g_PlatformId; |
| 600 | |
| 601 | #ifdef HAVE_ACL |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 602 | # ifndef PROTO |
| 603 | # include <aclapi.h> |
| 604 | # endif |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 605 | # ifndef PROTECTED_DACL_SECURITY_INFORMATION |
| 606 | # define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L |
| 607 | # endif |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 608 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 609 | /* |
| 610 | * These are needed to dynamically load the ADVAPI DLL, which is not |
| 611 | * implemented under Windows 95 (and causes VIM to crash) |
| 612 | */ |
Bram Moolenaar | 39efa89 | 2013-06-29 15:40:04 +0200 | [diff] [blame] | 613 | typedef DWORD (WINAPI *PSNSECINFO) (LPSTR, SE_OBJECT_TYPE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 614 | SECURITY_INFORMATION, PSID, PSID, PACL, PACL); |
Bram Moolenaar | 39efa89 | 2013-06-29 15:40:04 +0200 | [diff] [blame] | 615 | typedef DWORD (WINAPI *PGNSECINFO) (LPSTR, SE_OBJECT_TYPE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | SECURITY_INFORMATION, PSID *, PSID *, PACL *, PACL *, |
| 617 | PSECURITY_DESCRIPTOR *); |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 618 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 39efa89 | 2013-06-29 15:40:04 +0200 | [diff] [blame] | 619 | typedef DWORD (WINAPI *PSNSECINFOW) (LPWSTR, SE_OBJECT_TYPE, |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 620 | SECURITY_INFORMATION, PSID, PSID, PACL, PACL); |
Bram Moolenaar | 39efa89 | 2013-06-29 15:40:04 +0200 | [diff] [blame] | 621 | typedef DWORD (WINAPI *PGNSECINFOW) (LPWSTR, SE_OBJECT_TYPE, |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 622 | SECURITY_INFORMATION, PSID *, PSID *, PACL *, PACL *, |
| 623 | PSECURITY_DESCRIPTOR *); |
| 624 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 625 | |
| 626 | static HANDLE advapi_lib = NULL; /* Handle for ADVAPI library */ |
| 627 | static PSNSECINFO pSetNamedSecurityInfo; |
| 628 | static PGNSECINFO pGetNamedSecurityInfo; |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 629 | # ifdef FEAT_MBYTE |
| 630 | static PSNSECINFOW pSetNamedSecurityInfoW; |
| 631 | static PGNSECINFOW pGetNamedSecurityInfoW; |
| 632 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | #endif |
| 634 | |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 635 | typedef BOOL (WINAPI *PSETHANDLEINFORMATION)(HANDLE, DWORD, DWORD); |
| 636 | |
| 637 | static BOOL allowPiping = FALSE; |
| 638 | static PSETHANDLEINFORMATION pSetHandleInformation; |
| 639 | |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 640 | #ifdef HAVE_ACL |
| 641 | /* |
| 642 | * Enables or disables the specified privilege. |
| 643 | */ |
| 644 | static BOOL |
| 645 | win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable) |
| 646 | { |
Bram Moolenaar | b0d5c96 | 2014-01-12 13:24:51 +0100 | [diff] [blame] | 647 | BOOL bResult; |
| 648 | LUID luid; |
| 649 | HANDLE hToken; |
| 650 | TOKEN_PRIVILEGES tokenPrivileges; |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 651 | |
| 652 | if (!OpenProcessToken(GetCurrentProcess(), |
| 653 | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) |
| 654 | return FALSE; |
| 655 | |
| 656 | if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) |
| 657 | { |
| 658 | CloseHandle(hToken); |
| 659 | return FALSE; |
| 660 | } |
| 661 | |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 662 | tokenPrivileges.PrivilegeCount = 1; |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 663 | tokenPrivileges.Privileges[0].Luid = luid; |
| 664 | tokenPrivileges.Privileges[0].Attributes = bEnable ? |
| 665 | SE_PRIVILEGE_ENABLED : 0; |
| 666 | |
| 667 | bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, |
| 668 | sizeof(TOKEN_PRIVILEGES), NULL, NULL); |
| 669 | |
| 670 | CloseHandle(hToken); |
| 671 | |
| 672 | return bResult && GetLastError() == ERROR_SUCCESS; |
| 673 | } |
| 674 | #endif |
| 675 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 676 | /* |
| 677 | * Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or |
| 678 | * VER_PLATFORM_WIN32_WINDOWS (Win95). |
| 679 | */ |
| 680 | void |
| 681 | PlatformId(void) |
| 682 | { |
| 683 | static int done = FALSE; |
| 684 | |
| 685 | if (!done) |
| 686 | { |
| 687 | OSVERSIONINFO ovi; |
| 688 | |
| 689 | ovi.dwOSVersionInfoSize = sizeof(ovi); |
| 690 | GetVersionEx(&ovi); |
| 691 | |
| 692 | g_PlatformId = ovi.dwPlatformId; |
| 693 | |
Bram Moolenaar | f50eb78 | 2014-02-05 13:36:54 +0100 | [diff] [blame] | 694 | if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2) |
| 695 | || ovi.dwMajorVersion > 6) |
| 696 | win8_or_later = TRUE; |
| 697 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | #ifdef HAVE_ACL |
| 699 | /* |
| 700 | * Load the ADVAPI runtime if we are on anything |
| 701 | * other than Windows 95 |
| 702 | */ |
| 703 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 704 | { |
| 705 | /* |
| 706 | * do this load. Problems: Doesn't unload at end of run (this is |
| 707 | * theoretically okay, since Windows should unload it when VIM |
| 708 | * terminates). Should we be using the 'mch_libcall' routines? |
| 709 | * Seems like a lot of overhead to load/unload ADVAPI32.DLL each |
| 710 | * time we verify security... |
| 711 | */ |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 712 | advapi_lib = vimLoadLib("ADVAPI32.DLL"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 713 | if (advapi_lib != NULL) |
| 714 | { |
| 715 | pSetNamedSecurityInfo = (PSNSECINFO)GetProcAddress(advapi_lib, |
| 716 | "SetNamedSecurityInfoA"); |
| 717 | pGetNamedSecurityInfo = (PGNSECINFO)GetProcAddress(advapi_lib, |
| 718 | "GetNamedSecurityInfoA"); |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 719 | # ifdef FEAT_MBYTE |
| 720 | pSetNamedSecurityInfoW = (PSNSECINFOW)GetProcAddress(advapi_lib, |
| 721 | "SetNamedSecurityInfoW"); |
| 722 | pGetNamedSecurityInfoW = (PGNSECINFOW)GetProcAddress(advapi_lib, |
| 723 | "GetNamedSecurityInfoW"); |
| 724 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | if (pSetNamedSecurityInfo == NULL |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 726 | || pGetNamedSecurityInfo == NULL |
| 727 | # ifdef FEAT_MBYTE |
| 728 | || pSetNamedSecurityInfoW == NULL |
| 729 | || pGetNamedSecurityInfoW == NULL |
| 730 | # endif |
| 731 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | { |
| 733 | /* If we can't get the function addresses, set advapi_lib |
| 734 | * to NULL so that we don't use them. */ |
| 735 | FreeLibrary(advapi_lib); |
| 736 | advapi_lib = NULL; |
| 737 | } |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 738 | /* Enable privilege for getting or setting SACLs. */ |
| 739 | win32_enable_privilege(SE_SECURITY_NAME, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | #endif |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 743 | /* |
| 744 | * If we are on windows NT, try to load the pipe functions, only |
| 745 | * available from Win2K. |
| 746 | */ |
| 747 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 748 | { |
| 749 | HANDLE kernel32 = GetModuleHandle("kernel32"); |
| 750 | pSetHandleInformation = (PSETHANDLEINFORMATION)GetProcAddress( |
| 751 | kernel32, "SetHandleInformation"); |
| 752 | |
| 753 | allowPiping = pSetHandleInformation != NULL; |
| 754 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 755 | done = TRUE; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Return TRUE when running on Windows 95 (or 98 or ME). |
| 761 | * Only to be used after mch_init(). |
| 762 | */ |
| 763 | int |
| 764 | mch_windows95(void) |
| 765 | { |
| 766 | return g_PlatformId == VER_PLATFORM_WIN32_WINDOWS; |
| 767 | } |
| 768 | |
| 769 | #ifdef FEAT_GUI_W32 |
| 770 | /* |
| 771 | * Used to work around the "can't do synchronous spawn" |
| 772 | * problem on Win32s, without resorting to Universal Thunk. |
| 773 | */ |
| 774 | static int old_num_windows; |
| 775 | static int num_windows; |
| 776 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 777 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | static BOOL CALLBACK |
| 779 | win32ssynch_cb(HWND hwnd, LPARAM lparam) |
| 780 | { |
| 781 | num_windows++; |
| 782 | return TRUE; |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | #ifndef FEAT_GUI_W32 |
| 787 | |
| 788 | #define SHIFT (SHIFT_PRESSED) |
| 789 | #define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED) |
| 790 | #define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED) |
| 791 | #define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED) |
| 792 | |
| 793 | |
| 794 | /* When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode. |
| 795 | * We map function keys to their ANSI terminal equivalents, as produced |
| 796 | * by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any |
| 797 | * ANSI key with a value >= '\300' is nonstandard, but provided anyway |
| 798 | * so that the user can have access to all SHIFT-, CTRL-, and ALT- |
| 799 | * combinations of function/arrow/etc keys. |
| 800 | */ |
| 801 | |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 802 | static const struct |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | { |
| 804 | WORD wVirtKey; |
| 805 | BOOL fAnsiKey; |
| 806 | int chAlone; |
| 807 | int chShift; |
| 808 | int chCtrl; |
| 809 | int chAlt; |
| 810 | } VirtKeyMap[] = |
| 811 | { |
| 812 | |
| 813 | /* Key ANSI alone shift ctrl alt */ |
| 814 | { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, }, |
| 815 | |
| 816 | { VK_F1, TRUE, ';', 'T', '^', 'h', }, |
| 817 | { VK_F2, TRUE, '<', 'U', '_', 'i', }, |
| 818 | { VK_F3, TRUE, '=', 'V', '`', 'j', }, |
| 819 | { VK_F4, TRUE, '>', 'W', 'a', 'k', }, |
| 820 | { VK_F5, TRUE, '?', 'X', 'b', 'l', }, |
| 821 | { VK_F6, TRUE, '@', 'Y', 'c', 'm', }, |
| 822 | { VK_F7, TRUE, 'A', 'Z', 'd', 'n', }, |
| 823 | { VK_F8, TRUE, 'B', '[', 'e', 'o', }, |
| 824 | { VK_F9, TRUE, 'C', '\\', 'f', 'p', }, |
| 825 | { VK_F10, TRUE, 'D', ']', 'g', 'q', }, |
| 826 | { VK_F11, TRUE, '\205', '\207', '\211', '\213', }, |
| 827 | { VK_F12, TRUE, '\206', '\210', '\212', '\214', }, |
| 828 | |
| 829 | { VK_HOME, TRUE, 'G', '\302', 'w', '\303', }, |
| 830 | { VK_UP, TRUE, 'H', '\304', '\305', '\306', }, |
| 831 | { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, /*PgUp*/ |
| 832 | { VK_LEFT, TRUE, 'K', '\311', 's', '\312', }, |
| 833 | { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', }, |
| 834 | { VK_END, TRUE, 'O', '\315', 'u', '\316', }, |
| 835 | { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', }, |
| 836 | { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, /*PgDn*/ |
| 837 | { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', }, |
| 838 | { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', }, |
| 839 | |
| 840 | { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, /*PrtScrn*/ |
| 841 | |
| 842 | #if 0 |
| 843 | /* Most people don't have F13-F20, but what the hell... */ |
| 844 | { VK_F13, TRUE, '\332', '\333', '\334', '\335', }, |
| 845 | { VK_F14, TRUE, '\336', '\337', '\340', '\341', }, |
| 846 | { VK_F15, TRUE, '\342', '\343', '\344', '\345', }, |
| 847 | { VK_F16, TRUE, '\346', '\347', '\350', '\351', }, |
| 848 | { VK_F17, TRUE, '\352', '\353', '\354', '\355', }, |
| 849 | { VK_F18, TRUE, '\356', '\357', '\360', '\361', }, |
| 850 | { VK_F19, TRUE, '\362', '\363', '\364', '\365', }, |
| 851 | { VK_F20, TRUE, '\366', '\367', '\370', '\371', }, |
| 852 | #endif |
| 853 | { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, /* keyp '+' */ |
| 854 | { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, /* keyp '-' */ |
| 855 | /* { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, keyp '/' */ |
| 856 | { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, /* keyp '*' */ |
| 857 | |
| 858 | { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', }, |
| 859 | { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', }, |
| 860 | { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', }, |
| 861 | { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', }, |
| 862 | { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', }, |
| 863 | { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', }, |
| 864 | { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', }, |
| 865 | { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', }, |
| 866 | { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', }, |
| 867 | /* Sorry, out of number space! <negri>*/ |
| 868 | { VK_NUMPAD9,TRUE, '\376', '\377', '\377', '\367', }, |
| 869 | |
| 870 | }; |
| 871 | |
| 872 | |
| 873 | #ifdef _MSC_VER |
| 874 | // The ToAscii bug destroys several registers. Need to turn off optimization |
| 875 | // or the GetConsoleKeyboardLayoutName hack will fail in non-debug versions |
Bram Moolenaar | 7b5f832 | 2006-03-23 22:47:08 +0000 | [diff] [blame] | 876 | # pragma warning(push) |
| 877 | # pragma warning(disable: 4748) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 878 | # pragma optimize("", off) |
| 879 | #endif |
| 880 | |
| 881 | #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__) |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 882 | # define UChar UnicodeChar |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | #else |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 884 | # define UChar uChar.UnicodeChar |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | #endif |
| 886 | |
| 887 | /* The return code indicates key code size. */ |
| 888 | static int |
| 889 | #ifdef __BORLANDC__ |
| 890 | __stdcall |
| 891 | #endif |
| 892 | win32_kbd_patch_key( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 893 | KEY_EVENT_RECORD *pker) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 894 | { |
| 895 | UINT uMods = pker->dwControlKeyState; |
| 896 | static int s_iIsDead = 0; |
| 897 | static WORD awAnsiCode[2]; |
| 898 | static BYTE abKeystate[256]; |
| 899 | |
| 900 | |
| 901 | if (s_iIsDead == 2) |
| 902 | { |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 903 | pker->UChar = (WCHAR) awAnsiCode[1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 904 | s_iIsDead = 0; |
| 905 | return 1; |
| 906 | } |
| 907 | |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 908 | if (pker->UChar != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | return 1; |
| 910 | |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 911 | vim_memset(abKeystate, 0, sizeof (abKeystate)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | |
| 913 | // Should only be non-NULL on NT 4.0 |
| 914 | if (s_pfnGetConsoleKeyboardLayoutName != NULL) |
| 915 | { |
| 916 | CHAR szKLID[KL_NAMELENGTH]; |
| 917 | |
| 918 | if ((*s_pfnGetConsoleKeyboardLayoutName)(szKLID)) |
| 919 | (void)LoadKeyboardLayout(szKLID, KLF_ACTIVATE); |
| 920 | } |
| 921 | |
| 922 | /* Clear any pending dead keys */ |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 923 | ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | |
| 925 | if (uMods & SHIFT_PRESSED) |
| 926 | abKeystate[VK_SHIFT] = 0x80; |
| 927 | if (uMods & CAPSLOCK_ON) |
| 928 | abKeystate[VK_CAPITAL] = 1; |
| 929 | |
| 930 | if ((uMods & ALT_GR) == ALT_GR) |
| 931 | { |
| 932 | abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] = |
| 933 | abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80; |
| 934 | } |
| 935 | |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 936 | s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode, |
| 937 | abKeystate, awAnsiCode, 2, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | |
| 939 | if (s_iIsDead > 0) |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 940 | pker->UChar = (WCHAR) awAnsiCode[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | |
| 942 | return s_iIsDead; |
| 943 | } |
| 944 | |
| 945 | #ifdef _MSC_VER |
| 946 | /* MUST switch optimization on again here, otherwise a call to |
| 947 | * decode_key_event() may crash (e.g. when hitting caps-lock) */ |
| 948 | # pragma optimize("", on) |
Bram Moolenaar | 7b5f832 | 2006-03-23 22:47:08 +0000 | [diff] [blame] | 949 | # pragma warning(pop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | |
| 951 | # if (_MSC_VER < 1100) |
| 952 | /* MUST turn off global optimisation for this next function, or |
| 953 | * pressing ctrl-minus in insert mode crashes Vim when built with |
| 954 | * VC4.1. -- negri. */ |
| 955 | # pragma optimize("g", off) |
| 956 | # endif |
| 957 | #endif |
| 958 | |
| 959 | static BOOL g_fJustGotFocus = FALSE; |
| 960 | |
| 961 | /* |
| 962 | * Decode a KEY_EVENT into one or two keystrokes |
| 963 | */ |
| 964 | static BOOL |
| 965 | decode_key_event( |
| 966 | KEY_EVENT_RECORD *pker, |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 967 | WCHAR *pch, |
| 968 | WCHAR *pch2, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 969 | int *pmodifiers, |
| 970 | BOOL fDoPost) |
| 971 | { |
| 972 | int i; |
| 973 | const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL); |
| 974 | |
| 975 | *pch = *pch2 = NUL; |
| 976 | g_fJustGotFocus = FALSE; |
| 977 | |
| 978 | /* ignore key up events */ |
| 979 | if (!pker->bKeyDown) |
| 980 | return FALSE; |
| 981 | |
| 982 | /* ignore some keystrokes */ |
| 983 | switch (pker->wVirtualKeyCode) |
| 984 | { |
| 985 | /* modifiers */ |
| 986 | case VK_SHIFT: |
| 987 | case VK_CONTROL: |
| 988 | case VK_MENU: /* Alt key */ |
| 989 | return FALSE; |
| 990 | |
| 991 | default: |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | /* special cases */ |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 996 | if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | { |
| 998 | /* Ctrl-6 is Ctrl-^ */ |
| 999 | if (pker->wVirtualKeyCode == '6') |
| 1000 | { |
| 1001 | *pch = Ctrl_HAT; |
| 1002 | return TRUE; |
| 1003 | } |
| 1004 | /* Ctrl-2 is Ctrl-@ */ |
| 1005 | else if (pker->wVirtualKeyCode == '2') |
| 1006 | { |
| 1007 | *pch = NUL; |
| 1008 | return TRUE; |
| 1009 | } |
| 1010 | /* Ctrl-- is Ctrl-_ */ |
| 1011 | else if (pker->wVirtualKeyCode == 0xBD) |
| 1012 | { |
| 1013 | *pch = Ctrl__; |
| 1014 | return TRUE; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* Shift-TAB */ |
| 1019 | if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED)) |
| 1020 | { |
| 1021 | *pch = K_NUL; |
| 1022 | *pch2 = '\017'; |
| 1023 | return TRUE; |
| 1024 | } |
| 1025 | |
| 1026 | for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; ) |
| 1027 | { |
| 1028 | if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode) |
| 1029 | { |
| 1030 | if (nModifs == 0) |
| 1031 | *pch = VirtKeyMap[i].chAlone; |
| 1032 | else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0) |
| 1033 | *pch = VirtKeyMap[i].chShift; |
| 1034 | else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0) |
| 1035 | *pch = VirtKeyMap[i].chCtrl; |
| 1036 | else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0) |
| 1037 | *pch = VirtKeyMap[i].chAlt; |
| 1038 | |
| 1039 | if (*pch != 0) |
| 1040 | { |
| 1041 | if (VirtKeyMap[i].fAnsiKey) |
| 1042 | { |
| 1043 | *pch2 = *pch; |
| 1044 | *pch = K_NUL; |
| 1045 | } |
| 1046 | |
| 1047 | return TRUE; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | i = win32_kbd_patch_key(pker); |
| 1053 | |
| 1054 | if (i < 0) |
| 1055 | *pch = NUL; |
| 1056 | else |
| 1057 | { |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1058 | *pch = (i > 0) ? pker->UChar : NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | |
| 1060 | if (pmodifiers != NULL) |
| 1061 | { |
| 1062 | /* Pass on the ALT key as a modifier, but only when not combined |
| 1063 | * with CTRL (which is ALTGR). */ |
| 1064 | if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0) |
| 1065 | *pmodifiers |= MOD_MASK_ALT; |
| 1066 | |
| 1067 | /* Pass on SHIFT only for special keys, because we don't know when |
| 1068 | * it's already included with the character. */ |
| 1069 | if ((nModifs & SHIFT) != 0 && *pch <= 0x20) |
| 1070 | *pmodifiers |= MOD_MASK_SHIFT; |
| 1071 | |
| 1072 | /* Pass on CTRL only for non-special keys, because we don't know |
| 1073 | * when it's already included with the character. And not when |
| 1074 | * combined with ALT (which is ALTGR). */ |
| 1075 | if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0 |
| 1076 | && *pch >= 0x20 && *pch < 0x80) |
| 1077 | *pmodifiers |= MOD_MASK_CTRL; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return (*pch != NUL); |
| 1082 | } |
| 1083 | |
| 1084 | #ifdef _MSC_VER |
| 1085 | # pragma optimize("", on) |
| 1086 | #endif |
| 1087 | |
| 1088 | #endif /* FEAT_GUI_W32 */ |
| 1089 | |
| 1090 | |
| 1091 | #ifdef FEAT_MOUSE |
| 1092 | |
| 1093 | /* |
| 1094 | * For the GUI the mouse handling is in gui_w32.c. |
| 1095 | */ |
| 1096 | # ifdef FEAT_GUI_W32 |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1097 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1099 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | { |
| 1101 | } |
| 1102 | # else |
| 1103 | static int g_fMouseAvail = FALSE; /* mouse present */ |
| 1104 | static int g_fMouseActive = FALSE; /* mouse enabled */ |
| 1105 | static int g_nMouseClick = -1; /* mouse status */ |
| 1106 | static int g_xMouse; /* mouse x coordinate */ |
| 1107 | static int g_yMouse; /* mouse y coordinate */ |
| 1108 | |
| 1109 | /* |
| 1110 | * Enable or disable mouse input |
| 1111 | */ |
| 1112 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1113 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1114 | { |
| 1115 | DWORD cmodein; |
| 1116 | |
| 1117 | if (!g_fMouseAvail) |
| 1118 | return; |
| 1119 | |
| 1120 | g_fMouseActive = on; |
| 1121 | GetConsoleMode(g_hConIn, &cmodein); |
| 1122 | |
| 1123 | if (g_fMouseActive) |
| 1124 | cmodein |= ENABLE_MOUSE_INPUT; |
| 1125 | else |
| 1126 | cmodein &= ~ENABLE_MOUSE_INPUT; |
| 1127 | |
| 1128 | SetConsoleMode(g_hConIn, cmodein); |
| 1129 | } |
| 1130 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1131 | /* |
| 1132 | * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT, |
| 1133 | * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse |
| 1134 | * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG |
| 1135 | * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type, |
| 1136 | * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick, |
| 1137 | * and we return the mouse position in g_xMouse and g_yMouse. |
| 1138 | * |
| 1139 | * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more |
| 1140 | * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only |
| 1141 | * by MOUSE_LEFT, _MIDDLE, or _RIGHT. |
| 1142 | * |
| 1143 | * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE, |
| 1144 | * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, .... |
| 1145 | * |
| 1146 | * Windows will send us MOUSE_MOVED notifications whenever the mouse |
| 1147 | * moves, even if it stays within the same character cell. We ignore |
| 1148 | * all MOUSE_MOVED messages if the position hasn't really changed, and |
| 1149 | * we ignore all MOUSE_MOVED messages where no button is held down (i.e., |
| 1150 | * we're only interested in MOUSE_DRAG). |
| 1151 | * |
| 1152 | * All of this is complicated by the code that fakes MOUSE_MIDDLE on |
| 1153 | * 2-button mouses by pressing the left & right buttons simultaneously. |
| 1154 | * In practice, it's almost impossible to click both at the same time, |
| 1155 | * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE |
| 1156 | * in such cases, if the user is clicking quickly. |
| 1157 | */ |
| 1158 | static BOOL |
| 1159 | decode_mouse_event( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1160 | MOUSE_EVENT_RECORD *pmer) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | { |
| 1162 | static int s_nOldButton = -1; |
| 1163 | static int s_nOldMouseClick = -1; |
| 1164 | static int s_xOldMouse = -1; |
| 1165 | static int s_yOldMouse = -1; |
| 1166 | static linenr_T s_old_topline = 0; |
| 1167 | #ifdef FEAT_DIFF |
| 1168 | static int s_old_topfill = 0; |
| 1169 | #endif |
| 1170 | static int s_cClicks = 1; |
| 1171 | static BOOL s_fReleased = TRUE; |
| 1172 | static DWORD s_dwLastClickTime = 0; |
| 1173 | static BOOL s_fNextIsMiddle = FALSE; |
| 1174 | |
| 1175 | static DWORD cButtons = 0; /* number of buttons supported */ |
| 1176 | |
| 1177 | const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED; |
| 1178 | const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED; |
| 1179 | const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED; |
| 1180 | const DWORD LEFT_RIGHT = LEFT | RIGHT; |
| 1181 | |
| 1182 | int nButton; |
| 1183 | |
| 1184 | if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons)) |
| 1185 | cButtons = 2; |
| 1186 | |
| 1187 | if (!g_fMouseAvail || !g_fMouseActive) |
| 1188 | { |
| 1189 | g_nMouseClick = -1; |
| 1190 | return FALSE; |
| 1191 | } |
| 1192 | |
| 1193 | /* get a spurious MOUSE_EVENT immediately after receiving focus; ignore */ |
| 1194 | if (g_fJustGotFocus) |
| 1195 | { |
| 1196 | g_fJustGotFocus = FALSE; |
| 1197 | return FALSE; |
| 1198 | } |
| 1199 | |
| 1200 | /* unprocessed mouse click? */ |
| 1201 | if (g_nMouseClick != -1) |
| 1202 | return TRUE; |
| 1203 | |
| 1204 | nButton = -1; |
| 1205 | g_xMouse = pmer->dwMousePosition.X; |
| 1206 | g_yMouse = pmer->dwMousePosition.Y; |
| 1207 | |
| 1208 | if (pmer->dwEventFlags == MOUSE_MOVED) |
| 1209 | { |
| 1210 | /* ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these |
| 1211 | * events even when the mouse moves only within a char cell.) */ |
| 1212 | if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse) |
| 1213 | return FALSE; |
| 1214 | } |
| 1215 | |
| 1216 | /* If no buttons are pressed... */ |
| 1217 | if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0) |
| 1218 | { |
| 1219 | /* If the last thing returned was MOUSE_RELEASE, ignore this */ |
| 1220 | if (s_fReleased) |
| 1221 | return FALSE; |
| 1222 | |
| 1223 | nButton = MOUSE_RELEASE; |
| 1224 | s_fReleased = TRUE; |
| 1225 | } |
| 1226 | else /* one or more buttons pressed */ |
| 1227 | { |
| 1228 | /* on a 2-button mouse, hold down left and right buttons |
| 1229 | * simultaneously to get MIDDLE. */ |
| 1230 | |
| 1231 | if (cButtons == 2 && s_nOldButton != MOUSE_DRAG) |
| 1232 | { |
| 1233 | DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT); |
| 1234 | |
| 1235 | /* if either left or right button only is pressed, see if the |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1236 | * next mouse event has both of them pressed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | if (dwLR == LEFT || dwLR == RIGHT) |
| 1238 | { |
| 1239 | for (;;) |
| 1240 | { |
| 1241 | /* wait a short time for next input event */ |
| 1242 | if (WaitForSingleObject(g_hConIn, p_mouset / 3) |
| 1243 | != WAIT_OBJECT_0) |
| 1244 | break; |
| 1245 | else |
| 1246 | { |
| 1247 | DWORD cRecords = 0; |
| 1248 | INPUT_RECORD ir; |
| 1249 | MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent; |
| 1250 | |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1251 | peek_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (cRecords == 0 || ir.EventType != MOUSE_EVENT |
| 1254 | || !(pmer2->dwButtonState & LEFT_RIGHT)) |
| 1255 | break; |
| 1256 | else |
| 1257 | { |
| 1258 | if (pmer2->dwEventFlags != MOUSE_MOVED) |
| 1259 | { |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1260 | read_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | |
| 1262 | return decode_mouse_event(pmer2); |
| 1263 | } |
| 1264 | else if (s_xOldMouse == pmer2->dwMousePosition.X && |
| 1265 | s_yOldMouse == pmer2->dwMousePosition.Y) |
| 1266 | { |
| 1267 | /* throw away spurious mouse move */ |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1268 | read_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* are there any more mouse events in queue? */ |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1271 | peek_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (cRecords==0 || ir.EventType != MOUSE_EVENT) |
| 1274 | break; |
| 1275 | } |
| 1276 | else |
| 1277 | break; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | if (s_fNextIsMiddle) |
| 1285 | { |
| 1286 | nButton = (pmer->dwEventFlags == MOUSE_MOVED) |
| 1287 | ? MOUSE_DRAG : MOUSE_MIDDLE; |
| 1288 | s_fNextIsMiddle = FALSE; |
| 1289 | } |
| 1290 | else if (cButtons == 2 && |
| 1291 | ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT)) |
| 1292 | { |
| 1293 | nButton = MOUSE_MIDDLE; |
| 1294 | |
| 1295 | if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED) |
| 1296 | { |
| 1297 | s_fNextIsMiddle = TRUE; |
| 1298 | nButton = MOUSE_RELEASE; |
| 1299 | } |
| 1300 | } |
| 1301 | else if ((pmer->dwButtonState & LEFT) == LEFT) |
| 1302 | nButton = MOUSE_LEFT; |
| 1303 | else if ((pmer->dwButtonState & MIDDLE) == MIDDLE) |
| 1304 | nButton = MOUSE_MIDDLE; |
| 1305 | else if ((pmer->dwButtonState & RIGHT) == RIGHT) |
| 1306 | nButton = MOUSE_RIGHT; |
| 1307 | |
| 1308 | if (! s_fReleased && ! s_fNextIsMiddle |
| 1309 | && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG) |
| 1310 | return FALSE; |
| 1311 | |
| 1312 | s_fReleased = s_fNextIsMiddle; |
| 1313 | } |
| 1314 | |
| 1315 | if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK) |
| 1316 | { |
| 1317 | /* button pressed or released, without mouse moving */ |
| 1318 | if (nButton != -1 && nButton != MOUSE_RELEASE) |
| 1319 | { |
| 1320 | DWORD dwCurrentTime = GetTickCount(); |
| 1321 | |
| 1322 | if (s_xOldMouse != g_xMouse |
| 1323 | || s_yOldMouse != g_yMouse |
| 1324 | || s_nOldButton != nButton |
| 1325 | || s_old_topline != curwin->w_topline |
| 1326 | #ifdef FEAT_DIFF |
| 1327 | || s_old_topfill != curwin->w_topfill |
| 1328 | #endif |
| 1329 | || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset) |
| 1330 | { |
| 1331 | s_cClicks = 1; |
| 1332 | } |
| 1333 | else if (++s_cClicks > 4) |
| 1334 | { |
| 1335 | s_cClicks = 1; |
| 1336 | } |
| 1337 | |
| 1338 | s_dwLastClickTime = dwCurrentTime; |
| 1339 | } |
| 1340 | } |
| 1341 | else if (pmer->dwEventFlags == MOUSE_MOVED) |
| 1342 | { |
| 1343 | if (nButton != -1 && nButton != MOUSE_RELEASE) |
| 1344 | nButton = MOUSE_DRAG; |
| 1345 | |
| 1346 | s_cClicks = 1; |
| 1347 | } |
| 1348 | |
| 1349 | if (nButton == -1) |
| 1350 | return FALSE; |
| 1351 | |
| 1352 | if (nButton != MOUSE_RELEASE) |
| 1353 | s_nOldButton = nButton; |
| 1354 | |
| 1355 | g_nMouseClick = nButton; |
| 1356 | |
| 1357 | if (pmer->dwControlKeyState & SHIFT_PRESSED) |
| 1358 | g_nMouseClick |= MOUSE_SHIFT; |
| 1359 | if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)) |
| 1360 | g_nMouseClick |= MOUSE_CTRL; |
| 1361 | if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)) |
| 1362 | g_nMouseClick |= MOUSE_ALT; |
| 1363 | |
| 1364 | if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE) |
| 1365 | SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks); |
| 1366 | |
| 1367 | /* only pass on interesting (i.e., different) mouse events */ |
| 1368 | if (s_xOldMouse == g_xMouse |
| 1369 | && s_yOldMouse == g_yMouse |
| 1370 | && s_nOldMouseClick == g_nMouseClick) |
| 1371 | { |
| 1372 | g_nMouseClick = -1; |
| 1373 | return FALSE; |
| 1374 | } |
| 1375 | |
| 1376 | s_xOldMouse = g_xMouse; |
| 1377 | s_yOldMouse = g_yMouse; |
| 1378 | s_old_topline = curwin->w_topline; |
| 1379 | #ifdef FEAT_DIFF |
| 1380 | s_old_topfill = curwin->w_topfill; |
| 1381 | #endif |
| 1382 | s_nOldMouseClick = g_nMouseClick; |
| 1383 | |
| 1384 | return TRUE; |
| 1385 | } |
| 1386 | |
| 1387 | # endif /* FEAT_GUI_W32 */ |
| 1388 | #endif /* FEAT_MOUSE */ |
| 1389 | |
| 1390 | |
| 1391 | #ifdef MCH_CURSOR_SHAPE |
| 1392 | /* |
| 1393 | * Set the shape of the cursor. |
| 1394 | * 'thickness' can be from 1 (thin) to 99 (block) |
| 1395 | */ |
| 1396 | static void |
| 1397 | mch_set_cursor_shape(int thickness) |
| 1398 | { |
| 1399 | CONSOLE_CURSOR_INFO ConsoleCursorInfo; |
| 1400 | ConsoleCursorInfo.dwSize = thickness; |
| 1401 | ConsoleCursorInfo.bVisible = s_cursor_visible; |
| 1402 | |
| 1403 | SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo); |
| 1404 | if (s_cursor_visible) |
| 1405 | SetConsoleCursorPosition(g_hConOut, g_coord); |
| 1406 | } |
| 1407 | |
| 1408 | void |
| 1409 | mch_update_cursor(void) |
| 1410 | { |
| 1411 | int idx; |
| 1412 | int thickness; |
| 1413 | |
| 1414 | /* |
| 1415 | * How the cursor is drawn depends on the current mode. |
| 1416 | */ |
| 1417 | idx = get_shape_idx(FALSE); |
| 1418 | |
| 1419 | if (shape_table[idx].shape == SHAPE_BLOCK) |
| 1420 | thickness = 99; /* 100 doesn't work on W95 */ |
| 1421 | else |
| 1422 | thickness = shape_table[idx].percentage; |
| 1423 | mch_set_cursor_shape(thickness); |
| 1424 | } |
| 1425 | #endif |
| 1426 | |
| 1427 | #ifndef FEAT_GUI_W32 /* this isn't used for the GUI */ |
| 1428 | /* |
| 1429 | * Handle FOCUS_EVENT. |
| 1430 | */ |
| 1431 | static void |
| 1432 | handle_focus_event(INPUT_RECORD ir) |
| 1433 | { |
| 1434 | g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus; |
| 1435 | ui_focus_change((int)g_fJustGotFocus); |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * Wait until console input from keyboard or mouse is available, |
| 1440 | * or the time is up. |
| 1441 | * Return TRUE if something is available FALSE if not. |
| 1442 | */ |
| 1443 | static int |
| 1444 | WaitForChar(long msec) |
| 1445 | { |
| 1446 | DWORD dwNow = 0, dwEndTime = 0; |
| 1447 | INPUT_RECORD ir; |
| 1448 | DWORD cRecords; |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1449 | WCHAR ch, ch2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1450 | |
| 1451 | if (msec > 0) |
| 1452 | /* Wait until the specified time has elapsed. */ |
| 1453 | dwEndTime = GetTickCount() + msec; |
| 1454 | else if (msec < 0) |
| 1455 | /* Wait forever. */ |
| 1456 | dwEndTime = INFINITE; |
| 1457 | |
| 1458 | /* We need to loop until the end of the time period, because |
| 1459 | * we might get multiple unusable mouse events in that time. |
| 1460 | */ |
| 1461 | for (;;) |
| 1462 | { |
Bram Moolenaar | ca568ae | 2016-02-01 21:32:58 +0100 | [diff] [blame] | 1463 | #ifdef MESSAGE_QUEUE |
| 1464 | parse_queued_messages(); |
| 1465 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1466 | #ifdef FEAT_MZSCHEME |
| 1467 | mzvim_check_threads(); |
| 1468 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1469 | #ifdef FEAT_CLIENTSERVER |
| 1470 | serverProcessPendingMessages(); |
| 1471 | #endif |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1472 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1473 | if (0 |
| 1474 | #ifdef FEAT_MOUSE |
| 1475 | || g_nMouseClick != -1 |
| 1476 | #endif |
| 1477 | #ifdef FEAT_CLIENTSERVER |
| 1478 | || input_available() |
| 1479 | #endif |
| 1480 | ) |
| 1481 | return TRUE; |
| 1482 | |
| 1483 | if (msec > 0) |
| 1484 | { |
Bram Moolenaar | b7512b7 | 2013-08-10 12:45:09 +0200 | [diff] [blame] | 1485 | /* If the specified wait time has passed, return. Beware that |
| 1486 | * GetTickCount() may wrap around (overflow). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1487 | dwNow = GetTickCount(); |
Bram Moolenaar | b7512b7 | 2013-08-10 12:45:09 +0200 | [diff] [blame] | 1488 | if ((int)(dwNow - dwEndTime) >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | } |
| 1491 | if (msec != 0) |
| 1492 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1493 | DWORD dwWaitTime = dwEndTime - dwNow; |
| 1494 | |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 1495 | #ifdef FEAT_CHANNEL |
| 1496 | /* Check channel while waiting input. */ |
| 1497 | if (dwWaitTime > 100) |
| 1498 | dwWaitTime = 100; |
| 1499 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1500 | #ifdef FEAT_MZSCHEME |
| 1501 | if (mzthreads_allowed() && p_mzq > 0 |
| 1502 | && (msec < 0 || (long)dwWaitTime > p_mzq)) |
| 1503 | dwWaitTime = p_mzq; /* don't wait longer than 'mzquantum' */ |
| 1504 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | #ifdef FEAT_CLIENTSERVER |
| 1506 | /* Wait for either an event on the console input or a message in |
| 1507 | * the client-server window. */ |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 1508 | if (msg_wait_for_multiple_objects(1, &g_hConIn, FALSE, |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1509 | dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | #else |
Bram Moolenaar | bb86ebb | 2015-08-04 19:27:05 +0200 | [diff] [blame] | 1511 | if (wait_for_single_object(g_hConIn, dwWaitTime) != WAIT_OBJECT_0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | #endif |
| 1513 | continue; |
| 1514 | } |
| 1515 | |
| 1516 | cRecords = 0; |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1517 | peek_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1518 | |
| 1519 | #ifdef FEAT_MBYTE_IME |
| 1520 | if (State & CMDLINE && msg_row == Rows - 1) |
| 1521 | { |
| 1522 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 1523 | |
| 1524 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 1525 | { |
| 1526 | if (csbi.dwCursorPosition.Y != msg_row) |
| 1527 | { |
| 1528 | /* The screen is now messed up, must redraw the |
| 1529 | * command line and later all the windows. */ |
| 1530 | redraw_all_later(CLEAR); |
| 1531 | cmdline_row -= (msg_row - csbi.dwCursorPosition.Y); |
| 1532 | redrawcmd(); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | #endif |
| 1537 | |
| 1538 | if (cRecords > 0) |
| 1539 | { |
| 1540 | if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown) |
| 1541 | { |
| 1542 | #ifdef FEAT_MBYTE_IME |
| 1543 | /* Windows IME sends two '\n's with only one 'ENTER'. First: |
| 1544 | * wVirtualKeyCode == 13. second: wVirtualKeyCode == 0 */ |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1545 | if (ir.Event.KeyEvent.UChar == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | && ir.Event.KeyEvent.wVirtualKeyCode == 13) |
| 1547 | { |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1548 | read_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | continue; |
| 1550 | } |
| 1551 | #endif |
| 1552 | if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2, |
| 1553 | NULL, FALSE)) |
| 1554 | return TRUE; |
| 1555 | } |
| 1556 | |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1557 | read_console_input(g_hConIn, &ir, 1, &cRecords); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1558 | |
| 1559 | if (ir.EventType == FOCUS_EVENT) |
| 1560 | handle_focus_event(ir); |
| 1561 | else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT) |
| 1562 | shell_resized(); |
| 1563 | #ifdef FEAT_MOUSE |
| 1564 | else if (ir.EventType == MOUSE_EVENT |
| 1565 | && decode_mouse_event(&ir.Event.MouseEvent)) |
| 1566 | return TRUE; |
| 1567 | #endif |
| 1568 | } |
| 1569 | else if (msec == 0) |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | #ifdef FEAT_CLIENTSERVER |
| 1574 | /* Something might have been received while we were waiting. */ |
| 1575 | if (input_available()) |
| 1576 | return TRUE; |
| 1577 | #endif |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1578 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1579 | return FALSE; |
| 1580 | } |
| 1581 | |
| 1582 | #ifndef FEAT_GUI_MSWIN |
| 1583 | /* |
| 1584 | * return non-zero if a character is available |
| 1585 | */ |
| 1586 | int |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1587 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1588 | { |
| 1589 | return WaitForChar(0L); |
| 1590 | } |
| 1591 | #endif |
| 1592 | |
| 1593 | /* |
| 1594 | * Create the console input. Used when reading stdin doesn't work. |
| 1595 | */ |
| 1596 | static void |
| 1597 | create_conin(void) |
| 1598 | { |
| 1599 | g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE, |
| 1600 | FILE_SHARE_READ|FILE_SHARE_WRITE, |
| 1601 | (LPSECURITY_ATTRIBUTES) NULL, |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 1602 | OPEN_EXISTING, 0, (HANDLE)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1603 | did_create_conin = TRUE; |
| 1604 | } |
| 1605 | |
| 1606 | /* |
| 1607 | * Get a keystroke or a mouse event |
| 1608 | */ |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1609 | static WCHAR |
| 1610 | tgetch(int *pmodifiers, WCHAR *pch2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1611 | { |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1612 | WCHAR ch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | |
| 1614 | for (;;) |
| 1615 | { |
| 1616 | INPUT_RECORD ir; |
| 1617 | DWORD cRecords = 0; |
| 1618 | |
| 1619 | #ifdef FEAT_CLIENTSERVER |
| 1620 | (void)WaitForChar(-1L); |
| 1621 | if (input_available()) |
| 1622 | return 0; |
| 1623 | # ifdef FEAT_MOUSE |
| 1624 | if (g_nMouseClick != -1) |
| 1625 | return 0; |
| 1626 | # endif |
| 1627 | #endif |
Bram Moolenaar | 3a69e11 | 2014-01-10 13:51:42 +0100 | [diff] [blame] | 1628 | if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1629 | { |
| 1630 | if (did_create_conin) |
| 1631 | read_error_exit(); |
| 1632 | create_conin(); |
| 1633 | continue; |
| 1634 | } |
| 1635 | |
| 1636 | if (ir.EventType == KEY_EVENT) |
| 1637 | { |
| 1638 | if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2, |
| 1639 | pmodifiers, TRUE)) |
| 1640 | return ch; |
| 1641 | } |
| 1642 | else if (ir.EventType == FOCUS_EVENT) |
| 1643 | handle_focus_event(ir); |
| 1644 | else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT) |
| 1645 | shell_resized(); |
| 1646 | #ifdef FEAT_MOUSE |
| 1647 | else if (ir.EventType == MOUSE_EVENT) |
| 1648 | { |
| 1649 | if (decode_mouse_event(&ir.Event.MouseEvent)) |
| 1650 | return 0; |
| 1651 | } |
| 1652 | #endif |
| 1653 | } |
| 1654 | } |
| 1655 | #endif /* !FEAT_GUI_W32 */ |
| 1656 | |
| 1657 | |
| 1658 | /* |
Bram Moolenaar | f6a2b08 | 2012-06-29 13:14:03 +0200 | [diff] [blame] | 1659 | * mch_inchar(): low-level input function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | * Get one or more characters from the keyboard or the mouse. |
| 1661 | * If time == 0, do not wait for characters. |
| 1662 | * If time == n, wait a short time for characters. |
| 1663 | * If time == -1, wait forever for characters. |
| 1664 | * Returns the number of characters read into buf. |
| 1665 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1666 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | int |
| 1668 | mch_inchar( |
| 1669 | char_u *buf, |
| 1670 | int maxlen, |
| 1671 | long time, |
| 1672 | int tb_change_cnt) |
| 1673 | { |
| 1674 | #ifndef FEAT_GUI_W32 /* this isn't used for the GUI */ |
| 1675 | |
| 1676 | int len; |
| 1677 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | #define TYPEAHEADLEN 20 |
| 1679 | static char_u typeahead[TYPEAHEADLEN]; /* previously typed bytes. */ |
| 1680 | static int typeaheadlen = 0; |
| 1681 | |
| 1682 | /* First use any typeahead that was kept because "buf" was too small. */ |
| 1683 | if (typeaheadlen > 0) |
| 1684 | goto theend; |
| 1685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1686 | if (time >= 0) |
| 1687 | { |
| 1688 | if (!WaitForChar(time)) /* no character available */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1690 | } |
| 1691 | else /* time == -1, wait forever */ |
| 1692 | { |
| 1693 | mch_set_winsize_now(); /* Allow winsize changes from now on */ |
| 1694 | |
Bram Moolenaar | 3918c95 | 2005-03-15 22:34:55 +0000 | [diff] [blame] | 1695 | /* |
| 1696 | * If there is no character available within 2 seconds (default) |
| 1697 | * write the autoscript file to disk. Or cause the CursorHold event |
| 1698 | * to be triggered. |
| 1699 | */ |
| 1700 | if (!WaitForChar(p_ut)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1701 | { |
| 1702 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 1703 | if (trigger_cursorhold() && maxlen >= 3) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1704 | { |
Bram Moolenaar | 3918c95 | 2005-03-15 22:34:55 +0000 | [diff] [blame] | 1705 | buf[0] = K_SPECIAL; |
| 1706 | buf[1] = KS_EXTRA; |
| 1707 | buf[2] = (int)KE_CURSORHOLD; |
| 1708 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1709 | } |
| 1710 | #endif |
Bram Moolenaar | 702517d | 2005-06-27 22:34:07 +0000 | [diff] [blame] | 1711 | before_blocking(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * Try to read as many characters as there are, until the buffer is full. |
| 1717 | */ |
| 1718 | |
| 1719 | /* we will get at least one key. Get more if they are available. */ |
| 1720 | g_fCBrkPressed = FALSE; |
| 1721 | |
| 1722 | #ifdef MCH_WRITE_DUMP |
| 1723 | if (fdDump) |
| 1724 | fputc('[', fdDump); |
| 1725 | #endif |
| 1726 | |
| 1727 | /* Keep looping until there is something in the typeahead buffer and more |
| 1728 | * to get and still room in the buffer (up to two bytes for a char and |
| 1729 | * three bytes for a modifier). */ |
| 1730 | while ((typeaheadlen == 0 || WaitForChar(0L)) |
| 1731 | && typeaheadlen + 5 <= TYPEAHEADLEN) |
| 1732 | { |
| 1733 | if (typebuf_changed(tb_change_cnt)) |
| 1734 | { |
| 1735 | /* "buf" may be invalid now if a client put something in the |
| 1736 | * typeahead buffer and "buf" is in the typeahead buffer. */ |
| 1737 | typeaheadlen = 0; |
| 1738 | break; |
| 1739 | } |
| 1740 | #ifdef FEAT_MOUSE |
| 1741 | if (g_nMouseClick != -1) |
| 1742 | { |
| 1743 | # ifdef MCH_WRITE_DUMP |
| 1744 | if (fdDump) |
| 1745 | fprintf(fdDump, "{%02x @ %d, %d}", |
| 1746 | g_nMouseClick, g_xMouse, g_yMouse); |
| 1747 | # endif |
| 1748 | typeahead[typeaheadlen++] = ESC + 128; |
| 1749 | typeahead[typeaheadlen++] = 'M'; |
| 1750 | typeahead[typeaheadlen++] = g_nMouseClick; |
| 1751 | typeahead[typeaheadlen++] = g_xMouse + '!'; |
| 1752 | typeahead[typeaheadlen++] = g_yMouse + '!'; |
| 1753 | g_nMouseClick = -1; |
| 1754 | } |
| 1755 | else |
| 1756 | #endif |
| 1757 | { |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1758 | WCHAR ch2 = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1759 | int modifiers = 0; |
| 1760 | |
| 1761 | c = tgetch(&modifiers, &ch2); |
| 1762 | |
| 1763 | if (typebuf_changed(tb_change_cnt)) |
| 1764 | { |
| 1765 | /* "buf" may be invalid now if a client put something in the |
| 1766 | * typeahead buffer and "buf" is in the typeahead buffer. */ |
| 1767 | typeaheadlen = 0; |
| 1768 | break; |
| 1769 | } |
| 1770 | |
| 1771 | if (c == Ctrl_C && ctrl_c_interrupts) |
| 1772 | { |
| 1773 | #if defined(FEAT_CLIENTSERVER) |
| 1774 | trash_input_buf(); |
| 1775 | #endif |
| 1776 | got_int = TRUE; |
| 1777 | } |
| 1778 | |
| 1779 | #ifdef FEAT_MOUSE |
| 1780 | if (g_nMouseClick == -1) |
| 1781 | #endif |
| 1782 | { |
| 1783 | int n = 1; |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1784 | int conv = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1786 | #ifdef FEAT_MBYTE |
| 1787 | if (ch2 == NUL) |
| 1788 | { |
| 1789 | int i; |
| 1790 | char_u *p; |
| 1791 | WCHAR ch[2]; |
| 1792 | |
| 1793 | ch[0] = c; |
| 1794 | if (c >= 0xD800 && c <= 0xDBFF) /* High surrogate */ |
| 1795 | { |
| 1796 | ch[1] = tgetch(&modifiers, &ch2); |
| 1797 | n++; |
| 1798 | } |
| 1799 | p = utf16_to_enc(ch, &n); |
| 1800 | if (p != NULL) |
| 1801 | { |
| 1802 | for (i = 0; i < n; i++) |
| 1803 | typeahead[typeaheadlen + i] = p[i]; |
| 1804 | vim_free(p); |
| 1805 | } |
| 1806 | } |
| 1807 | else |
| 1808 | #endif |
| 1809 | typeahead[typeaheadlen] = c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | if (ch2 != NUL) |
| 1811 | { |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 1812 | typeahead[typeaheadlen + n] = 3; |
| 1813 | typeahead[typeaheadlen + n + 1] = (char_u)ch2; |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1814 | n += 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1815 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1816 | |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1817 | if (conv) |
| 1818 | { |
| 1819 | char_u *p = typeahead + typeaheadlen; |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1820 | |
Bram Moolenaar | 1ec4dd4 | 2015-01-20 19:39:35 +0100 | [diff] [blame] | 1821 | if (*p != K_NUL) |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1822 | { |
Bram Moolenaar | 1ec4dd4 | 2015-01-20 19:39:35 +0100 | [diff] [blame] | 1823 | char_u *e = typeahead + TYPEAHEADLEN; |
| 1824 | |
| 1825 | while (*p && p < e) |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1826 | { |
Bram Moolenaar | 1ec4dd4 | 2015-01-20 19:39:35 +0100 | [diff] [blame] | 1827 | if (*p == K_NUL) |
| 1828 | { |
| 1829 | ++p; |
| 1830 | mch_memmove(p + 1, p, ((size_t)(e - p)) - 1); |
| 1831 | *p = 3; |
| 1832 | ++n; |
| 1833 | } |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1834 | ++p; |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1835 | } |
Bram Moolenaar | 4550091 | 2014-07-09 20:51:07 +0200 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | /* Use the ALT key to set the 8th bit of the character |
| 1840 | * when it's one byte, the 8th bit isn't set yet and not |
| 1841 | * using a double-byte encoding (would become a lead |
| 1842 | * byte). */ |
| 1843 | if ((modifiers & MOD_MASK_ALT) |
| 1844 | && n == 1 |
| 1845 | && (typeahead[typeaheadlen] & 0x80) == 0 |
| 1846 | #ifdef FEAT_MBYTE |
| 1847 | && !enc_dbcs |
| 1848 | #endif |
| 1849 | ) |
| 1850 | { |
Bram Moolenaar | 85a3e5c | 2007-11-20 16:22:16 +0000 | [diff] [blame] | 1851 | #ifdef FEAT_MBYTE |
| 1852 | n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80, |
| 1853 | typeahead + typeaheadlen); |
| 1854 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | typeahead[typeaheadlen] |= 0x80; |
Bram Moolenaar | 85a3e5c | 2007-11-20 16:22:16 +0000 | [diff] [blame] | 1856 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | modifiers &= ~MOD_MASK_ALT; |
| 1858 | } |
| 1859 | |
| 1860 | if (modifiers != 0) |
| 1861 | { |
| 1862 | /* Prepend modifiers to the character. */ |
| 1863 | mch_memmove(typeahead + typeaheadlen + 3, |
| 1864 | typeahead + typeaheadlen, n); |
| 1865 | typeahead[typeaheadlen++] = K_SPECIAL; |
| 1866 | typeahead[typeaheadlen++] = (char_u)KS_MODIFIER; |
| 1867 | typeahead[typeaheadlen++] = modifiers; |
| 1868 | } |
| 1869 | |
| 1870 | typeaheadlen += n; |
| 1871 | |
| 1872 | #ifdef MCH_WRITE_DUMP |
| 1873 | if (fdDump) |
| 1874 | fputc(c, fdDump); |
| 1875 | #endif |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | #ifdef MCH_WRITE_DUMP |
| 1881 | if (fdDump) |
| 1882 | { |
| 1883 | fputs("]\n", fdDump); |
| 1884 | fflush(fdDump); |
| 1885 | } |
| 1886 | #endif |
| 1887 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1888 | theend: |
| 1889 | /* Move typeahead to "buf", as much as fits. */ |
| 1890 | len = 0; |
| 1891 | while (len < maxlen && typeaheadlen > 0) |
| 1892 | { |
| 1893 | buf[len++] = typeahead[0]; |
| 1894 | mch_memmove(typeahead, typeahead + 1, --typeaheadlen); |
| 1895 | } |
| 1896 | return len; |
| 1897 | |
| 1898 | #else /* FEAT_GUI_W32 */ |
| 1899 | return 0; |
| 1900 | #endif /* FEAT_GUI_W32 */ |
| 1901 | } |
| 1902 | |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 1903 | #ifndef PROTO |
| 1904 | # ifndef __MINGW32__ |
| 1905 | # include <shellapi.h> /* required for FindExecutable() */ |
| 1906 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | #endif |
| 1908 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1909 | /* |
| 1910 | * Return TRUE if "name" is in $PATH. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1911 | * TODO: Should somehow check if it's really executable. |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1912 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1913 | static int |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 1914 | executable_exists(char *name, char_u **path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | { |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1916 | char *dum; |
| 1917 | char fname[_MAX_PATH]; |
Bram Moolenaar | c40bdee | 2014-08-29 17:45:32 +0200 | [diff] [blame] | 1918 | char *curpath, *newpath; |
| 1919 | long n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1920 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1921 | #ifdef FEAT_MBYTE |
| 1922 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1924 | WCHAR *p = enc_to_utf16((char_u *)name, NULL); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1925 | WCHAR fnamew[_MAX_PATH]; |
| 1926 | WCHAR *dumw; |
Bram Moolenaar | c40bdee | 2014-08-29 17:45:32 +0200 | [diff] [blame] | 1927 | WCHAR *wcurpath, *wnewpath; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (p != NULL) |
| 1930 | { |
Bram Moolenaar | c40bdee | 2014-08-29 17:45:32 +0200 | [diff] [blame] | 1931 | wcurpath = _wgetenv(L"PATH"); |
| 1932 | wnewpath = (WCHAR*)alloc((unsigned)(wcslen(wcurpath) + 3) |
| 1933 | * sizeof(WCHAR)); |
| 1934 | if (wnewpath == NULL) |
| 1935 | return FALSE; |
| 1936 | wcscpy(wnewpath, L".;"); |
| 1937 | wcscat(wnewpath, wcurpath); |
| 1938 | n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw); |
| 1939 | vim_free(wnewpath); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1940 | vim_free(p); |
| 1941 | if (n > 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1942 | { |
| 1943 | if (n == 0) |
| 1944 | return FALSE; |
| 1945 | if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY) |
| 1946 | return FALSE; |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 1947 | if (path != NULL) |
| 1948 | *path = utf16_to_enc(fnamew, NULL); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1949 | return TRUE; |
| 1950 | } |
| 1951 | /* Retry with non-wide function (for Windows 98). */ |
| 1952 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1954 | #endif |
Bram Moolenaar | c40bdee | 2014-08-29 17:45:32 +0200 | [diff] [blame] | 1955 | |
| 1956 | curpath = getenv("PATH"); |
| 1957 | newpath = (char*)alloc((unsigned)(STRLEN(curpath) + 3)); |
| 1958 | if (newpath == NULL) |
| 1959 | return FALSE; |
| 1960 | STRCPY(newpath, ".;"); |
| 1961 | STRCAT(newpath, curpath); |
| 1962 | n = (long)SearchPath(newpath, name, NULL, _MAX_PATH, fname, &dum); |
| 1963 | vim_free(newpath); |
| 1964 | if (n == 0) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1965 | return FALSE; |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1966 | if (mch_isdir((char_u *)fname)) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1967 | return FALSE; |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 1968 | if (path != NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1969 | *path = vim_strsave((char_u *)fname); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1970 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 1973 | #if ((defined(__MINGW32__) || defined (__CYGWIN32__)) && \ |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 1974 | __MSVCRT_VERSION__ >= 0x800) || (defined(_MSC_VER) && _MSC_VER >= 1400) |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 1975 | /* |
| 1976 | * Bad parameter handler. |
| 1977 | * |
| 1978 | * Certain MS CRT functions will intentionally crash when passed invalid |
| 1979 | * parameters to highlight possible security holes. Setting this function as |
| 1980 | * the bad parameter handler will prevent the crash. |
| 1981 | * |
| 1982 | * In debug builds the parameters contain CRT information that might help track |
| 1983 | * down the source of a problem, but in non-debug builds the arguments are all |
| 1984 | * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is |
| 1985 | * worth allowing these to make debugging of issues easier. |
| 1986 | */ |
| 1987 | static void |
| 1988 | bad_param_handler(const wchar_t *expression, |
| 1989 | const wchar_t *function, |
| 1990 | const wchar_t *file, |
| 1991 | unsigned int line, |
| 1992 | uintptr_t pReserved) |
| 1993 | { |
| 1994 | } |
| 1995 | |
| 1996 | # define SET_INVALID_PARAM_HANDLER \ |
| 1997 | ((void)_set_invalid_parameter_handler(bad_param_handler)) |
| 1998 | #else |
| 1999 | # define SET_INVALID_PARAM_HANDLER |
| 2000 | #endif |
| 2001 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2002 | #ifdef FEAT_GUI_W32 |
| 2003 | |
| 2004 | /* |
| 2005 | * GUI version of mch_init(). |
| 2006 | */ |
| 2007 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2008 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | { |
| 2010 | #ifndef __MINGW32__ |
| 2011 | extern int _fmode; |
| 2012 | #endif |
| 2013 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 2014 | /* Silently handle invalid parameters to CRT functions */ |
| 2015 | SET_INVALID_PARAM_HANDLER; |
| 2016 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2017 | /* Let critical errors result in a failure, not in a dialog box. Required |
| 2018 | * for the timestamp test to work on removed floppies. */ |
| 2019 | SetErrorMode(SEM_FAILCRITICALERRORS); |
| 2020 | |
| 2021 | _fmode = O_BINARY; /* we do our own CR-LF translation */ |
| 2022 | |
| 2023 | /* Specify window size. Is there a place to get the default from? */ |
| 2024 | Rows = 25; |
| 2025 | Columns = 80; |
| 2026 | |
| 2027 | /* Look for 'vimrun' */ |
| 2028 | if (!gui_is_win32s()) |
| 2029 | { |
| 2030 | char_u vimrun_location[_MAX_PATH + 4]; |
| 2031 | |
| 2032 | /* First try in same directory as gvim.exe */ |
| 2033 | STRCPY(vimrun_location, exe_name); |
| 2034 | STRCPY(gettail(vimrun_location), "vimrun.exe"); |
| 2035 | if (mch_getperm(vimrun_location) >= 0) |
| 2036 | { |
| 2037 | if (*skiptowhite(vimrun_location) != NUL) |
| 2038 | { |
| 2039 | /* Enclose path with white space in double quotes. */ |
| 2040 | mch_memmove(vimrun_location + 1, vimrun_location, |
| 2041 | STRLEN(vimrun_location) + 1); |
| 2042 | *vimrun_location = '"'; |
| 2043 | STRCPY(gettail(vimrun_location), "vimrun\" "); |
| 2044 | } |
| 2045 | else |
| 2046 | STRCPY(gettail(vimrun_location), "vimrun "); |
| 2047 | |
| 2048 | vimrun_path = (char *)vim_strsave(vimrun_location); |
| 2049 | s_dont_use_vimrun = FALSE; |
| 2050 | } |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 2051 | else if (executable_exists("vimrun.exe", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2052 | s_dont_use_vimrun = FALSE; |
| 2053 | |
| 2054 | /* Don't give the warning for a missing vimrun.exe right now, but only |
| 2055 | * when vimrun was supposed to be used. Don't bother people that do |
| 2056 | * not need vimrun.exe. */ |
| 2057 | if (s_dont_use_vimrun) |
| 2058 | need_vimrun_warning = TRUE; |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'. |
| 2063 | * Otherwise the default "findstr /n" is used. |
| 2064 | */ |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 2065 | if (!executable_exists("findstr.exe", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2066 | set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0); |
| 2067 | |
| 2068 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 2069 | win_clip_init(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2070 | #endif |
| 2071 | } |
| 2072 | |
| 2073 | |
| 2074 | #else /* FEAT_GUI_W32 */ |
| 2075 | |
| 2076 | #define SRWIDTH(sr) ((sr).Right - (sr).Left + 1) |
| 2077 | #define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1) |
| 2078 | |
| 2079 | /* |
| 2080 | * ClearConsoleBuffer() |
| 2081 | * Description: |
| 2082 | * Clears the entire contents of the console screen buffer, using the |
| 2083 | * specified attribute. |
| 2084 | * Returns: |
| 2085 | * TRUE on success |
| 2086 | */ |
| 2087 | static BOOL |
| 2088 | ClearConsoleBuffer(WORD wAttribute) |
| 2089 | { |
| 2090 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 2091 | COORD coord; |
| 2092 | DWORD NumCells, dummy; |
| 2093 | |
| 2094 | if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 2095 | return FALSE; |
| 2096 | |
| 2097 | NumCells = csbi.dwSize.X * csbi.dwSize.Y; |
| 2098 | coord.X = 0; |
| 2099 | coord.Y = 0; |
| 2100 | if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells, |
| 2101 | coord, &dummy)) |
| 2102 | { |
| 2103 | return FALSE; |
| 2104 | } |
| 2105 | if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells, |
| 2106 | coord, &dummy)) |
| 2107 | { |
| 2108 | return FALSE; |
| 2109 | } |
| 2110 | |
| 2111 | return TRUE; |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * FitConsoleWindow() |
| 2116 | * Description: |
| 2117 | * Checks if the console window will fit within given buffer dimensions. |
| 2118 | * Also, if requested, will shrink the window to fit. |
| 2119 | * Returns: |
| 2120 | * TRUE on success |
| 2121 | */ |
| 2122 | static BOOL |
| 2123 | FitConsoleWindow( |
| 2124 | COORD dwBufferSize, |
| 2125 | BOOL WantAdjust) |
| 2126 | { |
| 2127 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 2128 | COORD dwWindowSize; |
| 2129 | BOOL NeedAdjust = FALSE; |
| 2130 | |
| 2131 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 2132 | { |
| 2133 | /* |
| 2134 | * A buffer resize will fail if the current console window does |
| 2135 | * not lie completely within that buffer. To avoid this, we might |
| 2136 | * have to move and possibly shrink the window. |
| 2137 | */ |
| 2138 | if (csbi.srWindow.Right >= dwBufferSize.X) |
| 2139 | { |
| 2140 | dwWindowSize.X = SRWIDTH(csbi.srWindow); |
| 2141 | if (dwWindowSize.X > dwBufferSize.X) |
| 2142 | dwWindowSize.X = dwBufferSize.X; |
| 2143 | csbi.srWindow.Right = dwBufferSize.X - 1; |
| 2144 | csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X; |
| 2145 | NeedAdjust = TRUE; |
| 2146 | } |
| 2147 | if (csbi.srWindow.Bottom >= dwBufferSize.Y) |
| 2148 | { |
| 2149 | dwWindowSize.Y = SRHEIGHT(csbi.srWindow); |
| 2150 | if (dwWindowSize.Y > dwBufferSize.Y) |
| 2151 | dwWindowSize.Y = dwBufferSize.Y; |
| 2152 | csbi.srWindow.Bottom = dwBufferSize.Y - 1; |
| 2153 | csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y; |
| 2154 | NeedAdjust = TRUE; |
| 2155 | } |
| 2156 | if (NeedAdjust && WantAdjust) |
| 2157 | { |
| 2158 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow)) |
| 2159 | return FALSE; |
| 2160 | } |
| 2161 | return TRUE; |
| 2162 | } |
| 2163 | |
| 2164 | return FALSE; |
| 2165 | } |
| 2166 | |
| 2167 | typedef struct ConsoleBufferStruct |
| 2168 | { |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2169 | BOOL IsValid; |
| 2170 | CONSOLE_SCREEN_BUFFER_INFO Info; |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2171 | PCHAR_INFO Buffer; |
| 2172 | COORD BufferSize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2173 | } ConsoleBuffer; |
| 2174 | |
| 2175 | /* |
| 2176 | * SaveConsoleBuffer() |
| 2177 | * Description: |
| 2178 | * Saves important information about the console buffer, including the |
| 2179 | * actual buffer contents. The saved information is suitable for later |
| 2180 | * restoration by RestoreConsoleBuffer(). |
| 2181 | * Returns: |
| 2182 | * TRUE if all information was saved; FALSE otherwise |
| 2183 | * If FALSE, still sets cb->IsValid if buffer characteristics were saved. |
| 2184 | */ |
| 2185 | static BOOL |
| 2186 | SaveConsoleBuffer( |
| 2187 | ConsoleBuffer *cb) |
| 2188 | { |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2189 | DWORD NumCells; |
| 2190 | COORD BufferCoord; |
| 2191 | SMALL_RECT ReadRegion; |
| 2192 | WORD Y, Y_incr; |
| 2193 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2194 | if (cb == NULL) |
| 2195 | return FALSE; |
| 2196 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2197 | if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | { |
| 2199 | cb->IsValid = FALSE; |
| 2200 | return FALSE; |
| 2201 | } |
| 2202 | cb->IsValid = TRUE; |
| 2203 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2204 | /* |
| 2205 | * Allocate a buffer large enough to hold the entire console screen |
| 2206 | * buffer. If this ConsoleBuffer structure has already been initialized |
| 2207 | * with a buffer of the correct size, then just use that one. |
| 2208 | */ |
| 2209 | if (!cb->IsValid || cb->Buffer == NULL || |
| 2210 | cb->BufferSize.X != cb->Info.dwSize.X || |
| 2211 | cb->BufferSize.Y != cb->Info.dwSize.Y) |
| 2212 | { |
| 2213 | cb->BufferSize.X = cb->Info.dwSize.X; |
| 2214 | cb->BufferSize.Y = cb->Info.dwSize.Y; |
| 2215 | NumCells = cb->BufferSize.X * cb->BufferSize.Y; |
| 2216 | vim_free(cb->Buffer); |
| 2217 | cb->Buffer = (PCHAR_INFO)alloc(NumCells * sizeof(CHAR_INFO)); |
| 2218 | if (cb->Buffer == NULL) |
| 2219 | return FALSE; |
| 2220 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2221 | |
| 2222 | /* |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2223 | * We will now copy the console screen buffer into our buffer. |
| 2224 | * ReadConsoleOutput() seems to be limited as far as how much you |
| 2225 | * can read at a time. Empirically, this number seems to be about |
| 2226 | * 12000 cells (rows * columns). Start at position (0, 0) and copy |
| 2227 | * in chunks until it is all copied. The chunks will all have the |
| 2228 | * same horizontal characteristics, so initialize them now. The |
| 2229 | * height of each chunk will be (12000 / width). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2230 | */ |
Bram Moolenaar | 6159424 | 2015-09-01 20:23:37 +0200 | [diff] [blame] | 2231 | BufferCoord.X = 0; |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2232 | ReadRegion.Left = 0; |
| 2233 | ReadRegion.Right = cb->Info.dwSize.X - 1; |
| 2234 | Y_incr = 12000 / cb->Info.dwSize.X; |
| 2235 | for (Y = 0; Y < cb->BufferSize.Y; Y += Y_incr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | { |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2237 | /* |
| 2238 | * Read into position (0, Y) in our buffer. |
| 2239 | */ |
| 2240 | BufferCoord.Y = Y; |
| 2241 | /* |
| 2242 | * Read the region whose top left corner is (0, Y) and whose bottom |
| 2243 | * right corner is (width - 1, Y + Y_incr - 1). This should define |
| 2244 | * a region of size width by Y_incr. Don't worry if this region is |
| 2245 | * too large for the remaining buffer; it will be cropped. |
| 2246 | */ |
| 2247 | ReadRegion.Top = Y; |
| 2248 | ReadRegion.Bottom = Y + Y_incr - 1; |
| 2249 | if (!ReadConsoleOutput(g_hConOut, /* output handle */ |
| 2250 | cb->Buffer, /* our buffer */ |
| 2251 | cb->BufferSize, /* dimensions of our buffer */ |
| 2252 | BufferCoord, /* offset in our buffer */ |
| 2253 | &ReadRegion)) /* region to save */ |
| 2254 | { |
| 2255 | vim_free(cb->Buffer); |
| 2256 | cb->Buffer = NULL; |
| 2257 | return FALSE; |
| 2258 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | return TRUE; |
| 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * RestoreConsoleBuffer() |
| 2266 | * Description: |
| 2267 | * Restores important information about the console buffer, including the |
| 2268 | * actual buffer contents, if desired. The information to restore is in |
| 2269 | * the same format used by SaveConsoleBuffer(). |
| 2270 | * Returns: |
| 2271 | * TRUE on success |
| 2272 | */ |
| 2273 | static BOOL |
| 2274 | RestoreConsoleBuffer( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2275 | ConsoleBuffer *cb, |
| 2276 | BOOL RestoreScreen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | { |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2278 | COORD BufferCoord; |
| 2279 | SMALL_RECT WriteRegion; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2280 | |
| 2281 | if (cb == NULL || !cb->IsValid) |
| 2282 | return FALSE; |
| 2283 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2284 | /* |
| 2285 | * Before restoring the buffer contents, clear the current buffer, and |
| 2286 | * restore the cursor position and window information. Doing this now |
| 2287 | * prevents old buffer contents from "flashing" onto the screen. |
| 2288 | */ |
| 2289 | if (RestoreScreen) |
| 2290 | ClearConsoleBuffer(cb->Info.wAttributes); |
| 2291 | |
| 2292 | FitConsoleWindow(cb->Info.dwSize, TRUE); |
| 2293 | if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize)) |
| 2294 | return FALSE; |
| 2295 | if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes)) |
| 2296 | return FALSE; |
| 2297 | |
| 2298 | if (!RestoreScreen) |
| 2299 | { |
| 2300 | /* |
| 2301 | * No need to restore the screen buffer contents, so we're done. |
| 2302 | */ |
| 2303 | return TRUE; |
| 2304 | } |
| 2305 | |
| 2306 | if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition)) |
| 2307 | return FALSE; |
| 2308 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow)) |
| 2309 | return FALSE; |
| 2310 | |
| 2311 | /* |
| 2312 | * Restore the screen buffer contents. |
| 2313 | */ |
| 2314 | if (cb->Buffer != NULL) |
| 2315 | { |
| 2316 | BufferCoord.X = 0; |
| 2317 | BufferCoord.Y = 0; |
| 2318 | WriteRegion.Left = 0; |
| 2319 | WriteRegion.Top = 0; |
| 2320 | WriteRegion.Right = cb->Info.dwSize.X - 1; |
| 2321 | WriteRegion.Bottom = cb->Info.dwSize.Y - 1; |
| 2322 | if (!WriteConsoleOutput(g_hConOut, /* output handle */ |
| 2323 | cb->Buffer, /* our buffer */ |
| 2324 | cb->BufferSize, /* dimensions of our buffer */ |
| 2325 | BufferCoord, /* offset in our buffer */ |
| 2326 | &WriteRegion)) /* region to restore */ |
| 2327 | { |
| 2328 | return FALSE; |
| 2329 | } |
| 2330 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | |
| 2332 | return TRUE; |
| 2333 | } |
| 2334 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2335 | #define FEAT_RESTORE_ORIG_SCREEN |
| 2336 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 2337 | static ConsoleBuffer g_cbOrig = { 0 }; |
| 2338 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | static ConsoleBuffer g_cbNonTermcap = { 0 }; |
| 2340 | static ConsoleBuffer g_cbTermcap = { 0 }; |
| 2341 | |
| 2342 | #ifdef FEAT_TITLE |
| 2343 | #ifdef __BORLANDC__ |
| 2344 | typedef HWND (__stdcall *GETCONSOLEWINDOWPROC)(VOID); |
| 2345 | #else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2346 | typedef HWND (WINAPI *GETCONSOLEWINDOWPROC)(VOID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | #endif |
| 2348 | char g_szOrigTitle[256] = { 0 }; |
| 2349 | HWND g_hWnd = NULL; /* also used in os_mswin.c */ |
| 2350 | static HICON g_hOrigIconSmall = NULL; |
| 2351 | static HICON g_hOrigIcon = NULL; |
| 2352 | static HICON g_hVimIcon = NULL; |
| 2353 | static BOOL g_fCanChangeIcon = FALSE; |
| 2354 | |
| 2355 | /* ICON* are not defined in VC++ 4.0 */ |
| 2356 | #ifndef ICON_SMALL |
| 2357 | #define ICON_SMALL 0 |
| 2358 | #endif |
| 2359 | #ifndef ICON_BIG |
| 2360 | #define ICON_BIG 1 |
| 2361 | #endif |
| 2362 | /* |
| 2363 | * GetConsoleIcon() |
| 2364 | * Description: |
| 2365 | * Attempts to retrieve the small icon and/or the big icon currently in |
| 2366 | * use by a given window. |
| 2367 | * Returns: |
| 2368 | * TRUE on success |
| 2369 | */ |
| 2370 | static BOOL |
| 2371 | GetConsoleIcon( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2372 | HWND hWnd, |
| 2373 | HICON *phIconSmall, |
| 2374 | HICON *phIcon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | { |
| 2376 | if (hWnd == NULL) |
| 2377 | return FALSE; |
| 2378 | |
| 2379 | if (phIconSmall != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2380 | *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON, |
| 2381 | (WPARAM)ICON_SMALL, (LPARAM)0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | if (phIcon != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2383 | *phIcon = (HICON)SendMessage(hWnd, WM_GETICON, |
| 2384 | (WPARAM)ICON_BIG, (LPARAM)0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2385 | return TRUE; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | * SetConsoleIcon() |
| 2390 | * Description: |
| 2391 | * Attempts to change the small icon and/or the big icon currently in |
| 2392 | * use by a given window. |
| 2393 | * Returns: |
| 2394 | * TRUE on success |
| 2395 | */ |
| 2396 | static BOOL |
| 2397 | SetConsoleIcon( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2398 | HWND hWnd, |
| 2399 | HICON hIconSmall, |
| 2400 | HICON hIcon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | if (hWnd == NULL) |
| 2403 | return FALSE; |
| 2404 | |
| 2405 | if (hIconSmall != NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2406 | SendMessage(hWnd, WM_SETICON, |
| 2407 | (WPARAM)ICON_SMALL, (LPARAM)hIconSmall); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | if (hIcon != NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2409 | SendMessage(hWnd, WM_SETICON, |
| 2410 | (WPARAM)ICON_BIG, (LPARAM) hIcon); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2411 | return TRUE; |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * SaveConsoleTitleAndIcon() |
| 2416 | * Description: |
| 2417 | * Saves the current console window title in g_szOrigTitle, for later |
| 2418 | * restoration. Also, attempts to obtain a handle to the console window, |
| 2419 | * and use it to save the small and big icons currently in use by the |
| 2420 | * console window. This is not always possible on some versions of Windows; |
| 2421 | * nor is it possible when running Vim remotely using Telnet (since the |
| 2422 | * console window the user sees is owned by a remote process). |
| 2423 | */ |
| 2424 | static void |
| 2425 | SaveConsoleTitleAndIcon(void) |
| 2426 | { |
| 2427 | GETCONSOLEWINDOWPROC GetConsoleWindowProc; |
| 2428 | |
| 2429 | /* Save the original title. */ |
| 2430 | if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle))) |
| 2431 | return; |
| 2432 | |
| 2433 | /* |
| 2434 | * Obtain a handle to the console window using GetConsoleWindow() from |
| 2435 | * KERNEL32.DLL; we need to handle in order to change the window icon. |
| 2436 | * This function only exists on NT-based Windows, starting with Windows |
| 2437 | * 2000. On older operating systems, we can't change the window icon |
| 2438 | * anyway. |
| 2439 | */ |
| 2440 | if ((GetConsoleWindowProc = (GETCONSOLEWINDOWPROC) |
| 2441 | GetProcAddress(GetModuleHandle("KERNEL32.DLL"), |
| 2442 | "GetConsoleWindow")) != NULL) |
| 2443 | { |
| 2444 | g_hWnd = (*GetConsoleWindowProc)(); |
| 2445 | } |
| 2446 | if (g_hWnd == NULL) |
| 2447 | return; |
| 2448 | |
| 2449 | /* Save the original console window icon. */ |
| 2450 | GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon); |
| 2451 | if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL) |
| 2452 | return; |
| 2453 | |
| 2454 | /* Extract the first icon contained in the Vim executable. */ |
Bram Moolenaar | cddc91c | 2014-09-23 21:53:41 +0200 | [diff] [blame] | 2455 | if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2456 | g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2457 | if (g_hVimIcon != NULL) |
| 2458 | g_fCanChangeIcon = TRUE; |
| 2459 | } |
| 2460 | #endif |
| 2461 | |
| 2462 | static int g_fWindInitCalled = FALSE; |
| 2463 | static int g_fTermcapMode = FALSE; |
| 2464 | static CONSOLE_CURSOR_INFO g_cci; |
| 2465 | static DWORD g_cmodein = 0; |
| 2466 | static DWORD g_cmodeout = 0; |
| 2467 | |
| 2468 | /* |
| 2469 | * non-GUI version of mch_init(). |
| 2470 | */ |
| 2471 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2472 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | { |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2474 | #ifndef FEAT_RESTORE_ORIG_SCREEN |
| 2475 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 2476 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | #ifndef __MINGW32__ |
| 2478 | extern int _fmode; |
| 2479 | #endif |
| 2480 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 2481 | /* Silently handle invalid parameters to CRT functions */ |
| 2482 | SET_INVALID_PARAM_HANDLER; |
| 2483 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2484 | /* Let critical errors result in a failure, not in a dialog box. Required |
| 2485 | * for the timestamp test to work on removed floppies. */ |
| 2486 | SetErrorMode(SEM_FAILCRITICALERRORS); |
| 2487 | |
| 2488 | _fmode = O_BINARY; /* we do our own CR-LF translation */ |
| 2489 | out_flush(); |
| 2490 | |
| 2491 | /* Obtain handles for the standard Console I/O devices */ |
| 2492 | if (read_cmd_fd == 0) |
| 2493 | g_hConIn = GetStdHandle(STD_INPUT_HANDLE); |
| 2494 | else |
| 2495 | create_conin(); |
| 2496 | g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 2497 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2498 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 2499 | /* Save the initial console buffer for later restoration */ |
| 2500 | SaveConsoleBuffer(&g_cbOrig); |
| 2501 | g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes; |
| 2502 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | /* Get current text attributes */ |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 2504 | GetConsoleScreenBufferInfo(g_hConOut, &csbi); |
| 2505 | g_attrCurrent = g_attrDefault = csbi.wAttributes; |
| 2506 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2507 | if (cterm_normal_fg_color == 0) |
| 2508 | cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1; |
| 2509 | if (cterm_normal_bg_color == 0) |
| 2510 | cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1; |
| 2511 | |
| 2512 | /* set termcap codes to current text attributes */ |
| 2513 | update_tcap(g_attrCurrent); |
| 2514 | |
| 2515 | GetConsoleCursorInfo(g_hConOut, &g_cci); |
| 2516 | GetConsoleMode(g_hConIn, &g_cmodein); |
| 2517 | GetConsoleMode(g_hConOut, &g_cmodeout); |
| 2518 | |
| 2519 | #ifdef FEAT_TITLE |
| 2520 | SaveConsoleTitleAndIcon(); |
| 2521 | /* |
| 2522 | * Set both the small and big icons of the console window to Vim's icon. |
| 2523 | * Note that Vim presently only has one size of icon (32x32), but it |
| 2524 | * automatically gets scaled down to 16x16 when setting the small icon. |
| 2525 | */ |
| 2526 | if (g_fCanChangeIcon) |
| 2527 | SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon); |
| 2528 | #endif |
| 2529 | |
| 2530 | ui_get_shellsize(); |
| 2531 | |
| 2532 | #ifdef MCH_WRITE_DUMP |
| 2533 | fdDump = fopen("dump", "wt"); |
| 2534 | |
| 2535 | if (fdDump) |
| 2536 | { |
| 2537 | time_t t; |
| 2538 | |
| 2539 | time(&t); |
| 2540 | fputs(ctime(&t), fdDump); |
| 2541 | fflush(fdDump); |
| 2542 | } |
| 2543 | #endif |
| 2544 | |
| 2545 | g_fWindInitCalled = TRUE; |
| 2546 | |
| 2547 | #ifdef FEAT_MOUSE |
| 2548 | g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); |
| 2549 | #endif |
| 2550 | |
| 2551 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 2552 | win_clip_init(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2553 | #endif |
| 2554 | |
| 2555 | /* This will be NULL on anything but NT 4.0 */ |
| 2556 | s_pfnGetConsoleKeyboardLayoutName = |
| 2557 | (PFNGCKLN) GetProcAddress(GetModuleHandle("kernel32.dll"), |
| 2558 | "GetConsoleKeyboardLayoutNameA"); |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * non-GUI version of mch_exit(). |
| 2563 | * Shut down and exit with status `r' |
| 2564 | * Careful: mch_exit() may be called before mch_init()! |
| 2565 | */ |
| 2566 | void |
| 2567 | mch_exit(int r) |
| 2568 | { |
| 2569 | stoptermcap(); |
| 2570 | |
| 2571 | if (g_fWindInitCalled) |
| 2572 | settmode(TMODE_COOK); |
| 2573 | |
| 2574 | ml_close_all(TRUE); /* remove all memfiles */ |
| 2575 | |
| 2576 | if (g_fWindInitCalled) |
| 2577 | { |
| 2578 | #ifdef FEAT_TITLE |
| 2579 | mch_restore_title(3); |
| 2580 | /* |
| 2581 | * Restore both the small and big icons of the console window to |
| 2582 | * what they were at startup. Don't do this when the window is |
| 2583 | * closed, Vim would hang here. |
| 2584 | */ |
| 2585 | if (g_fCanChangeIcon && !g_fForceExit) |
| 2586 | SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon); |
| 2587 | #endif |
| 2588 | |
| 2589 | #ifdef MCH_WRITE_DUMP |
| 2590 | if (fdDump) |
| 2591 | { |
| 2592 | time_t t; |
| 2593 | |
| 2594 | time(&t); |
| 2595 | fputs(ctime(&t), fdDump); |
| 2596 | fclose(fdDump); |
| 2597 | } |
| 2598 | fdDump = NULL; |
| 2599 | #endif |
| 2600 | } |
| 2601 | |
| 2602 | SetConsoleCursorInfo(g_hConOut, &g_cci); |
| 2603 | SetConsoleMode(g_hConIn, g_cmodein); |
| 2604 | SetConsoleMode(g_hConOut, g_cmodeout); |
| 2605 | |
| 2606 | #ifdef DYNAMIC_GETTEXT |
| 2607 | dyn_libintl_end(); |
| 2608 | #endif |
| 2609 | |
| 2610 | exit(r); |
| 2611 | } |
| 2612 | #endif /* !FEAT_GUI_W32 */ |
| 2613 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | /* |
| 2615 | * Do we have an interactive window? |
| 2616 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2617 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | int |
| 2619 | mch_check_win( |
| 2620 | int argc, |
| 2621 | char **argv) |
| 2622 | { |
| 2623 | get_exe_name(); |
| 2624 | |
| 2625 | #ifdef FEAT_GUI_W32 |
| 2626 | return OK; /* GUI always has a tty */ |
| 2627 | #else |
| 2628 | if (isatty(1)) |
| 2629 | return OK; |
| 2630 | return FAIL; |
| 2631 | #endif |
| 2632 | } |
| 2633 | |
| 2634 | |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2635 | #ifdef FEAT_MBYTE |
| 2636 | /* |
| 2637 | * fname_casew(): Wide version of fname_case(). Set the case of the file name, |
| 2638 | * if it already exists. When "len" is > 0, also expand short to long |
| 2639 | * filenames. |
| 2640 | * Return FAIL if wide functions are not available, OK otherwise. |
| 2641 | * NOTE: much of this is identical to fname_case(), keep in sync! |
| 2642 | */ |
| 2643 | static int |
| 2644 | fname_casew( |
| 2645 | WCHAR *name, |
| 2646 | int len) |
| 2647 | { |
| 2648 | WCHAR szTrueName[_MAX_PATH + 2]; |
| 2649 | WCHAR szTrueNameTemp[_MAX_PATH + 2]; |
| 2650 | WCHAR *ptrue, *ptruePrev; |
| 2651 | WCHAR *porig, *porigPrev; |
| 2652 | int flen; |
| 2653 | WIN32_FIND_DATAW fb; |
Bram Moolenaar | 73c6163 | 2013-12-07 14:48:10 +0100 | [diff] [blame] | 2654 | HANDLE hFind = INVALID_HANDLE_VALUE; |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2655 | int c; |
| 2656 | int slen; |
| 2657 | |
| 2658 | flen = (int)wcslen(name); |
| 2659 | if (flen > _MAX_PATH) |
| 2660 | return OK; |
| 2661 | |
| 2662 | /* slash_adjust(name) not needed, already adjusted by fname_case(). */ |
| 2663 | |
| 2664 | /* Build the new name in szTrueName[] one component at a time. */ |
| 2665 | porig = name; |
| 2666 | ptrue = szTrueName; |
| 2667 | |
| 2668 | if (iswalpha(porig[0]) && porig[1] == L':') |
| 2669 | { |
| 2670 | /* copy leading drive letter */ |
| 2671 | *ptrue++ = *porig++; |
| 2672 | *ptrue++ = *porig++; |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2673 | } |
Bram Moolenaar | 73c6163 | 2013-12-07 14:48:10 +0100 | [diff] [blame] | 2674 | *ptrue = NUL; /* in case nothing follows */ |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2675 | |
| 2676 | while (*porig != NUL) |
| 2677 | { |
| 2678 | /* copy \ characters */ |
| 2679 | while (*porig == psepc) |
| 2680 | *ptrue++ = *porig++; |
| 2681 | |
| 2682 | ptruePrev = ptrue; |
| 2683 | porigPrev = porig; |
| 2684 | while (*porig != NUL && *porig != psepc) |
| 2685 | { |
| 2686 | *ptrue++ = *porig++; |
| 2687 | } |
| 2688 | *ptrue = NUL; |
| 2689 | |
| 2690 | /* To avoid a slow failure append "\*" when searching a directory, |
| 2691 | * server or network share. */ |
| 2692 | wcscpy(szTrueNameTemp, szTrueName); |
| 2693 | slen = (int)wcslen(szTrueNameTemp); |
| 2694 | if (*porig == psepc && slen + 2 < _MAX_PATH) |
| 2695 | wcscpy(szTrueNameTemp + slen, L"\\*"); |
| 2696 | |
| 2697 | /* Skip "", "." and "..". */ |
| 2698 | if (ptrue > ptruePrev |
| 2699 | && (ptruePrev[0] != L'.' |
| 2700 | || (ptruePrev[1] != NUL |
| 2701 | && (ptruePrev[1] != L'.' || ptruePrev[2] != NUL))) |
| 2702 | && (hFind = FindFirstFileW(szTrueNameTemp, &fb)) |
| 2703 | != INVALID_HANDLE_VALUE) |
| 2704 | { |
| 2705 | c = *porig; |
| 2706 | *porig = NUL; |
| 2707 | |
| 2708 | /* Only use the match when it's the same name (ignoring case) or |
| 2709 | * expansion is allowed and there is a match with the short name |
| 2710 | * and there is enough room. */ |
| 2711 | if (_wcsicoll(porigPrev, fb.cFileName) == 0 |
| 2712 | || (len > 0 |
| 2713 | && (_wcsicoll(porigPrev, fb.cAlternateFileName) == 0 |
| 2714 | && (int)(ptruePrev - szTrueName) |
| 2715 | + (int)wcslen(fb.cFileName) < len))) |
| 2716 | { |
| 2717 | wcscpy(ptruePrev, fb.cFileName); |
| 2718 | |
| 2719 | /* Look for exact match and prefer it if found. Must be a |
| 2720 | * long name, otherwise there would be only one match. */ |
| 2721 | while (FindNextFileW(hFind, &fb)) |
| 2722 | { |
| 2723 | if (*fb.cAlternateFileName != NUL |
| 2724 | && (wcscoll(porigPrev, fb.cFileName) == 0 |
| 2725 | || (len > 0 |
| 2726 | && (_wcsicoll(porigPrev, |
| 2727 | fb.cAlternateFileName) == 0 |
| 2728 | && (int)(ptruePrev - szTrueName) |
| 2729 | + (int)wcslen(fb.cFileName) < len)))) |
| 2730 | { |
| 2731 | wcscpy(ptruePrev, fb.cFileName); |
| 2732 | break; |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | FindClose(hFind); |
| 2737 | *porig = c; |
| 2738 | ptrue = ptruePrev + wcslen(ptruePrev); |
| 2739 | } |
| 2740 | else if (hFind == INVALID_HANDLE_VALUE |
| 2741 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 2742 | return FAIL; |
| 2743 | } |
| 2744 | |
| 2745 | wcscpy(name, szTrueName); |
| 2746 | return OK; |
| 2747 | } |
| 2748 | #endif |
| 2749 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2750 | /* |
| 2751 | * fname_case(): Set the case of the file name, if it already exists. |
| 2752 | * When "len" is > 0, also expand short to long filenames. |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2753 | * NOTE: much of this is identical to fname_casew(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | */ |
| 2755 | void |
| 2756 | fname_case( |
| 2757 | char_u *name, |
| 2758 | int len) |
| 2759 | { |
| 2760 | char szTrueName[_MAX_PATH + 2]; |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2761 | char szTrueNameTemp[_MAX_PATH + 2]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2762 | char *ptrue, *ptruePrev; |
| 2763 | char *porig, *porigPrev; |
| 2764 | int flen; |
| 2765 | WIN32_FIND_DATA fb; |
| 2766 | HANDLE hFind; |
| 2767 | int c; |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2768 | int slen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2769 | |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 2770 | flen = (int)STRLEN(name); |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2771 | if (flen == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | |
| 2774 | slash_adjust(name); |
| 2775 | |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2776 | #ifdef FEAT_MBYTE |
| 2777 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2778 | { |
| 2779 | WCHAR *p = enc_to_utf16(name, NULL); |
| 2780 | |
| 2781 | if (p != NULL) |
| 2782 | { |
| 2783 | char_u *q; |
Bram Moolenaar | 21d89b6 | 2014-10-07 10:38:40 +0200 | [diff] [blame] | 2784 | WCHAR buf[_MAX_PATH + 1]; |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2785 | |
Bram Moolenaar | 21d89b6 | 2014-10-07 10:38:40 +0200 | [diff] [blame] | 2786 | wcsncpy(buf, p, _MAX_PATH); |
| 2787 | buf[_MAX_PATH] = L'\0'; |
Bram Moolenaar | 65f04f6 | 2013-08-30 17:29:16 +0200 | [diff] [blame] | 2788 | vim_free(p); |
| 2789 | |
| 2790 | if (fname_casew(buf, (len > 0) ? _MAX_PATH : 0) == OK) |
| 2791 | { |
| 2792 | q = utf16_to_enc(buf, NULL); |
| 2793 | if (q != NULL) |
| 2794 | { |
| 2795 | vim_strncpy(name, q, (len > 0) ? len - 1 : flen); |
| 2796 | vim_free(q); |
| 2797 | return; |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | /* Retry with non-wide function (for Windows 98). */ |
| 2802 | } |
| 2803 | #endif |
| 2804 | |
| 2805 | /* If 'enc' is utf-8, flen can be larger than _MAX_PATH. |
| 2806 | * So we should check this after calling wide function. */ |
| 2807 | if (flen > _MAX_PATH) |
| 2808 | return; |
| 2809 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | /* Build the new name in szTrueName[] one component at a time. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2811 | porig = (char *)name; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | ptrue = szTrueName; |
| 2813 | |
| 2814 | if (isalpha(porig[0]) && porig[1] == ':') |
| 2815 | { |
| 2816 | /* copy leading drive letter */ |
| 2817 | *ptrue++ = *porig++; |
| 2818 | *ptrue++ = *porig++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2819 | } |
Bram Moolenaar | 73c6163 | 2013-12-07 14:48:10 +0100 | [diff] [blame] | 2820 | *ptrue = NUL; /* in case nothing follows */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | |
| 2822 | while (*porig != NUL) |
| 2823 | { |
| 2824 | /* copy \ characters */ |
| 2825 | while (*porig == psepc) |
| 2826 | *ptrue++ = *porig++; |
| 2827 | |
| 2828 | ptruePrev = ptrue; |
| 2829 | porigPrev = porig; |
| 2830 | while (*porig != NUL && *porig != psepc) |
| 2831 | { |
| 2832 | #ifdef FEAT_MBYTE |
| 2833 | int l; |
| 2834 | |
| 2835 | if (enc_dbcs) |
| 2836 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2837 | l = (*mb_ptr2len)((char_u *)porig); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | while (--l >= 0) |
| 2839 | *ptrue++ = *porig++; |
| 2840 | } |
| 2841 | else |
| 2842 | #endif |
| 2843 | *ptrue++ = *porig++; |
| 2844 | } |
| 2845 | *ptrue = NUL; |
| 2846 | |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2847 | /* To avoid a slow failure append "\*" when searching a directory, |
| 2848 | * server or network share. */ |
| 2849 | STRCPY(szTrueNameTemp, szTrueName); |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 2850 | slen = (int)strlen(szTrueNameTemp); |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2851 | if (*porig == psepc && slen + 2 < _MAX_PATH) |
| 2852 | STRCPY(szTrueNameTemp + slen, "\\*"); |
| 2853 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | /* Skip "", "." and "..". */ |
| 2855 | if (ptrue > ptruePrev |
| 2856 | && (ptruePrev[0] != '.' |
| 2857 | || (ptruePrev[1] != NUL |
| 2858 | && (ptruePrev[1] != '.' || ptruePrev[2] != NUL))) |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2859 | && (hFind = FindFirstFile(szTrueNameTemp, &fb)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | != INVALID_HANDLE_VALUE) |
| 2861 | { |
| 2862 | c = *porig; |
| 2863 | *porig = NUL; |
| 2864 | |
| 2865 | /* Only use the match when it's the same name (ignoring case) or |
| 2866 | * expansion is allowed and there is a match with the short name |
| 2867 | * and there is enough room. */ |
| 2868 | if (_stricoll(porigPrev, fb.cFileName) == 0 |
| 2869 | || (len > 0 |
| 2870 | && (_stricoll(porigPrev, fb.cAlternateFileName) == 0 |
| 2871 | && (int)(ptruePrev - szTrueName) |
| 2872 | + (int)strlen(fb.cFileName) < len))) |
| 2873 | { |
| 2874 | STRCPY(ptruePrev, fb.cFileName); |
| 2875 | |
| 2876 | /* Look for exact match and prefer it if found. Must be a |
| 2877 | * long name, otherwise there would be only one match. */ |
| 2878 | while (FindNextFile(hFind, &fb)) |
| 2879 | { |
| 2880 | if (*fb.cAlternateFileName != NUL |
| 2881 | && (strcoll(porigPrev, fb.cFileName) == 0 |
| 2882 | || (len > 0 |
| 2883 | && (_stricoll(porigPrev, |
| 2884 | fb.cAlternateFileName) == 0 |
| 2885 | && (int)(ptruePrev - szTrueName) |
| 2886 | + (int)strlen(fb.cFileName) < len)))) |
| 2887 | { |
| 2888 | STRCPY(ptruePrev, fb.cFileName); |
| 2889 | break; |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | FindClose(hFind); |
| 2894 | *porig = c; |
| 2895 | ptrue = ptruePrev + strlen(ptruePrev); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | STRCPY(name, szTrueName); |
| 2900 | } |
| 2901 | |
| 2902 | |
| 2903 | /* |
| 2904 | * Insert user name in s[len]. |
| 2905 | */ |
| 2906 | int |
| 2907 | mch_get_user_name( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2908 | char_u *s, |
| 2909 | int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | { |
Bram Moolenaar | 41a0903 | 2007-10-01 18:34:34 +0000 | [diff] [blame] | 2911 | char szUserName[256 + 1]; /* UNLEN is 256 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2912 | DWORD cch = sizeof szUserName; |
| 2913 | |
Bram Moolenaar | c8020ee | 2013-12-11 18:18:06 +0100 | [diff] [blame] | 2914 | #ifdef FEAT_MBYTE |
| 2915 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2916 | { |
| 2917 | WCHAR wszUserName[256 + 1]; /* UNLEN is 256 */ |
| 2918 | DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR); |
| 2919 | |
| 2920 | if (GetUserNameW(wszUserName, &wcch)) |
| 2921 | { |
| 2922 | char_u *p = utf16_to_enc(wszUserName, NULL); |
| 2923 | |
| 2924 | if (p != NULL) |
| 2925 | { |
| 2926 | vim_strncpy(s, p, len - 1); |
| 2927 | vim_free(p); |
| 2928 | return OK; |
| 2929 | } |
| 2930 | } |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 2931 | else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 2932 | return FAIL; |
Bram Moolenaar | c8020ee | 2013-12-11 18:18:06 +0100 | [diff] [blame] | 2933 | /* Retry with non-wide function (for Windows 98). */ |
| 2934 | } |
| 2935 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | if (GetUserName(szUserName, &cch)) |
| 2937 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2938 | vim_strncpy(s, (char_u *)szUserName, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2939 | return OK; |
| 2940 | } |
| 2941 | s[0] = NUL; |
| 2942 | return FAIL; |
| 2943 | } |
| 2944 | |
| 2945 | |
| 2946 | /* |
| 2947 | * Insert host name in s[len]. |
| 2948 | */ |
| 2949 | void |
| 2950 | mch_get_host_name( |
| 2951 | char_u *s, |
| 2952 | int len) |
| 2953 | { |
| 2954 | DWORD cch = len; |
| 2955 | |
Bram Moolenaar | 2cc8738 | 2013-12-11 18:21:45 +0100 | [diff] [blame] | 2956 | #ifdef FEAT_MBYTE |
| 2957 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2958 | { |
| 2959 | WCHAR wszHostName[256 + 1]; |
| 2960 | DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR); |
| 2961 | |
| 2962 | if (GetComputerNameW(wszHostName, &wcch)) |
| 2963 | { |
| 2964 | char_u *p = utf16_to_enc(wszHostName, NULL); |
| 2965 | |
| 2966 | if (p != NULL) |
| 2967 | { |
| 2968 | vim_strncpy(s, p, len - 1); |
| 2969 | vim_free(p); |
| 2970 | return; |
| 2971 | } |
| 2972 | } |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 2973 | else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 2974 | return; |
Bram Moolenaar | 2cc8738 | 2013-12-11 18:21:45 +0100 | [diff] [blame] | 2975 | /* Retry with non-wide function (for Windows 98). */ |
| 2976 | } |
| 2977 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2978 | if (!GetComputerName((LPSTR)s, &cch)) |
| 2979 | vim_strncpy(s, (char_u *)"PC (Win32 Vim)", len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | /* |
| 2984 | * return process ID |
| 2985 | */ |
| 2986 | long |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2987 | mch_get_pid(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2988 | { |
| 2989 | return (long)GetCurrentProcessId(); |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | /* |
| 2994 | * Get name of current directory into buffer 'buf' of length 'len' bytes. |
| 2995 | * Return OK for success, FAIL for failure. |
| 2996 | */ |
| 2997 | int |
| 2998 | mch_dirname( |
| 2999 | char_u *buf, |
| 3000 | int len) |
| 3001 | { |
| 3002 | /* |
| 3003 | * Originally this was: |
| 3004 | * return (getcwd(buf, len) != NULL ? OK : FAIL); |
| 3005 | * But the Win32s known bug list says that getcwd() doesn't work |
| 3006 | * so use the Win32 system call instead. <Negri> |
| 3007 | */ |
| 3008 | #ifdef FEAT_MBYTE |
| 3009 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3010 | { |
| 3011 | WCHAR wbuf[_MAX_PATH + 1]; |
| 3012 | |
| 3013 | if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0) |
| 3014 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 3015 | char_u *p = utf16_to_enc(wbuf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (p != NULL) |
| 3018 | { |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 3019 | vim_strncpy(buf, p, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3020 | vim_free(p); |
| 3021 | return OK; |
| 3022 | } |
| 3023 | } |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 3024 | else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 3025 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | /* Retry with non-wide function (for Windows 98). */ |
| 3027 | } |
| 3028 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3029 | return (GetCurrentDirectory(len, (LPSTR)buf) != 0 ? OK : FAIL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | /* |
Bram Moolenaar | ffa2220 | 2013-11-21 12:34:11 +0100 | [diff] [blame] | 3033 | * Get file permissions for "name". |
| 3034 | * Return mode_t or -1 for error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3035 | */ |
| 3036 | long |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3037 | mch_getperm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3039 | struct stat st; |
Bram Moolenaar | ffa2220 | 2013-11-21 12:34:11 +0100 | [diff] [blame] | 3040 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3041 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3042 | n = mch_stat((char *)name, &st); |
Bram Moolenaar | 78cf3f0 | 2014-01-10 18:16:07 +0100 | [diff] [blame] | 3043 | return n == 0 ? (long)(unsigned short)st.st_mode : -1L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | |
| 3047 | /* |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3048 | * Set file permission for "name" to "perm". |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3049 | * |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3050 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3051 | */ |
| 3052 | int |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3053 | mch_setperm(char_u *name, long perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3054 | { |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3055 | long n = -1; |
| 3056 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3057 | #ifdef FEAT_MBYTE |
| 3058 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3059 | { |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3060 | WCHAR *p = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3061 | |
| 3062 | if (p != NULL) |
| 3063 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3064 | n = _wchmod(p, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3065 | vim_free(p); |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 3066 | if (n == -1 && g_PlatformId == VER_PLATFORM_WIN32_NT) |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3067 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | /* Retry with non-wide function (for Windows 98). */ |
| 3069 | } |
| 3070 | } |
Bram Moolenaar | e24a9c0 | 2013-07-24 13:49:22 +0200 | [diff] [blame] | 3071 | if (n == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3072 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3073 | n = _chmod((const char *)name, perm); |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3074 | if (n == -1) |
| 3075 | return FAIL; |
| 3076 | |
| 3077 | win32_set_archive(name); |
| 3078 | |
| 3079 | return OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | /* |
| 3083 | * Set hidden flag for "name". |
| 3084 | */ |
| 3085 | void |
| 3086 | mch_hide(char_u *name) |
| 3087 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3088 | int attrs = win32_getattrs(name); |
| 3089 | if (attrs == -1) |
| 3090 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3092 | attrs |= FILE_ATTRIBUTE_HIDDEN; |
| 3093 | win32_setattrs(name, attrs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | /* |
Bram Moolenaar | 8a52ba7 | 2015-11-02 14:45:56 +0100 | [diff] [blame] | 3097 | * Return TRUE if file "name" exists and is hidden. |
| 3098 | */ |
| 3099 | int |
| 3100 | mch_ishidden(char_u *name) |
| 3101 | { |
| 3102 | int f = win32_getattrs(name); |
| 3103 | |
| 3104 | if (f == -1) |
| 3105 | return FALSE; /* file does not exist at all */ |
| 3106 | |
| 3107 | return (f & FILE_ATTRIBUTE_HIDDEN) != 0; |
| 3108 | } |
| 3109 | |
| 3110 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3111 | * return TRUE if "name" is a directory |
| 3112 | * return FALSE if "name" is not a directory or upon error |
| 3113 | */ |
| 3114 | int |
| 3115 | mch_isdir(char_u *name) |
| 3116 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3117 | int f = win32_getattrs(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | |
| 3119 | if (f == -1) |
| 3120 | return FALSE; /* file does not exist at all */ |
| 3121 | |
| 3122 | return (f & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 3123 | } |
| 3124 | |
| 3125 | /* |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 3126 | * return TRUE if "name" is a directory, NOT a symlink to a directory |
| 3127 | * return FALSE if "name" is not a directory |
| 3128 | * return FALSE for error |
| 3129 | */ |
| 3130 | int |
| 3131 | mch_isrealdir(char_u *name) |
| 3132 | { |
| 3133 | return mch_isdir(name) && !mch_is_symbolic_link(name); |
| 3134 | } |
| 3135 | |
| 3136 | /* |
Bram Moolenaar | 3c9c99c | 2011-05-05 18:31:59 +0200 | [diff] [blame] | 3137 | * Create directory "name". |
| 3138 | * Return 0 on success, -1 on error. |
| 3139 | */ |
| 3140 | int |
| 3141 | mch_mkdir(char_u *name) |
| 3142 | { |
| 3143 | #ifdef FEAT_MBYTE |
| 3144 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3145 | { |
| 3146 | WCHAR *p; |
| 3147 | int retval; |
| 3148 | |
| 3149 | p = enc_to_utf16(name, NULL); |
| 3150 | if (p == NULL) |
| 3151 | return -1; |
| 3152 | retval = _wmkdir(p); |
| 3153 | vim_free(p); |
| 3154 | return retval; |
| 3155 | } |
| 3156 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3157 | return _mkdir((const char *)name); |
Bram Moolenaar | 3c9c99c | 2011-05-05 18:31:59 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* |
Bram Moolenaar | 4cf7679 | 2016-01-16 22:02:57 +0100 | [diff] [blame] | 3161 | * Delete directory "name". |
| 3162 | * Return 0 on success, -1 on error. |
| 3163 | */ |
| 3164 | int |
| 3165 | mch_rmdir(char_u *name) |
| 3166 | { |
| 3167 | #ifdef FEAT_MBYTE |
| 3168 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3169 | { |
| 3170 | WCHAR *p; |
| 3171 | int retval; |
| 3172 | |
| 3173 | p = enc_to_utf16(name, NULL); |
| 3174 | if (p == NULL) |
| 3175 | return -1; |
| 3176 | retval = _wrmdir(p); |
| 3177 | vim_free(p); |
| 3178 | return retval; |
| 3179 | } |
| 3180 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3181 | return _rmdir((const char *)name); |
Bram Moolenaar | 4cf7679 | 2016-01-16 22:02:57 +0100 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /* |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3185 | * Return TRUE if file "fname" has more than one link. |
| 3186 | */ |
| 3187 | int |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3188 | mch_is_hard_link(char_u *fname) |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3189 | { |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3190 | BY_HANDLE_FILE_INFORMATION info; |
| 3191 | |
| 3192 | return win32_fileinfo(fname, &info) == FILEINFO_OK |
| 3193 | && info.nNumberOfLinks > 1; |
| 3194 | } |
| 3195 | |
| 3196 | /* |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 3197 | * Return TRUE if "name" is a symbolic link (or a junction). |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3198 | */ |
| 3199 | int |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 3200 | mch_is_symbolic_link(char_u *name) |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3201 | { |
| 3202 | HANDLE hFind; |
| 3203 | int res = FALSE; |
| 3204 | WIN32_FIND_DATAA findDataA; |
| 3205 | DWORD fileFlags = 0, reparseTag = 0; |
| 3206 | #ifdef FEAT_MBYTE |
| 3207 | WCHAR *wn = NULL; |
| 3208 | WIN32_FIND_DATAW findDataW; |
| 3209 | |
| 3210 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 3211 | wn = enc_to_utf16(name, NULL); |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3212 | if (wn != NULL) |
| 3213 | { |
| 3214 | hFind = FindFirstFileW(wn, &findDataW); |
| 3215 | vim_free(wn); |
| 3216 | if (hFind == INVALID_HANDLE_VALUE |
| 3217 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 3218 | { |
| 3219 | /* Retry with non-wide function (for Windows 98). */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3220 | hFind = FindFirstFile((LPCSTR)name, &findDataA); |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3221 | if (hFind != INVALID_HANDLE_VALUE) |
| 3222 | { |
| 3223 | fileFlags = findDataA.dwFileAttributes; |
| 3224 | reparseTag = findDataA.dwReserved0; |
| 3225 | } |
| 3226 | } |
| 3227 | else |
| 3228 | { |
| 3229 | fileFlags = findDataW.dwFileAttributes; |
| 3230 | reparseTag = findDataW.dwReserved0; |
| 3231 | } |
| 3232 | } |
Bram Moolenaar | 03e114b | 2013-06-16 16:34:56 +0200 | [diff] [blame] | 3233 | else |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3234 | #endif |
Bram Moolenaar | 03e114b | 2013-06-16 16:34:56 +0200 | [diff] [blame] | 3235 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3236 | hFind = FindFirstFile((LPCSTR)name, &findDataA); |
Bram Moolenaar | 03e114b | 2013-06-16 16:34:56 +0200 | [diff] [blame] | 3237 | if (hFind != INVALID_HANDLE_VALUE) |
| 3238 | { |
| 3239 | fileFlags = findDataA.dwFileAttributes; |
| 3240 | reparseTag = findDataA.dwReserved0; |
| 3241 | } |
| 3242 | } |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3243 | |
| 3244 | if (hFind != INVALID_HANDLE_VALUE) |
| 3245 | FindClose(hFind); |
| 3246 | |
| 3247 | if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT) |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 3248 | && (reparseTag == IO_REPARSE_TAG_SYMLINK |
| 3249 | || reparseTag == IO_REPARSE_TAG_MOUNT_POINT)) |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3250 | res = TRUE; |
| 3251 | |
| 3252 | return res; |
| 3253 | } |
| 3254 | |
| 3255 | /* |
| 3256 | * Return TRUE if file "fname" has more than one link or if it is a symbolic |
| 3257 | * link. |
| 3258 | */ |
| 3259 | int |
| 3260 | mch_is_linked(char_u *fname) |
| 3261 | { |
| 3262 | if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname)) |
| 3263 | return TRUE; |
| 3264 | return FALSE; |
| 3265 | } |
| 3266 | |
| 3267 | /* |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3268 | * Get the by-handle-file-information for "fname". |
| 3269 | * Returns FILEINFO_OK when OK. |
| 3270 | * returns FILEINFO_ENC_FAIL when enc_to_utf16() failed. |
| 3271 | * Returns FILEINFO_READ_FAIL when CreateFile() failed. |
| 3272 | * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed. |
| 3273 | */ |
| 3274 | int |
| 3275 | win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info) |
| 3276 | { |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3277 | HANDLE hFile; |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3278 | int res = FILEINFO_READ_FAIL; |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3279 | #ifdef FEAT_MBYTE |
| 3280 | WCHAR *wn = NULL; |
| 3281 | |
| 3282 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3283 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 3284 | wn = enc_to_utf16(fname, NULL); |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3285 | if (wn == NULL) |
| 3286 | res = FILEINFO_ENC_FAIL; |
| 3287 | } |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3288 | if (wn != NULL) |
| 3289 | { |
| 3290 | hFile = CreateFileW(wn, /* file name */ |
| 3291 | GENERIC_READ, /* access mode */ |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3292 | FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3293 | NULL, /* security descriptor */ |
| 3294 | OPEN_EXISTING, /* creation disposition */ |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3295 | FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3296 | NULL); /* handle to template file */ |
| 3297 | if (hFile == INVALID_HANDLE_VALUE |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3298 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3299 | { |
| 3300 | /* Retry with non-wide function (for Windows 98). */ |
| 3301 | vim_free(wn); |
| 3302 | wn = NULL; |
| 3303 | } |
| 3304 | } |
| 3305 | if (wn == NULL) |
| 3306 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3307 | hFile = CreateFile((LPCSTR)fname, /* file name */ |
| 3308 | GENERIC_READ, /* access mode */ |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3309 | FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3310 | NULL, /* security descriptor */ |
| 3311 | OPEN_EXISTING, /* creation disposition */ |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3312 | FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3313 | NULL); /* handle to template file */ |
| 3314 | |
| 3315 | if (hFile != INVALID_HANDLE_VALUE) |
| 3316 | { |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 3317 | if (GetFileInformationByHandle(hFile, info) != 0) |
| 3318 | res = FILEINFO_OK; |
| 3319 | else |
| 3320 | res = FILEINFO_INFO_FAIL; |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3321 | CloseHandle(hFile); |
| 3322 | } |
| 3323 | |
| 3324 | #ifdef FEAT_MBYTE |
| 3325 | vim_free(wn); |
| 3326 | #endif |
| 3327 | return res; |
| 3328 | } |
| 3329 | |
| 3330 | /* |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3331 | * get file attributes for `name' |
| 3332 | * -1 : error |
| 3333 | * else FILE_ATTRIBUTE_* defined in winnt.h |
| 3334 | */ |
Bram Moolenaar | ffa2220 | 2013-11-21 12:34:11 +0100 | [diff] [blame] | 3335 | static int |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3336 | win32_getattrs(char_u *name) |
| 3337 | { |
| 3338 | int attr; |
| 3339 | #ifdef FEAT_MBYTE |
| 3340 | WCHAR *p = NULL; |
| 3341 | |
| 3342 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3343 | p = enc_to_utf16(name, NULL); |
| 3344 | |
| 3345 | if (p != NULL) |
| 3346 | { |
| 3347 | attr = GetFileAttributesW(p); |
| 3348 | if (attr < 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 3349 | { |
| 3350 | /* Retry with non-wide function (for Windows 98). */ |
| 3351 | vim_free(p); |
| 3352 | p = NULL; |
| 3353 | } |
| 3354 | } |
| 3355 | if (p == NULL) |
| 3356 | #endif |
| 3357 | attr = GetFileAttributes((char *)name); |
| 3358 | #ifdef FEAT_MBYTE |
| 3359 | vim_free(p); |
| 3360 | #endif |
| 3361 | return attr; |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * set file attributes for `name' to `attrs' |
| 3366 | * |
| 3367 | * return -1 for failure, 0 otherwise |
| 3368 | */ |
| 3369 | static |
| 3370 | int |
| 3371 | win32_setattrs(char_u *name, int attrs) |
| 3372 | { |
| 3373 | int res; |
| 3374 | #ifdef FEAT_MBYTE |
| 3375 | WCHAR *p = NULL; |
| 3376 | |
| 3377 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3378 | p = enc_to_utf16(name, NULL); |
| 3379 | |
| 3380 | if (p != NULL) |
| 3381 | { |
| 3382 | res = SetFileAttributesW(p, attrs); |
| 3383 | if (res == FALSE |
| 3384 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 3385 | { |
| 3386 | /* Retry with non-wide function (for Windows 98). */ |
| 3387 | vim_free(p); |
| 3388 | p = NULL; |
| 3389 | } |
| 3390 | } |
| 3391 | if (p == NULL) |
| 3392 | #endif |
| 3393 | res = SetFileAttributes((char *)name, attrs); |
| 3394 | #ifdef FEAT_MBYTE |
| 3395 | vim_free(p); |
| 3396 | #endif |
| 3397 | return res ? 0 : -1; |
| 3398 | } |
| 3399 | |
| 3400 | /* |
| 3401 | * Set archive flag for "name". |
| 3402 | */ |
| 3403 | static |
| 3404 | int |
| 3405 | win32_set_archive(char_u *name) |
| 3406 | { |
| 3407 | int attrs = win32_getattrs(name); |
| 3408 | if (attrs == -1) |
| 3409 | return -1; |
| 3410 | |
| 3411 | attrs |= FILE_ATTRIBUTE_ARCHIVE; |
| 3412 | return win32_setattrs(name, attrs); |
| 3413 | } |
| 3414 | |
| 3415 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3416 | * Return TRUE if file or directory "name" is writable (not readonly). |
| 3417 | * Strange semantics of Win32: a readonly directory is writable, but you can't |
| 3418 | * delete a file. Let's say this means it is writable. |
| 3419 | */ |
| 3420 | int |
| 3421 | mch_writable(char_u *name) |
| 3422 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3423 | int attrs = win32_getattrs(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3425 | return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY) |
| 3426 | || (attrs & FILE_ATTRIBUTE_DIRECTORY))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3429 | /* |
| 3430 | * Return 1 if "name" can be executed, 0 if not. |
Bram Moolenaar | 77b7710 | 2015-03-21 22:18:41 +0100 | [diff] [blame] | 3431 | * If "use_path" is FALSE only check if "name" is executable. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | * Return -1 if unknown. |
| 3433 | */ |
| 3434 | int |
Bram Moolenaar | 77b7710 | 2015-03-21 22:18:41 +0100 | [diff] [blame] | 3435 | mch_can_exe(char_u *name, char_u **path, int use_path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3437 | char_u buf[_MAX_PATH]; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3438 | int len = (int)STRLEN(name); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3439 | char_u *p; |
| 3440 | |
| 3441 | if (len >= _MAX_PATH) /* safety check */ |
| 3442 | return FALSE; |
Bram Moolenaar | 77b7710 | 2015-03-21 22:18:41 +0100 | [diff] [blame] | 3443 | if (!use_path) |
| 3444 | { |
| 3445 | /* TODO: check if file is really executable. */ |
| 3446 | return mch_getperm(name) != -1 && !mch_isdir(name); |
| 3447 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3448 | |
| 3449 | /* If there already is an extension try using the name directly. Also do |
| 3450 | * this with a Unix-shell like 'shell'. */ |
| 3451 | if (vim_strchr(gettail(name), '.') != NULL |
| 3452 | || strstr((char *)gettail(p_sh), "sh") != NULL) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3453 | if (executable_exists((char *)name, path)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3454 | return TRUE; |
| 3455 | |
| 3456 | /* |
| 3457 | * Loop over all extensions in $PATHEXT. |
| 3458 | */ |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 3459 | vim_strncpy(buf, name, _MAX_PATH - 1); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3460 | p = mch_getenv("PATHEXT"); |
| 3461 | if (p == NULL) |
| 3462 | p = (char_u *)".com;.exe;.bat;.cmd"; |
| 3463 | while (*p) |
| 3464 | { |
| 3465 | if (p[0] == '.' && (p[1] == NUL || p[1] == ';')) |
| 3466 | { |
| 3467 | /* A single "." means no extension is added. */ |
| 3468 | buf[len] = NUL; |
| 3469 | ++p; |
| 3470 | if (*p) |
| 3471 | ++p; |
| 3472 | } |
| 3473 | else |
| 3474 | copy_option_part(&p, buf + len, _MAX_PATH - len, ";"); |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3475 | if (executable_exists((char *)buf, path)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 3476 | return TRUE; |
| 3477 | } |
| 3478 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3479 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3480 | |
| 3481 | /* |
| 3482 | * Check what "name" is: |
| 3483 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 3484 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 3485 | * NODE_OTHER: non-writable things |
| 3486 | */ |
| 3487 | int |
| 3488 | mch_nodetype(char_u *name) |
| 3489 | { |
| 3490 | HANDLE hFile; |
| 3491 | int type; |
Bram Moolenaar | 4dee1bb | 2013-08-30 17:11:33 +0200 | [diff] [blame] | 3492 | #ifdef FEAT_MBYTE |
| 3493 | WCHAR *wn = NULL; |
| 3494 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3495 | |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 3496 | /* We can't open a file with a name "\\.\con" or "\\.\prn" and trying to |
| 3497 | * read from it later will cause Vim to hang. Thus return NODE_WRITABLE |
| 3498 | * here. */ |
| 3499 | if (STRNCMP(name, "\\\\.\\", 4) == 0) |
| 3500 | return NODE_WRITABLE; |
| 3501 | |
Bram Moolenaar | 4dee1bb | 2013-08-30 17:11:33 +0200 | [diff] [blame] | 3502 | #ifdef FEAT_MBYTE |
| 3503 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3504 | { |
| 3505 | wn = enc_to_utf16(name, NULL); |
| 3506 | if (wn != NULL) |
| 3507 | { |
| 3508 | hFile = CreateFileW(wn, /* file name */ |
| 3509 | GENERIC_WRITE, /* access mode */ |
| 3510 | 0, /* share mode */ |
| 3511 | NULL, /* security descriptor */ |
| 3512 | OPEN_EXISTING, /* creation disposition */ |
| 3513 | 0, /* file attributes */ |
| 3514 | NULL); /* handle to template file */ |
| 3515 | if (hFile == INVALID_HANDLE_VALUE |
| 3516 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 3517 | { |
| 3518 | /* Retry with non-wide function (for Windows 98). */ |
| 3519 | vim_free(wn); |
| 3520 | wn = NULL; |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | if (wn == NULL) |
| 3525 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3526 | hFile = CreateFile((LPCSTR)name, /* file name */ |
| 3527 | GENERIC_WRITE, /* access mode */ |
| 3528 | 0, /* share mode */ |
| 3529 | NULL, /* security descriptor */ |
| 3530 | OPEN_EXISTING, /* creation disposition */ |
| 3531 | 0, /* file attributes */ |
| 3532 | NULL); /* handle to template file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3533 | |
Bram Moolenaar | 4dee1bb | 2013-08-30 17:11:33 +0200 | [diff] [blame] | 3534 | #ifdef FEAT_MBYTE |
| 3535 | vim_free(wn); |
| 3536 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | if (hFile == INVALID_HANDLE_VALUE) |
| 3538 | return NODE_NORMAL; |
| 3539 | |
| 3540 | type = GetFileType(hFile); |
| 3541 | CloseHandle(hFile); |
| 3542 | if (type == FILE_TYPE_CHAR) |
| 3543 | return NODE_WRITABLE; |
| 3544 | if (type == FILE_TYPE_DISK) |
| 3545 | return NODE_NORMAL; |
| 3546 | return NODE_OTHER; |
| 3547 | } |
| 3548 | |
| 3549 | #ifdef HAVE_ACL |
| 3550 | struct my_acl |
| 3551 | { |
| 3552 | PSECURITY_DESCRIPTOR pSecurityDescriptor; |
| 3553 | PSID pSidOwner; |
| 3554 | PSID pSidGroup; |
| 3555 | PACL pDacl; |
| 3556 | PACL pSacl; |
| 3557 | }; |
| 3558 | #endif |
| 3559 | |
| 3560 | /* |
| 3561 | * Return a pointer to the ACL of file "fname" in allocated memory. |
| 3562 | * Return NULL if the ACL is not available for whatever reason. |
| 3563 | */ |
| 3564 | vim_acl_T |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3565 | mch_get_acl(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | { |
| 3567 | #ifndef HAVE_ACL |
| 3568 | return (vim_acl_T)NULL; |
| 3569 | #else |
| 3570 | struct my_acl *p = NULL; |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3571 | DWORD err; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3572 | |
| 3573 | /* This only works on Windows NT and 2000. */ |
| 3574 | if (g_PlatformId == VER_PLATFORM_WIN32_NT && advapi_lib != NULL) |
| 3575 | { |
| 3576 | p = (struct my_acl *)alloc_clear((unsigned)sizeof(struct my_acl)); |
| 3577 | if (p != NULL) |
| 3578 | { |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3579 | # ifdef FEAT_MBYTE |
| 3580 | WCHAR *wn = NULL; |
| 3581 | |
| 3582 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3583 | wn = enc_to_utf16(fname, NULL); |
| 3584 | if (wn != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | { |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3586 | /* Try to retrieve the entire security descriptor. */ |
| 3587 | err = pGetNamedSecurityInfoW( |
| 3588 | wn, // Abstract filename |
| 3589 | SE_FILE_OBJECT, // File Object |
| 3590 | OWNER_SECURITY_INFORMATION | |
| 3591 | GROUP_SECURITY_INFORMATION | |
| 3592 | DACL_SECURITY_INFORMATION | |
| 3593 | SACL_SECURITY_INFORMATION, |
| 3594 | &p->pSidOwner, // Ownership information. |
| 3595 | &p->pSidGroup, // Group membership. |
| 3596 | &p->pDacl, // Discretionary information. |
| 3597 | &p->pSacl, // For auditing purposes. |
| 3598 | &p->pSecurityDescriptor); |
| 3599 | if (err == ERROR_ACCESS_DENIED || |
| 3600 | err == ERROR_PRIVILEGE_NOT_HELD) |
| 3601 | { |
| 3602 | /* Retrieve only DACL. */ |
| 3603 | (void)pGetNamedSecurityInfoW( |
| 3604 | wn, |
| 3605 | SE_FILE_OBJECT, |
| 3606 | DACL_SECURITY_INFORMATION, |
| 3607 | NULL, |
| 3608 | NULL, |
| 3609 | &p->pDacl, |
| 3610 | NULL, |
| 3611 | &p->pSecurityDescriptor); |
| 3612 | } |
| 3613 | if (p->pSecurityDescriptor == NULL) |
| 3614 | { |
| 3615 | mch_free_acl((vim_acl_T)p); |
| 3616 | p = NULL; |
| 3617 | } |
| 3618 | vim_free(wn); |
| 3619 | } |
| 3620 | else |
| 3621 | # endif |
| 3622 | { |
| 3623 | /* Try to retrieve the entire security descriptor. */ |
| 3624 | err = pGetNamedSecurityInfo( |
| 3625 | (LPSTR)fname, // Abstract filename |
| 3626 | SE_FILE_OBJECT, // File Object |
| 3627 | OWNER_SECURITY_INFORMATION | |
| 3628 | GROUP_SECURITY_INFORMATION | |
| 3629 | DACL_SECURITY_INFORMATION | |
| 3630 | SACL_SECURITY_INFORMATION, |
| 3631 | &p->pSidOwner, // Ownership information. |
| 3632 | &p->pSidGroup, // Group membership. |
| 3633 | &p->pDacl, // Discretionary information. |
| 3634 | &p->pSacl, // For auditing purposes. |
| 3635 | &p->pSecurityDescriptor); |
| 3636 | if (err == ERROR_ACCESS_DENIED || |
| 3637 | err == ERROR_PRIVILEGE_NOT_HELD) |
| 3638 | { |
| 3639 | /* Retrieve only DACL. */ |
| 3640 | (void)pGetNamedSecurityInfo( |
| 3641 | (LPSTR)fname, |
| 3642 | SE_FILE_OBJECT, |
| 3643 | DACL_SECURITY_INFORMATION, |
| 3644 | NULL, |
| 3645 | NULL, |
| 3646 | &p->pDacl, |
| 3647 | NULL, |
| 3648 | &p->pSecurityDescriptor); |
| 3649 | } |
| 3650 | if (p->pSecurityDescriptor == NULL) |
| 3651 | { |
| 3652 | mch_free_acl((vim_acl_T)p); |
| 3653 | p = NULL; |
| 3654 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | } |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | return (vim_acl_T)p; |
| 3660 | #endif |
| 3661 | } |
| 3662 | |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3663 | #ifdef HAVE_ACL |
| 3664 | /* |
| 3665 | * Check if "acl" contains inherited ACE. |
| 3666 | */ |
| 3667 | static BOOL |
| 3668 | is_acl_inherited(PACL acl) |
| 3669 | { |
| 3670 | DWORD i; |
| 3671 | ACL_SIZE_INFORMATION acl_info; |
| 3672 | PACCESS_ALLOWED_ACE ace; |
| 3673 | |
| 3674 | acl_info.AceCount = 0; |
| 3675 | GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation); |
| 3676 | for (i = 0; i < acl_info.AceCount; i++) |
| 3677 | { |
| 3678 | GetAce(acl, i, (LPVOID *)&ace); |
| 3679 | if (ace->Header.AceFlags & INHERITED_ACE) |
| 3680 | return TRUE; |
| 3681 | } |
| 3682 | return FALSE; |
| 3683 | } |
| 3684 | #endif |
| 3685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3686 | /* |
| 3687 | * Set the ACL of file "fname" to "acl" (unless it's NULL). |
| 3688 | * Errors are ignored. |
| 3689 | * This must only be called with "acl" equal to what mch_get_acl() returned. |
| 3690 | */ |
| 3691 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3692 | mch_set_acl(char_u *fname, vim_acl_T acl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | { |
| 3694 | #ifdef HAVE_ACL |
| 3695 | struct my_acl *p = (struct my_acl *)acl; |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3696 | SECURITY_INFORMATION sec_info = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | |
| 3698 | if (p != NULL && advapi_lib != NULL) |
Bram Moolenaar | 2751592 | 2013-06-29 15:36:26 +0200 | [diff] [blame] | 3699 | { |
| 3700 | # ifdef FEAT_MBYTE |
| 3701 | WCHAR *wn = NULL; |
| 3702 | # endif |
| 3703 | |
| 3704 | /* Set security flags */ |
| 3705 | if (p->pSidOwner) |
| 3706 | sec_info |= OWNER_SECURITY_INFORMATION; |
| 3707 | if (p->pSidGroup) |
| 3708 | sec_info |= GROUP_SECURITY_INFORMATION; |
| 3709 | if (p->pDacl) |
| 3710 | { |
| 3711 | sec_info |= DACL_SECURITY_INFORMATION; |
| 3712 | /* Do not inherit its parent's DACL. |
| 3713 | * If the DACL is inherited, Cygwin permissions would be changed. |
| 3714 | */ |
| 3715 | if (!is_acl_inherited(p->pDacl)) |
| 3716 | sec_info |= PROTECTED_DACL_SECURITY_INFORMATION; |
| 3717 | } |
| 3718 | if (p->pSacl) |
| 3719 | sec_info |= SACL_SECURITY_INFORMATION; |
| 3720 | |
| 3721 | # ifdef FEAT_MBYTE |
| 3722 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 3723 | wn = enc_to_utf16(fname, NULL); |
| 3724 | if (wn != NULL) |
| 3725 | { |
| 3726 | (void)pSetNamedSecurityInfoW( |
| 3727 | wn, // Abstract filename |
| 3728 | SE_FILE_OBJECT, // File Object |
| 3729 | sec_info, |
| 3730 | p->pSidOwner, // Ownership information. |
| 3731 | p->pSidGroup, // Group membership. |
| 3732 | p->pDacl, // Discretionary information. |
| 3733 | p->pSacl // For auditing purposes. |
| 3734 | ); |
| 3735 | vim_free(wn); |
| 3736 | } |
| 3737 | else |
| 3738 | # endif |
| 3739 | { |
| 3740 | (void)pSetNamedSecurityInfo( |
| 3741 | (LPSTR)fname, // Abstract filename |
| 3742 | SE_FILE_OBJECT, // File Object |
| 3743 | sec_info, |
| 3744 | p->pSidOwner, // Ownership information. |
| 3745 | p->pSidGroup, // Group membership. |
| 3746 | p->pDacl, // Discretionary information. |
| 3747 | p->pSacl // For auditing purposes. |
| 3748 | ); |
| 3749 | } |
| 3750 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | #endif |
| 3752 | } |
| 3753 | |
| 3754 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3755 | mch_free_acl(vim_acl_T acl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | { |
| 3757 | #ifdef HAVE_ACL |
| 3758 | struct my_acl *p = (struct my_acl *)acl; |
| 3759 | |
| 3760 | if (p != NULL) |
| 3761 | { |
| 3762 | LocalFree(p->pSecurityDescriptor); // Free the memory just in case |
| 3763 | vim_free(p); |
| 3764 | } |
| 3765 | #endif |
| 3766 | } |
| 3767 | |
| 3768 | #ifndef FEAT_GUI_W32 |
| 3769 | |
| 3770 | /* |
| 3771 | * handler for ctrl-break, ctrl-c interrupts, and fatal events. |
| 3772 | */ |
| 3773 | static BOOL WINAPI |
| 3774 | handler_routine( |
| 3775 | DWORD dwCtrlType) |
| 3776 | { |
| 3777 | switch (dwCtrlType) |
| 3778 | { |
| 3779 | case CTRL_C_EVENT: |
| 3780 | if (ctrl_c_interrupts) |
| 3781 | g_fCtrlCPressed = TRUE; |
| 3782 | return TRUE; |
| 3783 | |
| 3784 | case CTRL_BREAK_EVENT: |
| 3785 | g_fCBrkPressed = TRUE; |
| 3786 | return TRUE; |
| 3787 | |
| 3788 | /* fatal events: shut down gracefully */ |
| 3789 | case CTRL_CLOSE_EVENT: |
| 3790 | case CTRL_LOGOFF_EVENT: |
| 3791 | case CTRL_SHUTDOWN_EVENT: |
| 3792 | windgoto((int)Rows - 1, 0); |
| 3793 | g_fForceExit = TRUE; |
| 3794 | |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 3795 | vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3796 | (dwCtrlType == CTRL_CLOSE_EVENT |
| 3797 | ? _("close") |
| 3798 | : dwCtrlType == CTRL_LOGOFF_EVENT |
| 3799 | ? _("logoff") |
| 3800 | : _("shutdown"))); |
| 3801 | #ifdef DEBUG |
| 3802 | OutputDebugString(IObuff); |
| 3803 | #endif |
| 3804 | |
| 3805 | preserve_exit(); /* output IObuff, preserve files and exit */ |
| 3806 | |
| 3807 | return TRUE; /* not reached */ |
| 3808 | |
| 3809 | default: |
| 3810 | return FALSE; |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | |
| 3815 | /* |
| 3816 | * set the tty in (raw) ? "raw" : "cooked" mode |
| 3817 | */ |
| 3818 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3819 | mch_settmode(int tmode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3820 | { |
| 3821 | DWORD cmodein; |
| 3822 | DWORD cmodeout; |
| 3823 | BOOL bEnableHandler; |
| 3824 | |
| 3825 | GetConsoleMode(g_hConIn, &cmodein); |
| 3826 | GetConsoleMode(g_hConOut, &cmodeout); |
| 3827 | if (tmode == TMODE_RAW) |
| 3828 | { |
| 3829 | cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | |
| 3830 | ENABLE_ECHO_INPUT); |
| 3831 | #ifdef FEAT_MOUSE |
| 3832 | if (g_fMouseActive) |
| 3833 | cmodein |= ENABLE_MOUSE_INPUT; |
| 3834 | #endif |
| 3835 | cmodeout &= ~(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 3836 | bEnableHandler = TRUE; |
| 3837 | } |
| 3838 | else /* cooked */ |
| 3839 | { |
| 3840 | cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | |
| 3841 | ENABLE_ECHO_INPUT); |
| 3842 | cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 3843 | bEnableHandler = FALSE; |
| 3844 | } |
| 3845 | SetConsoleMode(g_hConIn, cmodein); |
| 3846 | SetConsoleMode(g_hConOut, cmodeout); |
| 3847 | SetConsoleCtrlHandler(handler_routine, bEnableHandler); |
| 3848 | |
| 3849 | #ifdef MCH_WRITE_DUMP |
| 3850 | if (fdDump) |
| 3851 | { |
| 3852 | fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n", |
| 3853 | tmode == TMODE_RAW ? "raw" : |
| 3854 | tmode == TMODE_COOK ? "cooked" : "normal", |
| 3855 | cmodein, cmodeout); |
| 3856 | fflush(fdDump); |
| 3857 | } |
| 3858 | #endif |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | /* |
| 3863 | * Get the size of the current window in `Rows' and `Columns' |
| 3864 | * Return OK when size could be determined, FAIL otherwise. |
| 3865 | */ |
| 3866 | int |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3867 | mch_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3868 | { |
| 3869 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 3870 | |
| 3871 | if (!g_fTermcapMode && g_cbTermcap.IsValid) |
| 3872 | { |
| 3873 | /* |
| 3874 | * For some reason, we are trying to get the screen dimensions |
| 3875 | * even though we are not in termcap mode. The 'Rows' and 'Columns' |
| 3876 | * variables are really intended to mean the size of Vim screen |
| 3877 | * while in termcap mode. |
| 3878 | */ |
| 3879 | Rows = g_cbTermcap.Info.dwSize.Y; |
| 3880 | Columns = g_cbTermcap.Info.dwSize.X; |
| 3881 | } |
| 3882 | else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 3883 | { |
| 3884 | Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 3885 | Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 3886 | } |
| 3887 | else |
| 3888 | { |
| 3889 | Rows = 25; |
| 3890 | Columns = 80; |
| 3891 | } |
| 3892 | return OK; |
| 3893 | } |
| 3894 | |
| 3895 | /* |
| 3896 | * Set a console window to `xSize' * `ySize' |
| 3897 | */ |
| 3898 | static void |
| 3899 | ResizeConBufAndWindow( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3900 | HANDLE hConsole, |
| 3901 | int xSize, |
| 3902 | int ySize) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3903 | { |
| 3904 | CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */ |
| 3905 | SMALL_RECT srWindowRect; /* hold the new console size */ |
| 3906 | COORD coordScreen; |
| 3907 | |
| 3908 | #ifdef MCH_WRITE_DUMP |
| 3909 | if (fdDump) |
| 3910 | { |
| 3911 | fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize); |
| 3912 | fflush(fdDump); |
| 3913 | } |
| 3914 | #endif |
| 3915 | |
| 3916 | /* get the largest size we can size the console window to */ |
| 3917 | coordScreen = GetLargestConsoleWindowSize(hConsole); |
| 3918 | |
| 3919 | /* define the new console window size and scroll position */ |
| 3920 | srWindowRect.Left = srWindowRect.Top = (SHORT) 0; |
| 3921 | srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1); |
| 3922 | srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1); |
| 3923 | |
| 3924 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 3925 | { |
| 3926 | int sx, sy; |
| 3927 | |
| 3928 | sx = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 3929 | sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 3930 | if (sy < ySize || sx < xSize) |
| 3931 | { |
| 3932 | /* |
| 3933 | * Increasing number of lines/columns, do buffer first. |
| 3934 | * Use the maximal size in x and y direction. |
| 3935 | */ |
| 3936 | if (sy < ySize) |
| 3937 | coordScreen.Y = ySize; |
| 3938 | else |
| 3939 | coordScreen.Y = sy; |
| 3940 | if (sx < xSize) |
| 3941 | coordScreen.X = xSize; |
| 3942 | else |
| 3943 | coordScreen.X = sx; |
| 3944 | SetConsoleScreenBufferSize(hConsole, coordScreen); |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &srWindowRect)) |
| 3949 | { |
| 3950 | #ifdef MCH_WRITE_DUMP |
| 3951 | if (fdDump) |
| 3952 | { |
| 3953 | fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n", |
| 3954 | GetLastError()); |
| 3955 | fflush(fdDump); |
| 3956 | } |
| 3957 | #endif |
| 3958 | } |
| 3959 | |
| 3960 | /* define the new console buffer size */ |
| 3961 | coordScreen.X = xSize; |
| 3962 | coordScreen.Y = ySize; |
| 3963 | |
| 3964 | if (!SetConsoleScreenBufferSize(hConsole, coordScreen)) |
| 3965 | { |
| 3966 | #ifdef MCH_WRITE_DUMP |
| 3967 | if (fdDump) |
| 3968 | { |
| 3969 | fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n", |
| 3970 | GetLastError()); |
| 3971 | fflush(fdDump); |
| 3972 | } |
| 3973 | #endif |
| 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | /* |
| 3979 | * Set the console window to `Rows' * `Columns' |
| 3980 | */ |
| 3981 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3982 | mch_set_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | { |
| 3984 | COORD coordScreen; |
| 3985 | |
| 3986 | /* Don't change window size while still starting up */ |
| 3987 | if (suppress_winsize != 0) |
| 3988 | { |
| 3989 | suppress_winsize = 2; |
| 3990 | return; |
| 3991 | } |
| 3992 | |
| 3993 | if (term_console) |
| 3994 | { |
| 3995 | coordScreen = GetLargestConsoleWindowSize(g_hConOut); |
| 3996 | |
| 3997 | /* Clamp Rows and Columns to reasonable values */ |
| 3998 | if (Rows > coordScreen.Y) |
| 3999 | Rows = coordScreen.Y; |
| 4000 | if (Columns > coordScreen.X) |
| 4001 | Columns = coordScreen.X; |
| 4002 | |
| 4003 | ResizeConBufAndWindow(g_hConOut, Columns, Rows); |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | * Rows and/or Columns has changed. |
| 4009 | */ |
| 4010 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4011 | mch_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | { |
| 4013 | set_scroll_region(0, 0, Columns - 1, Rows - 1); |
| 4014 | } |
| 4015 | |
| 4016 | |
| 4017 | /* |
| 4018 | * Called when started up, to set the winsize that was delayed. |
| 4019 | */ |
| 4020 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4021 | mch_set_winsize_now(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | { |
| 4023 | if (suppress_winsize == 2) |
| 4024 | { |
| 4025 | suppress_winsize = 0; |
| 4026 | mch_set_shellsize(); |
| 4027 | shell_resized(); |
| 4028 | } |
| 4029 | suppress_winsize = 0; |
| 4030 | } |
| 4031 | #endif /* FEAT_GUI_W32 */ |
| 4032 | |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4033 | static BOOL |
| 4034 | vim_create_process( |
Bram Moolenaar | 36c85b2 | 2013-12-12 20:25:44 +0100 | [diff] [blame] | 4035 | char *cmd, |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4036 | BOOL inherit_handles, |
Bram Moolenaar | 3f1138e | 2014-01-05 13:29:26 +0100 | [diff] [blame] | 4037 | DWORD flags, |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4038 | STARTUPINFO *si, |
| 4039 | PROCESS_INFORMATION *pi) |
| 4040 | { |
| 4041 | # ifdef FEAT_MBYTE |
| 4042 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 4043 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4044 | WCHAR *wcmd = enc_to_utf16((char_u *)cmd, NULL); |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4045 | |
| 4046 | if (wcmd != NULL) |
| 4047 | { |
| 4048 | BOOL ret; |
| 4049 | ret = CreateProcessW( |
| 4050 | NULL, /* Executable name */ |
| 4051 | wcmd, /* Command to execute */ |
| 4052 | NULL, /* Process security attributes */ |
| 4053 | NULL, /* Thread security attributes */ |
| 4054 | inherit_handles, /* Inherit handles */ |
| 4055 | flags, /* Creation flags */ |
| 4056 | NULL, /* Environment */ |
| 4057 | NULL, /* Current directory */ |
Bram Moolenaar | 36c85b2 | 2013-12-12 20:25:44 +0100 | [diff] [blame] | 4058 | (LPSTARTUPINFOW)si, /* Startup information */ |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4059 | pi); /* Process information */ |
| 4060 | vim_free(wcmd); |
| 4061 | return ret; |
| 4062 | } |
| 4063 | } |
| 4064 | #endif |
| 4065 | return CreateProcess( |
| 4066 | NULL, /* Executable name */ |
| 4067 | cmd, /* Command to execute */ |
| 4068 | NULL, /* Process security attributes */ |
| 4069 | NULL, /* Thread security attributes */ |
| 4070 | inherit_handles, /* Inherit handles */ |
| 4071 | flags, /* Creation flags */ |
| 4072 | NULL, /* Environment */ |
| 4073 | NULL, /* Current directory */ |
| 4074 | si, /* Startup information */ |
| 4075 | pi); /* Process information */ |
| 4076 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4077 | |
| 4078 | |
| 4079 | #if defined(FEAT_GUI_W32) || defined(PROTO) |
| 4080 | |
| 4081 | /* |
| 4082 | * Specialised version of system() for Win32 GUI mode. |
| 4083 | * This version proceeds as follows: |
| 4084 | * 1. Create a console window for use by the subprocess |
| 4085 | * 2. Run the subprocess (it gets the allocated console by default) |
| 4086 | * 3. Wait for the subprocess to terminate and get its exit code |
| 4087 | * 4. Prompt the user to press a key to close the console window |
| 4088 | */ |
| 4089 | static int |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4090 | mch_system_classic(char *cmd, int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4091 | { |
| 4092 | STARTUPINFO si; |
| 4093 | PROCESS_INFORMATION pi; |
| 4094 | DWORD ret = 0; |
| 4095 | HWND hwnd = GetFocus(); |
| 4096 | |
| 4097 | si.cb = sizeof(si); |
| 4098 | si.lpReserved = NULL; |
| 4099 | si.lpDesktop = NULL; |
| 4100 | si.lpTitle = NULL; |
| 4101 | si.dwFlags = STARTF_USESHOWWINDOW; |
| 4102 | /* |
| 4103 | * It's nicer to run a filter command in a minimized window, but in |
| 4104 | * Windows 95 this makes the command MUCH slower. We can't do it under |
| 4105 | * Win32s either as it stops the synchronous spawn workaround working. |
Bram Moolenaar | 96e5cee | 2010-11-24 12:35:21 +0100 | [diff] [blame] | 4106 | * Don't activate the window to keep focus on Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4107 | */ |
| 4108 | if ((options & SHELL_DOOUT) && !mch_windows95() && !gui_is_win32s()) |
Bram Moolenaar | 96e5cee | 2010-11-24 12:35:21 +0100 | [diff] [blame] | 4109 | si.wShowWindow = SW_SHOWMINNOACTIVE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4110 | else |
| 4111 | si.wShowWindow = SW_SHOWNORMAL; |
| 4112 | si.cbReserved2 = 0; |
| 4113 | si.lpReserved2 = NULL; |
| 4114 | |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 4115 | /* There is a strange error on Windows 95 when using "c:\command.com". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | * When the "c:\\" is left out it works OK...? */ |
| 4117 | if (mch_windows95() |
| 4118 | && (STRNICMP(cmd, "c:/command.com", 14) == 0 |
| 4119 | || STRNICMP(cmd, "c:\\command.com", 14) == 0)) |
| 4120 | cmd += 3; |
| 4121 | |
| 4122 | /* Now, run the command */ |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4123 | vim_create_process(cmd, FALSE, |
| 4124 | CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE, &si, &pi); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4125 | |
| 4126 | /* Wait for the command to terminate before continuing */ |
| 4127 | if (g_PlatformId != VER_PLATFORM_WIN32s) |
| 4128 | { |
| 4129 | #ifdef FEAT_GUI |
| 4130 | int delay = 1; |
| 4131 | |
| 4132 | /* Keep updating the window while waiting for the shell to finish. */ |
| 4133 | for (;;) |
| 4134 | { |
| 4135 | MSG msg; |
| 4136 | |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 4137 | if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4138 | { |
| 4139 | TranslateMessage(&msg); |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 4140 | pDispatchMessage(&msg); |
Bram Moolenaar | e4195c5 | 2012-08-02 12:31:44 +0200 | [diff] [blame] | 4141 | delay = 1; |
| 4142 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | } |
| 4144 | if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) |
| 4145 | break; |
| 4146 | |
| 4147 | /* We start waiting for a very short time and then increase it, so |
| 4148 | * that we respond quickly when the process is quick, and don't |
| 4149 | * consume too much overhead when it's slow. */ |
| 4150 | if (delay < 50) |
| 4151 | delay += 10; |
| 4152 | } |
| 4153 | #else |
| 4154 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 4155 | #endif |
| 4156 | |
| 4157 | /* Get the command exit code */ |
| 4158 | GetExitCodeProcess(pi.hProcess, &ret); |
| 4159 | } |
| 4160 | else |
| 4161 | { |
| 4162 | /* |
| 4163 | * This ugly code is the only quick way of performing |
| 4164 | * a synchronous spawn under Win32s. Yuk. |
| 4165 | */ |
| 4166 | num_windows = 0; |
| 4167 | EnumWindows(win32ssynch_cb, 0); |
| 4168 | old_num_windows = num_windows; |
| 4169 | do |
| 4170 | { |
| 4171 | Sleep(1000); |
| 4172 | num_windows = 0; |
| 4173 | EnumWindows(win32ssynch_cb, 0); |
| 4174 | } while (num_windows == old_num_windows); |
| 4175 | ret = 0; |
| 4176 | } |
| 4177 | |
| 4178 | /* Close the handles to the subprocess, so that it goes away */ |
| 4179 | CloseHandle(pi.hThread); |
| 4180 | CloseHandle(pi.hProcess); |
| 4181 | |
| 4182 | /* Try to get input focus back. Doesn't always work though. */ |
| 4183 | PostMessage(hwnd, WM_SETFOCUS, 0, 0); |
| 4184 | |
| 4185 | return ret; |
| 4186 | } |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4187 | |
| 4188 | /* |
| 4189 | * Thread launched by the gui to send the current buffer data to the |
| 4190 | * process. This way avoid to hang up vim totally if the children |
| 4191 | * process take a long time to process the lines. |
| 4192 | */ |
| 4193 | static DWORD WINAPI |
| 4194 | sub_process_writer(LPVOID param) |
| 4195 | { |
| 4196 | HANDLE g_hChildStd_IN_Wr = param; |
| 4197 | linenr_T lnum = curbuf->b_op_start.lnum; |
| 4198 | DWORD len = 0; |
| 4199 | DWORD l; |
| 4200 | char_u *lp = ml_get(lnum); |
| 4201 | char_u *s; |
| 4202 | int written = 0; |
| 4203 | |
| 4204 | for (;;) |
| 4205 | { |
| 4206 | l = (DWORD)STRLEN(lp + written); |
| 4207 | if (l == 0) |
| 4208 | len = 0; |
| 4209 | else if (lp[written] == NL) |
| 4210 | { |
| 4211 | /* NL -> NUL translation */ |
| 4212 | WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL); |
| 4213 | } |
| 4214 | else |
| 4215 | { |
| 4216 | s = vim_strchr(lp + written, NL); |
| 4217 | WriteFile(g_hChildStd_IN_Wr, (char *)lp + written, |
| 4218 | s == NULL ? l : (DWORD)(s - (lp + written)), |
| 4219 | &len, NULL); |
| 4220 | } |
| 4221 | if (len == (int)l) |
| 4222 | { |
| 4223 | /* Finished a line, add a NL, unless this line should not have |
| 4224 | * one. */ |
| 4225 | if (lnum != curbuf->b_op_end.lnum |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 4226 | || (!curbuf->b_p_bin |
| 4227 | && curbuf->b_p_fixeol) |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4228 | || (lnum != curbuf->b_no_eol_lnum |
| 4229 | && (lnum != curbuf->b_ml.ml_line_count |
| 4230 | || curbuf->b_p_eol))) |
| 4231 | { |
Bram Moolenaar | af62ff3 | 2013-03-19 14:48:29 +0100 | [diff] [blame] | 4232 | WriteFile(g_hChildStd_IN_Wr, "\n", 1, (LPDWORD)&ignored, NULL); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | ++lnum; |
| 4236 | if (lnum > curbuf->b_op_end.lnum) |
| 4237 | break; |
| 4238 | |
| 4239 | lp = ml_get(lnum); |
| 4240 | written = 0; |
| 4241 | } |
| 4242 | else if (len > 0) |
| 4243 | written += len; |
| 4244 | } |
| 4245 | |
| 4246 | /* finished all the lines, close pipe */ |
| 4247 | CloseHandle(g_hChildStd_IN_Wr); |
| 4248 | ExitThread(0); |
| 4249 | } |
| 4250 | |
| 4251 | |
| 4252 | # define BUFLEN 100 /* length for buffer, stolen from unix version */ |
| 4253 | |
| 4254 | /* |
| 4255 | * This function read from the children's stdout and write the |
| 4256 | * data on screen or in the buffer accordingly. |
| 4257 | */ |
| 4258 | static void |
| 4259 | dump_pipe(int options, |
| 4260 | HANDLE g_hChildStd_OUT_Rd, |
| 4261 | garray_T *ga, |
| 4262 | char_u buffer[], |
| 4263 | DWORD *buffer_off) |
| 4264 | { |
| 4265 | DWORD availableBytes = 0; |
| 4266 | DWORD i; |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4267 | int ret; |
| 4268 | DWORD len; |
| 4269 | DWORD toRead; |
| 4270 | int repeatCount; |
| 4271 | |
| 4272 | /* we query the pipe to see if there is any data to read |
| 4273 | * to avoid to perform a blocking read */ |
| 4274 | ret = PeekNamedPipe(g_hChildStd_OUT_Rd, /* pipe to query */ |
| 4275 | NULL, /* optional buffer */ |
Bram Moolenaar | f6a2b08 | 2012-06-29 13:14:03 +0200 | [diff] [blame] | 4276 | 0, /* buffer size */ |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4277 | NULL, /* number of read bytes */ |
| 4278 | &availableBytes, /* available bytes total */ |
| 4279 | NULL); /* byteLeft */ |
| 4280 | |
| 4281 | repeatCount = 0; |
| 4282 | /* We got real data in the pipe, read it */ |
Bram Moolenaar | f6a2b08 | 2012-06-29 13:14:03 +0200 | [diff] [blame] | 4283 | while (ret != 0 && availableBytes > 0) |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4284 | { |
| 4285 | repeatCount++; |
| 4286 | toRead = |
| 4287 | # ifdef FEAT_MBYTE |
| 4288 | (DWORD)(BUFLEN - *buffer_off); |
| 4289 | # else |
| 4290 | (DWORD)BUFLEN; |
| 4291 | # endif |
| 4292 | toRead = availableBytes < toRead ? availableBytes : toRead; |
| 4293 | ReadFile(g_hChildStd_OUT_Rd, buffer |
| 4294 | # ifdef FEAT_MBYTE |
| 4295 | + *buffer_off, toRead |
| 4296 | # else |
| 4297 | , toRead |
| 4298 | # endif |
| 4299 | , &len, NULL); |
| 4300 | |
| 4301 | /* If we haven't read anything, there is a problem */ |
| 4302 | if (len == 0) |
| 4303 | break; |
| 4304 | |
| 4305 | availableBytes -= len; |
| 4306 | |
| 4307 | if (options & SHELL_READ) |
| 4308 | { |
| 4309 | /* Do NUL -> NL translation, append NL separated |
| 4310 | * lines to the current buffer. */ |
| 4311 | for (i = 0; i < len; ++i) |
| 4312 | { |
| 4313 | if (buffer[i] == NL) |
| 4314 | append_ga_line(ga); |
| 4315 | else if (buffer[i] == NUL) |
| 4316 | ga_append(ga, NL); |
| 4317 | else |
| 4318 | ga_append(ga, buffer[i]); |
| 4319 | } |
| 4320 | } |
| 4321 | # ifdef FEAT_MBYTE |
| 4322 | else if (has_mbyte) |
| 4323 | { |
| 4324 | int l; |
Bram Moolenaar | 2eba182 | 2011-08-27 15:10:04 +0200 | [diff] [blame] | 4325 | int c; |
| 4326 | char_u *p; |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4327 | |
| 4328 | len += *buffer_off; |
| 4329 | buffer[len] = NUL; |
| 4330 | |
| 4331 | /* Check if the last character in buffer[] is |
| 4332 | * incomplete, keep these bytes for the next |
| 4333 | * round. */ |
| 4334 | for (p = buffer; p < buffer + len; p += l) |
| 4335 | { |
| 4336 | l = mb_cptr2len(p); |
| 4337 | if (l == 0) |
| 4338 | l = 1; /* NUL byte? */ |
| 4339 | else if (MB_BYTE2LEN(*p) != l) |
| 4340 | break; |
| 4341 | } |
| 4342 | if (p == buffer) /* no complete character */ |
| 4343 | { |
| 4344 | /* avoid getting stuck at an illegal byte */ |
| 4345 | if (len >= 12) |
| 4346 | ++p; |
| 4347 | else |
| 4348 | { |
| 4349 | *buffer_off = len; |
| 4350 | return; |
| 4351 | } |
| 4352 | } |
| 4353 | c = *p; |
| 4354 | *p = NUL; |
| 4355 | msg_puts(buffer); |
| 4356 | if (p < buffer + len) |
| 4357 | { |
| 4358 | *p = c; |
| 4359 | *buffer_off = (DWORD)((buffer + len) - p); |
| 4360 | mch_memmove(buffer, p, *buffer_off); |
| 4361 | return; |
| 4362 | } |
| 4363 | *buffer_off = 0; |
| 4364 | } |
| 4365 | # endif /* FEAT_MBYTE */ |
| 4366 | else |
| 4367 | { |
| 4368 | buffer[len] = NUL; |
| 4369 | msg_puts(buffer); |
| 4370 | } |
| 4371 | |
| 4372 | windgoto(msg_row, msg_col); |
| 4373 | cursor_on(); |
| 4374 | out_flush(); |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | /* |
| 4379 | * Version of system to use for windows NT > 5.0 (Win2K), use pipe |
| 4380 | * for communication and doesn't open any new window. |
| 4381 | */ |
| 4382 | static int |
| 4383 | mch_system_piped(char *cmd, int options) |
| 4384 | { |
| 4385 | STARTUPINFO si; |
| 4386 | PROCESS_INFORMATION pi; |
| 4387 | DWORD ret = 0; |
| 4388 | |
| 4389 | HANDLE g_hChildStd_IN_Rd = NULL; |
| 4390 | HANDLE g_hChildStd_IN_Wr = NULL; |
| 4391 | HANDLE g_hChildStd_OUT_Rd = NULL; |
| 4392 | HANDLE g_hChildStd_OUT_Wr = NULL; |
| 4393 | |
| 4394 | char_u buffer[BUFLEN + 1]; /* reading buffer + size */ |
| 4395 | DWORD len; |
| 4396 | |
| 4397 | /* buffer used to receive keys */ |
| 4398 | char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ |
| 4399 | int ta_len = 0; /* valid bytes in ta_buf[] */ |
| 4400 | |
| 4401 | DWORD i; |
| 4402 | int c; |
| 4403 | int noread_cnt = 0; |
| 4404 | garray_T ga; |
| 4405 | int delay = 1; |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4406 | DWORD buffer_off = 0; /* valid bytes in buffer[] */ |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4407 | char *p = NULL; |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4408 | |
| 4409 | SECURITY_ATTRIBUTES saAttr; |
| 4410 | |
| 4411 | /* Set the bInheritHandle flag so pipe handles are inherited. */ |
| 4412 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4413 | saAttr.bInheritHandle = TRUE; |
| 4414 | saAttr.lpSecurityDescriptor = NULL; |
| 4415 | |
| 4416 | if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) |
| 4417 | /* Ensure the read handle to the pipe for STDOUT is not inherited. */ |
| 4418 | || ! pSetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) |
| 4419 | /* Create a pipe for the child process's STDIN. */ |
| 4420 | || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0) |
| 4421 | /* Ensure the write handle to the pipe for STDIN is not inherited. */ |
| 4422 | || ! pSetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) |
| 4423 | { |
| 4424 | CloseHandle(g_hChildStd_IN_Rd); |
| 4425 | CloseHandle(g_hChildStd_IN_Wr); |
| 4426 | CloseHandle(g_hChildStd_OUT_Rd); |
| 4427 | CloseHandle(g_hChildStd_OUT_Wr); |
| 4428 | MSG_PUTS(_("\nCannot create pipes\n")); |
| 4429 | } |
| 4430 | |
| 4431 | si.cb = sizeof(si); |
| 4432 | si.lpReserved = NULL; |
| 4433 | si.lpDesktop = NULL; |
| 4434 | si.lpTitle = NULL; |
| 4435 | si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 4436 | |
| 4437 | /* set-up our file redirection */ |
| 4438 | si.hStdError = g_hChildStd_OUT_Wr; |
| 4439 | si.hStdOutput = g_hChildStd_OUT_Wr; |
| 4440 | si.hStdInput = g_hChildStd_IN_Rd; |
| 4441 | si.wShowWindow = SW_HIDE; |
| 4442 | si.cbReserved2 = 0; |
| 4443 | si.lpReserved2 = NULL; |
| 4444 | |
| 4445 | if (options & SHELL_READ) |
| 4446 | ga_init2(&ga, 1, BUFLEN); |
| 4447 | |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4448 | if (cmd != NULL) |
| 4449 | { |
| 4450 | p = (char *)vim_strsave((char_u *)cmd); |
| 4451 | if (p != NULL) |
| 4452 | unescape_shellxquote((char_u *)p, p_sxe); |
| 4453 | else |
| 4454 | p = cmd; |
| 4455 | } |
| 4456 | |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4457 | /* Now, run the command. |
| 4458 | * About "Inherit handles" being TRUE: this command can be litigious, |
| 4459 | * handle inheritance was deactivated for pending temp file, but, if we |
| 4460 | * deactivate it, the pipes don't work for some reason. */ |
| 4461 | vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE, &si, &pi); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4462 | |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4463 | if (p != cmd) |
| 4464 | vim_free(p); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4465 | |
| 4466 | /* Close our unused side of the pipes */ |
| 4467 | CloseHandle(g_hChildStd_IN_Rd); |
| 4468 | CloseHandle(g_hChildStd_OUT_Wr); |
| 4469 | |
| 4470 | if (options & SHELL_WRITE) |
| 4471 | { |
| 4472 | HANDLE thread = |
| 4473 | CreateThread(NULL, /* security attributes */ |
| 4474 | 0, /* default stack size */ |
| 4475 | sub_process_writer, /* function to be executed */ |
| 4476 | g_hChildStd_IN_Wr, /* parameter */ |
| 4477 | 0, /* creation flag, start immediately */ |
| 4478 | NULL); /* we don't care about thread id */ |
| 4479 | CloseHandle(thread); |
| 4480 | g_hChildStd_IN_Wr = NULL; |
| 4481 | } |
| 4482 | |
| 4483 | /* Keep updating the window while waiting for the shell to finish. */ |
| 4484 | for (;;) |
| 4485 | { |
| 4486 | MSG msg; |
| 4487 | |
Bram Moolenaar | 175d070 | 2013-12-11 18:36:33 +0100 | [diff] [blame] | 4488 | if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4489 | { |
| 4490 | TranslateMessage(&msg); |
Bram Moolenaar | 175d070 | 2013-12-11 18:36:33 +0100 | [diff] [blame] | 4491 | pDispatchMessage(&msg); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | /* write pipe information in the window */ |
| 4495 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4496 | # ifdef FEAT_GUI |
| 4497 | || gui.in_use |
| 4498 | # endif |
| 4499 | ) |
| 4500 | { |
| 4501 | len = 0; |
| 4502 | if (!(options & SHELL_EXPAND) |
| 4503 | && ((options & |
| 4504 | (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) |
| 4505 | != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) |
| 4506 | # ifdef FEAT_GUI |
| 4507 | || gui.in_use |
| 4508 | # endif |
| 4509 | ) |
| 4510 | && (ta_len > 0 || noread_cnt > 4)) |
| 4511 | { |
| 4512 | if (ta_len == 0) |
| 4513 | { |
| 4514 | /* Get extra characters when we don't have any. Reset the |
| 4515 | * counter and timer. */ |
| 4516 | noread_cnt = 0; |
| 4517 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4518 | gettimeofday(&start_tv, NULL); |
| 4519 | # endif |
| 4520 | len = ui_inchar(ta_buf, BUFLEN, 10L, 0); |
| 4521 | } |
| 4522 | if (ta_len > 0 || len > 0) |
| 4523 | { |
| 4524 | /* |
| 4525 | * For pipes: Check for CTRL-C: send interrupt signal to |
| 4526 | * child. Check for CTRL-D: EOF, close pipe to child. |
| 4527 | */ |
| 4528 | if (len == 1 && cmd != NULL) |
| 4529 | { |
| 4530 | if (ta_buf[ta_len] == Ctrl_C) |
| 4531 | { |
| 4532 | /* Learn what exit code is expected, for |
| 4533 | * now put 9 as SIGKILL */ |
| 4534 | TerminateProcess(pi.hProcess, 9); |
| 4535 | } |
| 4536 | if (ta_buf[ta_len] == Ctrl_D) |
| 4537 | { |
| 4538 | CloseHandle(g_hChildStd_IN_Wr); |
| 4539 | g_hChildStd_IN_Wr = NULL; |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | /* replace K_BS by <BS> and K_DEL by <DEL> */ |
| 4544 | for (i = ta_len; i < ta_len + len; ++i) |
| 4545 | { |
| 4546 | if (ta_buf[i] == CSI && len - i > 2) |
| 4547 | { |
| 4548 | c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); |
| 4549 | if (c == K_DEL || c == K_KDEL || c == K_BS) |
| 4550 | { |
| 4551 | mch_memmove(ta_buf + i + 1, ta_buf + i + 3, |
| 4552 | (size_t)(len - i - 2)); |
| 4553 | if (c == K_DEL || c == K_KDEL) |
| 4554 | ta_buf[i] = DEL; |
| 4555 | else |
| 4556 | ta_buf[i] = Ctrl_H; |
| 4557 | len -= 2; |
| 4558 | } |
| 4559 | } |
| 4560 | else if (ta_buf[i] == '\r') |
| 4561 | ta_buf[i] = '\n'; |
| 4562 | # ifdef FEAT_MBYTE |
| 4563 | if (has_mbyte) |
| 4564 | i += (*mb_ptr2len_len)(ta_buf + i, |
| 4565 | ta_len + len - i) - 1; |
| 4566 | # endif |
| 4567 | } |
| 4568 | |
| 4569 | /* |
| 4570 | * For pipes: echo the typed characters. For a pty this |
| 4571 | * does not seem to work. |
| 4572 | */ |
| 4573 | for (i = ta_len; i < ta_len + len; ++i) |
| 4574 | { |
| 4575 | if (ta_buf[i] == '\n' || ta_buf[i] == '\b') |
| 4576 | msg_putchar(ta_buf[i]); |
| 4577 | # ifdef FEAT_MBYTE |
| 4578 | else if (has_mbyte) |
| 4579 | { |
| 4580 | int l = (*mb_ptr2len)(ta_buf + i); |
| 4581 | |
| 4582 | msg_outtrans_len(ta_buf + i, l); |
| 4583 | i += l - 1; |
| 4584 | } |
| 4585 | # endif |
| 4586 | else |
| 4587 | msg_outtrans_len(ta_buf + i, 1); |
| 4588 | } |
| 4589 | windgoto(msg_row, msg_col); |
| 4590 | out_flush(); |
| 4591 | |
| 4592 | ta_len += len; |
| 4593 | |
| 4594 | /* |
| 4595 | * Write the characters to the child, unless EOF has been |
| 4596 | * typed for pipes. Write one character at a time, to |
| 4597 | * avoid losing too much typeahead. When writing buffer |
| 4598 | * lines, drop the typed characters (only check for |
| 4599 | * CTRL-C). |
| 4600 | */ |
| 4601 | if (options & SHELL_WRITE) |
| 4602 | ta_len = 0; |
| 4603 | else if (g_hChildStd_IN_Wr != NULL) |
| 4604 | { |
| 4605 | WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf, |
| 4606 | 1, &len, NULL); |
| 4607 | // if we are typing in, we want to keep things reactive |
| 4608 | delay = 1; |
| 4609 | if (len > 0) |
| 4610 | { |
| 4611 | ta_len -= len; |
| 4612 | mch_memmove(ta_buf, ta_buf + len, ta_len); |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | if (ta_len) |
| 4620 | ui_inchar_undo(ta_buf, ta_len); |
| 4621 | |
| 4622 | if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) |
| 4623 | { |
Bram Moolenaar | 2eba182 | 2011-08-27 15:10:04 +0200 | [diff] [blame] | 4624 | dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4625 | break; |
| 4626 | } |
| 4627 | |
| 4628 | ++noread_cnt; |
Bram Moolenaar | 2eba182 | 2011-08-27 15:10:04 +0200 | [diff] [blame] | 4629 | dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4630 | |
| 4631 | /* We start waiting for a very short time and then increase it, so |
| 4632 | * that we respond quickly when the process is quick, and don't |
| 4633 | * consume too much overhead when it's slow. */ |
| 4634 | if (delay < 50) |
| 4635 | delay += 10; |
| 4636 | } |
| 4637 | |
| 4638 | /* Close the pipe */ |
| 4639 | CloseHandle(g_hChildStd_OUT_Rd); |
| 4640 | if (g_hChildStd_IN_Wr != NULL) |
| 4641 | CloseHandle(g_hChildStd_IN_Wr); |
| 4642 | |
| 4643 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 4644 | |
| 4645 | /* Get the command exit code */ |
| 4646 | GetExitCodeProcess(pi.hProcess, &ret); |
| 4647 | |
| 4648 | if (options & SHELL_READ) |
| 4649 | { |
| 4650 | if (ga.ga_len > 0) |
| 4651 | { |
| 4652 | append_ga_line(&ga); |
| 4653 | /* remember that the NL was missing */ |
| 4654 | curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; |
| 4655 | } |
| 4656 | else |
| 4657 | curbuf->b_no_eol_lnum = 0; |
| 4658 | ga_clear(&ga); |
| 4659 | } |
| 4660 | |
| 4661 | /* Close the handles to the subprocess, so that it goes away */ |
| 4662 | CloseHandle(pi.hThread); |
| 4663 | CloseHandle(pi.hProcess); |
| 4664 | |
| 4665 | return ret; |
| 4666 | } |
| 4667 | |
| 4668 | static int |
| 4669 | mch_system(char *cmd, int options) |
| 4670 | { |
| 4671 | /* if we can pipe and the shelltemp option is off */ |
| 4672 | if (allowPiping && !p_stmp) |
| 4673 | return mch_system_piped(cmd, options); |
| 4674 | else |
| 4675 | return mch_system_classic(cmd, options); |
| 4676 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4677 | #else |
| 4678 | |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4679 | # ifdef FEAT_MBYTE |
| 4680 | static int |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 4681 | mch_system(char *cmd, int options) |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4682 | { |
| 4683 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 4684 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4685 | WCHAR *wcmd = enc_to_utf16((char_u *)cmd, NULL); |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4686 | if (wcmd != NULL) |
| 4687 | { |
| 4688 | int ret = _wsystem(wcmd); |
| 4689 | vim_free(wcmd); |
| 4690 | return ret; |
| 4691 | } |
| 4692 | } |
| 4693 | return system(cmd); |
| 4694 | } |
| 4695 | # else |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 4696 | # define mch_system(c, o) system(c) |
Bram Moolenaar | 910cffb | 2013-12-11 17:58:35 +0100 | [diff] [blame] | 4697 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4698 | |
| 4699 | #endif |
| 4700 | |
| 4701 | /* |
| 4702 | * Either execute a command by calling the shell or start a new shell |
| 4703 | */ |
| 4704 | int |
| 4705 | mch_call_shell( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4706 | char_u *cmd, |
| 4707 | int options) /* SHELL_*, see vim.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4708 | { |
| 4709 | int x = 0; |
| 4710 | int tmode = cur_tmode; |
| 4711 | #ifdef FEAT_TITLE |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4712 | char szShellTitle[512]; |
Bram Moolenaar | 1df52d7 | 2014-10-15 22:50:10 +0200 | [diff] [blame] | 4713 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4714 | int did_set_title = FALSE; |
| 4715 | |
Bram Moolenaar | 1df52d7 | 2014-10-15 22:50:10 +0200 | [diff] [blame] | 4716 | /* Change the title to reflect that we are in a subshell. */ |
| 4717 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 4718 | { |
| 4719 | WCHAR szShellTitle[512]; |
| 4720 | |
| 4721 | if (GetConsoleTitleW(szShellTitle, |
| 4722 | sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0) |
| 4723 | { |
| 4724 | if (cmd == NULL) |
| 4725 | wcscat(szShellTitle, L" :sh"); |
| 4726 | else |
| 4727 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4728 | WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL); |
Bram Moolenaar | 1df52d7 | 2014-10-15 22:50:10 +0200 | [diff] [blame] | 4729 | |
| 4730 | if (wn != NULL) |
| 4731 | { |
| 4732 | wcscat(szShellTitle, L" - !"); |
| 4733 | if ((wcslen(szShellTitle) + wcslen(wn) < |
| 4734 | sizeof(szShellTitle)/sizeof(WCHAR))) |
| 4735 | wcscat(szShellTitle, wn); |
| 4736 | SetConsoleTitleW(szShellTitle); |
| 4737 | vim_free(wn); |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4738 | did_set_title = TRUE; |
Bram Moolenaar | 1df52d7 | 2014-10-15 22:50:10 +0200 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | } |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4743 | if (!did_set_title) |
| 4744 | # endif |
| 4745 | /* Change the title to reflect that we are in a subshell. */ |
| 4746 | if (GetConsoleTitle(szShellTitle, sizeof(szShellTitle) - 4) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4747 | { |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4748 | if (cmd == NULL) |
| 4749 | strcat(szShellTitle, " :sh"); |
| 4750 | else |
| 4751 | { |
| 4752 | strcat(szShellTitle, " - !"); |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4753 | if ((strlen(szShellTitle) + strlen((char *)cmd) |
| 4754 | < sizeof(szShellTitle))) |
| 4755 | strcat(szShellTitle, (char *)cmd); |
Bram Moolenaar | 799d6ab | 2014-10-16 16:16:37 +0200 | [diff] [blame] | 4756 | } |
| 4757 | SetConsoleTitle(szShellTitle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4758 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | #endif |
| 4760 | |
| 4761 | out_flush(); |
| 4762 | |
| 4763 | #ifdef MCH_WRITE_DUMP |
| 4764 | if (fdDump) |
| 4765 | { |
| 4766 | fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options); |
| 4767 | fflush(fdDump); |
| 4768 | } |
| 4769 | #endif |
| 4770 | |
| 4771 | /* |
| 4772 | * Catch all deadly signals while running the external command, because a |
| 4773 | * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us. |
| 4774 | */ |
| 4775 | signal(SIGINT, SIG_IGN); |
| 4776 | #if defined(__GNUC__) && !defined(__MINGW32__) |
| 4777 | signal(SIGKILL, SIG_IGN); |
| 4778 | #else |
| 4779 | signal(SIGBREAK, SIG_IGN); |
| 4780 | #endif |
| 4781 | signal(SIGILL, SIG_IGN); |
| 4782 | signal(SIGFPE, SIG_IGN); |
| 4783 | signal(SIGSEGV, SIG_IGN); |
| 4784 | signal(SIGTERM, SIG_IGN); |
| 4785 | signal(SIGABRT, SIG_IGN); |
| 4786 | |
| 4787 | if (options & SHELL_COOKED) |
| 4788 | settmode(TMODE_COOK); /* set to normal mode */ |
| 4789 | |
| 4790 | if (cmd == NULL) |
| 4791 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4792 | x = mch_system((char *)p_sh, options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4793 | } |
| 4794 | else |
| 4795 | { |
| 4796 | /* we use "command" or "cmd" to start the shell; slow but easy */ |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4797 | char_u *newcmd = NULL; |
| 4798 | char_u *cmdbase = cmd; |
| 4799 | long_u cmdlen; |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4800 | |
| 4801 | /* Skip a leading ", ( and "(. */ |
| 4802 | if (*cmdbase == '"' ) |
| 4803 | ++cmdbase; |
| 4804 | if (*cmdbase == '(') |
| 4805 | ++cmdbase; |
| 4806 | |
| 4807 | if ((STRNICMP(cmdbase, "start", 5) == 0) && vim_iswhite(cmdbase[5])) |
| 4808 | { |
| 4809 | STARTUPINFO si; |
| 4810 | PROCESS_INFORMATION pi; |
| 4811 | DWORD flags = CREATE_NEW_CONSOLE; |
| 4812 | char_u *p; |
| 4813 | |
Bram Moolenaar | fcc3f46 | 2014-01-24 19:55:37 +0100 | [diff] [blame] | 4814 | ZeroMemory(&si, sizeof(si)); |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4815 | si.cb = sizeof(si); |
| 4816 | si.lpReserved = NULL; |
| 4817 | si.lpDesktop = NULL; |
| 4818 | si.lpTitle = NULL; |
| 4819 | si.dwFlags = 0; |
| 4820 | si.cbReserved2 = 0; |
| 4821 | si.lpReserved2 = NULL; |
| 4822 | |
| 4823 | cmdbase = skipwhite(cmdbase + 5); |
| 4824 | if ((STRNICMP(cmdbase, "/min", 4) == 0) |
| 4825 | && vim_iswhite(cmdbase[4])) |
| 4826 | { |
| 4827 | cmdbase = skipwhite(cmdbase + 4); |
| 4828 | si.dwFlags = STARTF_USESHOWWINDOW; |
| 4829 | si.wShowWindow = SW_SHOWMINNOACTIVE; |
| 4830 | } |
| 4831 | else if ((STRNICMP(cmdbase, "/b", 2) == 0) |
| 4832 | && vim_iswhite(cmdbase[2])) |
| 4833 | { |
| 4834 | cmdbase = skipwhite(cmdbase + 2); |
| 4835 | flags = CREATE_NO_WINDOW; |
| 4836 | si.dwFlags = STARTF_USESTDHANDLES; |
| 4837 | si.hStdInput = CreateFile("\\\\.\\NUL", // File name |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4838 | GENERIC_READ, // Access flags |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4839 | 0, // Share flags |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4840 | NULL, // Security att. |
| 4841 | OPEN_EXISTING, // Open flags |
| 4842 | FILE_ATTRIBUTE_NORMAL, // File att. |
| 4843 | NULL); // Temp file |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4844 | si.hStdOutput = si.hStdInput; |
| 4845 | si.hStdError = si.hStdInput; |
| 4846 | } |
| 4847 | |
| 4848 | /* Remove a trailing ", ) and )" if they have a match |
| 4849 | * at the start of the command. */ |
| 4850 | if (cmdbase > cmd) |
| 4851 | { |
| 4852 | p = cmdbase + STRLEN(cmdbase); |
| 4853 | if (p > cmdbase && p[-1] == '"' && *cmd == '"') |
| 4854 | *--p = NUL; |
| 4855 | if (p > cmdbase && p[-1] == ')' |
| 4856 | && (*cmd =='(' || cmd[1] == '(')) |
| 4857 | *--p = NUL; |
| 4858 | } |
| 4859 | |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4860 | newcmd = cmdbase; |
| 4861 | unescape_shellxquote(cmdbase, p_sxe); |
| 4862 | |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4863 | /* |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4864 | * If creating new console, arguments are passed to the |
| 4865 | * 'cmd.exe' as-is. If it's not, arguments are not treated |
| 4866 | * correctly for current 'cmd.exe'. So unescape characters in |
| 4867 | * shellxescape except '|' for avoiding to be treated as |
| 4868 | * argument to them. Pass the arguments to sub-shell. |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4869 | */ |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4870 | if (flags != CREATE_NEW_CONSOLE) |
| 4871 | { |
| 4872 | char_u *subcmd; |
Bram Moolenaar | ee7d100 | 2012-02-22 15:34:08 +0100 | [diff] [blame] | 4873 | char_u *cmd_shell = mch_getenv("COMSPEC"); |
| 4874 | |
| 4875 | if (cmd_shell == NULL || *cmd_shell == NUL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4876 | cmd_shell = (char_u *)default_shell(); |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4877 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4878 | subcmd = vim_strsave_escaped_ext(cmdbase, |
| 4879 | (char_u *)"|", '^', FALSE); |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4880 | if (subcmd != NULL) |
| 4881 | { |
| 4882 | /* make "cmd.exe /c arguments" */ |
| 4883 | cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5; |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4884 | newcmd = lalloc(cmdlen, TRUE); |
| 4885 | if (newcmd != NULL) |
| 4886 | vim_snprintf((char *)newcmd, cmdlen, "%s /c %s", |
Bram Moolenaar | ee7d100 | 2012-02-22 15:34:08 +0100 | [diff] [blame] | 4887 | cmd_shell, subcmd); |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4888 | else |
| 4889 | newcmd = cmdbase; |
Bram Moolenaar | ee7d100 | 2012-02-22 15:34:08 +0100 | [diff] [blame] | 4890 | vim_free(subcmd); |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4891 | } |
| 4892 | } |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4893 | |
| 4894 | /* |
| 4895 | * Now, start the command as a process, so that it doesn't |
| 4896 | * inherit our handles which causes unpleasant dangling swap |
| 4897 | * files if we exit before the spawned process |
| 4898 | */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4899 | if (vim_create_process((char *)newcmd, FALSE, flags, &si, &pi)) |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4900 | x = 0; |
| 4901 | else |
| 4902 | { |
| 4903 | x = -1; |
| 4904 | #ifdef FEAT_GUI_W32 |
| 4905 | EMSG(_("E371: Command not found")); |
| 4906 | #endif |
| 4907 | } |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4908 | |
| 4909 | if (newcmd != cmdbase) |
| 4910 | vim_free(newcmd); |
| 4911 | |
Bram Moolenaar | fcc3f46 | 2014-01-24 19:55:37 +0100 | [diff] [blame] | 4912 | if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL) |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4913 | { |
Bram Moolenaar | fcc3f46 | 2014-01-24 19:55:37 +0100 | [diff] [blame] | 4914 | /* Close the handle to \\.\NUL created above. */ |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4915 | CloseHandle(si.hStdInput); |
| 4916 | } |
| 4917 | /* Close the handles to the subprocess, so that it goes away */ |
| 4918 | CloseHandle(pi.hThread); |
| 4919 | CloseHandle(pi.hProcess); |
| 4920 | } |
| 4921 | else |
| 4922 | { |
Bram Moolenaar | fb7df7b | 2012-02-22 13:07:05 +0100 | [diff] [blame] | 4923 | cmdlen = ( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4924 | #ifdef FEAT_GUI_W32 |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4925 | (allowPiping && !p_stmp ? 0 : STRLEN(vimrun_path)) + |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | #endif |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 4927 | STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10); |
| 4928 | |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4929 | newcmd = lalloc(cmdlen, TRUE); |
| 4930 | if (newcmd != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4931 | { |
| 4932 | #if defined(FEAT_GUI_W32) |
| 4933 | if (need_vimrun_warning) |
| 4934 | { |
| 4935 | MessageBox(NULL, |
| 4936 | _("VIMRUN.EXE not found in your $PATH.\n" |
| 4937 | "External commands will not pause after completion.\n" |
| 4938 | "See :help win32-vimrun for more information."), |
| 4939 | _("Vim Warning"), |
| 4940 | MB_ICONWARNING); |
| 4941 | need_vimrun_warning = FALSE; |
| 4942 | } |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4943 | if (!s_dont_use_vimrun && (!allowPiping || p_stmp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4944 | /* Use vimrun to execute the command. It opens a console |
| 4945 | * window, which can be closed without killing Vim. */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4946 | vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4947 | vimrun_path, |
| 4948 | (msg_silent != 0 || (options & SHELL_DOOUT)) |
| 4949 | ? "-s " : "", |
| 4950 | p_sh, p_shcf, cmd); |
| 4951 | else |
| 4952 | #endif |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4953 | vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 4954 | p_sh, p_shcf, cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4955 | x = mch_system((char *)newcmd, options); |
Bram Moolenaar | 6b707b4 | 2012-02-21 21:22:44 +0100 | [diff] [blame] | 4956 | vim_free(newcmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4957 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | |
| 4961 | if (tmode == TMODE_RAW) |
| 4962 | settmode(TMODE_RAW); /* set to raw mode */ |
| 4963 | |
| 4964 | /* Print the return value, unless "vimrun" was used. */ |
| 4965 | if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent |
| 4966 | #if defined(FEAT_GUI_W32) |
Bram Moolenaar | 4b9669f | 2011-07-07 16:20:52 +0200 | [diff] [blame] | 4967 | && ((options & SHELL_DOOUT) || s_dont_use_vimrun |
| 4968 | || (allowPiping && !p_stmp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4969 | #endif |
| 4970 | ) |
| 4971 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4972 | smsg((char_u *)_("shell returned %d"), x); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | msg_putchar('\n'); |
| 4974 | } |
| 4975 | #ifdef FEAT_TITLE |
| 4976 | resettitle(); |
| 4977 | #endif |
| 4978 | |
| 4979 | signal(SIGINT, SIG_DFL); |
| 4980 | #if defined(__GNUC__) && !defined(__MINGW32__) |
| 4981 | signal(SIGKILL, SIG_DFL); |
| 4982 | #else |
| 4983 | signal(SIGBREAK, SIG_DFL); |
| 4984 | #endif |
| 4985 | signal(SIGILL, SIG_DFL); |
| 4986 | signal(SIGFPE, SIG_DFL); |
| 4987 | signal(SIGSEGV, SIG_DFL); |
| 4988 | signal(SIGTERM, SIG_DFL); |
| 4989 | signal(SIGABRT, SIG_DFL); |
| 4990 | |
| 4991 | return x; |
| 4992 | } |
| 4993 | |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 4994 | #if defined(FEAT_JOB) || defined(PROTO) |
Bram Moolenaar | 7bffaa9 | 2016-03-10 21:46:03 +0100 | [diff] [blame] | 4995 | static HANDLE |
| 4996 | job_io_file_open( |
| 4997 | char_u *fname, |
| 4998 | DWORD dwDesiredAccess, |
| 4999 | DWORD dwShareMode, |
| 5000 | LPSECURITY_ATTRIBUTES lpSecurityAttributes, |
| 5001 | DWORD dwCreationDisposition, |
| 5002 | DWORD dwFlagsAndAttributes) |
| 5003 | { |
| 5004 | HANDLE h; |
| 5005 | # ifdef FEAT_MBYTE |
| 5006 | WCHAR *wn = NULL; |
| 5007 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 5008 | { |
| 5009 | wn = enc_to_utf16(fname, NULL); |
| 5010 | if (wn != NULL) |
| 5011 | { |
| 5012 | h = CreateFileW(wn, dwDesiredAccess, dwShareMode, |
| 5013 | lpSecurityAttributes, dwCreationDisposition, |
| 5014 | dwFlagsAndAttributes, NULL); |
| 5015 | vim_free(wn); |
| 5016 | if (h == INVALID_HANDLE_VALUE |
| 5017 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 5018 | wn = NULL; |
| 5019 | } |
| 5020 | } |
| 5021 | if (wn == NULL) |
| 5022 | # endif |
| 5023 | |
| 5024 | h = CreateFile((LPCSTR)fname, dwDesiredAccess, dwShareMode, |
| 5025 | lpSecurityAttributes, dwCreationDisposition, |
| 5026 | dwFlagsAndAttributes, NULL); |
| 5027 | return h; |
| 5028 | } |
| 5029 | |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5030 | void |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 5031 | mch_start_job(char *cmd, job_T *job, jobopt_T *options) |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5032 | { |
| 5033 | STARTUPINFO si; |
| 5034 | PROCESS_INFORMATION pi; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5035 | HANDLE jo; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5036 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5037 | SECURITY_ATTRIBUTES saAttr; |
| 5038 | channel_T *channel = NULL; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5039 | HANDLE ifd[2]; |
| 5040 | HANDLE ofd[2]; |
| 5041 | HANDLE efd[2]; |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5042 | |
| 5043 | int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL; |
| 5044 | int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL; |
| 5045 | int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL; |
| 5046 | int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE; |
| 5047 | int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE; |
| 5048 | int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE; |
| 5049 | int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT; |
| 5050 | |
| 5051 | if (use_out_for_err && use_null_for_out) |
| 5052 | use_null_for_err = TRUE; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5053 | |
| 5054 | ifd[0] = INVALID_HANDLE_VALUE; |
| 5055 | ifd[1] = INVALID_HANDLE_VALUE; |
| 5056 | ofd[0] = INVALID_HANDLE_VALUE; |
| 5057 | ofd[1] = INVALID_HANDLE_VALUE; |
| 5058 | efd[0] = INVALID_HANDLE_VALUE; |
| 5059 | efd[1] = INVALID_HANDLE_VALUE; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5060 | # endif |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5061 | |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5062 | jo = CreateJobObject(NULL, NULL); |
| 5063 | if (jo == NULL) |
| 5064 | { |
| 5065 | job->jv_status = JOB_FAILED; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5066 | goto failed; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | ZeroMemory(&pi, sizeof(pi)); |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5070 | ZeroMemory(&si, sizeof(si)); |
| 5071 | si.cb = sizeof(si); |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5072 | si.dwFlags |= STARTF_USESHOWWINDOW; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5073 | si.wShowWindow = SW_HIDE; |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5074 | |
Bram Moolenaar | 5d54a04 | 2016-02-16 16:39:51 +0100 | [diff] [blame] | 5075 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5076 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 5077 | saAttr.bInheritHandle = TRUE; |
| 5078 | saAttr.lpSecurityDescriptor = NULL; |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5079 | |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5080 | if (use_file_for_in) |
| 5081 | { |
| 5082 | char_u *fname = options->jo_io_name[PART_IN]; |
| 5083 | |
Bram Moolenaar | 7bffaa9 | 2016-03-10 21:46:03 +0100 | [diff] [blame] | 5084 | ifd[0] = job_io_file_open(fname, GENERIC_READ, |
| 5085 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 5086 | &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); |
| 5087 | if (ifd[0] == INVALID_HANDLE_VALUE) |
Bram Moolenaar | 94d0191 | 2016-03-08 13:48:51 +0100 | [diff] [blame] | 5088 | { |
| 5089 | EMSG2(_(e_notopen), fname); |
| 5090 | goto failed; |
| 5091 | } |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5092 | } |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5093 | else if (!use_null_for_in && |
| 5094 | (!CreatePipe(&ifd[0], &ifd[1], &saAttr, 0) |
| 5095 | || !pSetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0))) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5096 | goto failed; |
| 5097 | |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5098 | if (use_file_for_out) |
| 5099 | { |
| 5100 | char_u *fname = options->jo_io_name[PART_OUT]; |
| 5101 | |
Bram Moolenaar | 7bffaa9 | 2016-03-10 21:46:03 +0100 | [diff] [blame] | 5102 | ofd[1] = job_io_file_open(fname, GENERIC_WRITE, |
| 5103 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 5104 | &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL); |
| 5105 | if (ofd[1] == INVALID_HANDLE_VALUE) |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5106 | { |
| 5107 | EMSG2(_(e_notopen), fname); |
| 5108 | goto failed; |
| 5109 | } |
| 5110 | } |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5111 | else if (!use_null_for_out && |
| 5112 | (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0) |
| 5113 | || !pSetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0))) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5114 | goto failed; |
| 5115 | |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5116 | if (use_file_for_err) |
| 5117 | { |
| 5118 | char_u *fname = options->jo_io_name[PART_ERR]; |
| 5119 | |
Bram Moolenaar | 7bffaa9 | 2016-03-10 21:46:03 +0100 | [diff] [blame] | 5120 | efd[1] = job_io_file_open(fname, GENERIC_WRITE, |
| 5121 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 5122 | &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL); |
| 5123 | if (efd[1] == INVALID_HANDLE_VALUE) |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5124 | { |
| 5125 | EMSG2(_(e_notopen), fname); |
| 5126 | goto failed; |
| 5127 | } |
| 5128 | } |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5129 | else if (!use_out_for_err && !use_null_for_err && |
| 5130 | (!CreatePipe(&efd[0], &efd[1], &saAttr, 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5131 | || !pSetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0))) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5132 | goto failed; |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5133 | |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5134 | si.dwFlags |= STARTF_USESTDHANDLES; |
| 5135 | si.hStdInput = ifd[0]; |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5136 | si.hStdOutput = ofd[1]; |
| 5137 | si.hStdError = use_out_for_err ? ofd[1] : efd[1]; |
| 5138 | |
| 5139 | if (!use_null_for_in || !use_null_for_out || !use_null_for_err) |
| 5140 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame^] | 5141 | if (options->jo_set & JO_CHANNEL) |
| 5142 | { |
| 5143 | channel = options->jo_channel; |
| 5144 | if (channel != NULL) |
| 5145 | ++channel->ch_refcount; |
| 5146 | } |
| 5147 | else |
| 5148 | channel = add_channel(); |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5149 | if (channel == NULL) |
| 5150 | goto failed; |
| 5151 | } |
Bram Moolenaar | 5d54a04 | 2016-02-16 16:39:51 +0100 | [diff] [blame] | 5152 | # endif |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5153 | |
| 5154 | if (!vim_create_process(cmd, TRUE, |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5155 | CREATE_SUSPENDED | |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5156 | CREATE_DEFAULT_ERROR_MODE | |
| 5157 | CREATE_NEW_PROCESS_GROUP | |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5158 | CREATE_NEW_CONSOLE, |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5159 | &si, &pi)) |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5160 | { |
| 5161 | CloseHandle(jo); |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5162 | job->jv_status = JOB_FAILED; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5163 | goto failed; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5164 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5165 | |
| 5166 | if (!AssignProcessToJobObject(jo, pi.hProcess)) |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5167 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5168 | /* if failing, switch the way to terminate |
| 5169 | * process with TerminateProcess. */ |
| 5170 | CloseHandle(jo); |
| 5171 | jo = NULL; |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5172 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5173 | ResumeThread(pi.hThread); |
Bram Moolenaar | 75578a3 | 2016-03-10 16:33:31 +0100 | [diff] [blame] | 5174 | CloseHandle(pi.hThread); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5175 | job->jv_proc_info = pi; |
| 5176 | job->jv_job_object = jo; |
| 5177 | job->jv_status = JOB_STARTED; |
| 5178 | |
Bram Moolenaar | 5d54a04 | 2016-02-16 16:39:51 +0100 | [diff] [blame] | 5179 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | 94d0191 | 2016-03-08 13:48:51 +0100 | [diff] [blame] | 5180 | if (!use_file_for_in) |
| 5181 | CloseHandle(ifd[0]); |
Bram Moolenaar | 13d6fb1 | 2016-03-08 18:40:52 +0100 | [diff] [blame] | 5182 | if (!use_file_for_out) |
| 5183 | CloseHandle(ofd[1]); |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5184 | if (!use_out_for_err && !use_file_for_err) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5185 | CloseHandle(efd[1]); |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5186 | |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5187 | job->jv_channel = channel; |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5188 | if (channel != NULL) |
| 5189 | { |
| 5190 | channel_set_pipes(channel, |
| 5191 | use_file_for_in || use_null_for_in |
| 5192 | ? INVALID_FD : (sock_T)ifd[1], |
| 5193 | use_file_for_out || use_null_for_out |
| 5194 | ? INVALID_FD : (sock_T)ofd[0], |
| 5195 | use_out_for_err || use_file_for_err || use_null_for_err |
| 5196 | ? INVALID_FD : (sock_T)efd[0]); |
| 5197 | channel_set_job(channel, job, options); |
Bram Moolenaar | d5d3d30 | 2016-03-09 20:54:51 +0100 | [diff] [blame] | 5198 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5199 | # endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 5200 | return; |
| 5201 | |
| 5202 | failed: |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5203 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 5204 | CloseHandle(ifd[0]); |
| 5205 | CloseHandle(ofd[0]); |
| 5206 | CloseHandle(efd[0]); |
| 5207 | CloseHandle(ifd[1]); |
| 5208 | CloseHandle(ofd[1]); |
| 5209 | CloseHandle(efd[1]); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame^] | 5210 | channel_unref(channel); |
Bram Moolenaar | 71b0f7b | 2016-02-15 12:44:20 +0100 | [diff] [blame] | 5211 | # else |
| 5212 | ; /* make compiler happy */ |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5213 | # endif |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | char * |
| 5217 | mch_job_status(job_T *job) |
| 5218 | { |
| 5219 | DWORD dwExitCode = 0; |
| 5220 | |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5221 | if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode) |
| 5222 | || dwExitCode != STILL_ACTIVE) |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5223 | { |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5224 | job->jv_status = JOB_ENDED; |
Bram Moolenaar | eab089d | 2016-02-21 19:32:02 +0100 | [diff] [blame] | 5225 | job->jv_exitval = (int)dwExitCode; |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5226 | return "dead"; |
| 5227 | } |
| 5228 | return "run"; |
| 5229 | } |
| 5230 | |
| 5231 | int |
| 5232 | mch_stop_job(job_T *job, char_u *how) |
| 5233 | { |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5234 | int ret; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5235 | |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5236 | if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL) |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5237 | { |
| 5238 | if (job->jv_job_object != NULL) |
| 5239 | return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL; |
| 5240 | else |
| 5241 | return TerminateProcess(job->jv_proc_info.hProcess, 0) ? OK : FAIL; |
| 5242 | } |
| 5243 | |
| 5244 | if (!AttachConsole(job->jv_proc_info.dwProcessId)) |
| 5245 | return FAIL; |
| 5246 | ret = GenerateConsoleCtrlEvent( |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5247 | STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT, |
| 5248 | job->jv_proc_info.dwProcessId) |
| 5249 | ? OK : FAIL; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5250 | FreeConsole(); |
| 5251 | return ret; |
| 5252 | } |
| 5253 | |
| 5254 | /* |
| 5255 | * Clear the data related to "job". |
| 5256 | */ |
| 5257 | void |
| 5258 | mch_clear_job(job_T *job) |
| 5259 | { |
| 5260 | if (job->jv_status != JOB_FAILED) |
| 5261 | { |
| 5262 | if (job->jv_job_object != NULL) |
| 5263 | CloseHandle(job->jv_job_object); |
| 5264 | CloseHandle(job->jv_proc_info.hProcess); |
| 5265 | } |
Bram Moolenaar | 942d6b2 | 2016-02-07 19:57:16 +0100 | [diff] [blame] | 5266 | } |
| 5267 | #endif |
| 5268 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5269 | |
| 5270 | #ifndef FEAT_GUI_W32 |
| 5271 | |
| 5272 | /* |
| 5273 | * Start termcap mode |
| 5274 | */ |
| 5275 | static void |
| 5276 | termcap_mode_start(void) |
| 5277 | { |
| 5278 | DWORD cmodein; |
| 5279 | |
| 5280 | if (g_fTermcapMode) |
| 5281 | return; |
| 5282 | |
| 5283 | SaveConsoleBuffer(&g_cbNonTermcap); |
| 5284 | |
| 5285 | if (g_cbTermcap.IsValid) |
| 5286 | { |
| 5287 | /* |
| 5288 | * We've been in termcap mode before. Restore certain screen |
| 5289 | * characteristics, including the buffer size and the window |
| 5290 | * size. Since we will be redrawing the screen, we don't need |
| 5291 | * to restore the actual contents of the buffer. |
| 5292 | */ |
| 5293 | RestoreConsoleBuffer(&g_cbTermcap, FALSE); |
| 5294 | SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow); |
| 5295 | Rows = g_cbTermcap.Info.dwSize.Y; |
| 5296 | Columns = g_cbTermcap.Info.dwSize.X; |
| 5297 | } |
| 5298 | else |
| 5299 | { |
| 5300 | /* |
| 5301 | * This is our first time entering termcap mode. Clear the console |
| 5302 | * screen buffer, and resize the buffer to match the current window |
| 5303 | * size. We will use this as the size of our editing environment. |
| 5304 | */ |
| 5305 | ClearConsoleBuffer(g_attrCurrent); |
| 5306 | ResizeConBufAndWindow(g_hConOut, Columns, Rows); |
| 5307 | } |
| 5308 | |
| 5309 | #ifdef FEAT_TITLE |
| 5310 | resettitle(); |
| 5311 | #endif |
| 5312 | |
| 5313 | GetConsoleMode(g_hConIn, &cmodein); |
| 5314 | #ifdef FEAT_MOUSE |
| 5315 | if (g_fMouseActive) |
| 5316 | cmodein |= ENABLE_MOUSE_INPUT; |
| 5317 | else |
| 5318 | cmodein &= ~ENABLE_MOUSE_INPUT; |
| 5319 | #endif |
| 5320 | cmodein |= ENABLE_WINDOW_INPUT; |
| 5321 | SetConsoleMode(g_hConIn, cmodein); |
| 5322 | |
| 5323 | redraw_later_clear(); |
| 5324 | g_fTermcapMode = TRUE; |
| 5325 | } |
| 5326 | |
| 5327 | |
| 5328 | /* |
| 5329 | * End termcap mode |
| 5330 | */ |
| 5331 | static void |
| 5332 | termcap_mode_end(void) |
| 5333 | { |
| 5334 | DWORD cmodein; |
| 5335 | ConsoleBuffer *cb; |
| 5336 | COORD coord; |
| 5337 | DWORD dwDummy; |
| 5338 | |
| 5339 | if (!g_fTermcapMode) |
| 5340 | return; |
| 5341 | |
| 5342 | SaveConsoleBuffer(&g_cbTermcap); |
| 5343 | |
| 5344 | GetConsoleMode(g_hConIn, &cmodein); |
| 5345 | cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); |
| 5346 | SetConsoleMode(g_hConIn, cmodein); |
| 5347 | |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 5348 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 5349 | cb = exiting ? &g_cbOrig : &g_cbNonTermcap; |
| 5350 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | cb = &g_cbNonTermcap; |
Bram Moolenaar | 4c0aac5 | 2015-10-30 16:46:55 +0100 | [diff] [blame] | 5352 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5353 | RestoreConsoleBuffer(cb, p_rs); |
| 5354 | SetConsoleCursorInfo(g_hConOut, &g_cci); |
| 5355 | |
| 5356 | if (p_rs || exiting) |
| 5357 | { |
| 5358 | /* |
| 5359 | * Clear anything that happens to be on the current line. |
| 5360 | */ |
| 5361 | coord.X = 0; |
| 5362 | coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1)); |
| 5363 | FillConsoleOutputCharacter(g_hConOut, ' ', |
| 5364 | cb->Info.dwSize.X, coord, &dwDummy); |
| 5365 | /* |
| 5366 | * The following is just for aesthetics. If we are exiting without |
| 5367 | * restoring the screen, then we want to have a prompt string |
| 5368 | * appear at the bottom line. However, the command interpreter |
| 5369 | * seems to always advance the cursor one line before displaying |
| 5370 | * the prompt string, which causes the screen to scroll. To |
| 5371 | * counter this, move the cursor up one line before exiting. |
| 5372 | */ |
| 5373 | if (exiting && !p_rs) |
| 5374 | coord.Y--; |
| 5375 | /* |
| 5376 | * Position the cursor at the leftmost column of the desired row. |
| 5377 | */ |
| 5378 | SetConsoleCursorPosition(g_hConOut, coord); |
| 5379 | } |
| 5380 | |
| 5381 | g_fTermcapMode = FALSE; |
| 5382 | } |
| 5383 | #endif /* FEAT_GUI_W32 */ |
| 5384 | |
| 5385 | |
| 5386 | #ifdef FEAT_GUI_W32 |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5387 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5388 | void |
| 5389 | mch_write( |
| 5390 | char_u *s, |
| 5391 | int len) |
| 5392 | { |
| 5393 | /* never used */ |
| 5394 | } |
| 5395 | |
| 5396 | #else |
| 5397 | |
| 5398 | /* |
| 5399 | * clear `n' chars, starting from `coord' |
| 5400 | */ |
| 5401 | static void |
| 5402 | clear_chars( |
| 5403 | COORD coord, |
| 5404 | DWORD n) |
| 5405 | { |
| 5406 | DWORD dwDummy; |
| 5407 | |
| 5408 | FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy); |
| 5409 | FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy); |
| 5410 | } |
| 5411 | |
| 5412 | |
| 5413 | /* |
| 5414 | * Clear the screen |
| 5415 | */ |
| 5416 | static void |
| 5417 | clear_screen(void) |
| 5418 | { |
| 5419 | g_coord.X = g_coord.Y = 0; |
| 5420 | clear_chars(g_coord, Rows * Columns); |
| 5421 | } |
| 5422 | |
| 5423 | |
| 5424 | /* |
| 5425 | * Clear to end of display |
| 5426 | */ |
| 5427 | static void |
| 5428 | clear_to_end_of_display(void) |
| 5429 | { |
| 5430 | clear_chars(g_coord, (Rows - g_coord.Y - 1) |
| 5431 | * Columns + (Columns - g_coord.X)); |
| 5432 | } |
| 5433 | |
| 5434 | |
| 5435 | /* |
| 5436 | * Clear to end of line |
| 5437 | */ |
| 5438 | static void |
| 5439 | clear_to_end_of_line(void) |
| 5440 | { |
| 5441 | clear_chars(g_coord, Columns - g_coord.X); |
| 5442 | } |
| 5443 | |
| 5444 | |
| 5445 | /* |
| 5446 | * Scroll the scroll region up by `cLines' lines |
| 5447 | */ |
| 5448 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5449 | scroll(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5450 | { |
| 5451 | COORD oldcoord = g_coord; |
| 5452 | |
| 5453 | gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1); |
| 5454 | delete_lines(cLines); |
| 5455 | |
| 5456 | g_coord = oldcoord; |
| 5457 | } |
| 5458 | |
| 5459 | |
| 5460 | /* |
| 5461 | * Set the scroll region |
| 5462 | */ |
| 5463 | static void |
| 5464 | set_scroll_region( |
| 5465 | unsigned left, |
| 5466 | unsigned top, |
| 5467 | unsigned right, |
| 5468 | unsigned bottom) |
| 5469 | { |
| 5470 | if (left >= right |
| 5471 | || top >= bottom |
| 5472 | || right > (unsigned) Columns - 1 |
| 5473 | || bottom > (unsigned) Rows - 1) |
| 5474 | return; |
| 5475 | |
| 5476 | g_srScrollRegion.Left = left; |
| 5477 | g_srScrollRegion.Top = top; |
| 5478 | g_srScrollRegion.Right = right; |
| 5479 | g_srScrollRegion.Bottom = bottom; |
| 5480 | } |
| 5481 | |
| 5482 | |
| 5483 | /* |
| 5484 | * Insert `cLines' lines at the current cursor position |
| 5485 | */ |
| 5486 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5487 | insert_lines(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | { |
| 5489 | SMALL_RECT source; |
| 5490 | COORD dest; |
| 5491 | CHAR_INFO fill; |
| 5492 | |
| 5493 | dest.X = 0; |
| 5494 | dest.Y = g_coord.Y + cLines; |
| 5495 | |
| 5496 | source.Left = 0; |
| 5497 | source.Top = g_coord.Y; |
| 5498 | source.Right = g_srScrollRegion.Right; |
| 5499 | source.Bottom = g_srScrollRegion.Bottom - cLines; |
| 5500 | |
| 5501 | fill.Char.AsciiChar = ' '; |
| 5502 | fill.Attributes = g_attrCurrent; |
| 5503 | |
| 5504 | ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); |
| 5505 | |
| 5506 | /* Here we have to deal with a win32 console flake: If the scroll |
| 5507 | * region looks like abc and we scroll c to a and fill with d we get |
| 5508 | * cbd... if we scroll block c one line at a time to a, we get cdd... |
| 5509 | * vim expects cdd consistently... So we have to deal with that |
| 5510 | * here... (this also occurs scrolling the same way in the other |
| 5511 | * direction). */ |
| 5512 | |
| 5513 | if (source.Bottom < dest.Y) |
| 5514 | { |
| 5515 | COORD coord; |
| 5516 | |
| 5517 | coord.X = 0; |
| 5518 | coord.Y = source.Bottom; |
| 5519 | clear_chars(coord, Columns * (dest.Y - source.Bottom)); |
| 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | |
| 5524 | /* |
| 5525 | * Delete `cLines' lines at the current cursor position |
| 5526 | */ |
| 5527 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5528 | delete_lines(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5529 | { |
| 5530 | SMALL_RECT source; |
| 5531 | COORD dest; |
| 5532 | CHAR_INFO fill; |
| 5533 | int nb; |
| 5534 | |
| 5535 | dest.X = 0; |
| 5536 | dest.Y = g_coord.Y; |
| 5537 | |
| 5538 | source.Left = 0; |
| 5539 | source.Top = g_coord.Y + cLines; |
| 5540 | source.Right = g_srScrollRegion.Right; |
| 5541 | source.Bottom = g_srScrollRegion.Bottom; |
| 5542 | |
| 5543 | fill.Char.AsciiChar = ' '; |
| 5544 | fill.Attributes = g_attrCurrent; |
| 5545 | |
| 5546 | ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); |
| 5547 | |
| 5548 | /* Here we have to deal with a win32 console flake: If the scroll |
| 5549 | * region looks like abc and we scroll c to a and fill with d we get |
| 5550 | * cbd... if we scroll block c one line at a time to a, we get cdd... |
| 5551 | * vim expects cdd consistently... So we have to deal with that |
| 5552 | * here... (this also occurs scrolling the same way in the other |
| 5553 | * direction). */ |
| 5554 | |
| 5555 | nb = dest.Y + (source.Bottom - source.Top) + 1; |
| 5556 | |
| 5557 | if (nb < source.Top) |
| 5558 | { |
| 5559 | COORD coord; |
| 5560 | |
| 5561 | coord.X = 0; |
| 5562 | coord.Y = nb; |
| 5563 | clear_chars(coord, Columns * (source.Top - nb)); |
| 5564 | } |
| 5565 | } |
| 5566 | |
| 5567 | |
| 5568 | /* |
| 5569 | * Set the cursor position |
| 5570 | */ |
| 5571 | static void |
| 5572 | gotoxy( |
| 5573 | unsigned x, |
| 5574 | unsigned y) |
| 5575 | { |
| 5576 | if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows) |
| 5577 | return; |
| 5578 | |
| 5579 | /* external cursor coords are 1-based; internal are 0-based */ |
| 5580 | g_coord.X = x - 1; |
| 5581 | g_coord.Y = y - 1; |
| 5582 | SetConsoleCursorPosition(g_hConOut, g_coord); |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | /* |
| 5587 | * Set the current text attribute = (foreground | background) |
| 5588 | * See ../doc/os_win32.txt for the numbers. |
| 5589 | */ |
| 5590 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5591 | textattr(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5592 | { |
Bram Moolenaar | 6383b92 | 2015-03-24 17:12:19 +0100 | [diff] [blame] | 5593 | g_attrCurrent = wAttr & 0xff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5594 | |
| 5595 | SetConsoleTextAttribute(g_hConOut, wAttr); |
| 5596 | } |
| 5597 | |
| 5598 | |
| 5599 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5600 | textcolor(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5601 | { |
Bram Moolenaar | 6383b92 | 2015-03-24 17:12:19 +0100 | [diff] [blame] | 5602 | g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5603 | |
| 5604 | SetConsoleTextAttribute(g_hConOut, g_attrCurrent); |
| 5605 | } |
| 5606 | |
| 5607 | |
| 5608 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5609 | textbackground(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5610 | { |
Bram Moolenaar | 6383b92 | 2015-03-24 17:12:19 +0100 | [diff] [blame] | 5611 | g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5612 | |
| 5613 | SetConsoleTextAttribute(g_hConOut, g_attrCurrent); |
| 5614 | } |
| 5615 | |
| 5616 | |
| 5617 | /* |
| 5618 | * restore the default text attribute (whatever we started with) |
| 5619 | */ |
| 5620 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5621 | normvideo(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5622 | { |
| 5623 | textattr(g_attrDefault); |
| 5624 | } |
| 5625 | |
| 5626 | |
| 5627 | static WORD g_attrPreStandout = 0; |
| 5628 | |
| 5629 | /* |
| 5630 | * Make the text standout, by brightening it |
| 5631 | */ |
| 5632 | static void |
| 5633 | standout(void) |
| 5634 | { |
| 5635 | g_attrPreStandout = g_attrCurrent; |
| 5636 | textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY)); |
| 5637 | } |
| 5638 | |
| 5639 | |
| 5640 | /* |
| 5641 | * Turn off standout mode |
| 5642 | */ |
| 5643 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5644 | standend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5645 | { |
| 5646 | if (g_attrPreStandout) |
| 5647 | { |
| 5648 | textattr(g_attrPreStandout); |
| 5649 | g_attrPreStandout = 0; |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | |
| 5654 | /* |
Bram Moolenaar | ff1d0d4 | 2007-05-10 17:24:16 +0000 | [diff] [blame] | 5655 | * Set normal fg/bg color, based on T_ME. Called when t_me has been set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | */ |
| 5657 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5658 | mch_set_normal_colors(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5659 | { |
| 5660 | char_u *p; |
| 5661 | int n; |
| 5662 | |
| 5663 | cterm_normal_fg_color = (g_attrDefault & 0xf) + 1; |
| 5664 | cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1; |
| 5665 | if (T_ME[0] == ESC && T_ME[1] == '|') |
| 5666 | { |
| 5667 | p = T_ME + 2; |
| 5668 | n = getdigits(&p); |
| 5669 | if (*p == 'm' && n > 0) |
| 5670 | { |
| 5671 | cterm_normal_fg_color = (n & 0xf) + 1; |
| 5672 | cterm_normal_bg_color = ((n >> 4) & 0xf) + 1; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | |
| 5678 | /* |
| 5679 | * visual bell: flash the screen |
| 5680 | */ |
| 5681 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5682 | visual_bell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5683 | { |
| 5684 | COORD coordOrigin = {0, 0}; |
| 5685 | WORD attrFlash = ~g_attrCurrent & 0xff; |
| 5686 | |
| 5687 | DWORD dwDummy; |
| 5688 | LPWORD oldattrs = (LPWORD)alloc(Rows * Columns * sizeof(WORD)); |
| 5689 | |
| 5690 | if (oldattrs == NULL) |
| 5691 | return; |
| 5692 | ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, |
| 5693 | coordOrigin, &dwDummy); |
| 5694 | FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns, |
| 5695 | coordOrigin, &dwDummy); |
| 5696 | |
| 5697 | Sleep(15); /* wait for 15 msec */ |
| 5698 | WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, |
| 5699 | coordOrigin, &dwDummy); |
| 5700 | vim_free(oldattrs); |
| 5701 | } |
| 5702 | |
| 5703 | |
| 5704 | /* |
| 5705 | * Make the cursor visible or invisible |
| 5706 | */ |
| 5707 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5708 | cursor_visible(BOOL fVisible) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5709 | { |
| 5710 | s_cursor_visible = fVisible; |
| 5711 | #ifdef MCH_CURSOR_SHAPE |
| 5712 | mch_update_cursor(); |
| 5713 | #endif |
| 5714 | } |
| 5715 | |
| 5716 | |
| 5717 | /* |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 5718 | * write `cbToWrite' bytes in `pchBuf' to the screen |
| 5719 | * Returns the number of bytes actually written (at least one). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5720 | */ |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 5721 | static DWORD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5722 | write_chars( |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 5723 | char_u *pchBuf, |
| 5724 | DWORD cbToWrite) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5725 | { |
| 5726 | COORD coord = g_coord; |
| 5727 | DWORD written; |
| 5728 | |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 5729 | #ifdef FEAT_MBYTE |
| 5730 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 5731 | { |
| 5732 | static WCHAR *unicodebuf = NULL; |
| 5733 | static int unibuflen = 0; |
| 5734 | int length; |
| 5735 | DWORD n, cchwritten, cells; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5736 | |
Bram Moolenaar | ac360bf | 2015-09-01 20:31:20 +0200 | [diff] [blame] | 5737 | length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0); |
| 5738 | if (unicodebuf == NULL || length > unibuflen) |
| 5739 | { |
| 5740 | vim_free(unicodebuf); |
| 5741 | unicodebuf = (WCHAR *)lalloc(length * sizeof(WCHAR), FALSE); |
| 5742 | unibuflen = length; |
| 5743 | } |
| 5744 | MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pchBuf, cbToWrite, |
| 5745 | unicodebuf, unibuflen); |
| 5746 | |
| 5747 | cells = mb_string2cells(pchBuf, cbToWrite); |
| 5748 | FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells, |
| 5749 | coord, &written); |
| 5750 | /* When writing fails or didn't write a single character, pretend one |
| 5751 | * character was written, otherwise we get stuck. */ |
| 5752 | if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length, |
| 5753 | coord, &cchwritten) == 0 |
| 5754 | || cchwritten == 0) |
| 5755 | cchwritten = 1; |
| 5756 | |
| 5757 | if (cchwritten == length) |
| 5758 | { |
| 5759 | written = cbToWrite; |
| 5760 | g_coord.X += (SHORT)cells; |
| 5761 | } |
| 5762 | else |
| 5763 | { |
| 5764 | char_u *p = pchBuf; |
| 5765 | for (n = 0; n < cchwritten; n++) |
| 5766 | mb_cptr_adv(p); |
| 5767 | written = p - pchBuf; |
| 5768 | g_coord.X += (SHORT)mb_string2cells(pchBuf, written); |
| 5769 | } |
| 5770 | } |
| 5771 | else |
| 5772 | #endif |
| 5773 | { |
| 5774 | FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cbToWrite, |
| 5775 | coord, &written); |
| 5776 | /* When writing fails or didn't write a single character, pretend one |
| 5777 | * character was written, otherwise we get stuck. */ |
| 5778 | if (WriteConsoleOutputCharacter(g_hConOut, (LPCSTR)pchBuf, cbToWrite, |
| 5779 | coord, &written) == 0 |
| 5780 | || written == 0) |
| 5781 | written = 1; |
| 5782 | |
| 5783 | g_coord.X += (SHORT) written; |
| 5784 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5785 | |
| 5786 | while (g_coord.X > g_srScrollRegion.Right) |
| 5787 | { |
| 5788 | g_coord.X -= (SHORT) Columns; |
| 5789 | if (g_coord.Y < g_srScrollRegion.Bottom) |
| 5790 | g_coord.Y++; |
| 5791 | } |
| 5792 | |
| 5793 | gotoxy(g_coord.X + 1, g_coord.Y + 1); |
| 5794 | |
| 5795 | return written; |
| 5796 | } |
| 5797 | |
| 5798 | |
| 5799 | /* |
| 5800 | * mch_write(): write the output buffer to the screen, translating ESC |
| 5801 | * sequences into calls to console output routines. |
| 5802 | */ |
| 5803 | void |
| 5804 | mch_write( |
| 5805 | char_u *s, |
| 5806 | int len) |
| 5807 | { |
| 5808 | s[len] = NUL; |
| 5809 | |
| 5810 | if (!term_console) |
| 5811 | { |
| 5812 | write(1, s, (unsigned)len); |
| 5813 | return; |
| 5814 | } |
| 5815 | |
| 5816 | /* translate ESC | sequences into faked bios calls */ |
| 5817 | while (len--) |
| 5818 | { |
| 5819 | /* optimization: use one single write_chars for runs of text, |
| 5820 | * rather than once per character It ain't curses, but it helps. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 5821 | DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5822 | |
| 5823 | if (p_wd) |
| 5824 | { |
| 5825 | WaitForChar(p_wd); |
| 5826 | if (prefix != 0) |
| 5827 | prefix = 1; |
| 5828 | } |
| 5829 | |
| 5830 | if (prefix != 0) |
| 5831 | { |
| 5832 | DWORD nWritten; |
| 5833 | |
| 5834 | nWritten = write_chars(s, prefix); |
| 5835 | #ifdef MCH_WRITE_DUMP |
| 5836 | if (fdDump) |
| 5837 | { |
| 5838 | fputc('>', fdDump); |
| 5839 | fwrite(s, sizeof(char_u), nWritten, fdDump); |
| 5840 | fputs("<\n", fdDump); |
| 5841 | } |
| 5842 | #endif |
| 5843 | len -= (nWritten - 1); |
| 5844 | s += nWritten; |
| 5845 | } |
| 5846 | else if (s[0] == '\n') |
| 5847 | { |
| 5848 | /* \n, newline: go to the beginning of the next line or scroll */ |
| 5849 | if (g_coord.Y == g_srScrollRegion.Bottom) |
| 5850 | { |
| 5851 | scroll(1); |
| 5852 | gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1); |
| 5853 | } |
| 5854 | else |
| 5855 | { |
| 5856 | gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2); |
| 5857 | } |
| 5858 | #ifdef MCH_WRITE_DUMP |
| 5859 | if (fdDump) |
| 5860 | fputs("\\n\n", fdDump); |
| 5861 | #endif |
| 5862 | s++; |
| 5863 | } |
| 5864 | else if (s[0] == '\r') |
| 5865 | { |
| 5866 | /* \r, carriage return: go to beginning of line */ |
| 5867 | gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1); |
| 5868 | #ifdef MCH_WRITE_DUMP |
| 5869 | if (fdDump) |
| 5870 | fputs("\\r\n", fdDump); |
| 5871 | #endif |
| 5872 | s++; |
| 5873 | } |
| 5874 | else if (s[0] == '\b') |
| 5875 | { |
| 5876 | /* \b, backspace: move cursor one position left */ |
| 5877 | if (g_coord.X > g_srScrollRegion.Left) |
| 5878 | g_coord.X--; |
| 5879 | else if (g_coord.Y > g_srScrollRegion.Top) |
| 5880 | { |
| 5881 | g_coord.X = g_srScrollRegion.Right; |
| 5882 | g_coord.Y--; |
| 5883 | } |
| 5884 | gotoxy(g_coord.X + 1, g_coord.Y + 1); |
| 5885 | #ifdef MCH_WRITE_DUMP |
| 5886 | if (fdDump) |
| 5887 | fputs("\\b\n", fdDump); |
| 5888 | #endif |
| 5889 | s++; |
| 5890 | } |
| 5891 | else if (s[0] == '\a') |
| 5892 | { |
| 5893 | /* \a, bell */ |
| 5894 | MessageBeep(0xFFFFFFFF); |
| 5895 | #ifdef MCH_WRITE_DUMP |
| 5896 | if (fdDump) |
| 5897 | fputs("\\a\n", fdDump); |
| 5898 | #endif |
| 5899 | s++; |
| 5900 | } |
| 5901 | else if (s[0] == ESC && len >= 3-1 && s[1] == '|') |
| 5902 | { |
| 5903 | #ifdef MCH_WRITE_DUMP |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 5904 | char_u *old_s = s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5905 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 5906 | char_u *p; |
| 5907 | int arg1 = 0, arg2 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5908 | |
| 5909 | switch (s[2]) |
| 5910 | { |
| 5911 | /* one or two numeric arguments, separated by ';' */ |
| 5912 | |
| 5913 | case '0': case '1': case '2': case '3': case '4': |
| 5914 | case '5': case '6': case '7': case '8': case '9': |
| 5915 | p = s + 2; |
| 5916 | arg1 = getdigits(&p); /* no check for length! */ |
| 5917 | if (p > s + len) |
| 5918 | break; |
| 5919 | |
| 5920 | if (*p == ';') |
| 5921 | { |
| 5922 | ++p; |
| 5923 | arg2 = getdigits(&p); /* no check for length! */ |
| 5924 | if (p > s + len) |
| 5925 | break; |
| 5926 | |
| 5927 | if (*p == 'H') |
| 5928 | gotoxy(arg2, arg1); |
| 5929 | else if (*p == 'r') |
| 5930 | set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1); |
| 5931 | } |
| 5932 | else if (*p == 'A') |
| 5933 | { |
| 5934 | /* move cursor up arg1 lines in same column */ |
| 5935 | gotoxy(g_coord.X + 1, |
| 5936 | max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1); |
| 5937 | } |
| 5938 | else if (*p == 'C') |
| 5939 | { |
| 5940 | /* move cursor right arg1 columns in same line */ |
| 5941 | gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1, |
| 5942 | g_coord.Y + 1); |
| 5943 | } |
| 5944 | else if (*p == 'H') |
| 5945 | { |
| 5946 | gotoxy(1, arg1); |
| 5947 | } |
| 5948 | else if (*p == 'L') |
| 5949 | { |
| 5950 | insert_lines(arg1); |
| 5951 | } |
| 5952 | else if (*p == 'm') |
| 5953 | { |
| 5954 | if (arg1 == 0) |
| 5955 | normvideo(); |
| 5956 | else |
| 5957 | textattr((WORD) arg1); |
| 5958 | } |
| 5959 | else if (*p == 'f') |
| 5960 | { |
| 5961 | textcolor((WORD) arg1); |
| 5962 | } |
| 5963 | else if (*p == 'b') |
| 5964 | { |
| 5965 | textbackground((WORD) arg1); |
| 5966 | } |
| 5967 | else if (*p == 'M') |
| 5968 | { |
| 5969 | delete_lines(arg1); |
| 5970 | } |
| 5971 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5972 | len -= (int)(p - s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5973 | s = p + 1; |
| 5974 | break; |
| 5975 | |
| 5976 | |
| 5977 | /* Three-character escape sequences */ |
| 5978 | |
| 5979 | case 'A': |
| 5980 | /* move cursor up one line in same column */ |
| 5981 | gotoxy(g_coord.X + 1, |
| 5982 | max(g_srScrollRegion.Top, g_coord.Y - 1) + 1); |
| 5983 | goto got3; |
| 5984 | |
| 5985 | case 'B': |
| 5986 | visual_bell(); |
| 5987 | goto got3; |
| 5988 | |
| 5989 | case 'C': |
| 5990 | /* move cursor right one column in same line */ |
| 5991 | gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1, |
| 5992 | g_coord.Y + 1); |
| 5993 | goto got3; |
| 5994 | |
| 5995 | case 'E': |
| 5996 | termcap_mode_end(); |
| 5997 | goto got3; |
| 5998 | |
| 5999 | case 'F': |
| 6000 | standout(); |
| 6001 | goto got3; |
| 6002 | |
| 6003 | case 'f': |
| 6004 | standend(); |
| 6005 | goto got3; |
| 6006 | |
| 6007 | case 'H': |
| 6008 | gotoxy(1, 1); |
| 6009 | goto got3; |
| 6010 | |
| 6011 | case 'j': |
| 6012 | clear_to_end_of_display(); |
| 6013 | goto got3; |
| 6014 | |
| 6015 | case 'J': |
| 6016 | clear_screen(); |
| 6017 | goto got3; |
| 6018 | |
| 6019 | case 'K': |
| 6020 | clear_to_end_of_line(); |
| 6021 | goto got3; |
| 6022 | |
| 6023 | case 'L': |
| 6024 | insert_lines(1); |
| 6025 | goto got3; |
| 6026 | |
| 6027 | case 'M': |
| 6028 | delete_lines(1); |
| 6029 | goto got3; |
| 6030 | |
| 6031 | case 'S': |
| 6032 | termcap_mode_start(); |
| 6033 | goto got3; |
| 6034 | |
| 6035 | case 'V': |
| 6036 | cursor_visible(TRUE); |
| 6037 | goto got3; |
| 6038 | |
| 6039 | case 'v': |
| 6040 | cursor_visible(FALSE); |
| 6041 | goto got3; |
| 6042 | |
| 6043 | got3: |
| 6044 | s += 3; |
| 6045 | len -= 2; |
| 6046 | } |
| 6047 | |
| 6048 | #ifdef MCH_WRITE_DUMP |
| 6049 | if (fdDump) |
| 6050 | { |
| 6051 | fputs("ESC | ", fdDump); |
| 6052 | fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump); |
| 6053 | fputc('\n', fdDump); |
| 6054 | } |
| 6055 | #endif |
| 6056 | } |
| 6057 | else |
| 6058 | { |
| 6059 | /* Write a single character */ |
| 6060 | DWORD nWritten; |
| 6061 | |
| 6062 | nWritten = write_chars(s, 1); |
| 6063 | #ifdef MCH_WRITE_DUMP |
| 6064 | if (fdDump) |
| 6065 | { |
| 6066 | fputc('>', fdDump); |
| 6067 | fwrite(s, sizeof(char_u), nWritten, fdDump); |
| 6068 | fputs("<\n", fdDump); |
| 6069 | } |
| 6070 | #endif |
| 6071 | |
| 6072 | len -= (nWritten - 1); |
| 6073 | s += nWritten; |
| 6074 | } |
| 6075 | } |
| 6076 | |
| 6077 | #ifdef MCH_WRITE_DUMP |
| 6078 | if (fdDump) |
| 6079 | fflush(fdDump); |
| 6080 | #endif |
| 6081 | } |
| 6082 | |
| 6083 | #endif /* FEAT_GUI_W32 */ |
| 6084 | |
| 6085 | |
| 6086 | /* |
| 6087 | * Delay for half a second. |
| 6088 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6089 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | void |
| 6091 | mch_delay( |
| 6092 | long msec, |
| 6093 | int ignoreinput) |
| 6094 | { |
| 6095 | #ifdef FEAT_GUI_W32 |
| 6096 | Sleep((int)msec); /* never wait for input */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6097 | #else /* Console */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6098 | if (ignoreinput) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6099 | # ifdef FEAT_MZSCHEME |
| 6100 | if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq) |
| 6101 | { |
| 6102 | int towait = p_mzq; |
| 6103 | |
| 6104 | /* if msec is large enough, wait by portions in p_mzq */ |
| 6105 | while (msec > 0) |
| 6106 | { |
| 6107 | mzvim_check_threads(); |
| 6108 | if (msec < towait) |
| 6109 | towait = msec; |
| 6110 | Sleep(towait); |
| 6111 | msec -= towait; |
| 6112 | } |
| 6113 | } |
| 6114 | else |
| 6115 | # endif |
| 6116 | Sleep((int)msec); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6117 | else |
| 6118 | WaitForChar(msec); |
| 6119 | #endif |
| 6120 | } |
| 6121 | |
| 6122 | |
| 6123 | /* |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 6124 | * This version of remove is not scared by a readonly (backup) file. |
| 6125 | * This can also remove a symbolic link like Unix. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6126 | * Return 0 for success, -1 for failure. |
| 6127 | */ |
| 6128 | int |
| 6129 | mch_remove(char_u *name) |
| 6130 | { |
| 6131 | #ifdef FEAT_MBYTE |
| 6132 | WCHAR *wn = NULL; |
| 6133 | int n; |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 6134 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6135 | |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 6136 | /* |
| 6137 | * On Windows, deleting a directory's symbolic link is done by |
| 6138 | * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact. |
| 6139 | */ |
| 6140 | if (mch_isdir(name) && mch_is_symbolic_link(name)) |
| 6141 | return mch_rmdir(name); |
| 6142 | |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 6143 | win32_setattrs(name, FILE_ATTRIBUTE_NORMAL); |
| 6144 | |
| 6145 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6146 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 6147 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 6148 | wn = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6149 | if (wn != NULL) |
| 6150 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6151 | n = DeleteFileW(wn) ? 0 : -1; |
| 6152 | vim_free(wn); |
| 6153 | if (n == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 6154 | return n; |
| 6155 | /* Retry with non-wide function (for Windows 98). */ |
| 6156 | } |
| 6157 | } |
| 6158 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6159 | return DeleteFile((LPCSTR)name) ? 0 : -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | |
| 6163 | /* |
| 6164 | * check for an "interrupt signal": CTRL-break or CTRL-C |
| 6165 | */ |
| 6166 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6167 | mch_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6168 | { |
| 6169 | #ifndef FEAT_GUI_W32 /* never used */ |
| 6170 | if (g_fCtrlCPressed || g_fCBrkPressed) |
| 6171 | { |
| 6172 | g_fCtrlCPressed = g_fCBrkPressed = FALSE; |
| 6173 | got_int = TRUE; |
| 6174 | } |
| 6175 | #endif |
| 6176 | } |
| 6177 | |
Bram Moolenaar | ee27397 | 2016-01-02 21:11:51 +0100 | [diff] [blame] | 6178 | /* physical RAM to leave for the OS */ |
| 6179 | #define WINNT_RESERVE_BYTES (256*1024*1024) |
| 6180 | #define WIN95_RESERVE_BYTES (8*1024*1024) |
| 6181 | |
| 6182 | /* |
| 6183 | * How much main memory in KiB that can be used by VIM. |
| 6184 | */ |
| 6185 | /*ARGSUSED*/ |
| 6186 | long_u |
| 6187 | mch_total_mem(int special) |
| 6188 | { |
| 6189 | PlatformId(); |
| 6190 | #if (defined(_MSC_VER) && (WINVER > 0x0400)) || defined(MEMORYSTATUSEX) |
| 6191 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 6192 | { |
| 6193 | MEMORYSTATUSEX ms; |
| 6194 | |
| 6195 | /* Need to use GlobalMemoryStatusEx() when there is more memory than |
| 6196 | * what fits in 32 bits. But it's not always available. */ |
| 6197 | ms.dwLength = sizeof(MEMORYSTATUSEX); |
| 6198 | GlobalMemoryStatusEx(&ms); |
| 6199 | if (ms.ullAvailVirtual < ms.ullTotalPhys) |
| 6200 | { |
| 6201 | /* Process address space fits in physical RAM, use all of it. */ |
| 6202 | return (long_u)(ms.ullAvailVirtual / 1024); |
| 6203 | } |
| 6204 | if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES) |
| 6205 | { |
| 6206 | /* Catch old NT box or perverse hardware setup. */ |
| 6207 | return (long_u)((ms.ullTotalPhys / 2) / 1024); |
| 6208 | } |
| 6209 | /* Use physical RAM less reserve for OS + data. */ |
| 6210 | return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024); |
| 6211 | } |
| 6212 | else |
| 6213 | #endif |
| 6214 | { |
| 6215 | /* Pre-XP or 95 OS handling. */ |
| 6216 | MEMORYSTATUS ms; |
| 6217 | long_u os_reserve_bytes; |
| 6218 | |
| 6219 | ms.dwLength = sizeof(MEMORYSTATUS); |
| 6220 | GlobalMemoryStatus(&ms); |
| 6221 | if (ms.dwAvailVirtual < ms.dwTotalPhys) |
| 6222 | { |
| 6223 | /* Process address space fits in physical RAM, use all of it. */ |
| 6224 | return (long_u)(ms.dwAvailVirtual / 1024); |
| 6225 | } |
| 6226 | os_reserve_bytes = (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 6227 | ? WINNT_RESERVE_BYTES |
| 6228 | : WIN95_RESERVE_BYTES; |
| 6229 | if (ms.dwTotalPhys <= os_reserve_bytes) |
| 6230 | { |
| 6231 | /* Catch old boxes or perverse hardware setup. */ |
| 6232 | return (long_u)((ms.dwTotalPhys / 2) / 1024); |
| 6233 | } |
| 6234 | /* Use physical RAM less reserve for OS + data. */ |
| 6235 | return (long_u)((ms.dwTotalPhys - os_reserve_bytes) / 1024); |
| 6236 | } |
| 6237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6238 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6239 | #ifdef FEAT_MBYTE |
| 6240 | /* |
| 6241 | * Same code as below, but with wide functions and no comments. |
| 6242 | * Return 0 for success, non-zero for failure. |
| 6243 | */ |
| 6244 | int |
| 6245 | mch_wrename(WCHAR *wold, WCHAR *wnew) |
| 6246 | { |
| 6247 | WCHAR *p; |
| 6248 | int i; |
| 6249 | WCHAR szTempFile[_MAX_PATH + 1]; |
| 6250 | WCHAR szNewPath[_MAX_PATH + 1]; |
| 6251 | HANDLE hf; |
| 6252 | |
| 6253 | if (!mch_windows95()) |
| 6254 | { |
| 6255 | p = wold; |
| 6256 | for (i = 0; wold[i] != NUL; ++i) |
| 6257 | if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':') |
| 6258 | && wold[i + 1] != 0) |
| 6259 | p = wold + i + 1; |
| 6260 | if ((int)(wold + i - p) < 8 || p[6] != '~') |
| 6261 | return (MoveFileW(wold, wnew) == 0); |
| 6262 | } |
| 6263 | |
| 6264 | if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL) |
| 6265 | return -1; |
| 6266 | *p = NUL; |
| 6267 | |
| 6268 | if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0) |
| 6269 | return -2; |
| 6270 | |
| 6271 | if (!DeleteFileW(szTempFile)) |
| 6272 | return -3; |
| 6273 | |
| 6274 | if (!MoveFileW(wold, szTempFile)) |
| 6275 | return -4; |
| 6276 | |
| 6277 | if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 6278 | FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) |
| 6279 | return -5; |
| 6280 | if (!CloseHandle(hf)) |
| 6281 | return -6; |
| 6282 | |
| 6283 | if (!MoveFileW(szTempFile, wnew)) |
| 6284 | { |
| 6285 | (void)MoveFileW(szTempFile, wold); |
| 6286 | return -7; |
| 6287 | } |
| 6288 | |
| 6289 | DeleteFileW(szTempFile); |
| 6290 | |
| 6291 | if (!DeleteFileW(wold)) |
| 6292 | return -8; |
| 6293 | |
| 6294 | return 0; |
| 6295 | } |
| 6296 | #endif |
| 6297 | |
| 6298 | |
| 6299 | /* |
| 6300 | * mch_rename() works around a bug in rename (aka MoveFile) in |
| 6301 | * Windows 95: rename("foo.bar", "foo.bar~") will generate a |
| 6302 | * file whose short file name is "FOO.BAR" (its long file name will |
| 6303 | * be correct: "foo.bar~"). Because a file can be accessed by |
| 6304 | * either its SFN or its LFN, "foo.bar" has effectively been |
| 6305 | * renamed to "foo.bar", which is not at all what was wanted. This |
| 6306 | * seems to happen only when renaming files with three-character |
| 6307 | * extensions by appending a suffix that does not include ".". |
| 6308 | * Windows NT gets it right, however, with an SFN of "FOO~1.BAR". |
| 6309 | * |
| 6310 | * There is another problem, which isn't really a bug but isn't right either: |
| 6311 | * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be |
| 6312 | * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with |
| 6313 | * service pack 6. Doesn't seem to happen on Windows 98. |
| 6314 | * |
| 6315 | * Like rename(), returns 0 upon success, non-zero upon failure. |
| 6316 | * Should probably set errno appropriately when errors occur. |
| 6317 | */ |
| 6318 | int |
| 6319 | mch_rename( |
| 6320 | const char *pszOldFile, |
| 6321 | const char *pszNewFile) |
| 6322 | { |
| 6323 | char szTempFile[_MAX_PATH+1]; |
| 6324 | char szNewPath[_MAX_PATH+1]; |
| 6325 | char *pszFilePart; |
| 6326 | HANDLE hf; |
| 6327 | #ifdef FEAT_MBYTE |
| 6328 | WCHAR *wold = NULL; |
| 6329 | WCHAR *wnew = NULL; |
| 6330 | int retval = -1; |
| 6331 | |
| 6332 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 6333 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 6334 | wold = enc_to_utf16((char_u *)pszOldFile, NULL); |
| 6335 | wnew = enc_to_utf16((char_u *)pszNewFile, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6336 | if (wold != NULL && wnew != NULL) |
| 6337 | retval = mch_wrename(wold, wnew); |
| 6338 | vim_free(wold); |
| 6339 | vim_free(wnew); |
| 6340 | if (retval == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 6341 | return retval; |
| 6342 | /* Retry with non-wide function (for Windows 98). */ |
| 6343 | } |
| 6344 | #endif |
| 6345 | |
| 6346 | /* |
| 6347 | * No need to play tricks if not running Windows 95, unless the file name |
| 6348 | * contains a "~" as the seventh character. |
| 6349 | */ |
| 6350 | if (!mch_windows95()) |
| 6351 | { |
| 6352 | pszFilePart = (char *)gettail((char_u *)pszOldFile); |
| 6353 | if (STRLEN(pszFilePart) < 8 || pszFilePart[6] != '~') |
| 6354 | return rename(pszOldFile, pszNewFile); |
| 6355 | } |
| 6356 | |
| 6357 | /* Get base path of new file name. Undocumented feature: If pszNewFile is |
| 6358 | * a directory, no error is returned and pszFilePart will be NULL. */ |
| 6359 | if (GetFullPathName(pszNewFile, _MAX_PATH, szNewPath, &pszFilePart) == 0 |
| 6360 | || pszFilePart == NULL) |
| 6361 | return -1; |
| 6362 | *pszFilePart = NUL; |
| 6363 | |
| 6364 | /* Get (and create) a unique temporary file name in directory of new file */ |
| 6365 | if (GetTempFileName(szNewPath, "VIM", 0, szTempFile) == 0) |
| 6366 | return -2; |
| 6367 | |
| 6368 | /* blow the temp file away */ |
| 6369 | if (!DeleteFile(szTempFile)) |
| 6370 | return -3; |
| 6371 | |
| 6372 | /* rename old file to the temp file */ |
| 6373 | if (!MoveFile(pszOldFile, szTempFile)) |
| 6374 | return -4; |
| 6375 | |
| 6376 | /* now create an empty file called pszOldFile; this prevents the operating |
| 6377 | * system using pszOldFile as an alias (SFN) if we're renaming within the |
| 6378 | * same directory. For example, we're editing a file called |
| 6379 | * filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt |
| 6380 | * to filena~1.txt~ (i.e., we're making a backup while writing it), the |
| 6381 | * SFN for filena~1.txt~ will be filena~1.txt, by default, which will |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6382 | * cause all sorts of problems later in buf_write(). So, we create an |
| 6383 | * empty file called filena~1.txt and the system will have to find some |
| 6384 | * other SFN for filena~1.txt~, such as filena~2.txt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6385 | */ |
| 6386 | if ((hf = CreateFile(pszOldFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 6387 | FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) |
| 6388 | return -5; |
| 6389 | if (!CloseHandle(hf)) |
| 6390 | return -6; |
| 6391 | |
| 6392 | /* rename the temp file to the new file */ |
| 6393 | if (!MoveFile(szTempFile, pszNewFile)) |
| 6394 | { |
| 6395 | /* Renaming failed. Rename the file back to its old name, so that it |
| 6396 | * looks like nothing happened. */ |
| 6397 | (void)MoveFile(szTempFile, pszOldFile); |
| 6398 | |
| 6399 | return -7; |
| 6400 | } |
| 6401 | |
| 6402 | /* Seems to be left around on Novell filesystems */ |
| 6403 | DeleteFile(szTempFile); |
| 6404 | |
| 6405 | /* finally, remove the empty old file */ |
| 6406 | if (!DeleteFile(pszOldFile)) |
| 6407 | return -8; |
| 6408 | |
| 6409 | return 0; /* success */ |
| 6410 | } |
| 6411 | |
| 6412 | /* |
| 6413 | * Get the default shell for the current hardware platform |
| 6414 | */ |
| 6415 | char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6416 | default_shell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6417 | { |
| 6418 | char* psz = NULL; |
| 6419 | |
| 6420 | PlatformId(); |
| 6421 | |
| 6422 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) /* Windows NT */ |
| 6423 | psz = "cmd.exe"; |
| 6424 | else if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) /* Windows 95 */ |
| 6425 | psz = "command.com"; |
| 6426 | |
| 6427 | return psz; |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| 6431 | * mch_access() extends access() to do more detailed check on network drives. |
| 6432 | * Returns 0 if file "n" has access rights according to "p", -1 otherwise. |
| 6433 | */ |
| 6434 | int |
| 6435 | mch_access(char *n, int p) |
| 6436 | { |
| 6437 | HANDLE hFile; |
| 6438 | DWORD am; |
| 6439 | int retval = -1; /* default: fail */ |
| 6440 | #ifdef FEAT_MBYTE |
| 6441 | WCHAR *wn = NULL; |
| 6442 | |
| 6443 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6444 | wn = enc_to_utf16((char_u *)n, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6445 | #endif |
| 6446 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6447 | if (mch_isdir((char_u *)n)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6448 | { |
| 6449 | char TempName[_MAX_PATH + 16] = ""; |
| 6450 | #ifdef FEAT_MBYTE |
| 6451 | WCHAR TempNameW[_MAX_PATH + 16] = L""; |
| 6452 | #endif |
| 6453 | |
| 6454 | if (p & R_OK) |
| 6455 | { |
| 6456 | /* Read check is performed by seeing if we can do a find file on |
| 6457 | * the directory for any file. */ |
| 6458 | #ifdef FEAT_MBYTE |
| 6459 | if (wn != NULL) |
| 6460 | { |
| 6461 | int i; |
| 6462 | WIN32_FIND_DATAW d; |
| 6463 | |
| 6464 | for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i) |
| 6465 | TempNameW[i] = wn[i]; |
| 6466 | if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/') |
| 6467 | TempNameW[i++] = '\\'; |
| 6468 | TempNameW[i++] = '*'; |
| 6469 | TempNameW[i++] = 0; |
| 6470 | |
| 6471 | hFile = FindFirstFileW(TempNameW, &d); |
| 6472 | if (hFile == INVALID_HANDLE_VALUE) |
| 6473 | { |
| 6474 | if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 6475 | goto getout; |
| 6476 | |
| 6477 | /* Retry with non-wide function (for Windows 98). */ |
| 6478 | vim_free(wn); |
| 6479 | wn = NULL; |
| 6480 | } |
| 6481 | else |
| 6482 | (void)FindClose(hFile); |
| 6483 | } |
| 6484 | if (wn == NULL) |
| 6485 | #endif |
| 6486 | { |
| 6487 | char *pch; |
| 6488 | WIN32_FIND_DATA d; |
| 6489 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6490 | vim_strncpy((char_u *)TempName, (char_u *)n, _MAX_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6491 | pch = TempName + STRLEN(TempName) - 1; |
| 6492 | if (*pch != '\\' && *pch != '/') |
| 6493 | *++pch = '\\'; |
| 6494 | *++pch = '*'; |
| 6495 | *++pch = NUL; |
| 6496 | |
| 6497 | hFile = FindFirstFile(TempName, &d); |
| 6498 | if (hFile == INVALID_HANDLE_VALUE) |
| 6499 | goto getout; |
| 6500 | (void)FindClose(hFile); |
| 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | if (p & W_OK) |
| 6505 | { |
| 6506 | /* Trying to create a temporary file in the directory should catch |
| 6507 | * directories on read-only network shares. However, in |
| 6508 | * directories whose ACL allows writes but denies deletes will end |
| 6509 | * up keeping the temporary file :-(. */ |
| 6510 | #ifdef FEAT_MBYTE |
| 6511 | if (wn != NULL) |
| 6512 | { |
| 6513 | if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW)) |
| 6514 | { |
| 6515 | if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 6516 | goto getout; |
| 6517 | |
| 6518 | /* Retry with non-wide function (for Windows 98). */ |
| 6519 | vim_free(wn); |
| 6520 | wn = NULL; |
| 6521 | } |
| 6522 | else |
| 6523 | DeleteFileW(TempNameW); |
| 6524 | } |
| 6525 | if (wn == NULL) |
| 6526 | #endif |
| 6527 | { |
| 6528 | if (!GetTempFileName(n, "VIM", 0, TempName)) |
| 6529 | goto getout; |
| 6530 | mch_remove((char_u *)TempName); |
| 6531 | } |
| 6532 | } |
| 6533 | } |
| 6534 | else |
| 6535 | { |
| 6536 | /* Trying to open the file for the required access does ACL, read-only |
| 6537 | * network share, and file attribute checks. */ |
| 6538 | am = ((p & W_OK) ? GENERIC_WRITE : 0) |
| 6539 | | ((p & R_OK) ? GENERIC_READ : 0); |
| 6540 | #ifdef FEAT_MBYTE |
| 6541 | if (wn != NULL) |
| 6542 | { |
| 6543 | hFile = CreateFileW(wn, am, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 6544 | if (hFile == INVALID_HANDLE_VALUE |
| 6545 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 6546 | { |
| 6547 | /* Retry with non-wide function (for Windows 98). */ |
| 6548 | vim_free(wn); |
| 6549 | wn = NULL; |
| 6550 | } |
| 6551 | } |
| 6552 | if (wn == NULL) |
| 6553 | #endif |
| 6554 | hFile = CreateFile(n, am, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 6555 | if (hFile == INVALID_HANDLE_VALUE) |
| 6556 | goto getout; |
| 6557 | CloseHandle(hFile); |
| 6558 | } |
| 6559 | |
| 6560 | retval = 0; /* success */ |
| 6561 | getout: |
| 6562 | #ifdef FEAT_MBYTE |
| 6563 | vim_free(wn); |
| 6564 | #endif |
| 6565 | return retval; |
| 6566 | } |
| 6567 | |
| 6568 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 6569 | /* |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 6570 | * Version of open() that may use UTF-16 file name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | */ |
| 6572 | int |
| 6573 | mch_open(char *name, int flags, int mode) |
| 6574 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6575 | /* _wopen() does not work with Borland C 5.5: creates a read-only file. */ |
| 6576 | # ifndef __BORLANDC__ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6577 | WCHAR *wn; |
| 6578 | int f; |
| 6579 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6580 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6581 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6582 | wn = enc_to_utf16((char_u *)name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6583 | if (wn != NULL) |
| 6584 | { |
| 6585 | f = _wopen(wn, flags, mode); |
| 6586 | vim_free(wn); |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 6587 | if (f >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6588 | return f; |
| 6589 | /* Retry with non-wide function (for Windows 98). Can't use |
| 6590 | * GetLastError() here and it's unclear what errno gets set to if |
| 6591 | * the _wopen() fails for missing wide functions. */ |
| 6592 | } |
| 6593 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6594 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6595 | |
Bram Moolenaar | f9e6c3b | 2014-11-05 18:36:03 +0100 | [diff] [blame] | 6596 | /* open() can open a file which name is longer than _MAX_PATH bytes |
| 6597 | * and shorter than _MAX_PATH characters successfully, but sometimes it |
| 6598 | * causes unexpected error in another part. We make it an error explicitly |
| 6599 | * here. */ |
| 6600 | if (strlen(name) >= _MAX_PATH) |
| 6601 | return -1; |
| 6602 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6603 | return open(name, flags, mode); |
| 6604 | } |
| 6605 | |
| 6606 | /* |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 6607 | * Version of fopen() that may use UTF-16 file name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6608 | */ |
| 6609 | FILE * |
| 6610 | mch_fopen(char *name, char *mode) |
| 6611 | { |
| 6612 | WCHAR *wn, *wm; |
| 6613 | FILE *f = NULL; |
| 6614 | |
| 6615 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage |
| 6616 | # ifdef __BORLANDC__ |
| 6617 | /* Wide functions of Borland C 5.5 do not work on Windows 98. */ |
| 6618 | && g_PlatformId == VER_PLATFORM_WIN32_NT |
| 6619 | # endif |
| 6620 | ) |
| 6621 | { |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 6622 | # if defined(DEBUG) && _MSC_VER >= 1400 |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 6623 | /* Work around an annoying assertion in the Microsoft debug CRT |
| 6624 | * when mode's text/binary setting doesn't match _get_fmode(). */ |
| 6625 | char newMode = mode[strlen(mode) - 1]; |
| 6626 | int oldMode = 0; |
| 6627 | |
| 6628 | _get_fmode(&oldMode); |
| 6629 | if (newMode == 't') |
| 6630 | _set_fmode(_O_TEXT); |
| 6631 | else if (newMode == 'b') |
| 6632 | _set_fmode(_O_BINARY); |
| 6633 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 6634 | wn = enc_to_utf16((char_u *)name, NULL); |
| 6635 | wm = enc_to_utf16((char_u *)mode, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6636 | if (wn != NULL && wm != NULL) |
| 6637 | f = _wfopen(wn, wm); |
| 6638 | vim_free(wn); |
| 6639 | vim_free(wm); |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 6640 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 6641 | # if defined(DEBUG) && _MSC_VER >= 1400 |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 6642 | _set_fmode(oldMode); |
| 6643 | # endif |
| 6644 | |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 6645 | if (f != NULL || g_PlatformId == VER_PLATFORM_WIN32_NT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6646 | return f; |
| 6647 | /* Retry with non-wide function (for Windows 98). Can't use |
| 6648 | * GetLastError() here and it's unclear what errno gets set to if |
| 6649 | * the _wfopen() fails for missing wide functions. */ |
| 6650 | } |
| 6651 | |
Bram Moolenaar | f9e6c3b | 2014-11-05 18:36:03 +0100 | [diff] [blame] | 6652 | /* fopen() can open a file which name is longer than _MAX_PATH bytes |
| 6653 | * and shorter than _MAX_PATH characters successfully, but sometimes it |
| 6654 | * causes unexpected error in another part. We make it an error explicitly |
| 6655 | * here. */ |
| 6656 | if (strlen(name) >= _MAX_PATH) |
| 6657 | return NULL; |
| 6658 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | return fopen(name, mode); |
| 6660 | } |
| 6661 | #endif |
| 6662 | |
| 6663 | #ifdef FEAT_MBYTE |
| 6664 | /* |
| 6665 | * SUB STREAM (aka info stream) handling: |
| 6666 | * |
| 6667 | * NTFS can have sub streams for each file. Normal contents of file is |
| 6668 | * stored in the main stream, and extra contents (author information and |
| 6669 | * title and so on) can be stored in sub stream. After Windows 2000, user |
| 6670 | * can access and store those informations in sub streams via explorer's |
| 6671 | * property menuitem in right click menu. Those informations in sub streams |
| 6672 | * were lost when copying only the main stream. So we have to copy sub |
| 6673 | * streams. |
| 6674 | * |
| 6675 | * Incomplete explanation: |
| 6676 | * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp |
| 6677 | * More useful info and an example: |
| 6678 | * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams |
| 6679 | */ |
| 6680 | |
| 6681 | /* |
| 6682 | * Copy info stream data "substream". Read from the file with BackupRead(sh) |
| 6683 | * and write to stream "substream" of file "to". |
| 6684 | * Errors are ignored. |
| 6685 | */ |
| 6686 | static void |
| 6687 | copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len) |
| 6688 | { |
| 6689 | HANDLE hTo; |
| 6690 | WCHAR *to_name; |
| 6691 | |
| 6692 | to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR)); |
| 6693 | wcscpy(to_name, to); |
| 6694 | wcscat(to_name, substream); |
| 6695 | |
| 6696 | hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, |
| 6697 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 6698 | if (hTo != INVALID_HANDLE_VALUE) |
| 6699 | { |
| 6700 | long done; |
| 6701 | DWORD todo; |
| 6702 | DWORD readcnt, written; |
| 6703 | char buf[4096]; |
| 6704 | |
| 6705 | /* Copy block of bytes at a time. Abort when something goes wrong. */ |
| 6706 | for (done = 0; done < len; done += written) |
| 6707 | { |
| 6708 | /* (size_t) cast for Borland C 5.5 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6709 | todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf) |
| 6710 | : (size_t)(len - done)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6711 | if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt, |
| 6712 | FALSE, FALSE, context) |
| 6713 | || readcnt != todo |
| 6714 | || !WriteFile(hTo, buf, todo, &written, NULL) |
| 6715 | || written != todo) |
| 6716 | break; |
| 6717 | } |
| 6718 | CloseHandle(hTo); |
| 6719 | } |
| 6720 | |
| 6721 | free(to_name); |
| 6722 | } |
| 6723 | |
| 6724 | /* |
| 6725 | * Copy info streams from file "from" to file "to". |
| 6726 | */ |
| 6727 | static void |
| 6728 | copy_infostreams(char_u *from, char_u *to) |
| 6729 | { |
| 6730 | WCHAR *fromw; |
| 6731 | WCHAR *tow; |
| 6732 | HANDLE sh; |
| 6733 | WIN32_STREAM_ID sid; |
| 6734 | int headersize; |
| 6735 | WCHAR streamname[_MAX_PATH]; |
| 6736 | DWORD readcount; |
| 6737 | void *context = NULL; |
| 6738 | DWORD lo, hi; |
| 6739 | int len; |
| 6740 | |
| 6741 | /* Convert the file names to wide characters. */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 6742 | fromw = enc_to_utf16(from, NULL); |
| 6743 | tow = enc_to_utf16(to, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6744 | if (fromw != NULL && tow != NULL) |
| 6745 | { |
| 6746 | /* Open the file for reading. */ |
| 6747 | sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 6748 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 6749 | if (sh != INVALID_HANDLE_VALUE) |
| 6750 | { |
| 6751 | /* Use BackupRead() to find the info streams. Repeat until we |
| 6752 | * have done them all.*/ |
| 6753 | for (;;) |
| 6754 | { |
| 6755 | /* Get the header to find the length of the stream name. If |
| 6756 | * the "readcount" is zero we have done all info streams. */ |
| 6757 | ZeroMemory(&sid, sizeof(WIN32_STREAM_ID)); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6758 | headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6759 | if (!BackupRead(sh, (LPBYTE)&sid, headersize, |
| 6760 | &readcount, FALSE, FALSE, &context) |
| 6761 | || readcount == 0) |
| 6762 | break; |
| 6763 | |
| 6764 | /* We only deal with streams that have a name. The normal |
| 6765 | * file data appears to be without a name, even though docs |
| 6766 | * suggest it is called "::$DATA". */ |
| 6767 | if (sid.dwStreamNameSize > 0) |
| 6768 | { |
| 6769 | /* Read the stream name. */ |
| 6770 | if (!BackupRead(sh, (LPBYTE)streamname, |
| 6771 | sid.dwStreamNameSize, |
| 6772 | &readcount, FALSE, FALSE, &context)) |
| 6773 | break; |
| 6774 | |
| 6775 | /* Copy an info stream with a name ":anything:$DATA". |
| 6776 | * Skip "::$DATA", it has no stream name (examples suggest |
| 6777 | * it might be used for the normal file contents). |
| 6778 | * Note that BackupRead() counts bytes, but the name is in |
| 6779 | * wide characters. */ |
| 6780 | len = readcount / sizeof(WCHAR); |
| 6781 | streamname[len] = 0; |
| 6782 | if (len > 7 && wcsicmp(streamname + len - 6, |
| 6783 | L":$DATA") == 0) |
| 6784 | { |
| 6785 | streamname[len - 6] = 0; |
| 6786 | copy_substream(sh, &context, tow, streamname, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6787 | (long)sid.Size.u.LowPart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | /* Advance to the next stream. We might try seeking too far, |
| 6792 | * but BackupSeek() doesn't skip over stream borders, thus |
| 6793 | * that's OK. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6794 | (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6795 | &lo, &hi, &context); |
| 6796 | } |
| 6797 | |
| 6798 | /* Clear the context. */ |
| 6799 | (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context); |
| 6800 | |
| 6801 | CloseHandle(sh); |
| 6802 | } |
| 6803 | } |
| 6804 | vim_free(fromw); |
| 6805 | vim_free(tow); |
| 6806 | } |
| 6807 | #endif |
| 6808 | |
| 6809 | /* |
| 6810 | * Copy file attributes from file "from" to file "to". |
| 6811 | * For Windows NT and later we copy info streams. |
| 6812 | * Always returns zero, errors are ignored. |
| 6813 | */ |
| 6814 | int |
| 6815 | mch_copy_file_attribute(char_u *from, char_u *to) |
| 6816 | { |
| 6817 | #ifdef FEAT_MBYTE |
| 6818 | /* File streams only work on Windows NT and later. */ |
| 6819 | PlatformId(); |
| 6820 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 6821 | copy_infostreams(from, to); |
| 6822 | #endif |
| 6823 | return 0; |
| 6824 | } |
| 6825 | |
| 6826 | #if defined(MYRESETSTKOFLW) || defined(PROTO) |
| 6827 | /* |
| 6828 | * Recreate a destroyed stack guard page in win32. |
| 6829 | * Written by Benjamin Peterson. |
| 6830 | */ |
| 6831 | |
| 6832 | /* These magic numbers are from the MS header files */ |
| 6833 | #define MIN_STACK_WIN9X 17 |
| 6834 | #define MIN_STACK_WINNT 2 |
| 6835 | |
| 6836 | /* |
| 6837 | * This function does the same thing as _resetstkoflw(), which is only |
| 6838 | * available in DevStudio .net and later. |
| 6839 | * Returns 0 for failure, 1 for success. |
| 6840 | */ |
| 6841 | int |
| 6842 | myresetstkoflw(void) |
| 6843 | { |
| 6844 | BYTE *pStackPtr; |
| 6845 | BYTE *pGuardPage; |
| 6846 | BYTE *pStackBase; |
| 6847 | BYTE *pLowestPossiblePage; |
| 6848 | MEMORY_BASIC_INFORMATION mbi; |
| 6849 | SYSTEM_INFO si; |
| 6850 | DWORD nPageSize; |
| 6851 | DWORD dummy; |
| 6852 | |
| 6853 | /* This code will not work on win32s. */ |
| 6854 | PlatformId(); |
| 6855 | if (g_PlatformId == VER_PLATFORM_WIN32s) |
| 6856 | return 0; |
| 6857 | |
| 6858 | /* We need to know the system page size. */ |
| 6859 | GetSystemInfo(&si); |
| 6860 | nPageSize = si.dwPageSize; |
| 6861 | |
| 6862 | /* ...and the current stack pointer */ |
| 6863 | pStackPtr = (BYTE*)_alloca(1); |
| 6864 | |
| 6865 | /* ...and the base of the stack. */ |
| 6866 | if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0) |
| 6867 | return 0; |
| 6868 | pStackBase = (BYTE*)mbi.AllocationBase; |
| 6869 | |
| 6870 | /* ...and the page thats min_stack_req pages away from stack base; this is |
| 6871 | * the lowest page we could use. */ |
| 6872 | pLowestPossiblePage = pStackBase + ((g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 6873 | ? MIN_STACK_WINNT : MIN_STACK_WIN9X) * nPageSize; |
| 6874 | |
| 6875 | /* On Win95, we want the next page down from the end of the stack. */ |
| 6876 | if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) |
| 6877 | { |
| 6878 | /* Find the page that's only 1 page down from the page that the stack |
| 6879 | * ptr is in. */ |
| 6880 | pGuardPage = (BYTE*)((DWORD)nPageSize * (((DWORD)pStackPtr |
| 6881 | / (DWORD)nPageSize) - 1)); |
| 6882 | if (pGuardPage < pLowestPossiblePage) |
| 6883 | return 0; |
| 6884 | |
| 6885 | /* Apply the noaccess attribute to the page -- there's no guard |
| 6886 | * attribute in win95-type OSes. */ |
| 6887 | if (!VirtualProtect(pGuardPage, nPageSize, PAGE_NOACCESS, &dummy)) |
| 6888 | return 0; |
| 6889 | } |
| 6890 | else |
| 6891 | { |
| 6892 | /* On NT, however, we want the first committed page in the stack Start |
| 6893 | * at the stack base and move forward through memory until we find a |
| 6894 | * committed block. */ |
| 6895 | BYTE *pBlock = pStackBase; |
| 6896 | |
Bram Moolenaar | a466c99 | 2005-07-09 21:03:22 +0000 | [diff] [blame] | 6897 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | { |
| 6899 | if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0) |
| 6900 | return 0; |
| 6901 | |
| 6902 | pBlock += mbi.RegionSize; |
| 6903 | |
| 6904 | if (mbi.State & MEM_COMMIT) |
| 6905 | break; |
| 6906 | } |
| 6907 | |
| 6908 | /* mbi now describes the first committed block in the stack. */ |
| 6909 | if (mbi.Protect & PAGE_GUARD) |
| 6910 | return 1; |
| 6911 | |
| 6912 | /* decide where the guard page should start */ |
| 6913 | if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage) |
| 6914 | pGuardPage = pLowestPossiblePage; |
| 6915 | else |
| 6916 | pGuardPage = (BYTE*)mbi.BaseAddress; |
| 6917 | |
| 6918 | /* allocate the guard page */ |
| 6919 | if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE)) |
| 6920 | return 0; |
| 6921 | |
| 6922 | /* apply the guard attribute to the page */ |
| 6923 | if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD, |
| 6924 | &dummy)) |
| 6925 | return 0; |
| 6926 | } |
| 6927 | |
| 6928 | return 1; |
| 6929 | } |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6930 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6931 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6932 | |
| 6933 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 6934 | /* |
| 6935 | * The command line arguments in UCS2 |
| 6936 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6937 | static int nArgsW = 0; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6938 | static LPWSTR *ArglistW = NULL; |
| 6939 | static int global_argc = 0; |
| 6940 | static char **global_argv; |
| 6941 | |
| 6942 | static int used_file_argc = 0; /* last argument in global_argv[] used |
| 6943 | for the argument list. */ |
| 6944 | static int *used_file_indexes = NULL; /* indexes in global_argv[] for |
| 6945 | command line arguments added to |
| 6946 | the argument list */ |
| 6947 | static int used_file_count = 0; /* nr of entries in used_file_indexes */ |
| 6948 | static int used_file_literal = FALSE; /* take file names literally */ |
| 6949 | static int used_file_full_path = FALSE; /* file name was full path */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 6950 | static int used_file_diff_mode = FALSE; /* file name was with diff mode */ |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6951 | static int used_alist_count = 0; |
| 6952 | |
| 6953 | |
| 6954 | /* |
| 6955 | * Get the command line arguments. Unicode version. |
| 6956 | * Returns argc. Zero when something fails. |
| 6957 | */ |
| 6958 | int |
| 6959 | get_cmd_argsW(char ***argvp) |
| 6960 | { |
| 6961 | char **argv = NULL; |
| 6962 | int argc = 0; |
| 6963 | int i; |
| 6964 | |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 6965 | free_cmd_argsW(); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6966 | ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW); |
| 6967 | if (ArglistW != NULL) |
| 6968 | { |
| 6969 | argv = malloc((nArgsW + 1) * sizeof(char *)); |
| 6970 | if (argv != NULL) |
| 6971 | { |
| 6972 | argc = nArgsW; |
| 6973 | argv[argc] = NULL; |
| 6974 | for (i = 0; i < argc; ++i) |
| 6975 | { |
| 6976 | int len; |
| 6977 | |
| 6978 | /* Convert each Unicode argument to the current codepage. */ |
| 6979 | WideCharToMultiByte_alloc(GetACP(), 0, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6980 | ArglistW[i], (int)wcslen(ArglistW[i]) + 1, |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6981 | (LPSTR *)&argv[i], &len, 0, 0); |
| 6982 | if (argv[i] == NULL) |
| 6983 | { |
| 6984 | /* Out of memory, clear everything. */ |
| 6985 | while (i > 0) |
| 6986 | free(argv[--i]); |
| 6987 | free(argv); |
Bram Moolenaar | 73c6163 | 2013-12-07 14:48:10 +0100 | [diff] [blame] | 6988 | argv = NULL; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6989 | argc = 0; |
| 6990 | } |
| 6991 | } |
| 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | global_argc = argc; |
| 6996 | global_argv = argv; |
| 6997 | if (argc > 0) |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 6998 | { |
| 6999 | if (used_file_indexes != NULL) |
| 7000 | free(used_file_indexes); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7001 | used_file_indexes = malloc(argc * sizeof(int)); |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 7002 | } |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7003 | |
| 7004 | if (argvp != NULL) |
| 7005 | *argvp = argv; |
| 7006 | return argc; |
| 7007 | } |
| 7008 | |
| 7009 | void |
| 7010 | free_cmd_argsW(void) |
| 7011 | { |
| 7012 | if (ArglistW != NULL) |
| 7013 | { |
| 7014 | GlobalFree(ArglistW); |
| 7015 | ArglistW = NULL; |
| 7016 | } |
| 7017 | } |
| 7018 | |
| 7019 | /* |
| 7020 | * Remember "name" is an argument that was added to the argument list. |
| 7021 | * This avoids that we have to re-parse the argument list when fix_arg_enc() |
| 7022 | * is called. |
| 7023 | */ |
| 7024 | void |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7025 | used_file_arg(char *name, int literal, int full_path, int diff_mode) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7026 | { |
| 7027 | int i; |
| 7028 | |
| 7029 | if (used_file_indexes == NULL) |
| 7030 | return; |
| 7031 | for (i = used_file_argc + 1; i < global_argc; ++i) |
| 7032 | if (STRCMP(global_argv[i], name) == 0) |
| 7033 | { |
| 7034 | used_file_argc = i; |
| 7035 | used_file_indexes[used_file_count++] = i; |
| 7036 | break; |
| 7037 | } |
| 7038 | used_file_literal = literal; |
| 7039 | used_file_full_path = full_path; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7040 | used_file_diff_mode = diff_mode; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | /* |
| 7044 | * Remember the length of the argument list as it was. If it changes then we |
| 7045 | * leave it alone when 'encoding' is set. |
| 7046 | */ |
| 7047 | void |
| 7048 | set_alist_count(void) |
| 7049 | { |
| 7050 | used_alist_count = GARGCOUNT; |
| 7051 | } |
| 7052 | |
| 7053 | /* |
| 7054 | * Fix the encoding of the command line arguments. Invoked when 'encoding' |
| 7055 | * has been changed while starting up. Use the UCS-2 command line arguments |
| 7056 | * and convert them to 'encoding'. |
| 7057 | */ |
| 7058 | void |
| 7059 | fix_arg_enc(void) |
| 7060 | { |
| 7061 | int i; |
| 7062 | int idx; |
| 7063 | char_u *str; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7064 | int *fnum_list; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7065 | |
| 7066 | /* Safety checks: |
| 7067 | * - if argument count differs between the wide and non-wide argument |
| 7068 | * list, something must be wrong. |
| 7069 | * - the file name arguments must have been located. |
| 7070 | * - the length of the argument list wasn't changed by the user. |
| 7071 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7072 | if (global_argc != nArgsW |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7073 | || ArglistW == NULL |
| 7074 | || used_file_indexes == NULL |
| 7075 | || used_file_count == 0 |
| 7076 | || used_alist_count != GARGCOUNT) |
| 7077 | return; |
| 7078 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7079 | /* Remember the buffer numbers for the arguments. */ |
| 7080 | fnum_list = (int *)alloc((int)sizeof(int) * GARGCOUNT); |
| 7081 | if (fnum_list == NULL) |
| 7082 | return; /* out of memory */ |
| 7083 | for (i = 0; i < GARGCOUNT; ++i) |
| 7084 | fnum_list[i] = GARGLIST[i].ae_fnum; |
| 7085 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7086 | /* Clear the argument list. Make room for the new arguments. */ |
| 7087 | alist_clear(&global_alist); |
| 7088 | if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7089 | return; /* out of memory */ |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7090 | |
| 7091 | for (i = 0; i < used_file_count; ++i) |
| 7092 | { |
| 7093 | idx = used_file_indexes[i]; |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 7094 | str = utf16_to_enc(ArglistW[idx], NULL); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7095 | if (str != NULL) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7096 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7097 | #ifdef FEAT_DIFF |
| 7098 | /* When using diff mode may need to concatenate file name to |
| 7099 | * directory name. Just like it's done in main(). */ |
| 7100 | if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0 |
| 7101 | && !mch_isdir(alist_name(&GARGLIST[0]))) |
| 7102 | { |
| 7103 | char_u *r; |
| 7104 | |
| 7105 | r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE); |
| 7106 | if (r != NULL) |
| 7107 | { |
| 7108 | vim_free(str); |
| 7109 | str = r; |
| 7110 | } |
| 7111 | } |
| 7112 | #endif |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7113 | /* Re-use the old buffer by renaming it. When not using literal |
| 7114 | * names it's done by alist_expand() below. */ |
| 7115 | if (used_file_literal) |
| 7116 | buf_set_name(fnum_list[i], str); |
| 7117 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7118 | alist_add(&global_alist, str, used_file_literal ? 2 : 0); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7119 | } |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | if (!used_file_literal) |
| 7123 | { |
| 7124 | /* Now expand wildcards in the arguments. */ |
| 7125 | /* Temporarily add '(' and ')' to 'isfname'. These are valid |
| 7126 | * filename characters but are excluded from 'isfname' to make |
| 7127 | * "gf" work on a file name in parenthesis (e.g.: see vim.h). */ |
| 7128 | do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)"); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7129 | alist_expand(fnum_list, used_alist_count); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7130 | do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF"); |
| 7131 | } |
| 7132 | |
| 7133 | /* If wildcard expansion failed, we are editing the first file of the |
| 7134 | * arglist and there is no file name: Edit the first argument now. */ |
| 7135 | if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL) |
| 7136 | { |
| 7137 | do_cmdline_cmd((char_u *)":rewind"); |
| 7138 | if (GARGCOUNT == 1 && used_file_full_path) |
| 7139 | (void)vim_chdirfile(alist_name(&GARGLIST[0])); |
| 7140 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7141 | |
| 7142 | set_alist_count(); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 7143 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7144 | #endif |