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