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