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