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