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