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