Mercurial > vim
annotate src/os_win32.c @ 7184:0a256475412f v7.4.902
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Oct 30 16:46:55 2015 +0100
patch 7.4.902
Problem: Problems with using the MS-Windows console.
Solution: Revert patches 7.4.851, 7.4.876 and 7.4.886 until we find a better
solution. (suggested by Ken Takata)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 30 Oct 2015 17:00:04 +0100 |
parents | 46390681bd80 |
children | 272f04b41f51 |
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); | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
216 static DWORD write_chars(char_u *pchBuf, DWORD cbToWrite); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
217 static WCHAR tgetch(int *pmodifiers, WCHAR *pch2); |
7 | 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) | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
268 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
269 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents); |
5633 | 270 } |
271 | |
5580 | 272 if (s_dwMax == 0) |
273 { | |
5590 | 274 if (nLength == -1) |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
275 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
276 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents)) |
5590 | 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__) | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
871 # define UChar UnicodeChar |
7 | 872 #else |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
873 # define UChar uChar.UnicodeChar |
7 | 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 { | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
892 pker->UChar = (WCHAR) awAnsiCode[1]; |
7 | 893 s_iIsDead = 0; |
894 return 1; | |
895 } | |
896 | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
897 if (pker->UChar != 0) |
7 | 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 */ | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
912 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0); |
7 | 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 | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
925 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
926 abKeystate, awAnsiCode, 2, 0); |
7 | 927 |
928 if (s_iIsDead > 0) | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
929 pker->UChar = (WCHAR) awAnsiCode[0]; |
7 | 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, | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
956 WCHAR *pch, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
957 WCHAR *pch2, |
7 | 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 */ | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
985 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL) |
7 | 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 { | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1047 *pch = (i > 0) ? pker->UChar : NUL; |
7 | 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; | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1439 WCHAR ch, ch2; |
7 | 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 */ | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1526 if (ir.Event.KeyEvent.UChar == 0 |
7 | 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 */ | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1589 static WCHAR |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1590 tgetch(int *pmodifiers, WCHAR *pch2) |
7 | 1591 { |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1592 WCHAR ch; |
7 | 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; | |
1661 | |
1662 /* First use any typeahead that was kept because "buf" was too small. */ | |
1663 if (typeaheadlen > 0) | |
1664 goto theend; | |
1665 | |
1666 #ifdef FEAT_SNIFF | |
1667 if (want_sniff_request) | |
1668 { | |
1669 if (sniff_request_waiting) | |
1670 { | |
1671 /* return K_SNIFF */ | |
1672 typeahead[typeaheadlen++] = CSI; | |
1673 typeahead[typeaheadlen++] = (char_u)KS_EXTRA; | |
1674 typeahead[typeaheadlen++] = (char_u)KE_SNIFF; | |
1675 sniff_request_waiting = 0; | |
1676 want_sniff_request = 0; | |
1677 goto theend; | |
1678 } | |
1679 else if (time < 0 || time > 250) | |
1680 { | |
1681 /* don't wait too long, a request might be pending */ | |
1682 time = 250; | |
1683 } | |
1684 } | |
1685 #endif | |
1686 | |
1687 if (time >= 0) | |
1688 { | |
1689 if (!WaitForChar(time)) /* no character available */ | |
1690 return 0; | |
1691 } | |
1692 else /* time == -1, wait forever */ | |
1693 { | |
1694 mch_set_winsize_now(); /* Allow winsize changes from now on */ | |
1695 | |
203 | 1696 /* |
1697 * If there is no character available within 2 seconds (default) | |
1698 * write the autoscript file to disk. Or cause the CursorHold event | |
1699 * to be triggered. | |
1700 */ | |
1701 if (!WaitForChar(p_ut)) | |
7 | 1702 { |
1703 #ifdef FEAT_AUTOCMD | |
609 | 1704 if (trigger_cursorhold() && maxlen >= 3) |
7 | 1705 { |
203 | 1706 buf[0] = K_SPECIAL; |
1707 buf[1] = KS_EXTRA; | |
1708 buf[2] = (int)KE_CURSORHOLD; | |
1709 return 3; | |
7 | 1710 } |
1711 #endif | |
368 | 1712 before_blocking(); |
7 | 1713 } |
1714 } | |
1715 | |
1716 /* | |
1717 * Try to read as many characters as there are, until the buffer is full. | |
1718 */ | |
1719 | |
1720 /* we will get at least one key. Get more if they are available. */ | |
1721 g_fCBrkPressed = FALSE; | |
1722 | |
1723 #ifdef MCH_WRITE_DUMP | |
1724 if (fdDump) | |
1725 fputc('[', fdDump); | |
1726 #endif | |
1727 | |
1728 /* Keep looping until there is something in the typeahead buffer and more | |
1729 * to get and still room in the buffer (up to two bytes for a char and | |
1730 * three bytes for a modifier). */ | |
1731 while ((typeaheadlen == 0 || WaitForChar(0L)) | |
1732 && typeaheadlen + 5 <= TYPEAHEADLEN) | |
1733 { | |
1734 if (typebuf_changed(tb_change_cnt)) | |
1735 { | |
1736 /* "buf" may be invalid now if a client put something in the | |
1737 * typeahead buffer and "buf" is in the typeahead buffer. */ | |
1738 typeaheadlen = 0; | |
1739 break; | |
1740 } | |
1741 #ifdef FEAT_MOUSE | |
1742 if (g_nMouseClick != -1) | |
1743 { | |
1744 # ifdef MCH_WRITE_DUMP | |
1745 if (fdDump) | |
1746 fprintf(fdDump, "{%02x @ %d, %d}", | |
1747 g_nMouseClick, g_xMouse, g_yMouse); | |
1748 # endif | |
1749 typeahead[typeaheadlen++] = ESC + 128; | |
1750 typeahead[typeaheadlen++] = 'M'; | |
1751 typeahead[typeaheadlen++] = g_nMouseClick; | |
1752 typeahead[typeaheadlen++] = g_xMouse + '!'; | |
1753 typeahead[typeaheadlen++] = g_yMouse + '!'; | |
1754 g_nMouseClick = -1; | |
1755 } | |
1756 else | |
1757 #endif | |
1758 { | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1759 WCHAR ch2 = NUL; |
7 | 1760 int modifiers = 0; |
1761 | |
1762 c = tgetch(&modifiers, &ch2); | |
1763 | |
1764 if (typebuf_changed(tb_change_cnt)) | |
1765 { | |
1766 /* "buf" may be invalid now if a client put something in the | |
1767 * typeahead buffer and "buf" is in the typeahead buffer. */ | |
1768 typeaheadlen = 0; | |
1769 break; | |
1770 } | |
1771 | |
1772 if (c == Ctrl_C && ctrl_c_interrupts) | |
1773 { | |
1774 #if defined(FEAT_CLIENTSERVER) | |
1775 trash_input_buf(); | |
1776 #endif | |
1777 got_int = TRUE; | |
1778 } | |
1779 | |
1780 #ifdef FEAT_MOUSE | |
1781 if (g_nMouseClick == -1) | |
1782 #endif | |
1783 { | |
1784 int n = 1; | |
6047 | 1785 int conv = FALSE; |
1786 | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1787 #ifdef FEAT_MBYTE |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1788 if (ch2 == NUL) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1789 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1790 int i; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1791 char_u *p; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1792 WCHAR ch[2]; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1793 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1794 ch[0] = c; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1795 if (c >= 0xD800 && c <= 0xDBFF) /* High surrogate */ |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1796 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1797 ch[1] = tgetch(&modifiers, &ch2); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1798 n++; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1799 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1800 p = utf16_to_enc(ch, &n); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1801 if (p != NULL) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1802 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1803 for (i = 0; i < n; i++) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1804 typeahead[typeaheadlen + i] = p[i]; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1805 vim_free(p); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1806 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1807 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1808 else |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1809 #endif |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1810 typeahead[typeaheadlen] = c; |
7 | 1811 if (ch2 != NUL) |
1812 { | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1813 typeahead[typeaheadlen + n] = 3; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
1814 typeahead[typeaheadlen + n + 1] = (char_u)ch2; |
6047 | 1815 n += 2; |
7 | 1816 } |
1817 | |
6047 | 1818 if (conv) |
1819 { | |
1820 char_u *p = typeahead + typeaheadlen; | |
6524 | 1821 |
1822 if (*p != K_NUL) | |
6047 | 1823 { |
6524 | 1824 char_u *e = typeahead + TYPEAHEADLEN; |
1825 | |
1826 while (*p && p < e) | |
6047 | 1827 { |
6524 | 1828 if (*p == K_NUL) |
1829 { | |
1830 ++p; | |
1831 mch_memmove(p + 1, p, ((size_t)(e - p)) - 1); | |
1832 *p = 3; | |
1833 ++n; | |
1834 } | |
6047 | 1835 ++p; |
1836 } | |
1837 } | |
1838 } | |
1839 | |
7 | 1840 /* Use the ALT key to set the 8th bit of the character |
1841 * when it's one byte, the 8th bit isn't set yet and not | |
1842 * using a double-byte encoding (would become a lead | |
1843 * byte). */ | |
1844 if ((modifiers & MOD_MASK_ALT) | |
1845 && n == 1 | |
1846 && (typeahead[typeaheadlen] & 0x80) == 0 | |
1847 #ifdef FEAT_MBYTE | |
1848 && !enc_dbcs | |
1849 #endif | |
1850 ) | |
1851 { | |
1443 | 1852 #ifdef FEAT_MBYTE |
1853 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80, | |
1854 typeahead + typeaheadlen); | |
1855 #else | |
7 | 1856 typeahead[typeaheadlen] |= 0x80; |
1443 | 1857 #endif |
7 | 1858 modifiers &= ~MOD_MASK_ALT; |
1859 } | |
1860 | |
1861 if (modifiers != 0) | |
1862 { | |
1863 /* Prepend modifiers to the character. */ | |
1864 mch_memmove(typeahead + typeaheadlen + 3, | |
1865 typeahead + typeaheadlen, n); | |
1866 typeahead[typeaheadlen++] = K_SPECIAL; | |
1867 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER; | |
1868 typeahead[typeaheadlen++] = modifiers; | |
1869 } | |
1870 | |
1871 typeaheadlen += n; | |
1872 | |
1873 #ifdef MCH_WRITE_DUMP | |
1874 if (fdDump) | |
1875 fputc(c, fdDump); | |
1876 #endif | |
1877 } | |
1878 } | |
1879 } | |
1880 | |
1881 #ifdef MCH_WRITE_DUMP | |
1882 if (fdDump) | |
1883 { | |
1884 fputs("]\n", fdDump); | |
1885 fflush(fdDump); | |
1886 } | |
1887 #endif | |
1888 | |
1889 theend: | |
1890 /* Move typeahead to "buf", as much as fits. */ | |
1891 len = 0; | |
1892 while (len < maxlen && typeaheadlen > 0) | |
1893 { | |
1894 buf[len++] = typeahead[0]; | |
1895 mch_memmove(typeahead, typeahead + 1, --typeaheadlen); | |
1896 } | |
1897 return len; | |
1898 | |
1899 #else /* FEAT_GUI_W32 */ | |
1900 return 0; | |
1901 #endif /* FEAT_GUI_W32 */ | |
1902 } | |
1903 | |
3927 | 1904 #ifndef PROTO |
1905 # ifndef __MINGW32__ | |
1906 # include <shellapi.h> /* required for FindExecutable() */ | |
1907 # endif | |
7 | 1908 #endif |
1909 | |
9 | 1910 /* |
1911 * Return TRUE if "name" is in $PATH. | |
10 | 1912 * TODO: Should somehow check if it's really executable. |
9 | 1913 */ |
7 | 1914 static int |
5782 | 1915 executable_exists(char *name, char_u **path) |
7 | 1916 { |
9 | 1917 char *dum; |
1918 char fname[_MAX_PATH]; | |
6185 | 1919 char *curpath, *newpath; |
1920 long n; | |
9 | 1921 |
1922 #ifdef FEAT_MBYTE | |
1923 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
7 | 1924 { |
1752 | 1925 WCHAR *p = enc_to_utf16(name, NULL); |
9 | 1926 WCHAR fnamew[_MAX_PATH]; |
1927 WCHAR *dumw; | |
6185 | 1928 WCHAR *wcurpath, *wnewpath; |
9 | 1929 |
1930 if (p != NULL) | |
1931 { | |
6185 | 1932 wcurpath = _wgetenv(L"PATH"); |
1933 wnewpath = (WCHAR*)alloc((unsigned)(wcslen(wcurpath) + 3) | |
1934 * sizeof(WCHAR)); | |
1935 if (wnewpath == NULL) | |
1936 return FALSE; | |
1937 wcscpy(wnewpath, L".;"); | |
1938 wcscat(wnewpath, wcurpath); | |
1939 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw); | |
1940 vim_free(wnewpath); | |
9 | 1941 vim_free(p); |
1942 if (n > 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
1943 { | |
1944 if (n == 0) | |
1945 return FALSE; | |
1946 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY) | |
1947 return FALSE; | |
5782 | 1948 if (path != NULL) |
1949 *path = utf16_to_enc(fnamew, NULL); | |
9 | 1950 return TRUE; |
1951 } | |
1952 /* Retry with non-wide function (for Windows 98). */ | |
1953 } | |
7 | 1954 } |
9 | 1955 #endif |
6185 | 1956 |
1957 curpath = getenv("PATH"); | |
1958 newpath = (char*)alloc((unsigned)(STRLEN(curpath) + 3)); | |
1959 if (newpath == NULL) | |
1960 return FALSE; | |
1961 STRCPY(newpath, ".;"); | |
1962 STRCAT(newpath, curpath); | |
1963 n = (long)SearchPath(newpath, name, NULL, _MAX_PATH, fname, &dum); | |
1964 vim_free(newpath); | |
1965 if (n == 0) | |
9 | 1966 return FALSE; |
1967 if (mch_isdir(fname)) | |
1968 return FALSE; | |
5782 | 1969 if (path != NULL) |
1970 *path = vim_strsave(fname); | |
9 | 1971 return TRUE; |
7 | 1972 } |
1973 | |
2584 | 1974 #if ((defined(__MINGW32__) || defined (__CYGWIN32__)) && \ |
2935 | 1975 __MSVCRT_VERSION__ >= 0x800) || (defined(_MSC_VER) && _MSC_VER >= 1400) |
2584 | 1976 /* |
1977 * Bad parameter handler. | |
1978 * | |
1979 * Certain MS CRT functions will intentionally crash when passed invalid | |
1980 * parameters to highlight possible security holes. Setting this function as | |
1981 * the bad parameter handler will prevent the crash. | |
1982 * | |
1983 * In debug builds the parameters contain CRT information that might help track | |
1984 * down the source of a problem, but in non-debug builds the arguments are all | |
1985 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is | |
1986 * worth allowing these to make debugging of issues easier. | |
1987 */ | |
1988 static void | |
1989 bad_param_handler(const wchar_t *expression, | |
1990 const wchar_t *function, | |
1991 const wchar_t *file, | |
1992 unsigned int line, | |
1993 uintptr_t pReserved) | |
1994 { | |
1995 } | |
1996 | |
1997 # define SET_INVALID_PARAM_HANDLER \ | |
1998 ((void)_set_invalid_parameter_handler(bad_param_handler)) | |
1999 #else | |
2000 # define SET_INVALID_PARAM_HANDLER | |
2001 #endif | |
2002 | |
7 | 2003 #ifdef FEAT_GUI_W32 |
2004 | |
2005 /* | |
2006 * GUI version of mch_init(). | |
2007 */ | |
2008 void | |
26 | 2009 mch_init(void) |
7 | 2010 { |
2011 #ifndef __MINGW32__ | |
2012 extern int _fmode; | |
2013 #endif | |
2014 | |
2584 | 2015 /* Silently handle invalid parameters to CRT functions */ |
2016 SET_INVALID_PARAM_HANDLER; | |
2017 | |
7 | 2018 /* Let critical errors result in a failure, not in a dialog box. Required |
2019 * for the timestamp test to work on removed floppies. */ | |
2020 SetErrorMode(SEM_FAILCRITICALERRORS); | |
2021 | |
2022 _fmode = O_BINARY; /* we do our own CR-LF translation */ | |
2023 | |
2024 /* Specify window size. Is there a place to get the default from? */ | |
2025 Rows = 25; | |
2026 Columns = 80; | |
2027 | |
2028 /* Look for 'vimrun' */ | |
2029 if (!gui_is_win32s()) | |
2030 { | |
2031 char_u vimrun_location[_MAX_PATH + 4]; | |
2032 | |
2033 /* First try in same directory as gvim.exe */ | |
2034 STRCPY(vimrun_location, exe_name); | |
2035 STRCPY(gettail(vimrun_location), "vimrun.exe"); | |
2036 if (mch_getperm(vimrun_location) >= 0) | |
2037 { | |
2038 if (*skiptowhite(vimrun_location) != NUL) | |
2039 { | |
2040 /* Enclose path with white space in double quotes. */ | |
2041 mch_memmove(vimrun_location + 1, vimrun_location, | |
2042 STRLEN(vimrun_location) + 1); | |
2043 *vimrun_location = '"'; | |
2044 STRCPY(gettail(vimrun_location), "vimrun\" "); | |
2045 } | |
2046 else | |
2047 STRCPY(gettail(vimrun_location), "vimrun "); | |
2048 | |
2049 vimrun_path = (char *)vim_strsave(vimrun_location); | |
2050 s_dont_use_vimrun = FALSE; | |
2051 } | |
5782 | 2052 else if (executable_exists("vimrun.exe", NULL)) |
7 | 2053 s_dont_use_vimrun = FALSE; |
2054 | |
2055 /* Don't give the warning for a missing vimrun.exe right now, but only | |
2056 * when vimrun was supposed to be used. Don't bother people that do | |
2057 * not need vimrun.exe. */ | |
2058 if (s_dont_use_vimrun) | |
2059 need_vimrun_warning = TRUE; | |
2060 } | |
2061 | |
2062 /* | |
2063 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'. | |
2064 * Otherwise the default "findstr /n" is used. | |
2065 */ | |
5782 | 2066 if (!executable_exists("findstr.exe", NULL)) |
7 | 2067 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0); |
2068 | |
2069 #ifdef FEAT_CLIPBOARD | |
4168 | 2070 win_clip_init(); |
7 | 2071 #endif |
2072 } | |
2073 | |
2074 | |
2075 #else /* FEAT_GUI_W32 */ | |
2076 | |
2077 #define SRWIDTH(sr) ((sr).Right - (sr).Left + 1) | |
2078 #define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1) | |
2079 | |
2080 /* | |
2081 * ClearConsoleBuffer() | |
2082 * Description: | |
2083 * Clears the entire contents of the console screen buffer, using the | |
2084 * specified attribute. | |
2085 * Returns: | |
2086 * TRUE on success | |
2087 */ | |
2088 static BOOL | |
2089 ClearConsoleBuffer(WORD wAttribute) | |
2090 { | |
2091 CONSOLE_SCREEN_BUFFER_INFO csbi; | |
2092 COORD coord; | |
2093 DWORD NumCells, dummy; | |
2094 | |
2095 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
2096 return FALSE; | |
2097 | |
2098 NumCells = csbi.dwSize.X * csbi.dwSize.Y; | |
2099 coord.X = 0; | |
2100 coord.Y = 0; | |
2101 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells, | |
2102 coord, &dummy)) | |
2103 { | |
2104 return FALSE; | |
2105 } | |
2106 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells, | |
2107 coord, &dummy)) | |
2108 { | |
2109 return FALSE; | |
2110 } | |
2111 | |
2112 return TRUE; | |
2113 } | |
2114 | |
2115 /* | |
2116 * FitConsoleWindow() | |
2117 * Description: | |
2118 * Checks if the console window will fit within given buffer dimensions. | |
2119 * Also, if requested, will shrink the window to fit. | |
2120 * Returns: | |
2121 * TRUE on success | |
2122 */ | |
2123 static BOOL | |
2124 FitConsoleWindow( | |
2125 COORD dwBufferSize, | |
2126 BOOL WantAdjust) | |
2127 { | |
2128 CONSOLE_SCREEN_BUFFER_INFO csbi; | |
2129 COORD dwWindowSize; | |
2130 BOOL NeedAdjust = FALSE; | |
2131 | |
2132 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
2133 { | |
2134 /* | |
2135 * A buffer resize will fail if the current console window does | |
2136 * not lie completely within that buffer. To avoid this, we might | |
2137 * have to move and possibly shrink the window. | |
2138 */ | |
2139 if (csbi.srWindow.Right >= dwBufferSize.X) | |
2140 { | |
2141 dwWindowSize.X = SRWIDTH(csbi.srWindow); | |
2142 if (dwWindowSize.X > dwBufferSize.X) | |
2143 dwWindowSize.X = dwBufferSize.X; | |
2144 csbi.srWindow.Right = dwBufferSize.X - 1; | |
2145 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X; | |
2146 NeedAdjust = TRUE; | |
2147 } | |
2148 if (csbi.srWindow.Bottom >= dwBufferSize.Y) | |
2149 { | |
2150 dwWindowSize.Y = SRHEIGHT(csbi.srWindow); | |
2151 if (dwWindowSize.Y > dwBufferSize.Y) | |
2152 dwWindowSize.Y = dwBufferSize.Y; | |
2153 csbi.srWindow.Bottom = dwBufferSize.Y - 1; | |
2154 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y; | |
2155 NeedAdjust = TRUE; | |
2156 } | |
2157 if (NeedAdjust && WantAdjust) | |
2158 { | |
2159 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow)) | |
2160 return FALSE; | |
2161 } | |
2162 return TRUE; | |
2163 } | |
2164 | |
2165 return FALSE; | |
2166 } | |
2167 | |
2168 typedef struct ConsoleBufferStruct | |
2169 { | |
26 | 2170 BOOL IsValid; |
2171 CONSOLE_SCREEN_BUFFER_INFO Info; | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2172 PCHAR_INFO Buffer; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2173 COORD BufferSize; |
7 | 2174 } ConsoleBuffer; |
2175 | |
2176 /* | |
2177 * SaveConsoleBuffer() | |
2178 * Description: | |
2179 * Saves important information about the console buffer, including the | |
2180 * actual buffer contents. The saved information is suitable for later | |
2181 * restoration by RestoreConsoleBuffer(). | |
2182 * Returns: | |
2183 * TRUE if all information was saved; FALSE otherwise | |
2184 * If FALSE, still sets cb->IsValid if buffer characteristics were saved. | |
2185 */ | |
2186 static BOOL | |
2187 SaveConsoleBuffer( | |
2188 ConsoleBuffer *cb) | |
2189 { | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2190 DWORD NumCells; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2191 COORD BufferCoord; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2192 SMALL_RECT ReadRegion; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2193 WORD Y, Y_incr; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2194 |
7 | 2195 if (cb == NULL) |
2196 return FALSE; | |
2197 | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2198 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info)) |
7 | 2199 { |
2200 cb->IsValid = FALSE; | |
2201 return FALSE; | |
2202 } | |
2203 cb->IsValid = TRUE; | |
2204 | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2205 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2206 * Allocate a buffer large enough to hold the entire console screen |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2207 * buffer. If this ConsoleBuffer structure has already been initialized |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2208 * with a buffer of the correct size, then just use that one. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2209 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2210 if (!cb->IsValid || cb->Buffer == NULL || |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2211 cb->BufferSize.X != cb->Info.dwSize.X || |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2212 cb->BufferSize.Y != cb->Info.dwSize.Y) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2213 { |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2214 cb->BufferSize.X = cb->Info.dwSize.X; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2215 cb->BufferSize.Y = cb->Info.dwSize.Y; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2216 NumCells = cb->BufferSize.X * cb->BufferSize.Y; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2217 vim_free(cb->Buffer); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2218 cb->Buffer = (PCHAR_INFO)alloc(NumCells * sizeof(CHAR_INFO)); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2219 if (cb->Buffer == NULL) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2220 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2221 } |
7 | 2222 |
2223 /* | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2224 * We will now copy the console screen buffer into our buffer. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2225 * ReadConsoleOutput() seems to be limited as far as how much you |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2226 * can read at a time. Empirically, this number seems to be about |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2227 * 12000 cells (rows * columns). Start at position (0, 0) and copy |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2228 * in chunks until it is all copied. The chunks will all have the |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2229 * same horizontal characteristics, so initialize them now. The |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2230 * height of each chunk will be (12000 / width). |
7 | 2231 */ |
7078
383d6f39669b
commit https://github.com/vim/vim/commit/615942452eb74eee7d8386fd3d76a1534181fa06
Christian Brabandt <cb@256bit.org>
parents:
6981
diff
changeset
|
2232 BufferCoord.X = 0; |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2233 ReadRegion.Left = 0; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2234 ReadRegion.Right = cb->Info.dwSize.X - 1; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2235 Y_incr = 12000 / cb->Info.dwSize.X; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2236 for (Y = 0; Y < cb->BufferSize.Y; Y += Y_incr) |
7 | 2237 { |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2238 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2239 * Read into position (0, Y) in our buffer. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2240 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2241 BufferCoord.Y = Y; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2242 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2243 * Read the region whose top left corner is (0, Y) and whose bottom |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2244 * right corner is (width - 1, Y + Y_incr - 1). This should define |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2245 * a region of size width by Y_incr. Don't worry if this region is |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2246 * too large for the remaining buffer; it will be cropped. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2247 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2248 ReadRegion.Top = Y; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2249 ReadRegion.Bottom = Y + Y_incr - 1; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2250 if (!ReadConsoleOutput(g_hConOut, /* output handle */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2251 cb->Buffer, /* our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2252 cb->BufferSize, /* dimensions of our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2253 BufferCoord, /* offset in our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2254 &ReadRegion)) /* region to save */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2255 { |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2256 vim_free(cb->Buffer); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2257 cb->Buffer = NULL; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2258 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2259 } |
7 | 2260 } |
2261 | |
2262 return TRUE; | |
2263 } | |
2264 | |
2265 /* | |
2266 * RestoreConsoleBuffer() | |
2267 * Description: | |
2268 * Restores important information about the console buffer, including the | |
2269 * actual buffer contents, if desired. The information to restore is in | |
2270 * the same format used by SaveConsoleBuffer(). | |
2271 * Returns: | |
2272 * TRUE on success | |
2273 */ | |
2274 static BOOL | |
2275 RestoreConsoleBuffer( | |
26 | 2276 ConsoleBuffer *cb, |
2277 BOOL RestoreScreen) | |
7 | 2278 { |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2279 COORD BufferCoord; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2280 SMALL_RECT WriteRegion; |
7 | 2281 |
2282 if (cb == NULL || !cb->IsValid) | |
2283 return FALSE; | |
2284 | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2285 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2286 * Before restoring the buffer contents, clear the current buffer, and |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2287 * restore the cursor position and window information. Doing this now |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2288 * prevents old buffer contents from "flashing" onto the screen. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2289 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2290 if (RestoreScreen) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2291 ClearConsoleBuffer(cb->Info.wAttributes); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2292 |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2293 FitConsoleWindow(cb->Info.dwSize, TRUE); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2294 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize)) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2295 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2296 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes)) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2297 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2298 |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2299 if (!RestoreScreen) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2300 { |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2301 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2302 * No need to restore the screen buffer contents, so we're done. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2303 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2304 return TRUE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2305 } |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2306 |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2307 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition)) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2308 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2309 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow)) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2310 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2311 |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2312 /* |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2313 * Restore the screen buffer contents. |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2314 */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2315 if (cb->Buffer != NULL) |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2316 { |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2317 BufferCoord.X = 0; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2318 BufferCoord.Y = 0; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2319 WriteRegion.Left = 0; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2320 WriteRegion.Top = 0; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2321 WriteRegion.Right = cb->Info.dwSize.X - 1; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2322 WriteRegion.Bottom = cb->Info.dwSize.Y - 1; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2323 if (!WriteConsoleOutput(g_hConOut, /* output handle */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2324 cb->Buffer, /* our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2325 cb->BufferSize, /* dimensions of our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2326 BufferCoord, /* offset in our buffer */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2327 &WriteRegion)) /* region to restore */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2328 { |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2329 return FALSE; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2330 } |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2331 } |
7 | 2332 |
2333 return TRUE; | |
2334 } | |
2335 | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2336 #define FEAT_RESTORE_ORIG_SCREEN |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2337 #ifdef FEAT_RESTORE_ORIG_SCREEN |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2338 static ConsoleBuffer g_cbOrig = { 0 }; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2339 #endif |
7 | 2340 static ConsoleBuffer g_cbNonTermcap = { 0 }; |
2341 static ConsoleBuffer g_cbTermcap = { 0 }; | |
2342 | |
2343 #ifdef FEAT_TITLE | |
2344 #ifdef __BORLANDC__ | |
2345 typedef HWND (__stdcall *GETCONSOLEWINDOWPROC)(VOID); | |
2346 #else | |
2347 typedef WINBASEAPI HWND (WINAPI *GETCONSOLEWINDOWPROC)(VOID); | |
2348 #endif | |
2349 char g_szOrigTitle[256] = { 0 }; | |
2350 HWND g_hWnd = NULL; /* also used in os_mswin.c */ | |
2351 static HICON g_hOrigIconSmall = NULL; | |
2352 static HICON g_hOrigIcon = NULL; | |
2353 static HICON g_hVimIcon = NULL; | |
2354 static BOOL g_fCanChangeIcon = FALSE; | |
2355 | |
2356 /* ICON* are not defined in VC++ 4.0 */ | |
2357 #ifndef ICON_SMALL | |
2358 #define ICON_SMALL 0 | |
2359 #endif | |
2360 #ifndef ICON_BIG | |
2361 #define ICON_BIG 1 | |
2362 #endif | |
2363 /* | |
2364 * GetConsoleIcon() | |
2365 * Description: | |
2366 * Attempts to retrieve the small icon and/or the big icon currently in | |
2367 * use by a given window. | |
2368 * Returns: | |
2369 * TRUE on success | |
2370 */ | |
2371 static BOOL | |
2372 GetConsoleIcon( | |
26 | 2373 HWND hWnd, |
2374 HICON *phIconSmall, | |
2375 HICON *phIcon) | |
7 | 2376 { |
2377 if (hWnd == NULL) | |
2378 return FALSE; | |
2379 | |
2380 if (phIconSmall != NULL) | |
26 | 2381 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON, |
2382 (WPARAM)ICON_SMALL, (LPARAM)0); | |
7 | 2383 if (phIcon != NULL) |
26 | 2384 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON, |
2385 (WPARAM)ICON_BIG, (LPARAM)0); | |
7 | 2386 return TRUE; |
2387 } | |
2388 | |
2389 /* | |
2390 * SetConsoleIcon() | |
2391 * Description: | |
2392 * Attempts to change the small icon and/or the big icon currently in | |
2393 * use by a given window. | |
2394 * Returns: | |
2395 * TRUE on success | |
2396 */ | |
2397 static BOOL | |
2398 SetConsoleIcon( | |
26 | 2399 HWND hWnd, |
2400 HICON hIconSmall, | |
2401 HICON hIcon) | |
7 | 2402 { |
26 | 2403 HICON hPrevIconSmall; |
2404 HICON hPrevIcon; | |
7 | 2405 |
2406 if (hWnd == NULL) | |
2407 return FALSE; | |
2408 | |
2409 if (hIconSmall != NULL) | |
26 | 2410 hPrevIconSmall = (HICON)SendMessage(hWnd, WM_SETICON, |
2411 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall); | |
7 | 2412 if (hIcon != NULL) |
26 | 2413 hPrevIcon = (HICON)SendMessage(hWnd, WM_SETICON, |
2414 (WPARAM)ICON_BIG,(LPARAM) hIcon); | |
7 | 2415 return TRUE; |
2416 } | |
2417 | |
2418 /* | |
2419 * SaveConsoleTitleAndIcon() | |
2420 * Description: | |
2421 * Saves the current console window title in g_szOrigTitle, for later | |
2422 * restoration. Also, attempts to obtain a handle to the console window, | |
2423 * and use it to save the small and big icons currently in use by the | |
2424 * console window. This is not always possible on some versions of Windows; | |
2425 * nor is it possible when running Vim remotely using Telnet (since the | |
2426 * console window the user sees is owned by a remote process). | |
2427 */ | |
2428 static void | |
2429 SaveConsoleTitleAndIcon(void) | |
2430 { | |
2431 GETCONSOLEWINDOWPROC GetConsoleWindowProc; | |
2432 | |
2433 /* Save the original title. */ | |
2434 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle))) | |
2435 return; | |
2436 | |
2437 /* | |
2438 * Obtain a handle to the console window using GetConsoleWindow() from | |
2439 * KERNEL32.DLL; we need to handle in order to change the window icon. | |
2440 * This function only exists on NT-based Windows, starting with Windows | |
2441 * 2000. On older operating systems, we can't change the window icon | |
2442 * anyway. | |
2443 */ | |
2444 if ((GetConsoleWindowProc = (GETCONSOLEWINDOWPROC) | |
2445 GetProcAddress(GetModuleHandle("KERNEL32.DLL"), | |
2446 "GetConsoleWindow")) != NULL) | |
2447 { | |
2448 g_hWnd = (*GetConsoleWindowProc)(); | |
2449 } | |
2450 if (g_hWnd == NULL) | |
2451 return; | |
2452 | |
2453 /* Save the original console window icon. */ | |
2454 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon); | |
2455 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL) | |
2456 return; | |
2457 | |
2458 /* Extract the first icon contained in the Vim executable. */ | |
6249 | 2459 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL) |
2460 g_hVimIcon = ExtractIcon(NULL, exe_name, 0); | |
7 | 2461 if (g_hVimIcon != NULL) |
2462 g_fCanChangeIcon = TRUE; | |
2463 } | |
2464 #endif | |
2465 | |
2466 static int g_fWindInitCalled = FALSE; | |
2467 static int g_fTermcapMode = FALSE; | |
2468 static CONSOLE_CURSOR_INFO g_cci; | |
2469 static DWORD g_cmodein = 0; | |
2470 static DWORD g_cmodeout = 0; | |
2471 | |
2472 /* | |
2473 * non-GUI version of mch_init(). | |
2474 */ | |
2475 void | |
26 | 2476 mch_init(void) |
7 | 2477 { |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2478 #ifndef FEAT_RESTORE_ORIG_SCREEN |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2479 CONSOLE_SCREEN_BUFFER_INFO csbi; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2480 #endif |
7 | 2481 #ifndef __MINGW32__ |
2482 extern int _fmode; | |
2483 #endif | |
2484 | |
2584 | 2485 /* Silently handle invalid parameters to CRT functions */ |
2486 SET_INVALID_PARAM_HANDLER; | |
2487 | |
7 | 2488 /* Let critical errors result in a failure, not in a dialog box. Required |
2489 * for the timestamp test to work on removed floppies. */ | |
2490 SetErrorMode(SEM_FAILCRITICALERRORS); | |
2491 | |
2492 _fmode = O_BINARY; /* we do our own CR-LF translation */ | |
2493 out_flush(); | |
2494 | |
2495 /* Obtain handles for the standard Console I/O devices */ | |
2496 if (read_cmd_fd == 0) | |
2497 g_hConIn = GetStdHandle(STD_INPUT_HANDLE); | |
2498 else | |
2499 create_conin(); | |
2500 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2501 |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2502 #ifdef FEAT_RESTORE_ORIG_SCREEN |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2503 /* Save the initial console buffer for later restoration */ |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2504 SaveConsoleBuffer(&g_cbOrig); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2505 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2506 #else |
7 | 2507 /* Get current text attributes */ |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2508 GetConsoleScreenBufferInfo(g_hConOut, &csbi); |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2509 g_attrCurrent = g_attrDefault = csbi.wAttributes; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
2510 #endif |
7 | 2511 if (cterm_normal_fg_color == 0) |
2512 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1; | |
2513 if (cterm_normal_bg_color == 0) | |
2514 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1; | |
2515 | |
2516 /* set termcap codes to current text attributes */ | |
2517 update_tcap(g_attrCurrent); | |
2518 | |
2519 GetConsoleCursorInfo(g_hConOut, &g_cci); | |
2520 GetConsoleMode(g_hConIn, &g_cmodein); | |
2521 GetConsoleMode(g_hConOut, &g_cmodeout); | |
2522 | |
2523 #ifdef FEAT_TITLE | |
2524 SaveConsoleTitleAndIcon(); | |
2525 /* | |
2526 * Set both the small and big icons of the console window to Vim's icon. | |
2527 * Note that Vim presently only has one size of icon (32x32), but it | |
2528 * automatically gets scaled down to 16x16 when setting the small icon. | |
2529 */ | |
2530 if (g_fCanChangeIcon) | |
2531 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon); | |
2532 #endif | |
2533 | |
2534 ui_get_shellsize(); | |
2535 | |
2536 #ifdef MCH_WRITE_DUMP | |
2537 fdDump = fopen("dump", "wt"); | |
2538 | |
2539 if (fdDump) | |
2540 { | |
2541 time_t t; | |
2542 | |
2543 time(&t); | |
2544 fputs(ctime(&t), fdDump); | |
2545 fflush(fdDump); | |
2546 } | |
2547 #endif | |
2548 | |
2549 g_fWindInitCalled = TRUE; | |
2550 | |
2551 #ifdef FEAT_MOUSE | |
2552 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); | |
2553 #endif | |
2554 | |
2555 #ifdef FEAT_CLIPBOARD | |
4168 | 2556 win_clip_init(); |
7 | 2557 #endif |
2558 | |
2559 /* This will be NULL on anything but NT 4.0 */ | |
2560 s_pfnGetConsoleKeyboardLayoutName = | |
2561 (PFNGCKLN) GetProcAddress(GetModuleHandle("kernel32.dll"), | |
2562 "GetConsoleKeyboardLayoutNameA"); | |
2563 } | |
2564 | |
2565 /* | |
2566 * non-GUI version of mch_exit(). | |
2567 * Shut down and exit with status `r' | |
2568 * Careful: mch_exit() may be called before mch_init()! | |
2569 */ | |
2570 void | |
2571 mch_exit(int r) | |
2572 { | |
2573 stoptermcap(); | |
2574 | |
2575 if (g_fWindInitCalled) | |
2576 settmode(TMODE_COOK); | |
2577 | |
2578 ml_close_all(TRUE); /* remove all memfiles */ | |
2579 | |
2580 if (g_fWindInitCalled) | |
2581 { | |
2582 #ifdef FEAT_TITLE | |
2583 mch_restore_title(3); | |
2584 /* | |
2585 * Restore both the small and big icons of the console window to | |
2586 * what they were at startup. Don't do this when the window is | |
2587 * closed, Vim would hang here. | |
2588 */ | |
2589 if (g_fCanChangeIcon && !g_fForceExit) | |
2590 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon); | |
2591 #endif | |
2592 | |
2593 #ifdef MCH_WRITE_DUMP | |
2594 if (fdDump) | |
2595 { | |
2596 time_t t; | |
2597 | |
2598 time(&t); | |
2599 fputs(ctime(&t), fdDump); | |
2600 fclose(fdDump); | |
2601 } | |
2602 fdDump = NULL; | |
2603 #endif | |
2604 } | |
2605 | |
2606 SetConsoleCursorInfo(g_hConOut, &g_cci); | |
2607 SetConsoleMode(g_hConIn, g_cmodein); | |
2608 SetConsoleMode(g_hConOut, g_cmodeout); | |
2609 | |
2610 #ifdef DYNAMIC_GETTEXT | |
2611 dyn_libintl_end(); | |
2612 #endif | |
2613 | |
2614 exit(r); | |
2615 } | |
2616 #endif /* !FEAT_GUI_W32 */ | |
2617 | |
2618 /* | |
2619 * Do we have an interactive window? | |
2620 */ | |
323 | 2621 /*ARGSUSED*/ |
7 | 2622 int |
2623 mch_check_win( | |
2624 int argc, | |
2625 char **argv) | |
2626 { | |
2627 get_exe_name(); | |
2628 | |
2629 #ifdef FEAT_GUI_W32 | |
2630 return OK; /* GUI always has a tty */ | |
2631 #else | |
2632 if (isatty(1)) | |
2633 return OK; | |
2634 return FAIL; | |
2635 #endif | |
2636 } | |
2637 | |
2638 | |
5326 | 2639 #ifdef FEAT_MBYTE |
2640 /* | |
2641 * fname_casew(): Wide version of fname_case(). Set the case of the file name, | |
2642 * if it already exists. When "len" is > 0, also expand short to long | |
2643 * filenames. | |
2644 * Return FAIL if wide functions are not available, OK otherwise. | |
2645 * NOTE: much of this is identical to fname_case(), keep in sync! | |
2646 */ | |
2647 static int | |
2648 fname_casew( | |
2649 WCHAR *name, | |
2650 int len) | |
2651 { | |
2652 WCHAR szTrueName[_MAX_PATH + 2]; | |
2653 WCHAR szTrueNameTemp[_MAX_PATH + 2]; | |
2654 WCHAR *ptrue, *ptruePrev; | |
2655 WCHAR *porig, *porigPrev; | |
2656 int flen; | |
2657 WIN32_FIND_DATAW fb; | |
5529 | 2658 HANDLE hFind = INVALID_HANDLE_VALUE; |
5326 | 2659 int c; |
2660 int slen; | |
2661 | |
2662 flen = (int)wcslen(name); | |
2663 if (flen > _MAX_PATH) | |
2664 return OK; | |
2665 | |
2666 /* slash_adjust(name) not needed, already adjusted by fname_case(). */ | |
2667 | |
2668 /* Build the new name in szTrueName[] one component at a time. */ | |
2669 porig = name; | |
2670 ptrue = szTrueName; | |
2671 | |
2672 if (iswalpha(porig[0]) && porig[1] == L':') | |
2673 { | |
2674 /* copy leading drive letter */ | |
2675 *ptrue++ = *porig++; | |
2676 *ptrue++ = *porig++; | |
2677 } | |
5529 | 2678 *ptrue = NUL; /* in case nothing follows */ |
5326 | 2679 |
2680 while (*porig != NUL) | |
2681 { | |
2682 /* copy \ characters */ | |
2683 while (*porig == psepc) | |
2684 *ptrue++ = *porig++; | |
2685 | |
2686 ptruePrev = ptrue; | |
2687 porigPrev = porig; | |
2688 while (*porig != NUL && *porig != psepc) | |
2689 { | |
2690 *ptrue++ = *porig++; | |
2691 } | |
2692 *ptrue = NUL; | |
2693 | |
2694 /* To avoid a slow failure append "\*" when searching a directory, | |
2695 * server or network share. */ | |
2696 wcscpy(szTrueNameTemp, szTrueName); | |
2697 slen = (int)wcslen(szTrueNameTemp); | |
2698 if (*porig == psepc && slen + 2 < _MAX_PATH) | |
2699 wcscpy(szTrueNameTemp + slen, L"\\*"); | |
2700 | |
2701 /* Skip "", "." and "..". */ | |
2702 if (ptrue > ptruePrev | |
2703 && (ptruePrev[0] != L'.' | |
2704 || (ptruePrev[1] != NUL | |
2705 && (ptruePrev[1] != L'.' || ptruePrev[2] != NUL))) | |
2706 && (hFind = FindFirstFileW(szTrueNameTemp, &fb)) | |
2707 != INVALID_HANDLE_VALUE) | |
2708 { | |
2709 c = *porig; | |
2710 *porig = NUL; | |
2711 | |
2712 /* Only use the match when it's the same name (ignoring case) or | |
2713 * expansion is allowed and there is a match with the short name | |
2714 * and there is enough room. */ | |
2715 if (_wcsicoll(porigPrev, fb.cFileName) == 0 | |
2716 || (len > 0 | |
2717 && (_wcsicoll(porigPrev, fb.cAlternateFileName) == 0 | |
2718 && (int)(ptruePrev - szTrueName) | |
2719 + (int)wcslen(fb.cFileName) < len))) | |
2720 { | |
2721 wcscpy(ptruePrev, fb.cFileName); | |
2722 | |
2723 /* Look for exact match and prefer it if found. Must be a | |
2724 * long name, otherwise there would be only one match. */ | |
2725 while (FindNextFileW(hFind, &fb)) | |
2726 { | |
2727 if (*fb.cAlternateFileName != NUL | |
2728 && (wcscoll(porigPrev, fb.cFileName) == 0 | |
2729 || (len > 0 | |
2730 && (_wcsicoll(porigPrev, | |
2731 fb.cAlternateFileName) == 0 | |
2732 && (int)(ptruePrev - szTrueName) | |
2733 + (int)wcslen(fb.cFileName) < len)))) | |
2734 { | |
2735 wcscpy(ptruePrev, fb.cFileName); | |
2736 break; | |
2737 } | |
2738 } | |
2739 } | |
2740 FindClose(hFind); | |
2741 *porig = c; | |
2742 ptrue = ptruePrev + wcslen(ptruePrev); | |
2743 } | |
2744 else if (hFind == INVALID_HANDLE_VALUE | |
2745 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
2746 return FAIL; | |
2747 } | |
2748 | |
2749 wcscpy(name, szTrueName); | |
2750 return OK; | |
2751 } | |
2752 #endif | |
2753 | |
7 | 2754 /* |
2755 * fname_case(): Set the case of the file name, if it already exists. | |
2756 * When "len" is > 0, also expand short to long filenames. | |
5326 | 2757 * NOTE: much of this is identical to fname_casew(), keep in sync! |
7 | 2758 */ |
2759 void | |
2760 fname_case( | |
2761 char_u *name, | |
2762 int len) | |
2763 { | |
2764 char szTrueName[_MAX_PATH + 2]; | |
2604 | 2765 char szTrueNameTemp[_MAX_PATH + 2]; |
7 | 2766 char *ptrue, *ptruePrev; |
2767 char *porig, *porigPrev; | |
2768 int flen; | |
2769 WIN32_FIND_DATA fb; | |
2770 HANDLE hFind; | |
2771 int c; | |
2604 | 2772 int slen; |
7 | 2773 |
434 | 2774 flen = (int)STRLEN(name); |
5326 | 2775 if (flen == 0) |
7 | 2776 return; |
2777 | |
2778 slash_adjust(name); | |
2779 | |
5326 | 2780 #ifdef FEAT_MBYTE |
2781 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
2782 { | |
2783 WCHAR *p = enc_to_utf16(name, NULL); | |
2784 | |
2785 if (p != NULL) | |
2786 { | |
2787 char_u *q; | |
6262 | 2788 WCHAR buf[_MAX_PATH + 1]; |
2789 | |
2790 wcsncpy(buf, p, _MAX_PATH); | |
2791 buf[_MAX_PATH] = L'\0'; | |
5326 | 2792 vim_free(p); |
2793 | |
2794 if (fname_casew(buf, (len > 0) ? _MAX_PATH : 0) == OK) | |
2795 { | |
2796 q = utf16_to_enc(buf, NULL); | |
2797 if (q != NULL) | |
2798 { | |
2799 vim_strncpy(name, q, (len > 0) ? len - 1 : flen); | |
2800 vim_free(q); | |
2801 return; | |
2802 } | |
2803 } | |
2804 } | |
2805 /* Retry with non-wide function (for Windows 98). */ | |
2806 } | |
2807 #endif | |
2808 | |
2809 /* If 'enc' is utf-8, flen can be larger than _MAX_PATH. | |
2810 * So we should check this after calling wide function. */ | |
2811 if (flen > _MAX_PATH) | |
2812 return; | |
2813 | |
7 | 2814 /* Build the new name in szTrueName[] one component at a time. */ |
2815 porig = name; | |
2816 ptrue = szTrueName; | |
2817 | |
2818 if (isalpha(porig[0]) && porig[1] == ':') | |
2819 { | |
2820 /* copy leading drive letter */ | |
2821 *ptrue++ = *porig++; | |
2822 *ptrue++ = *porig++; | |
2823 } | |
5529 | 2824 *ptrue = NUL; /* in case nothing follows */ |
7 | 2825 |
2826 while (*porig != NUL) | |
2827 { | |
2828 /* copy \ characters */ | |
2829 while (*porig == psepc) | |
2830 *ptrue++ = *porig++; | |
2831 | |
2832 ptruePrev = ptrue; | |
2833 porigPrev = porig; | |
2834 while (*porig != NUL && *porig != psepc) | |
2835 { | |
2836 #ifdef FEAT_MBYTE | |
2837 int l; | |
2838 | |
2839 if (enc_dbcs) | |
2840 { | |
474 | 2841 l = (*mb_ptr2len)(porig); |
7 | 2842 while (--l >= 0) |
2843 *ptrue++ = *porig++; | |
2844 } | |
2845 else | |
2846 #endif | |
2847 *ptrue++ = *porig++; | |
2848 } | |
2849 *ptrue = NUL; | |
2850 | |
2604 | 2851 /* To avoid a slow failure append "\*" when searching a directory, |
2852 * server or network share. */ | |
2853 STRCPY(szTrueNameTemp, szTrueName); | |
2615 | 2854 slen = (int)strlen(szTrueNameTemp); |
2604 | 2855 if (*porig == psepc && slen + 2 < _MAX_PATH) |
2856 STRCPY(szTrueNameTemp + slen, "\\*"); | |
2857 | |
7 | 2858 /* Skip "", "." and "..". */ |
2859 if (ptrue > ptruePrev | |
2860 && (ptruePrev[0] != '.' | |
2861 || (ptruePrev[1] != NUL | |
2862 && (ptruePrev[1] != '.' || ptruePrev[2] != NUL))) | |
2604 | 2863 && (hFind = FindFirstFile(szTrueNameTemp, &fb)) |
7 | 2864 != INVALID_HANDLE_VALUE) |
2865 { | |
2866 c = *porig; | |
2867 *porig = NUL; | |
2868 | |
2869 /* Only use the match when it's the same name (ignoring case) or | |
2870 * expansion is allowed and there is a match with the short name | |
2871 * and there is enough room. */ | |
2872 if (_stricoll(porigPrev, fb.cFileName) == 0 | |
2873 || (len > 0 | |
2874 && (_stricoll(porigPrev, fb.cAlternateFileName) == 0 | |
2875 && (int)(ptruePrev - szTrueName) | |
2876 + (int)strlen(fb.cFileName) < len))) | |
2877 { | |
2878 STRCPY(ptruePrev, fb.cFileName); | |
2879 | |
2880 /* Look for exact match and prefer it if found. Must be a | |
2881 * long name, otherwise there would be only one match. */ | |
2882 while (FindNextFile(hFind, &fb)) | |
2883 { | |
2884 if (*fb.cAlternateFileName != NUL | |
2885 && (strcoll(porigPrev, fb.cFileName) == 0 | |
2886 || (len > 0 | |
2887 && (_stricoll(porigPrev, | |
2888 fb.cAlternateFileName) == 0 | |
2889 && (int)(ptruePrev - szTrueName) | |
2890 + (int)strlen(fb.cFileName) < len)))) | |
2891 { | |
2892 STRCPY(ptruePrev, fb.cFileName); | |
2893 break; | |
2894 } | |
2895 } | |
2896 } | |
2897 FindClose(hFind); | |
2898 *porig = c; | |
2899 ptrue = ptruePrev + strlen(ptruePrev); | |
2900 } | |
2901 } | |
2902 | |
2903 STRCPY(name, szTrueName); | |
2904 } | |
2905 | |
2906 | |
2907 /* | |
2908 * Insert user name in s[len]. | |
2909 */ | |
2910 int | |
2911 mch_get_user_name( | |
26 | 2912 char_u *s, |
2913 int len) | |
7 | 2914 { |
1414 | 2915 char szUserName[256 + 1]; /* UNLEN is 256 */ |
7 | 2916 DWORD cch = sizeof szUserName; |
2917 | |
5549 | 2918 #ifdef FEAT_MBYTE |
2919 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
2920 { | |
2921 WCHAR wszUserName[256 + 1]; /* UNLEN is 256 */ | |
2922 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR); | |
2923 | |
2924 if (GetUserNameW(wszUserName, &wcch)) | |
2925 { | |
2926 char_u *p = utf16_to_enc(wszUserName, NULL); | |
2927 | |
2928 if (p != NULL) | |
2929 { | |
2930 vim_strncpy(s, p, len - 1); | |
2931 vim_free(p); | |
2932 return OK; | |
2933 } | |
2934 } | |
5657 | 2935 else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
2936 return FAIL; | |
5549 | 2937 /* Retry with non-wide function (for Windows 98). */ |
2938 } | |
2939 #endif | |
7 | 2940 if (GetUserName(szUserName, &cch)) |
2941 { | |
417 | 2942 vim_strncpy(s, szUserName, len - 1); |
7 | 2943 return OK; |
2944 } | |
2945 s[0] = NUL; | |
2946 return FAIL; | |
2947 } | |
2948 | |
2949 | |
2950 /* | |
2951 * Insert host name in s[len]. | |
2952 */ | |
2953 void | |
2954 mch_get_host_name( | |
2955 char_u *s, | |
2956 int len) | |
2957 { | |
2958 DWORD cch = len; | |
2959 | |
5551 | 2960 #ifdef FEAT_MBYTE |
2961 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
2962 { | |
2963 WCHAR wszHostName[256 + 1]; | |
2964 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR); | |
2965 | |
2966 if (GetComputerNameW(wszHostName, &wcch)) | |
2967 { | |
2968 char_u *p = utf16_to_enc(wszHostName, NULL); | |
2969 | |
2970 if (p != NULL) | |
2971 { | |
2972 vim_strncpy(s, p, len - 1); | |
2973 vim_free(p); | |
2974 return; | |
2975 } | |
2976 } | |
5657 | 2977 else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
2978 return; | |
5551 | 2979 /* Retry with non-wide function (for Windows 98). */ |
2980 } | |
2981 #endif | |
7 | 2982 if (!GetComputerName(s, &cch)) |
417 | 2983 vim_strncpy(s, "PC (Win32 Vim)", len - 1); |
7 | 2984 } |
2985 | |
2986 | |
2987 /* | |
2988 * return process ID | |
2989 */ | |
2990 long | |
26 | 2991 mch_get_pid(void) |
7 | 2992 { |
2993 return (long)GetCurrentProcessId(); | |
2994 } | |
2995 | |
2996 | |
2997 /* | |
2998 * Get name of current directory into buffer 'buf' of length 'len' bytes. | |
2999 * Return OK for success, FAIL for failure. | |
3000 */ | |
3001 int | |
3002 mch_dirname( | |
3003 char_u *buf, | |
3004 int len) | |
3005 { | |
3006 /* | |
3007 * Originally this was: | |
3008 * return (getcwd(buf, len) != NULL ? OK : FAIL); | |
3009 * But the Win32s known bug list says that getcwd() doesn't work | |
3010 * so use the Win32 system call instead. <Negri> | |
3011 */ | |
3012 #ifdef FEAT_MBYTE | |
3013 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
3014 { | |
3015 WCHAR wbuf[_MAX_PATH + 1]; | |
3016 | |
3017 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0) | |
3018 { | |
1752 | 3019 char_u *p = utf16_to_enc(wbuf, NULL); |
7 | 3020 |
3021 if (p != NULL) | |
3022 { | |
417 | 3023 vim_strncpy(buf, p, len - 1); |
7 | 3024 vim_free(p); |
3025 return OK; | |
3026 } | |
3027 } | |
5657 | 3028 else if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
3029 return FAIL; | |
7 | 3030 /* Retry with non-wide function (for Windows 98). */ |
3031 } | |
3032 #endif | |
3033 return (GetCurrentDirectory(len, buf) != 0 ? OK : FAIL); | |
3034 } | |
3035 | |
3036 /* | |
5494 | 3037 * Get file permissions for "name". |
3038 * Return mode_t or -1 for error. | |
7 | 3039 */ |
3040 long | |
26 | 3041 mch_getperm(char_u *name) |
7 | 3042 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3043 struct stat st; |
5494 | 3044 int n; |
3045 | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3046 n = mch_stat(name, &st); |
5588 | 3047 return n == 0 ? (long)(unsigned short)st.st_mode : -1L; |
7 | 3048 } |
3049 | |
3050 | |
3051 /* | |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3052 * Set file permission for "name" to "perm". |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3053 * |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3054 * Return FAIL for failure, OK otherwise. |
7 | 3055 */ |
3056 int | |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3057 mch_setperm(char_u *name, long perm) |
7 | 3058 { |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3059 long n = -1; |
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3060 |
7 | 3061 #ifdef FEAT_MBYTE |
3062 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
3063 { | |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3064 WCHAR *p = enc_to_utf16(name, NULL); |
7 | 3065 |
3066 if (p != NULL) | |
3067 { | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3068 n = _wchmod(p, perm); |
7 | 3069 vim_free(p); |
5657 | 3070 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
|
3071 return FAIL; |
7 | 3072 /* Retry with non-wide function (for Windows 98). */ |
3073 } | |
3074 } | |
5229
1261caf9bc51
updated for version 7.4a.040
Bram Moolenaar <bram@vim.org>
parents:
5112
diff
changeset
|
3075 if (n == -1) |
7 | 3076 #endif |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3077 n = _chmod(name, perm); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3078 if (n == -1) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3079 return FAIL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3080 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3081 win32_set_archive(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3082 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3083 return OK; |
7 | 3084 } |
3085 | |
3086 /* | |
3087 * Set hidden flag for "name". | |
3088 */ | |
3089 void | |
3090 mch_hide(char_u *name) | |
3091 { | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3092 int attrs = win32_getattrs(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3093 if (attrs == -1) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3094 return; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3095 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3096 attrs |= FILE_ATTRIBUTE_HIDDEN; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3097 win32_setattrs(name, attrs); |
7 | 3098 } |
3099 | |
3100 /* | |
3101 * return TRUE if "name" is a directory | |
3102 * return FALSE if "name" is not a directory or upon error | |
3103 */ | |
3104 int | |
3105 mch_isdir(char_u *name) | |
3106 { | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3107 int f = win32_getattrs(name); |
7 | 3108 |
3109 if (f == -1) | |
3110 return FALSE; /* file does not exist at all */ | |
3111 | |
3112 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0; | |
3113 } | |
3114 | |
3115 /* | |
2803 | 3116 * Create directory "name". |
3117 * Return 0 on success, -1 on error. | |
3118 */ | |
3119 int | |
3120 mch_mkdir(char_u *name) | |
3121 { | |
3122 #ifdef FEAT_MBYTE | |
3123 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
3124 { | |
3125 WCHAR *p; | |
3126 int retval; | |
3127 | |
3128 p = enc_to_utf16(name, NULL); | |
3129 if (p == NULL) | |
3130 return -1; | |
3131 retval = _wmkdir(p); | |
3132 vim_free(p); | |
3133 return retval; | |
3134 } | |
3135 #endif | |
3136 return _mkdir(name); | |
3137 } | |
3138 | |
3139 /* | |
696 | 3140 * Return TRUE if file "fname" has more than one link. |
3141 */ | |
3142 int | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3143 mch_is_hard_link(char_u *fname) |
696 | 3144 { |
2793 | 3145 BY_HANDLE_FILE_INFORMATION info; |
3146 | |
3147 return win32_fileinfo(fname, &info) == FILEINFO_OK | |
3148 && info.nNumberOfLinks > 1; | |
3149 } | |
3150 | |
3151 /* | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3152 * 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
|
3153 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3154 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3155 mch_is_symbolic_link(char_u *fname) |
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 HANDLE hFind; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3158 int res = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3159 WIN32_FIND_DATAA findDataA; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3160 DWORD fileFlags = 0, reparseTag = 0; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3161 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3162 WCHAR *wn = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3163 WIN32_FIND_DATAW findDataW; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3164 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3165 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3166 wn = enc_to_utf16(fname, NULL); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3167 if (wn != NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3168 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3169 hFind = FindFirstFileW(wn, &findDataW); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3170 vim_free(wn); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3171 if (hFind == INVALID_HANDLE_VALUE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3172 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
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 /* Retry with non-wide function (for Windows 98). */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3175 hFind = FindFirstFile(fname, &findDataA); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3176 if (hFind != INVALID_HANDLE_VALUE) |
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 fileFlags = findDataA.dwFileAttributes; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3179 reparseTag = findDataA.dwReserved0; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3180 } |
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 else |
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 fileFlags = findDataW.dwFileAttributes; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3185 reparseTag = findDataW.dwReserved0; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3186 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3187 } |
4930
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3188 else |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3189 #endif |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3190 { |
4930
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3191 hFind = FindFirstFile(fname, &findDataA); |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3192 if (hFind != INVALID_HANDLE_VALUE) |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3193 { |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3194 fileFlags = findDataA.dwFileAttributes; |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3195 reparseTag = findDataA.dwReserved0; |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
3196 } |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3197 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3198 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3199 if (hFind != INVALID_HANDLE_VALUE) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3200 FindClose(hFind); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3201 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3202 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3203 && reparseTag == IO_REPARSE_TAG_SYMLINK) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3204 res = TRUE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3205 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3206 return res; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3207 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3208 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3209 /* |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3210 * 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
|
3211 * link. |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3212 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3213 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3214 mch_is_linked(char_u *fname) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3215 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3216 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
|
3217 return TRUE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3218 return FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3219 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3220 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3221 /* |
2793 | 3222 * Get the by-handle-file-information for "fname". |
3223 * Returns FILEINFO_OK when OK. | |
3224 * returns FILEINFO_ENC_FAIL when enc_to_utf16() failed. | |
3225 * Returns FILEINFO_READ_FAIL when CreateFile() failed. | |
3226 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed. | |
3227 */ | |
3228 int | |
3229 win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info) | |
3230 { | |
696 | 3231 HANDLE hFile; |
2793 | 3232 int res = FILEINFO_READ_FAIL; |
696 | 3233 #ifdef FEAT_MBYTE |
3234 WCHAR *wn = NULL; | |
3235 | |
3236 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
2793 | 3237 { |
1752 | 3238 wn = enc_to_utf16(fname, NULL); |
2793 | 3239 if (wn == NULL) |
3240 res = FILEINFO_ENC_FAIL; | |
3241 } | |
696 | 3242 if (wn != NULL) |
3243 { | |
3244 hFile = CreateFileW(wn, /* file name */ | |
3245 GENERIC_READ, /* access mode */ | |
2793 | 3246 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
696 | 3247 NULL, /* security descriptor */ |
3248 OPEN_EXISTING, /* creation disposition */ | |
2793 | 3249 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
696 | 3250 NULL); /* handle to template file */ |
3251 if (hFile == INVALID_HANDLE_VALUE | |
2793 | 3252 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
696 | 3253 { |
3254 /* Retry with non-wide function (for Windows 98). */ | |
3255 vim_free(wn); | |
3256 wn = NULL; | |
3257 } | |
3258 } | |
3259 if (wn == NULL) | |
3260 #endif | |
3261 hFile = CreateFile(fname, /* file name */ | |
3262 GENERIC_READ, /* access mode */ | |
2793 | 3263 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
696 | 3264 NULL, /* security descriptor */ |
3265 OPEN_EXISTING, /* creation disposition */ | |
2793 | 3266 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
696 | 3267 NULL); /* handle to template file */ |
3268 | |
3269 if (hFile != INVALID_HANDLE_VALUE) | |
3270 { | |
2793 | 3271 if (GetFileInformationByHandle(hFile, info) != 0) |
3272 res = FILEINFO_OK; | |
3273 else | |
3274 res = FILEINFO_INFO_FAIL; | |
696 | 3275 CloseHandle(hFile); |
3276 } | |
3277 | |
3278 #ifdef FEAT_MBYTE | |
3279 vim_free(wn); | |
3280 #endif | |
3281 return res; | |
3282 } | |
3283 | |
3284 /* | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3285 * get file attributes for `name' |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3286 * -1 : error |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3287 * else FILE_ATTRIBUTE_* defined in winnt.h |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3288 */ |
5494 | 3289 static int |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3290 win32_getattrs(char_u *name) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3291 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3292 int attr; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3293 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3294 WCHAR *p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3295 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3296 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3297 p = enc_to_utf16(name, NULL); |
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 if (p != NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3300 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3301 attr = GetFileAttributesW(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3302 if (attr < 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3303 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3304 /* Retry with non-wide function (for Windows 98). */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3305 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3306 p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3307 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3308 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3309 if (p == NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3310 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3311 attr = GetFileAttributes((char *)name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3312 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3313 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3314 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3315 return attr; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3316 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3317 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3318 /* |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3319 * set file attributes for `name' to `attrs' |
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 * return -1 for failure, 0 otherwise |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3322 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3323 static |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3324 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3325 win32_setattrs(char_u *name, int attrs) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3326 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3327 int res; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3328 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3329 WCHAR *p = NULL; |
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 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3332 p = enc_to_utf16(name, NULL); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3333 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3334 if (p != NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3335 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3336 res = SetFileAttributesW(p, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3337 if (res == FALSE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3338 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3339 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3340 /* Retry with non-wide function (for Windows 98). */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3341 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3342 p = NULL; |
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 if (p == NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3346 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3347 res = SetFileAttributes((char *)name, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3348 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3349 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3350 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3351 return res ? 0 : -1; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3352 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3353 |
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 * Set archive flag for "name". |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3356 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3357 static |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3358 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3359 win32_set_archive(char_u *name) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3360 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3361 int attrs = win32_getattrs(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3362 if (attrs == -1) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3363 return -1; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3364 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3365 attrs |= FILE_ATTRIBUTE_ARCHIVE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3366 return win32_setattrs(name, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3367 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3368 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3369 /* |
7 | 3370 * Return TRUE if file or directory "name" is writable (not readonly). |
3371 * Strange semantics of Win32: a readonly directory is writable, but you can't | |
3372 * delete a file. Let's say this means it is writable. | |
3373 */ | |
3374 int | |
3375 mch_writable(char_u *name) | |
3376 { | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3377 int attrs = win32_getattrs(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3378 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3379 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
3380 || (attrs & FILE_ATTRIBUTE_DIRECTORY))); |
7 | 3381 } |
3382 | |
3383 /* | |
3384 * Return 1 if "name" can be executed, 0 if not. | |
6700 | 3385 * If "use_path" is FALSE only check if "name" is executable. |
7 | 3386 * Return -1 if unknown. |
3387 */ | |
3388 int | |
6700 | 3389 mch_can_exe(char_u *name, char_u **path, int use_path) |
7 | 3390 { |
10 | 3391 char_u buf[_MAX_PATH]; |
835 | 3392 int len = (int)STRLEN(name); |
10 | 3393 char_u *p; |
3394 | |
3395 if (len >= _MAX_PATH) /* safety check */ | |
3396 return FALSE; | |
6700 | 3397 if (!use_path) |
3398 { | |
3399 /* TODO: check if file is really executable. */ | |
3400 return mch_getperm(name) != -1 && !mch_isdir(name); | |
3401 } | |
10 | 3402 |
3403 /* If there already is an extension try using the name directly. Also do | |
3404 * this with a Unix-shell like 'shell'. */ | |
3405 if (vim_strchr(gettail(name), '.') != NULL | |
3406 || strstr((char *)gettail(p_sh), "sh") != NULL) | |
5782 | 3407 if (executable_exists((char *)name, path)) |
10 | 3408 return TRUE; |
3409 | |
3410 /* | |
3411 * Loop over all extensions in $PATHEXT. | |
3412 */ | |
417 | 3413 vim_strncpy(buf, name, _MAX_PATH - 1); |
10 | 3414 p = mch_getenv("PATHEXT"); |
3415 if (p == NULL) | |
3416 p = (char_u *)".com;.exe;.bat;.cmd"; | |
3417 while (*p) | |
3418 { | |
3419 if (p[0] == '.' && (p[1] == NUL || p[1] == ';')) | |
3420 { | |
3421 /* A single "." means no extension is added. */ | |
3422 buf[len] = NUL; | |
3423 ++p; | |
3424 if (*p) | |
3425 ++p; | |
3426 } | |
3427 else | |
3428 copy_option_part(&p, buf + len, _MAX_PATH - len, ";"); | |
5782 | 3429 if (executable_exists((char *)buf, path)) |
10 | 3430 return TRUE; |
3431 } | |
3432 return FALSE; | |
7 | 3433 } |
3434 | |
3435 /* | |
3436 * Check what "name" is: | |
3437 * NODE_NORMAL: file or directory (or doesn't exist) | |
3438 * NODE_WRITABLE: writable device, socket, fifo, etc. | |
3439 * NODE_OTHER: non-writable things | |
3440 */ | |
3441 int | |
3442 mch_nodetype(char_u *name) | |
3443 { | |
3444 HANDLE hFile; | |
3445 int type; | |
5324 | 3446 #ifdef FEAT_MBYTE |
3447 WCHAR *wn = NULL; | |
3448 #endif | |
7 | 3449 |
1004 | 3450 /* We can't open a file with a name "\\.\con" or "\\.\prn" and trying to |
3451 * read from it later will cause Vim to hang. Thus return NODE_WRITABLE | |
3452 * here. */ | |
3453 if (STRNCMP(name, "\\\\.\\", 4) == 0) | |
3454 return NODE_WRITABLE; | |
3455 | |
5324 | 3456 #ifdef FEAT_MBYTE |
3457 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
3458 { | |
3459 wn = enc_to_utf16(name, NULL); | |
3460 if (wn != NULL) | |
3461 { | |
3462 hFile = CreateFileW(wn, /* file name */ | |
3463 GENERIC_WRITE, /* access mode */ | |
3464 0, /* share mode */ | |
3465 NULL, /* security descriptor */ | |
3466 OPEN_EXISTING, /* creation disposition */ | |
3467 0, /* file attributes */ | |
3468 NULL); /* handle to template file */ | |
3469 if (hFile == INVALID_HANDLE_VALUE | |
3470 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
3471 { | |
3472 /* Retry with non-wide function (for Windows 98). */ | |
3473 vim_free(wn); | |
3474 wn = NULL; | |
3475 } | |
3476 } | |
3477 } | |
3478 if (wn == NULL) | |
3479 #endif | |
3480 hFile = CreateFile(name, /* file name */ | |
3481 GENERIC_WRITE, /* access mode */ | |
3482 0, /* share mode */ | |
3483 NULL, /* security descriptor */ | |
3484 OPEN_EXISTING, /* creation disposition */ | |
3485 0, /* file attributes */ | |
3486 NULL); /* handle to template file */ | |
3487 | |
3488 #ifdef FEAT_MBYTE | |
3489 vim_free(wn); | |
3490 #endif | |
7 | 3491 if (hFile == INVALID_HANDLE_VALUE) |
3492 return NODE_NORMAL; | |
3493 | |
3494 type = GetFileType(hFile); | |
3495 CloseHandle(hFile); | |
3496 if (type == FILE_TYPE_CHAR) | |
3497 return NODE_WRITABLE; | |
3498 if (type == FILE_TYPE_DISK) | |
3499 return NODE_NORMAL; | |
3500 return NODE_OTHER; | |
3501 } | |
3502 | |
3503 #ifdef HAVE_ACL | |
3504 struct my_acl | |
3505 { | |
3506 PSECURITY_DESCRIPTOR pSecurityDescriptor; | |
3507 PSID pSidOwner; | |
3508 PSID pSidGroup; | |
3509 PACL pDacl; | |
3510 PACL pSacl; | |
3511 }; | |
3512 #endif | |
3513 | |
3514 /* | |
3515 * Return a pointer to the ACL of file "fname" in allocated memory. | |
3516 * Return NULL if the ACL is not available for whatever reason. | |
3517 */ | |
3518 vim_acl_T | |
26 | 3519 mch_get_acl(char_u *fname) |
7 | 3520 { |
3521 #ifndef HAVE_ACL | |
3522 return (vim_acl_T)NULL; | |
3523 #else | |
3524 struct my_acl *p = NULL; | |
5047
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3525 DWORD err; |
7 | 3526 |
3527 /* This only works on Windows NT and 2000. */ | |
3528 if (g_PlatformId == VER_PLATFORM_WIN32_NT && advapi_lib != NULL) | |
3529 { | |
3530 p = (struct my_acl *)alloc_clear((unsigned)sizeof(struct my_acl)); | |
3531 if (p != NULL) | |
3532 { | |
5047
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3533 # ifdef FEAT_MBYTE |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3534 WCHAR *wn = NULL; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3535 |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3536 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3537 wn = enc_to_utf16(fname, NULL); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3538 if (wn != NULL) |
7 | 3539 { |
5047
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3540 /* Try to retrieve the entire security descriptor. */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3541 err = pGetNamedSecurityInfoW( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3542 wn, // Abstract filename |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3543 SE_FILE_OBJECT, // File Object |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3544 OWNER_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3545 GROUP_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3546 DACL_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3547 SACL_SECURITY_INFORMATION, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3548 &p->pSidOwner, // Ownership information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3549 &p->pSidGroup, // Group membership. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3550 &p->pDacl, // Discretionary information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3551 &p->pSacl, // For auditing purposes. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3552 &p->pSecurityDescriptor); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3553 if (err == ERROR_ACCESS_DENIED || |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3554 err == ERROR_PRIVILEGE_NOT_HELD) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3555 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3556 /* Retrieve only DACL. */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3557 (void)pGetNamedSecurityInfoW( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3558 wn, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3559 SE_FILE_OBJECT, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3560 DACL_SECURITY_INFORMATION, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3561 NULL, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3562 NULL, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3563 &p->pDacl, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3564 NULL, |
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 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3567 if (p->pSecurityDescriptor == NULL) |
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 mch_free_acl((vim_acl_T)p); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3570 p = NULL; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3571 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3572 vim_free(wn); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3573 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3574 else |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3575 # endif |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3576 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3577 /* Try to retrieve the entire security descriptor. */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3578 err = pGetNamedSecurityInfo( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3579 (LPSTR)fname, // Abstract filename |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3580 SE_FILE_OBJECT, // File Object |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3581 OWNER_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3582 GROUP_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3583 DACL_SECURITY_INFORMATION | |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3584 SACL_SECURITY_INFORMATION, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3585 &p->pSidOwner, // Ownership information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3586 &p->pSidGroup, // Group membership. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3587 &p->pDacl, // Discretionary information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3588 &p->pSacl, // For auditing purposes. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3589 &p->pSecurityDescriptor); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3590 if (err == ERROR_ACCESS_DENIED || |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3591 err == ERROR_PRIVILEGE_NOT_HELD) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3592 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3593 /* Retrieve only DACL. */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3594 (void)pGetNamedSecurityInfo( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3595 (LPSTR)fname, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3596 SE_FILE_OBJECT, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3597 DACL_SECURITY_INFORMATION, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3598 NULL, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3599 NULL, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3600 &p->pDacl, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3601 NULL, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3602 &p->pSecurityDescriptor); |
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 if (p->pSecurityDescriptor == NULL) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3605 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3606 mch_free_acl((vim_acl_T)p); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3607 p = NULL; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3608 } |
7 | 3609 } |
3610 } | |
3611 } | |
3612 | |
3613 return (vim_acl_T)p; | |
3614 #endif | |
3615 } | |
3616 | |
5047
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3617 #ifdef HAVE_ACL |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3618 /* |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3619 * Check if "acl" contains inherited ACE. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3620 */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3621 static BOOL |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3622 is_acl_inherited(PACL acl) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3623 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3624 DWORD i; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3625 ACL_SIZE_INFORMATION acl_info; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3626 PACCESS_ALLOWED_ACE ace; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3627 |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3628 acl_info.AceCount = 0; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3629 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3630 for (i = 0; i < acl_info.AceCount; i++) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3631 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3632 GetAce(acl, i, (LPVOID *)&ace); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3633 if (ace->Header.AceFlags & INHERITED_ACE) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3634 return TRUE; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3635 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3636 return FALSE; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3637 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3638 #endif |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3639 |
7 | 3640 /* |
3641 * Set the ACL of file "fname" to "acl" (unless it's NULL). | |
3642 * Errors are ignored. | |
3643 * This must only be called with "acl" equal to what mch_get_acl() returned. | |
3644 */ | |
3645 void | |
26 | 3646 mch_set_acl(char_u *fname, vim_acl_T acl) |
7 | 3647 { |
3648 #ifdef HAVE_ACL | |
3649 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
|
3650 SECURITY_INFORMATION sec_info = 0; |
7 | 3651 |
3652 if (p != NULL && advapi_lib != NULL) | |
5047
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3653 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3654 # ifdef FEAT_MBYTE |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3655 WCHAR *wn = NULL; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3656 # endif |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3657 |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3658 /* Set security flags */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3659 if (p->pSidOwner) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3660 sec_info |= OWNER_SECURITY_INFORMATION; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3661 if (p->pSidGroup) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3662 sec_info |= GROUP_SECURITY_INFORMATION; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3663 if (p->pDacl) |
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 sec_info |= DACL_SECURITY_INFORMATION; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3666 /* Do not inherit its parent's DACL. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3667 * 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
|
3668 */ |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3669 if (!is_acl_inherited(p->pDacl)) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3670 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3671 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3672 if (p->pSacl) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3673 sec_info |= SACL_SECURITY_INFORMATION; |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3674 |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3675 # ifdef FEAT_MBYTE |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3676 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3677 wn = enc_to_utf16(fname, NULL); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3678 if (wn != NULL) |
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 (void)pSetNamedSecurityInfoW( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3681 wn, // Abstract filename |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3682 SE_FILE_OBJECT, // File Object |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3683 sec_info, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3684 p->pSidOwner, // Ownership information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3685 p->pSidGroup, // Group membership. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3686 p->pDacl, // Discretionary information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3687 p->pSacl // For auditing purposes. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3688 ); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3689 vim_free(wn); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3690 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3691 else |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3692 # endif |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3693 { |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3694 (void)pSetNamedSecurityInfo( |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3695 (LPSTR)fname, // Abstract filename |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3696 SE_FILE_OBJECT, // File Object |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3697 sec_info, |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3698 p->pSidOwner, // Ownership information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3699 p->pSidGroup, // Group membership. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3700 p->pDacl, // Discretionary information. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3701 p->pSacl // For auditing purposes. |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3702 ); |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3703 } |
cabdcfe72dc3
updated for version 7.3.1267
Bram Moolenaar <bram@vim.org>
parents:
4930
diff
changeset
|
3704 } |
7 | 3705 #endif |
3706 } | |
3707 | |
3708 void | |
26 | 3709 mch_free_acl(vim_acl_T acl) |
7 | 3710 { |
3711 #ifdef HAVE_ACL | |
3712 struct my_acl *p = (struct my_acl *)acl; | |
3713 | |
3714 if (p != NULL) | |
3715 { | |
3716 LocalFree(p->pSecurityDescriptor); // Free the memory just in case | |
3717 vim_free(p); | |
3718 } | |
3719 #endif | |
3720 } | |
3721 | |
3722 #ifndef FEAT_GUI_W32 | |
3723 | |
3724 /* | |
3725 * handler for ctrl-break, ctrl-c interrupts, and fatal events. | |
3726 */ | |
3727 static BOOL WINAPI | |
3728 handler_routine( | |
3729 DWORD dwCtrlType) | |
3730 { | |
3731 switch (dwCtrlType) | |
3732 { | |
3733 case CTRL_C_EVENT: | |
3734 if (ctrl_c_interrupts) | |
3735 g_fCtrlCPressed = TRUE; | |
3736 return TRUE; | |
3737 | |
3738 case CTRL_BREAK_EVENT: | |
3739 g_fCBrkPressed = TRUE; | |
3740 return TRUE; | |
3741 | |
3742 /* fatal events: shut down gracefully */ | |
3743 case CTRL_CLOSE_EVENT: | |
3744 case CTRL_LOGOFF_EVENT: | |
3745 case CTRL_SHUTDOWN_EVENT: | |
3746 windgoto((int)Rows - 1, 0); | |
3747 g_fForceExit = TRUE; | |
3748 | |
1569 | 3749 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"), |
7 | 3750 (dwCtrlType == CTRL_CLOSE_EVENT |
3751 ? _("close") | |
3752 : dwCtrlType == CTRL_LOGOFF_EVENT | |
3753 ? _("logoff") | |
3754 : _("shutdown"))); | |
3755 #ifdef DEBUG | |
3756 OutputDebugString(IObuff); | |
3757 #endif | |
3758 | |
3759 preserve_exit(); /* output IObuff, preserve files and exit */ | |
3760 | |
3761 return TRUE; /* not reached */ | |
3762 | |
3763 default: | |
3764 return FALSE; | |
3765 } | |
3766 } | |
3767 | |
3768 | |
3769 /* | |
3770 * set the tty in (raw) ? "raw" : "cooked" mode | |
3771 */ | |
3772 void | |
26 | 3773 mch_settmode(int tmode) |
7 | 3774 { |
3775 DWORD cmodein; | |
3776 DWORD cmodeout; | |
3777 BOOL bEnableHandler; | |
3778 | |
3779 GetConsoleMode(g_hConIn, &cmodein); | |
3780 GetConsoleMode(g_hConOut, &cmodeout); | |
3781 if (tmode == TMODE_RAW) | |
3782 { | |
3783 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | | |
3784 ENABLE_ECHO_INPUT); | |
3785 #ifdef FEAT_MOUSE | |
3786 if (g_fMouseActive) | |
3787 cmodein |= ENABLE_MOUSE_INPUT; | |
3788 #endif | |
3789 cmodeout &= ~(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); | |
3790 bEnableHandler = TRUE; | |
3791 } | |
3792 else /* cooked */ | |
3793 { | |
3794 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | | |
3795 ENABLE_ECHO_INPUT); | |
3796 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); | |
3797 bEnableHandler = FALSE; | |
3798 } | |
3799 SetConsoleMode(g_hConIn, cmodein); | |
3800 SetConsoleMode(g_hConOut, cmodeout); | |
3801 SetConsoleCtrlHandler(handler_routine, bEnableHandler); | |
3802 | |
3803 #ifdef MCH_WRITE_DUMP | |
3804 if (fdDump) | |
3805 { | |
3806 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n", | |
3807 tmode == TMODE_RAW ? "raw" : | |
3808 tmode == TMODE_COOK ? "cooked" : "normal", | |
3809 cmodein, cmodeout); | |
3810 fflush(fdDump); | |
3811 } | |
3812 #endif | |
3813 } | |
3814 | |
3815 | |
3816 /* | |
3817 * Get the size of the current window in `Rows' and `Columns' | |
3818 * Return OK when size could be determined, FAIL otherwise. | |
3819 */ | |
3820 int | |
26 | 3821 mch_get_shellsize(void) |
7 | 3822 { |
3823 CONSOLE_SCREEN_BUFFER_INFO csbi; | |
3824 | |
3825 if (!g_fTermcapMode && g_cbTermcap.IsValid) | |
3826 { | |
3827 /* | |
3828 * For some reason, we are trying to get the screen dimensions | |
3829 * even though we are not in termcap mode. The 'Rows' and 'Columns' | |
3830 * variables are really intended to mean the size of Vim screen | |
3831 * while in termcap mode. | |
3832 */ | |
3833 Rows = g_cbTermcap.Info.dwSize.Y; | |
3834 Columns = g_cbTermcap.Info.dwSize.X; | |
3835 } | |
3836 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
3837 { | |
3838 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; | |
3839 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; | |
3840 } | |
3841 else | |
3842 { | |
3843 Rows = 25; | |
3844 Columns = 80; | |
3845 } | |
3846 return OK; | |
3847 } | |
3848 | |
3849 /* | |
3850 * Set a console window to `xSize' * `ySize' | |
3851 */ | |
3852 static void | |
3853 ResizeConBufAndWindow( | |
26 | 3854 HANDLE hConsole, |
3855 int xSize, | |
3856 int ySize) | |
7 | 3857 { |
3858 CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */ | |
3859 SMALL_RECT srWindowRect; /* hold the new console size */ | |
3860 COORD coordScreen; | |
3861 | |
3862 #ifdef MCH_WRITE_DUMP | |
3863 if (fdDump) | |
3864 { | |
3865 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize); | |
3866 fflush(fdDump); | |
3867 } | |
3868 #endif | |
3869 | |
3870 /* get the largest size we can size the console window to */ | |
3871 coordScreen = GetLargestConsoleWindowSize(hConsole); | |
3872 | |
3873 /* define the new console window size and scroll position */ | |
3874 srWindowRect.Left = srWindowRect.Top = (SHORT) 0; | |
3875 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1); | |
3876 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1); | |
3877 | |
3878 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
3879 { | |
3880 int sx, sy; | |
3881 | |
3882 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1; | |
3883 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; | |
3884 if (sy < ySize || sx < xSize) | |
3885 { | |
3886 /* | |
3887 * Increasing number of lines/columns, do buffer first. | |
3888 * Use the maximal size in x and y direction. | |
3889 */ | |
3890 if (sy < ySize) | |
3891 coordScreen.Y = ySize; | |
3892 else | |
3893 coordScreen.Y = sy; | |
3894 if (sx < xSize) | |
3895 coordScreen.X = xSize; | |
3896 else | |
3897 coordScreen.X = sx; | |
3898 SetConsoleScreenBufferSize(hConsole, coordScreen); | |
3899 } | |
3900 } | |
3901 | |
3902 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &srWindowRect)) | |
3903 { | |
3904 #ifdef MCH_WRITE_DUMP | |
3905 if (fdDump) | |
3906 { | |
3907 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n", | |
3908 GetLastError()); | |
3909 fflush(fdDump); | |
3910 } | |
3911 #endif | |
3912 } | |
3913 | |
3914 /* define the new console buffer size */ | |
3915 coordScreen.X = xSize; | |
3916 coordScreen.Y = ySize; | |
3917 | |
3918 if (!SetConsoleScreenBufferSize(hConsole, coordScreen)) | |
3919 { | |
3920 #ifdef MCH_WRITE_DUMP | |
3921 if (fdDump) | |
3922 { | |
3923 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n", | |
3924 GetLastError()); | |
3925 fflush(fdDump); | |
3926 } | |
3927 #endif | |
3928 } | |
3929 } | |
3930 | |
3931 | |
3932 /* | |
3933 * Set the console window to `Rows' * `Columns' | |
3934 */ | |
3935 void | |
26 | 3936 mch_set_shellsize(void) |
7 | 3937 { |
3938 COORD coordScreen; | |
3939 | |
3940 /* Don't change window size while still starting up */ | |
3941 if (suppress_winsize != 0) | |
3942 { | |
3943 suppress_winsize = 2; | |
3944 return; | |
3945 } | |
3946 | |
3947 if (term_console) | |
3948 { | |
3949 coordScreen = GetLargestConsoleWindowSize(g_hConOut); | |
3950 | |
3951 /* Clamp Rows and Columns to reasonable values */ | |
3952 if (Rows > coordScreen.Y) | |
3953 Rows = coordScreen.Y; | |
3954 if (Columns > coordScreen.X) | |
3955 Columns = coordScreen.X; | |
3956 | |
3957 ResizeConBufAndWindow(g_hConOut, Columns, Rows); | |
3958 } | |
3959 } | |
3960 | |
3961 /* | |
3962 * Rows and/or Columns has changed. | |
3963 */ | |
3964 void | |
26 | 3965 mch_new_shellsize(void) |
7 | 3966 { |
3967 set_scroll_region(0, 0, Columns - 1, Rows - 1); | |
3968 } | |
3969 | |
3970 | |
3971 /* | |
3972 * Called when started up, to set the winsize that was delayed. | |
3973 */ | |
3974 void | |
26 | 3975 mch_set_winsize_now(void) |
7 | 3976 { |
3977 if (suppress_winsize == 2) | |
3978 { | |
3979 suppress_winsize = 0; | |
3980 mch_set_shellsize(); | |
3981 shell_resized(); | |
3982 } | |
3983 suppress_winsize = 0; | |
3984 } | |
3985 #endif /* FEAT_GUI_W32 */ | |
3986 | |
5547 | 3987 static BOOL |
3988 vim_create_process( | |
5556 | 3989 char *cmd, |
5569 | 3990 BOOL inherit_handles, |
5547 | 3991 DWORD flags, |
3992 STARTUPINFO *si, | |
3993 PROCESS_INFORMATION *pi) | |
3994 { | |
3995 # ifdef FEAT_MBYTE | |
3996 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
3997 { | |
3998 WCHAR *wcmd = enc_to_utf16(cmd, NULL); | |
3999 | |
4000 if (wcmd != NULL) | |
4001 { | |
4002 BOOL ret; | |
4003 ret = CreateProcessW( | |
4004 NULL, /* Executable name */ | |
4005 wcmd, /* Command to execute */ | |
4006 NULL, /* Process security attributes */ | |
4007 NULL, /* Thread security attributes */ | |
4008 inherit_handles, /* Inherit handles */ | |
4009 flags, /* Creation flags */ | |
4010 NULL, /* Environment */ | |
4011 NULL, /* Current directory */ | |
5556 | 4012 (LPSTARTUPINFOW)si, /* Startup information */ |
5547 | 4013 pi); /* Process information */ |
4014 vim_free(wcmd); | |
4015 return ret; | |
4016 } | |
4017 } | |
4018 #endif | |
4019 return CreateProcess( | |
4020 NULL, /* Executable name */ | |
4021 cmd, /* Command to execute */ | |
4022 NULL, /* Process security attributes */ | |
4023 NULL, /* Thread security attributes */ | |
4024 inherit_handles, /* Inherit handles */ | |
4025 flags, /* Creation flags */ | |
4026 NULL, /* Environment */ | |
4027 NULL, /* Current directory */ | |
4028 si, /* Startup information */ | |
4029 pi); /* Process information */ | |
4030 } | |
7 | 4031 |
4032 | |
4033 #if defined(FEAT_GUI_W32) || defined(PROTO) | |
4034 | |
4035 /* | |
4036 * Specialised version of system() for Win32 GUI mode. | |
4037 * This version proceeds as follows: | |
4038 * 1. Create a console window for use by the subprocess | |
4039 * 2. Run the subprocess (it gets the allocated console by default) | |
4040 * 3. Wait for the subprocess to terminate and get its exit code | |
4041 * 4. Prompt the user to press a key to close the console window | |
4042 */ | |
4043 static int | |
2935 | 4044 mch_system_classic(char *cmd, int options) |
7 | 4045 { |
4046 STARTUPINFO si; | |
4047 PROCESS_INFORMATION pi; | |
4048 DWORD ret = 0; | |
4049 HWND hwnd = GetFocus(); | |
4050 | |
4051 si.cb = sizeof(si); | |
4052 si.lpReserved = NULL; | |
4053 si.lpDesktop = NULL; | |
4054 si.lpTitle = NULL; | |
4055 si.dwFlags = STARTF_USESHOWWINDOW; | |
4056 /* | |
4057 * It's nicer to run a filter command in a minimized window, but in | |
4058 * Windows 95 this makes the command MUCH slower. We can't do it under | |
4059 * Win32s either as it stops the synchronous spawn workaround working. | |
2643 | 4060 * Don't activate the window to keep focus on Vim. |
7 | 4061 */ |
4062 if ((options & SHELL_DOOUT) && !mch_windows95() && !gui_is_win32s()) | |
2643 | 4063 si.wShowWindow = SW_SHOWMINNOACTIVE; |
7 | 4064 else |
4065 si.wShowWindow = SW_SHOWNORMAL; | |
4066 si.cbReserved2 = 0; | |
4067 si.lpReserved2 = NULL; | |
4068 | |
4069 /* There is a strange error on Windows 95 when using "c:\\command.com". | |
4070 * When the "c:\\" is left out it works OK...? */ | |
4071 if (mch_windows95() | |
4072 && (STRNICMP(cmd, "c:/command.com", 14) == 0 | |
4073 || STRNICMP(cmd, "c:\\command.com", 14) == 0)) | |
4074 cmd += 3; | |
4075 | |
4076 /* Now, run the command */ | |
5547 | 4077 vim_create_process(cmd, FALSE, |
4078 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE, &si, &pi); | |
7 | 4079 |
4080 /* Wait for the command to terminate before continuing */ | |
4081 if (g_PlatformId != VER_PLATFORM_WIN32s) | |
4082 { | |
4083 #ifdef FEAT_GUI | |
4084 int delay = 1; | |
4085 | |
4086 /* Keep updating the window while waiting for the shell to finish. */ | |
4087 for (;;) | |
4088 { | |
4089 MSG msg; | |
4090 | |
3010 | 4091 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
7 | 4092 { |
4093 TranslateMessage(&msg); | |
3010 | 4094 pDispatchMessage(&msg); |
3720 | 4095 delay = 1; |
4096 continue; | |
7 | 4097 } |
4098 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) | |
4099 break; | |
4100 | |
4101 /* We start waiting for a very short time and then increase it, so | |
4102 * that we respond quickly when the process is quick, and don't | |
4103 * consume too much overhead when it's slow. */ | |
4104 if (delay < 50) | |
4105 delay += 10; | |
4106 } | |
4107 #else | |
4108 WaitForSingleObject(pi.hProcess, INFINITE); | |
4109 #endif | |
4110 | |
4111 /* Get the command exit code */ | |
4112 GetExitCodeProcess(pi.hProcess, &ret); | |
4113 } | |
4114 else | |
4115 { | |
4116 /* | |
4117 * This ugly code is the only quick way of performing | |
4118 * a synchronous spawn under Win32s. Yuk. | |
4119 */ | |
4120 num_windows = 0; | |
4121 EnumWindows(win32ssynch_cb, 0); | |
4122 old_num_windows = num_windows; | |
4123 do | |
4124 { | |
4125 Sleep(1000); | |
4126 num_windows = 0; | |
4127 EnumWindows(win32ssynch_cb, 0); | |
4128 } while (num_windows == old_num_windows); | |
4129 ret = 0; | |
4130 } | |
4131 | |
4132 /* Close the handles to the subprocess, so that it goes away */ | |
4133 CloseHandle(pi.hThread); | |
4134 CloseHandle(pi.hProcess); | |
4135 | |
4136 /* Try to get input focus back. Doesn't always work though. */ | |
4137 PostMessage(hwnd, WM_SETFOCUS, 0, 0); | |
4138 | |
4139 return ret; | |
4140 } | |
2935 | 4141 |
4142 /* | |
4143 * Thread launched by the gui to send the current buffer data to the | |
4144 * process. This way avoid to hang up vim totally if the children | |
4145 * process take a long time to process the lines. | |
4146 */ | |
4147 static DWORD WINAPI | |
4148 sub_process_writer(LPVOID param) | |
4149 { | |
4150 HANDLE g_hChildStd_IN_Wr = param; | |
4151 linenr_T lnum = curbuf->b_op_start.lnum; | |
4152 DWORD len = 0; | |
4153 DWORD l; | |
4154 char_u *lp = ml_get(lnum); | |
4155 char_u *s; | |
4156 int written = 0; | |
4157 | |
4158 for (;;) | |
4159 { | |
4160 l = (DWORD)STRLEN(lp + written); | |
4161 if (l == 0) | |
4162 len = 0; | |
4163 else if (lp[written] == NL) | |
4164 { | |
4165 /* NL -> NUL translation */ | |
4166 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL); | |
4167 } | |
4168 else | |
4169 { | |
4170 s = vim_strchr(lp + written, NL); | |
4171 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written, | |
4172 s == NULL ? l : (DWORD)(s - (lp + written)), | |
4173 &len, NULL); | |
4174 } | |
4175 if (len == (int)l) | |
4176 { | |
4177 /* Finished a line, add a NL, unless this line should not have | |
4178 * one. */ | |
4179 if (lnum != curbuf->b_op_end.lnum | |
6933 | 4180 || (!curbuf->b_p_bin |
4181 && curbuf->b_p_fixeol) | |
2935 | 4182 || (lnum != curbuf->b_no_eol_lnum |
4183 && (lnum != curbuf->b_ml.ml_line_count | |
4184 || curbuf->b_p_eol))) | |
4185 { | |
4238 | 4186 WriteFile(g_hChildStd_IN_Wr, "\n", 1, (LPDWORD)&ignored, NULL); |
2935 | 4187 } |
4188 | |
4189 ++lnum; | |
4190 if (lnum > curbuf->b_op_end.lnum) | |
4191 break; | |
4192 | |
4193 lp = ml_get(lnum); | |
4194 written = 0; | |
4195 } | |
4196 else if (len > 0) | |
4197 written += len; | |
4198 } | |
4199 | |
4200 /* finished all the lines, close pipe */ | |
4201 CloseHandle(g_hChildStd_IN_Wr); | |
4202 ExitThread(0); | |
4203 } | |
4204 | |
4205 | |
4206 # define BUFLEN 100 /* length for buffer, stolen from unix version */ | |
4207 | |
4208 /* | |
4209 * This function read from the children's stdout and write the | |
4210 * data on screen or in the buffer accordingly. | |
4211 */ | |
4212 static void | |
4213 dump_pipe(int options, | |
4214 HANDLE g_hChildStd_OUT_Rd, | |
4215 garray_T *ga, | |
4216 char_u buffer[], | |
4217 DWORD *buffer_off) | |
4218 { | |
4219 DWORD availableBytes = 0; | |
4220 DWORD i; | |
4221 int ret; | |
4222 DWORD len; | |
4223 DWORD toRead; | |
4224 int repeatCount; | |
4225 | |
4226 /* we query the pipe to see if there is any data to read | |
4227 * to avoid to perform a blocking read */ | |
4228 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, /* pipe to query */ | |
4229 NULL, /* optional buffer */ | |
3622 | 4230 0, /* buffer size */ |
2935 | 4231 NULL, /* number of read bytes */ |
4232 &availableBytes, /* available bytes total */ | |
4233 NULL); /* byteLeft */ | |
4234 | |
4235 repeatCount = 0; | |
4236 /* We got real data in the pipe, read it */ | |
3622 | 4237 while (ret != 0 && availableBytes > 0) |
2935 | 4238 { |
4239 repeatCount++; | |
4240 toRead = | |
4241 # ifdef FEAT_MBYTE | |
4242 (DWORD)(BUFLEN - *buffer_off); | |
4243 # else | |
4244 (DWORD)BUFLEN; | |
4245 # endif | |
4246 toRead = availableBytes < toRead ? availableBytes : toRead; | |
4247 ReadFile(g_hChildStd_OUT_Rd, buffer | |
4248 # ifdef FEAT_MBYTE | |
4249 + *buffer_off, toRead | |
4250 # else | |
4251 , toRead | |
4252 # endif | |
4253 , &len, NULL); | |
4254 | |
4255 /* If we haven't read anything, there is a problem */ | |
4256 if (len == 0) | |
4257 break; | |
4258 | |
4259 availableBytes -= len; | |
4260 | |
4261 if (options & SHELL_READ) | |
4262 { | |
4263 /* Do NUL -> NL translation, append NL separated | |
4264 * lines to the current buffer. */ | |
4265 for (i = 0; i < len; ++i) | |
4266 { | |
4267 if (buffer[i] == NL) | |
4268 append_ga_line(ga); | |
4269 else if (buffer[i] == NUL) | |
4270 ga_append(ga, NL); | |
4271 else | |
4272 ga_append(ga, buffer[i]); | |
4273 } | |
4274 } | |
4275 # ifdef FEAT_MBYTE | |
4276 else if (has_mbyte) | |
4277 { | |
4278 int l; | |
3030 | 4279 int c; |
4280 char_u *p; | |
2935 | 4281 |
4282 len += *buffer_off; | |
4283 buffer[len] = NUL; | |
4284 | |
4285 /* Check if the last character in buffer[] is | |
4286 * incomplete, keep these bytes for the next | |
4287 * round. */ | |
4288 for (p = buffer; p < buffer + len; p += l) | |
4289 { | |
4290 l = mb_cptr2len(p); | |
4291 if (l == 0) | |
4292 l = 1; /* NUL byte? */ | |
4293 else if (MB_BYTE2LEN(*p) != l) | |
4294 break; | |
4295 } | |
4296 if (p == buffer) /* no complete character */ | |
4297 { | |
4298 /* avoid getting stuck at an illegal byte */ | |
4299 if (len >= 12) | |
4300 ++p; | |
4301 else | |
4302 { | |
4303 *buffer_off = len; | |
4304 return; | |
4305 } | |
4306 } | |
4307 c = *p; | |
4308 *p = NUL; | |
4309 msg_puts(buffer); | |
4310 if (p < buffer + len) | |
4311 { | |
4312 *p = c; | |
4313 *buffer_off = (DWORD)((buffer + len) - p); | |
4314 mch_memmove(buffer, p, *buffer_off); | |
4315 return; | |
4316 } | |
4317 *buffer_off = 0; | |
4318 } | |
4319 # endif /* FEAT_MBYTE */ | |
4320 else | |
4321 { | |
4322 buffer[len] = NUL; | |
4323 msg_puts(buffer); | |
4324 } | |
4325 | |
4326 windgoto(msg_row, msg_col); | |
4327 cursor_on(); | |
4328 out_flush(); | |
4329 } | |
4330 } | |
4331 | |
4332 /* | |
4333 * Version of system to use for windows NT > 5.0 (Win2K), use pipe | |
4334 * for communication and doesn't open any new window. | |
4335 */ | |
4336 static int | |
4337 mch_system_piped(char *cmd, int options) | |
4338 { | |
4339 STARTUPINFO si; | |
4340 PROCESS_INFORMATION pi; | |
4341 DWORD ret = 0; | |
4342 | |
4343 HANDLE g_hChildStd_IN_Rd = NULL; | |
4344 HANDLE g_hChildStd_IN_Wr = NULL; | |
4345 HANDLE g_hChildStd_OUT_Rd = NULL; | |
4346 HANDLE g_hChildStd_OUT_Wr = NULL; | |
4347 | |
4348 char_u buffer[BUFLEN + 1]; /* reading buffer + size */ | |
4349 DWORD len; | |
4350 | |
4351 /* buffer used to receive keys */ | |
4352 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ | |
4353 int ta_len = 0; /* valid bytes in ta_buf[] */ | |
4354 | |
4355 DWORD i; | |
4356 int c; | |
4357 int noread_cnt = 0; | |
4358 garray_T ga; | |
4359 int delay = 1; | |
4360 DWORD buffer_off = 0; /* valid bytes in buffer[] */ | |
3361 | 4361 char *p = NULL; |
2935 | 4362 |
4363 SECURITY_ATTRIBUTES saAttr; | |
4364 | |
4365 /* Set the bInheritHandle flag so pipe handles are inherited. */ | |
4366 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); | |
4367 saAttr.bInheritHandle = TRUE; | |
4368 saAttr.lpSecurityDescriptor = NULL; | |
4369 | |
4370 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) | |
4371 /* Ensure the read handle to the pipe for STDOUT is not inherited. */ | |
4372 || ! pSetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) | |
4373 /* Create a pipe for the child process's STDIN. */ | |
4374 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0) | |
4375 /* Ensure the write handle to the pipe for STDIN is not inherited. */ | |
4376 || ! pSetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) | |
4377 { | |
4378 CloseHandle(g_hChildStd_IN_Rd); | |
4379 CloseHandle(g_hChildStd_IN_Wr); | |
4380 CloseHandle(g_hChildStd_OUT_Rd); | |
4381 CloseHandle(g_hChildStd_OUT_Wr); | |
4382 MSG_PUTS(_("\nCannot create pipes\n")); | |
4383 } | |
4384 | |
4385 si.cb = sizeof(si); | |
4386 si.lpReserved = NULL; | |
4387 si.lpDesktop = NULL; | |
4388 si.lpTitle = NULL; | |
4389 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; | |
4390 | |
4391 /* set-up our file redirection */ | |
4392 si.hStdError = g_hChildStd_OUT_Wr; | |
4393 si.hStdOutput = g_hChildStd_OUT_Wr; | |
4394 si.hStdInput = g_hChildStd_IN_Rd; | |
4395 si.wShowWindow = SW_HIDE; | |
4396 si.cbReserved2 = 0; | |
4397 si.lpReserved2 = NULL; | |
4398 | |
4399 if (options & SHELL_READ) | |
4400 ga_init2(&ga, 1, BUFLEN); | |
4401 | |
3361 | 4402 if (cmd != NULL) |
4403 { | |
4404 p = (char *)vim_strsave((char_u *)cmd); | |
4405 if (p != NULL) | |
4406 unescape_shellxquote((char_u *)p, p_sxe); | |
4407 else | |
4408 p = cmd; | |
4409 } | |
4410 | |
5547 | 4411 /* Now, run the command. |
4412 * About "Inherit handles" being TRUE: this command can be litigious, | |
4413 * handle inheritance was deactivated for pending temp file, but, if we | |
4414 * deactivate it, the pipes don't work for some reason. */ | |
4415 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE, &si, &pi); | |
2935 | 4416 |
3361 | 4417 if (p != cmd) |
4418 vim_free(p); | |
2935 | 4419 |
4420 /* Close our unused side of the pipes */ | |
4421 CloseHandle(g_hChildStd_IN_Rd); | |
4422 CloseHandle(g_hChildStd_OUT_Wr); | |
4423 | |
4424 if (options & SHELL_WRITE) | |
4425 { | |
4426 HANDLE thread = | |
4427 CreateThread(NULL, /* security attributes */ | |
4428 0, /* default stack size */ | |
4429 sub_process_writer, /* function to be executed */ | |
4430 g_hChildStd_IN_Wr, /* parameter */ | |
4431 0, /* creation flag, start immediately */ | |
4432 NULL); /* we don't care about thread id */ | |
4433 CloseHandle(thread); | |
4434 g_hChildStd_IN_Wr = NULL; | |
4435 } | |
4436 | |
4437 /* Keep updating the window while waiting for the shell to finish. */ | |
4438 for (;;) | |
4439 { | |
4440 MSG msg; | |
4441 | |
5553 | 4442 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
2935 | 4443 { |
4444 TranslateMessage(&msg); | |
5553 | 4445 pDispatchMessage(&msg); |
2935 | 4446 } |
4447 | |
4448 /* write pipe information in the window */ | |
4449 if ((options & (SHELL_READ|SHELL_WRITE)) | |
4450 # ifdef FEAT_GUI | |
4451 || gui.in_use | |
4452 # endif | |
4453 ) | |
4454 { | |
4455 len = 0; | |
4456 if (!(options & SHELL_EXPAND) | |
4457 && ((options & | |
4458 (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) | |
4459 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) | |
4460 # ifdef FEAT_GUI | |
4461 || gui.in_use | |
4462 # endif | |
4463 ) | |
4464 && (ta_len > 0 || noread_cnt > 4)) | |
4465 { | |
4466 if (ta_len == 0) | |
4467 { | |
4468 /* Get extra characters when we don't have any. Reset the | |
4469 * counter and timer. */ | |
4470 noread_cnt = 0; | |
4471 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
4472 gettimeofday(&start_tv, NULL); | |
4473 # endif | |
4474 len = ui_inchar(ta_buf, BUFLEN, 10L, 0); | |
4475 } | |
4476 if (ta_len > 0 || len > 0) | |
4477 { | |
4478 /* | |
4479 * For pipes: Check for CTRL-C: send interrupt signal to | |
4480 * child. Check for CTRL-D: EOF, close pipe to child. | |
4481 */ | |
4482 if (len == 1 && cmd != NULL) | |
4483 { | |
4484 if (ta_buf[ta_len] == Ctrl_C) | |
4485 { | |
4486 /* Learn what exit code is expected, for | |
4487 * now put 9 as SIGKILL */ | |
4488 TerminateProcess(pi.hProcess, 9); | |
4489 } | |
4490 if (ta_buf[ta_len] == Ctrl_D) | |
4491 { | |
4492 CloseHandle(g_hChildStd_IN_Wr); | |
4493 g_hChildStd_IN_Wr = NULL; | |
4494 } | |
4495 } | |
4496 | |
4497 /* replace K_BS by <BS> and K_DEL by <DEL> */ | |
4498 for (i = ta_len; i < ta_len + len; ++i) | |
4499 { | |
4500 if (ta_buf[i] == CSI && len - i > 2) | |
4501 { | |
4502 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); | |
4503 if (c == K_DEL || c == K_KDEL || c == K_BS) | |
4504 { | |
4505 mch_memmove(ta_buf + i + 1, ta_buf + i + 3, | |
4506 (size_t)(len - i - 2)); | |
4507 if (c == K_DEL || c == K_KDEL) | |
4508 ta_buf[i] = DEL; | |
4509 else | |
4510 ta_buf[i] = Ctrl_H; | |
4511 len -= 2; | |
4512 } | |
4513 } | |
4514 else if (ta_buf[i] == '\r') | |
4515 ta_buf[i] = '\n'; | |
4516 # ifdef FEAT_MBYTE | |
4517 if (has_mbyte) | |
4518 i += (*mb_ptr2len_len)(ta_buf + i, | |
4519 ta_len + len - i) - 1; | |
4520 # endif | |
4521 } | |
4522 | |
4523 /* | |
4524 * For pipes: echo the typed characters. For a pty this | |
4525 * does not seem to work. | |
4526 */ | |
4527 for (i = ta_len; i < ta_len + len; ++i) | |
4528 { | |
4529 if (ta_buf[i] == '\n' || ta_buf[i] == '\b') | |
4530 msg_putchar(ta_buf[i]); | |
4531 # ifdef FEAT_MBYTE | |
4532 else if (has_mbyte) | |
4533 { | |
4534 int l = (*mb_ptr2len)(ta_buf + i); | |
4535 | |
4536 msg_outtrans_len(ta_buf + i, l); | |
4537 i += l - 1; | |
4538 } | |
4539 # endif | |
4540 else | |
4541 msg_outtrans_len(ta_buf + i, 1); | |
4542 } | |
4543 windgoto(msg_row, msg_col); | |
4544 out_flush(); | |
4545 | |
4546 ta_len += len; | |
4547 | |
4548 /* | |
4549 * Write the characters to the child, unless EOF has been | |
4550 * typed for pipes. Write one character at a time, to | |
4551 * avoid losing too much typeahead. When writing buffer | |
4552 * lines, drop the typed characters (only check for | |
4553 * CTRL-C). | |
4554 */ | |
4555 if (options & SHELL_WRITE) | |
4556 ta_len = 0; | |
4557 else if (g_hChildStd_IN_Wr != NULL) | |
4558 { | |
4559 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf, | |
4560 1, &len, NULL); | |
4561 // if we are typing in, we want to keep things reactive | |
4562 delay = 1; | |
4563 if (len > 0) | |
4564 { | |
4565 ta_len -= len; | |
4566 mch_memmove(ta_buf, ta_buf + len, ta_len); | |
4567 } | |
4568 } | |
4569 } | |
4570 } | |
4571 } | |
4572 | |
4573 if (ta_len) | |
4574 ui_inchar_undo(ta_buf, ta_len); | |
4575 | |
4576 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) | |
4577 { | |
3030 | 4578 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
2935 | 4579 break; |
4580 } | |
4581 | |
4582 ++noread_cnt; | |
3030 | 4583 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
2935 | 4584 |
4585 /* We start waiting for a very short time and then increase it, so | |
4586 * that we respond quickly when the process is quick, and don't | |
4587 * consume too much overhead when it's slow. */ | |
4588 if (delay < 50) | |
4589 delay += 10; | |
4590 } | |
4591 | |
4592 /* Close the pipe */ | |
4593 CloseHandle(g_hChildStd_OUT_Rd); | |
4594 if (g_hChildStd_IN_Wr != NULL) | |
4595 CloseHandle(g_hChildStd_IN_Wr); | |
4596 | |
4597 WaitForSingleObject(pi.hProcess, INFINITE); | |
4598 | |
4599 /* Get the command exit code */ | |
4600 GetExitCodeProcess(pi.hProcess, &ret); | |
4601 | |
4602 if (options & SHELL_READ) | |
4603 { | |
4604 if (ga.ga_len > 0) | |
4605 { | |
4606 append_ga_line(&ga); | |
4607 /* remember that the NL was missing */ | |
4608 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; | |
4609 } | |
4610 else | |
4611 curbuf->b_no_eol_lnum = 0; | |
4612 ga_clear(&ga); | |
4613 } | |
4614 | |
4615 /* Close the handles to the subprocess, so that it goes away */ | |
4616 CloseHandle(pi.hThread); | |
4617 CloseHandle(pi.hProcess); | |
4618 | |
4619 return ret; | |
4620 } | |
4621 | |
4622 static int | |
4623 mch_system(char *cmd, int options) | |
4624 { | |
4625 /* if we can pipe and the shelltemp option is off */ | |
4626 if (allowPiping && !p_stmp) | |
4627 return mch_system_piped(cmd, options); | |
4628 else | |
4629 return mch_system_classic(cmd, options); | |
4630 } | |
7 | 4631 #else |
4632 | |
5547 | 4633 # ifdef FEAT_MBYTE |
4634 static int | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
4635 mch_system(char *cmd, int options) |
5547 | 4636 { |
4637 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
4638 { | |
4639 WCHAR *wcmd = enc_to_utf16(cmd, NULL); | |
4640 if (wcmd != NULL) | |
4641 { | |
4642 int ret = _wsystem(wcmd); | |
4643 vim_free(wcmd); | |
4644 return ret; | |
4645 } | |
4646 } | |
4647 return system(cmd); | |
4648 } | |
4649 # else | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
4650 # define mch_system(c, o) system(c) |
5547 | 4651 # endif |
7 | 4652 |
4653 #endif | |
4654 | |
4655 /* | |
4656 * Either execute a command by calling the shell or start a new shell | |
4657 */ | |
4658 int | |
4659 mch_call_shell( | |
26 | 4660 char_u *cmd, |
4661 int options) /* SHELL_*, see vim.h */ | |
7 | 4662 { |
4663 int x = 0; | |
4664 int tmode = cur_tmode; | |
4665 #ifdef FEAT_TITLE | |
6293 | 4666 char szShellTitle[512]; |
6290 | 4667 # ifdef FEAT_MBYTE |
6293 | 4668 int did_set_title = FALSE; |
4669 | |
6290 | 4670 /* Change the title to reflect that we are in a subshell. */ |
4671 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
4672 { | |
4673 WCHAR szShellTitle[512]; | |
4674 | |
4675 if (GetConsoleTitleW(szShellTitle, | |
4676 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0) | |
4677 { | |
4678 if (cmd == NULL) | |
4679 wcscat(szShellTitle, L" :sh"); | |
4680 else | |
4681 { | |
4682 WCHAR *wn = enc_to_utf16(cmd, NULL); | |
4683 | |
4684 if (wn != NULL) | |
4685 { | |
4686 wcscat(szShellTitle, L" - !"); | |
4687 if ((wcslen(szShellTitle) + wcslen(wn) < | |
4688 sizeof(szShellTitle)/sizeof(WCHAR))) | |
4689 wcscat(szShellTitle, wn); | |
4690 SetConsoleTitleW(szShellTitle); | |
4691 vim_free(wn); | |
6293 | 4692 did_set_title = TRUE; |
6290 | 4693 } |
4694 } | |
4695 } | |
4696 } | |
6293 | 4697 if (!did_set_title) |
4698 # endif | |
4699 /* Change the title to reflect that we are in a subshell. */ | |
4700 if (GetConsoleTitle(szShellTitle, sizeof(szShellTitle) - 4) > 0) | |
7 | 4701 { |
6293 | 4702 if (cmd == NULL) |
4703 strcat(szShellTitle, " :sh"); | |
4704 else | |
4705 { | |
4706 strcat(szShellTitle, " - !"); | |
4707 if ((strlen(szShellTitle) + strlen(cmd) < sizeof(szShellTitle))) | |
4708 strcat(szShellTitle, cmd); | |
4709 } | |
4710 SetConsoleTitle(szShellTitle); | |
7 | 4711 } |
4712 #endif | |
4713 | |
4714 out_flush(); | |
4715 | |
4716 #ifdef MCH_WRITE_DUMP | |
4717 if (fdDump) | |
4718 { | |
4719 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options); | |
4720 fflush(fdDump); | |
4721 } | |
4722 #endif | |
4723 | |
4724 /* | |
4725 * Catch all deadly signals while running the external command, because a | |
4726 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us. | |
4727 */ | |
4728 signal(SIGINT, SIG_IGN); | |
4729 #if defined(__GNUC__) && !defined(__MINGW32__) | |
4730 signal(SIGKILL, SIG_IGN); | |
4731 #else | |
4732 signal(SIGBREAK, SIG_IGN); | |
4733 #endif | |
4734 signal(SIGILL, SIG_IGN); | |
4735 signal(SIGFPE, SIG_IGN); | |
4736 signal(SIGSEGV, SIG_IGN); | |
4737 signal(SIGTERM, SIG_IGN); | |
4738 signal(SIGABRT, SIG_IGN); | |
4739 | |
4740 if (options & SHELL_COOKED) | |
4741 settmode(TMODE_COOK); /* set to normal mode */ | |
4742 | |
4743 if (cmd == NULL) | |
4744 { | |
4745 x = mch_system(p_sh, options); | |
4746 } | |
4747 else | |
4748 { | |
4749 /* we use "command" or "cmd" to start the shell; slow but easy */ | |
3363 | 4750 char_u *newcmd = NULL; |
4751 char_u *cmdbase = cmd; | |
4752 long_u cmdlen; | |
3361 | 4753 |
4754 /* Skip a leading ", ( and "(. */ | |
4755 if (*cmdbase == '"' ) | |
4756 ++cmdbase; | |
4757 if (*cmdbase == '(') | |
4758 ++cmdbase; | |
4759 | |
4760 if ((STRNICMP(cmdbase, "start", 5) == 0) && vim_iswhite(cmdbase[5])) | |
4761 { | |
4762 STARTUPINFO si; | |
4763 PROCESS_INFORMATION pi; | |
4764 DWORD flags = CREATE_NEW_CONSOLE; | |
4765 char_u *p; | |
4766 | |
5627 | 4767 ZeroMemory(&si, sizeof(si)); |
3361 | 4768 si.cb = sizeof(si); |
4769 si.lpReserved = NULL; | |
4770 si.lpDesktop = NULL; | |
4771 si.lpTitle = NULL; | |
4772 si.dwFlags = 0; | |
4773 si.cbReserved2 = 0; | |
4774 si.lpReserved2 = NULL; | |
4775 | |
4776 cmdbase = skipwhite(cmdbase + 5); | |
4777 if ((STRNICMP(cmdbase, "/min", 4) == 0) | |
4778 && vim_iswhite(cmdbase[4])) | |
4779 { | |
4780 cmdbase = skipwhite(cmdbase + 4); | |
4781 si.dwFlags = STARTF_USESHOWWINDOW; | |
4782 si.wShowWindow = SW_SHOWMINNOACTIVE; | |
4783 } | |
4784 else if ((STRNICMP(cmdbase, "/b", 2) == 0) | |
4785 && vim_iswhite(cmdbase[2])) | |
4786 { | |
4787 cmdbase = skipwhite(cmdbase + 2); | |
4788 flags = CREATE_NO_WINDOW; | |
4789 si.dwFlags = STARTF_USESTDHANDLES; | |
4790 si.hStdInput = CreateFile("\\\\.\\NUL", // File name | |
3363 | 4791 GENERIC_READ, // Access flags |
3361 | 4792 0, // Share flags |
3363 | 4793 NULL, // Security att. |
4794 OPEN_EXISTING, // Open flags | |
4795 FILE_ATTRIBUTE_NORMAL, // File att. | |
4796 NULL); // Temp file | |
3361 | 4797 si.hStdOutput = si.hStdInput; |
4798 si.hStdError = si.hStdInput; | |
4799 } | |
4800 | |
4801 /* Remove a trailing ", ) and )" if they have a match | |
4802 * at the start of the command. */ | |
4803 if (cmdbase > cmd) | |
4804 { | |
4805 p = cmdbase + STRLEN(cmdbase); | |
4806 if (p > cmdbase && p[-1] == '"' && *cmd == '"') | |
4807 *--p = NUL; | |
4808 if (p > cmdbase && p[-1] == ')' | |
4809 && (*cmd =='(' || cmd[1] == '(')) | |
4810 *--p = NUL; | |
4811 } | |
4812 | |
3363 | 4813 newcmd = cmdbase; |
4814 unescape_shellxquote(cmdbase, p_sxe); | |
4815 | |
3361 | 4816 /* |
3363 | 4817 * If creating new console, arguments are passed to the |
4818 * 'cmd.exe' as-is. If it's not, arguments are not treated | |
4819 * correctly for current 'cmd.exe'. So unescape characters in | |
4820 * shellxescape except '|' for avoiding to be treated as | |
4821 * argument to them. Pass the arguments to sub-shell. | |
3361 | 4822 */ |
3363 | 4823 if (flags != CREATE_NEW_CONSOLE) |
4824 { | |
4825 char_u *subcmd; | |
3367 | 4826 char_u *cmd_shell = mch_getenv("COMSPEC"); |
4827 | |
4828 if (cmd_shell == NULL || *cmd_shell == NUL) | |
4829 cmd_shell = default_shell(); | |
3363 | 4830 |
4831 subcmd = vim_strsave_escaped_ext(cmdbase, "|", '^', FALSE); | |
4832 if (subcmd != NULL) | |
4833 { | |
4834 /* make "cmd.exe /c arguments" */ | |
4835 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5; | |
4836 newcmd = lalloc(cmdlen, TRUE); | |
4837 if (newcmd != NULL) | |
4838 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s", | |
3367 | 4839 cmd_shell, subcmd); |
3363 | 4840 else |
4841 newcmd = cmdbase; | |
3367 | 4842 vim_free(subcmd); |
3363 | 4843 } |
4844 } | |
3361 | 4845 |
4846 /* | |
4847 * Now, start the command as a process, so that it doesn't | |
4848 * inherit our handles which causes unpleasant dangling swap | |
4849 * files if we exit before the spawned process | |
4850 */ | |
5547 | 4851 if (vim_create_process(newcmd, FALSE, flags, &si, &pi)) |
3361 | 4852 x = 0; |
4853 else | |
4854 { | |
4855 x = -1; | |
4856 #ifdef FEAT_GUI_W32 | |
4857 EMSG(_("E371: Command not found")); | |
4858 #endif | |
4859 } | |
3363 | 4860 |
4861 if (newcmd != cmdbase) | |
4862 vim_free(newcmd); | |
4863 | |
5627 | 4864 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL) |
3361 | 4865 { |
5627 | 4866 /* Close the handle to \\.\NUL created above. */ |
3361 | 4867 CloseHandle(si.hStdInput); |
4868 } | |
4869 /* Close the handles to the subprocess, so that it goes away */ | |
4870 CloseHandle(pi.hThread); | |
4871 CloseHandle(pi.hProcess); | |
4872 } | |
4873 else | |
4874 { | |
3363 | 4875 cmdlen = ( |
7 | 4876 #ifdef FEAT_GUI_W32 |
2935 | 4877 (allowPiping && !p_stmp ? 0 : STRLEN(vimrun_path)) + |
7 | 4878 #endif |
1569 | 4879 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10); |
4880 | |
3361 | 4881 newcmd = lalloc(cmdlen, TRUE); |
4882 if (newcmd != NULL) | |
7 | 4883 { |
4884 #if defined(FEAT_GUI_W32) | |
4885 if (need_vimrun_warning) | |
4886 { | |
4887 MessageBox(NULL, | |
4888 _("VIMRUN.EXE not found in your $PATH.\n" | |
4889 "External commands will not pause after completion.\n" | |
4890 "See :help win32-vimrun for more information."), | |
4891 _("Vim Warning"), | |
4892 MB_ICONWARNING); | |
4893 need_vimrun_warning = FALSE; | |
4894 } | |
2935 | 4895 if (!s_dont_use_vimrun && (!allowPiping || p_stmp)) |
7 | 4896 /* Use vimrun to execute the command. It opens a console |
4897 * 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
|
4898 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s", |
7 | 4899 vimrun_path, |
4900 (msg_silent != 0 || (options & SHELL_DOOUT)) | |
4901 ? "-s " : "", | |
4902 p_sh, p_shcf, cmd); | |
4903 else | |
4904 #endif | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4905 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", |
1569 | 4906 p_sh, p_shcf, cmd); |
7 | 4907 x = mch_system((char *)newcmd, options); |
3361 | 4908 vim_free(newcmd); |
7 | 4909 } |
4910 } | |
4911 } | |
4912 | |
4913 if (tmode == TMODE_RAW) | |
4914 settmode(TMODE_RAW); /* set to raw mode */ | |
4915 | |
4916 /* Print the return value, unless "vimrun" was used. */ | |
4917 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent | |
4918 #if defined(FEAT_GUI_W32) | |
2935 | 4919 && ((options & SHELL_DOOUT) || s_dont_use_vimrun |
4920 || (allowPiping && !p_stmp)) | |
7 | 4921 #endif |
4922 ) | |
4923 { | |
4924 smsg(_("shell returned %d"), x); | |
4925 msg_putchar('\n'); | |
4926 } | |
4927 #ifdef FEAT_TITLE | |
4928 resettitle(); | |
4929 #endif | |
4930 | |
4931 signal(SIGINT, SIG_DFL); | |
4932 #if defined(__GNUC__) && !defined(__MINGW32__) | |
4933 signal(SIGKILL, SIG_DFL); | |
4934 #else | |
4935 signal(SIGBREAK, SIG_DFL); | |
4936 #endif | |
4937 signal(SIGILL, SIG_DFL); | |
4938 signal(SIGFPE, SIG_DFL); | |
4939 signal(SIGSEGV, SIG_DFL); | |
4940 signal(SIGTERM, SIG_DFL); | |
4941 signal(SIGABRT, SIG_DFL); | |
4942 | |
4943 return x; | |
4944 } | |
4945 | |
4946 | |
4947 #ifndef FEAT_GUI_W32 | |
4948 | |
4949 /* | |
4950 * Start termcap mode | |
4951 */ | |
4952 static void | |
4953 termcap_mode_start(void) | |
4954 { | |
4955 DWORD cmodein; | |
4956 | |
4957 if (g_fTermcapMode) | |
4958 return; | |
4959 | |
4960 SaveConsoleBuffer(&g_cbNonTermcap); | |
4961 | |
4962 if (g_cbTermcap.IsValid) | |
4963 { | |
4964 /* | |
4965 * We've been in termcap mode before. Restore certain screen | |
4966 * characteristics, including the buffer size and the window | |
4967 * size. Since we will be redrawing the screen, we don't need | |
4968 * to restore the actual contents of the buffer. | |
4969 */ | |
4970 RestoreConsoleBuffer(&g_cbTermcap, FALSE); | |
4971 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow); | |
4972 Rows = g_cbTermcap.Info.dwSize.Y; | |
4973 Columns = g_cbTermcap.Info.dwSize.X; | |
4974 } | |
4975 else | |
4976 { | |
4977 /* | |
4978 * This is our first time entering termcap mode. Clear the console | |
4979 * screen buffer, and resize the buffer to match the current window | |
4980 * size. We will use this as the size of our editing environment. | |
4981 */ | |
4982 ClearConsoleBuffer(g_attrCurrent); | |
4983 ResizeConBufAndWindow(g_hConOut, Columns, Rows); | |
4984 } | |
4985 | |
4986 #ifdef FEAT_TITLE | |
4987 resettitle(); | |
4988 #endif | |
4989 | |
4990 GetConsoleMode(g_hConIn, &cmodein); | |
4991 #ifdef FEAT_MOUSE | |
4992 if (g_fMouseActive) | |
4993 cmodein |= ENABLE_MOUSE_INPUT; | |
4994 else | |
4995 cmodein &= ~ENABLE_MOUSE_INPUT; | |
4996 #endif | |
4997 cmodein |= ENABLE_WINDOW_INPUT; | |
4998 SetConsoleMode(g_hConIn, cmodein); | |
4999 | |
5000 redraw_later_clear(); | |
5001 g_fTermcapMode = TRUE; | |
5002 } | |
5003 | |
5004 | |
5005 /* | |
5006 * End termcap mode | |
5007 */ | |
5008 static void | |
5009 termcap_mode_end(void) | |
5010 { | |
5011 DWORD cmodein; | |
5012 ConsoleBuffer *cb; | |
5013 COORD coord; | |
5014 DWORD dwDummy; | |
5015 | |
5016 if (!g_fTermcapMode) | |
5017 return; | |
5018 | |
5019 SaveConsoleBuffer(&g_cbTermcap); | |
5020 | |
5021 GetConsoleMode(g_hConIn, &cmodein); | |
5022 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); | |
5023 SetConsoleMode(g_hConIn, cmodein); | |
5024 | |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
5025 #ifdef FEAT_RESTORE_ORIG_SCREEN |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
5026 cb = exiting ? &g_cbOrig : &g_cbNonTermcap; |
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
5027 #else |
7 | 5028 cb = &g_cbNonTermcap; |
7184
0a256475412f
commit https://github.com/vim/vim/commit/4c0aac57599092da404f6726e88701ba441a4a6d
Christian Brabandt <cb@256bit.org>
parents:
7150
diff
changeset
|
5029 #endif |
7 | 5030 RestoreConsoleBuffer(cb, p_rs); |
5031 SetConsoleCursorInfo(g_hConOut, &g_cci); | |
5032 | |
5033 if (p_rs || exiting) | |
5034 { | |
5035 /* | |
5036 * Clear anything that happens to be on the current line. | |
5037 */ | |
5038 coord.X = 0; | |
5039 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1)); | |
5040 FillConsoleOutputCharacter(g_hConOut, ' ', | |
5041 cb->Info.dwSize.X, coord, &dwDummy); | |
5042 /* | |
5043 * The following is just for aesthetics. If we are exiting without | |
5044 * restoring the screen, then we want to have a prompt string | |
5045 * appear at the bottom line. However, the command interpreter | |
5046 * seems to always advance the cursor one line before displaying | |
5047 * the prompt string, which causes the screen to scroll. To | |
5048 * counter this, move the cursor up one line before exiting. | |
5049 */ | |
5050 if (exiting && !p_rs) | |
5051 coord.Y--; | |
5052 /* | |
5053 * Position the cursor at the leftmost column of the desired row. | |
5054 */ | |
5055 SetConsoleCursorPosition(g_hConOut, coord); | |
5056 } | |
5057 | |
5058 g_fTermcapMode = FALSE; | |
5059 } | |
5060 #endif /* FEAT_GUI_W32 */ | |
5061 | |
5062 | |
5063 #ifdef FEAT_GUI_W32 | |
323 | 5064 /*ARGSUSED*/ |
7 | 5065 void |
5066 mch_write( | |
5067 char_u *s, | |
5068 int len) | |
5069 { | |
5070 /* never used */ | |
5071 } | |
5072 | |
5073 #else | |
5074 | |
5075 /* | |
5076 * clear `n' chars, starting from `coord' | |
5077 */ | |
5078 static void | |
5079 clear_chars( | |
5080 COORD coord, | |
5081 DWORD n) | |
5082 { | |
5083 DWORD dwDummy; | |
5084 | |
5085 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy); | |
5086 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy); | |
5087 } | |
5088 | |
5089 | |
5090 /* | |
5091 * Clear the screen | |
5092 */ | |
5093 static void | |
5094 clear_screen(void) | |
5095 { | |
5096 g_coord.X = g_coord.Y = 0; | |
5097 clear_chars(g_coord, Rows * Columns); | |
5098 } | |
5099 | |
5100 | |
5101 /* | |
5102 * Clear to end of display | |
5103 */ | |
5104 static void | |
5105 clear_to_end_of_display(void) | |
5106 { | |
5107 clear_chars(g_coord, (Rows - g_coord.Y - 1) | |
5108 * Columns + (Columns - g_coord.X)); | |
5109 } | |
5110 | |
5111 | |
5112 /* | |
5113 * Clear to end of line | |
5114 */ | |
5115 static void | |
5116 clear_to_end_of_line(void) | |
5117 { | |
5118 clear_chars(g_coord, Columns - g_coord.X); | |
5119 } | |
5120 | |
5121 | |
5122 /* | |
5123 * Scroll the scroll region up by `cLines' lines | |
5124 */ | |
5125 static void | |
26 | 5126 scroll(unsigned cLines) |
7 | 5127 { |
5128 COORD oldcoord = g_coord; | |
5129 | |
5130 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1); | |
5131 delete_lines(cLines); | |
5132 | |
5133 g_coord = oldcoord; | |
5134 } | |
5135 | |
5136 | |
5137 /* | |
5138 * Set the scroll region | |
5139 */ | |
5140 static void | |
5141 set_scroll_region( | |
5142 unsigned left, | |
5143 unsigned top, | |
5144 unsigned right, | |
5145 unsigned bottom) | |
5146 { | |
5147 if (left >= right | |
5148 || top >= bottom | |
5149 || right > (unsigned) Columns - 1 | |
5150 || bottom > (unsigned) Rows - 1) | |
5151 return; | |
5152 | |
5153 g_srScrollRegion.Left = left; | |
5154 g_srScrollRegion.Top = top; | |
5155 g_srScrollRegion.Right = right; | |
5156 g_srScrollRegion.Bottom = bottom; | |
5157 } | |
5158 | |
5159 | |
5160 /* | |
5161 * Insert `cLines' lines at the current cursor position | |
5162 */ | |
5163 static void | |
26 | 5164 insert_lines(unsigned cLines) |
7 | 5165 { |
5166 SMALL_RECT source; | |
5167 COORD dest; | |
5168 CHAR_INFO fill; | |
5169 | |
5170 dest.X = 0; | |
5171 dest.Y = g_coord.Y + cLines; | |
5172 | |
5173 source.Left = 0; | |
5174 source.Top = g_coord.Y; | |
5175 source.Right = g_srScrollRegion.Right; | |
5176 source.Bottom = g_srScrollRegion.Bottom - cLines; | |
5177 | |
5178 fill.Char.AsciiChar = ' '; | |
5179 fill.Attributes = g_attrCurrent; | |
5180 | |
5181 ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); | |
5182 | |
5183 /* Here we have to deal with a win32 console flake: If the scroll | |
5184 * region looks like abc and we scroll c to a and fill with d we get | |
5185 * cbd... if we scroll block c one line at a time to a, we get cdd... | |
5186 * vim expects cdd consistently... So we have to deal with that | |
5187 * here... (this also occurs scrolling the same way in the other | |
5188 * direction). */ | |
5189 | |
5190 if (source.Bottom < dest.Y) | |
5191 { | |
5192 COORD coord; | |
5193 | |
5194 coord.X = 0; | |
5195 coord.Y = source.Bottom; | |
5196 clear_chars(coord, Columns * (dest.Y - source.Bottom)); | |
5197 } | |
5198 } | |
5199 | |
5200 | |
5201 /* | |
5202 * Delete `cLines' lines at the current cursor position | |
5203 */ | |
5204 static void | |
26 | 5205 delete_lines(unsigned cLines) |
7 | 5206 { |
5207 SMALL_RECT source; | |
5208 COORD dest; | |
5209 CHAR_INFO fill; | |
5210 int nb; | |
5211 | |
5212 dest.X = 0; | |
5213 dest.Y = g_coord.Y; | |
5214 | |
5215 source.Left = 0; | |
5216 source.Top = g_coord.Y + cLines; | |
5217 source.Right = g_srScrollRegion.Right; | |
5218 source.Bottom = g_srScrollRegion.Bottom; | |
5219 | |
5220 fill.Char.AsciiChar = ' '; | |
5221 fill.Attributes = g_attrCurrent; | |
5222 | |
5223 ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); | |
5224 | |
5225 /* Here we have to deal with a win32 console flake: If the scroll | |
5226 * region looks like abc and we scroll c to a and fill with d we get | |
5227 * cbd... if we scroll block c one line at a time to a, we get cdd... | |
5228 * vim expects cdd consistently... So we have to deal with that | |
5229 * here... (this also occurs scrolling the same way in the other | |
5230 * direction). */ | |
5231 | |
5232 nb = dest.Y + (source.Bottom - source.Top) + 1; | |
5233 | |
5234 if (nb < source.Top) | |
5235 { | |
5236 COORD coord; | |
5237 | |
5238 coord.X = 0; | |
5239 coord.Y = nb; | |
5240 clear_chars(coord, Columns * (source.Top - nb)); | |
5241 } | |
5242 } | |
5243 | |
5244 | |
5245 /* | |
5246 * Set the cursor position | |
5247 */ | |
5248 static void | |
5249 gotoxy( | |
5250 unsigned x, | |
5251 unsigned y) | |
5252 { | |
5253 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows) | |
5254 return; | |
5255 | |
5256 /* external cursor coords are 1-based; internal are 0-based */ | |
5257 g_coord.X = x - 1; | |
5258 g_coord.Y = y - 1; | |
5259 SetConsoleCursorPosition(g_hConOut, g_coord); | |
5260 } | |
5261 | |
5262 | |
5263 /* | |
5264 * Set the current text attribute = (foreground | background) | |
5265 * See ../doc/os_win32.txt for the numbers. | |
5266 */ | |
5267 static void | |
26 | 5268 textattr(WORD wAttr) |
7 | 5269 { |
6710 | 5270 g_attrCurrent = wAttr & 0xff; |
7 | 5271 |
5272 SetConsoleTextAttribute(g_hConOut, wAttr); | |
5273 } | |
5274 | |
5275 | |
5276 static void | |
26 | 5277 textcolor(WORD wAttr) |
7 | 5278 { |
6710 | 5279 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f); |
7 | 5280 |
5281 SetConsoleTextAttribute(g_hConOut, g_attrCurrent); | |
5282 } | |
5283 | |
5284 | |
5285 static void | |
26 | 5286 textbackground(WORD wAttr) |
7 | 5287 { |
6710 | 5288 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4); |
7 | 5289 |
5290 SetConsoleTextAttribute(g_hConOut, g_attrCurrent); | |
5291 } | |
5292 | |
5293 | |
5294 /* | |
5295 * restore the default text attribute (whatever we started with) | |
5296 */ | |
5297 static void | |
26 | 5298 normvideo(void) |
7 | 5299 { |
5300 textattr(g_attrDefault); | |
5301 } | |
5302 | |
5303 | |
5304 static WORD g_attrPreStandout = 0; | |
5305 | |
5306 /* | |
5307 * Make the text standout, by brightening it | |
5308 */ | |
5309 static void | |
5310 standout(void) | |
5311 { | |
5312 g_attrPreStandout = g_attrCurrent; | |
5313 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY)); | |
5314 } | |
5315 | |
5316 | |
5317 /* | |
5318 * Turn off standout mode | |
5319 */ | |
5320 static void | |
26 | 5321 standend(void) |
7 | 5322 { |
5323 if (g_attrPreStandout) | |
5324 { | |
5325 textattr(g_attrPreStandout); | |
5326 g_attrPreStandout = 0; | |
5327 } | |
5328 } | |
5329 | |
5330 | |
5331 /* | |
1199 | 5332 * Set normal fg/bg color, based on T_ME. Called when t_me has been set. |
7 | 5333 */ |
5334 void | |
26 | 5335 mch_set_normal_colors(void) |
7 | 5336 { |
5337 char_u *p; | |
5338 int n; | |
5339 | |
5340 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1; | |
5341 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1; | |
5342 if (T_ME[0] == ESC && T_ME[1] == '|') | |
5343 { | |
5344 p = T_ME + 2; | |
5345 n = getdigits(&p); | |
5346 if (*p == 'm' && n > 0) | |
5347 { | |
5348 cterm_normal_fg_color = (n & 0xf) + 1; | |
5349 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1; | |
5350 } | |
5351 } | |
5352 } | |
5353 | |
5354 | |
5355 /* | |
5356 * visual bell: flash the screen | |
5357 */ | |
5358 static void | |
26 | 5359 visual_bell(void) |
7 | 5360 { |
5361 COORD coordOrigin = {0, 0}; | |
5362 WORD attrFlash = ~g_attrCurrent & 0xff; | |
5363 | |
5364 DWORD dwDummy; | |
5365 LPWORD oldattrs = (LPWORD)alloc(Rows * Columns * sizeof(WORD)); | |
5366 | |
5367 if (oldattrs == NULL) | |
5368 return; | |
5369 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, | |
5370 coordOrigin, &dwDummy); | |
5371 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns, | |
5372 coordOrigin, &dwDummy); | |
5373 | |
5374 Sleep(15); /* wait for 15 msec */ | |
5375 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, | |
5376 coordOrigin, &dwDummy); | |
5377 vim_free(oldattrs); | |
5378 } | |
5379 | |
5380 | |
5381 /* | |
5382 * Make the cursor visible or invisible | |
5383 */ | |
5384 static void | |
26 | 5385 cursor_visible(BOOL fVisible) |
7 | 5386 { |
5387 s_cursor_visible = fVisible; | |
5388 #ifdef MCH_CURSOR_SHAPE | |
5389 mch_update_cursor(); | |
5390 #endif | |
5391 } | |
5392 | |
5393 | |
5394 /* | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5395 * write `cbToWrite' bytes in `pchBuf' to the screen |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5396 * Returns the number of bytes actually written (at least one). |
7 | 5397 */ |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5398 static DWORD |
7 | 5399 write_chars( |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5400 char_u *pchBuf, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5401 DWORD cbToWrite) |
7 | 5402 { |
5403 COORD coord = g_coord; | |
5404 DWORD written; | |
5405 | |
7080
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5406 #ifdef FEAT_MBYTE |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5407 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5408 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5409 static WCHAR *unicodebuf = NULL; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5410 static int unibuflen = 0; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5411 int length; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5412 DWORD n, cchwritten, cells; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5413 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5414 length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5415 if (unicodebuf == NULL || length > unibuflen) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5416 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5417 vim_free(unicodebuf); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5418 unicodebuf = (WCHAR *)lalloc(length * sizeof(WCHAR), FALSE); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5419 unibuflen = length; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5420 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5421 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pchBuf, cbToWrite, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5422 unicodebuf, unibuflen); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5423 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5424 cells = mb_string2cells(pchBuf, cbToWrite); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5425 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5426 coord, &written); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5427 /* When writing fails or didn't write a single character, pretend one |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5428 * character was written, otherwise we get stuck. */ |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5429 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5430 coord, &cchwritten) == 0 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5431 || cchwritten == 0) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5432 cchwritten = 1; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5433 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5434 if (cchwritten == length) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5435 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5436 written = cbToWrite; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5437 g_coord.X += (SHORT)cells; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5438 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5439 else |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5440 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5441 char_u *p = pchBuf; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5442 for (n = 0; n < cchwritten; n++) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5443 mb_cptr_adv(p); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5444 written = p - pchBuf; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5445 g_coord.X += (SHORT)mb_string2cells(pchBuf, written); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5446 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5447 } |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5448 else |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5449 #endif |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5450 { |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5451 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cbToWrite, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5452 coord, &written); |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5453 /* When writing fails or didn't write a single character, pretend one |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5454 * character was written, otherwise we get stuck. */ |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5455 if (WriteConsoleOutputCharacter(g_hConOut, (LPCSTR)pchBuf, cbToWrite, |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5456 coord, &written) == 0 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5457 || written == 0) |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5458 written = 1; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5459 |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5460 g_coord.X += (SHORT) written; |
1a34f5272977
commit https://github.com/vim/vim/commit/ac360bf2ca293735fc7c6654dc2b3066f4c62488
Christian Brabandt <cb@256bit.org>
parents:
7078
diff
changeset
|
5461 } |
7 | 5462 |
5463 while (g_coord.X > g_srScrollRegion.Right) | |
5464 { | |
5465 g_coord.X -= (SHORT) Columns; | |
5466 if (g_coord.Y < g_srScrollRegion.Bottom) | |
5467 g_coord.Y++; | |
5468 } | |
5469 | |
5470 gotoxy(g_coord.X + 1, g_coord.Y + 1); | |
5471 | |
5472 return written; | |
5473 } | |
5474 | |
5475 | |
5476 /* | |
5477 * mch_write(): write the output buffer to the screen, translating ESC | |
5478 * sequences into calls to console output routines. | |
5479 */ | |
5480 void | |
5481 mch_write( | |
5482 char_u *s, | |
5483 int len) | |
5484 { | |
5485 s[len] = NUL; | |
5486 | |
5487 if (!term_console) | |
5488 { | |
5489 write(1, s, (unsigned)len); | |
5490 return; | |
5491 } | |
5492 | |
5493 /* translate ESC | sequences into faked bios calls */ | |
5494 while (len--) | |
5495 { | |
5496 /* optimization: use one single write_chars for runs of text, | |
5497 * rather than once per character It ain't curses, but it helps. */ | |
835 | 5498 DWORD prefix = (DWORD)strcspn(s, "\n\r\b\a\033"); |
7 | 5499 |
5500 if (p_wd) | |
5501 { | |
5502 WaitForChar(p_wd); | |
5503 if (prefix != 0) | |
5504 prefix = 1; | |
5505 } | |
5506 | |
5507 if (prefix != 0) | |
5508 { | |
5509 DWORD nWritten; | |
5510 | |
5511 nWritten = write_chars(s, prefix); | |
5512 #ifdef MCH_WRITE_DUMP | |
5513 if (fdDump) | |
5514 { | |
5515 fputc('>', fdDump); | |
5516 fwrite(s, sizeof(char_u), nWritten, fdDump); | |
5517 fputs("<\n", fdDump); | |
5518 } | |
5519 #endif | |
5520 len -= (nWritten - 1); | |
5521 s += nWritten; | |
5522 } | |
5523 else if (s[0] == '\n') | |
5524 { | |
5525 /* \n, newline: go to the beginning of the next line or scroll */ | |
5526 if (g_coord.Y == g_srScrollRegion.Bottom) | |
5527 { | |
5528 scroll(1); | |
5529 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1); | |
5530 } | |
5531 else | |
5532 { | |
5533 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2); | |
5534 } | |
5535 #ifdef MCH_WRITE_DUMP | |
5536 if (fdDump) | |
5537 fputs("\\n\n", fdDump); | |
5538 #endif | |
5539 s++; | |
5540 } | |
5541 else if (s[0] == '\r') | |
5542 { | |
5543 /* \r, carriage return: go to beginning of line */ | |
5544 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1); | |
5545 #ifdef MCH_WRITE_DUMP | |
5546 if (fdDump) | |
5547 fputs("\\r\n", fdDump); | |
5548 #endif | |
5549 s++; | |
5550 } | |
5551 else if (s[0] == '\b') | |
5552 { | |
5553 /* \b, backspace: move cursor one position left */ | |
5554 if (g_coord.X > g_srScrollRegion.Left) | |
5555 g_coord.X--; | |
5556 else if (g_coord.Y > g_srScrollRegion.Top) | |
5557 { | |
5558 g_coord.X = g_srScrollRegion.Right; | |
5559 g_coord.Y--; | |
5560 } | |
5561 gotoxy(g_coord.X + 1, g_coord.Y + 1); | |
5562 #ifdef MCH_WRITE_DUMP | |
5563 if (fdDump) | |
5564 fputs("\\b\n", fdDump); | |
5565 #endif | |
5566 s++; | |
5567 } | |
5568 else if (s[0] == '\a') | |
5569 { | |
5570 /* \a, bell */ | |
5571 MessageBeep(0xFFFFFFFF); | |
5572 #ifdef MCH_WRITE_DUMP | |
5573 if (fdDump) | |
5574 fputs("\\a\n", fdDump); | |
5575 #endif | |
5576 s++; | |
5577 } | |
5578 else if (s[0] == ESC && len >= 3-1 && s[1] == '|') | |
5579 { | |
5580 #ifdef MCH_WRITE_DUMP | |
24 | 5581 char_u *old_s = s; |
7 | 5582 #endif |
24 | 5583 char_u *p; |
5584 int arg1 = 0, arg2 = 0; | |
7 | 5585 |
5586 switch (s[2]) | |
5587 { | |
5588 /* one or two numeric arguments, separated by ';' */ | |
5589 | |
5590 case '0': case '1': case '2': case '3': case '4': | |
5591 case '5': case '6': case '7': case '8': case '9': | |
5592 p = s + 2; | |
5593 arg1 = getdigits(&p); /* no check for length! */ | |
5594 if (p > s + len) | |
5595 break; | |
5596 | |
5597 if (*p == ';') | |
5598 { | |
5599 ++p; | |
5600 arg2 = getdigits(&p); /* no check for length! */ | |
5601 if (p > s + len) | |
5602 break; | |
5603 | |
5604 if (*p == 'H') | |
5605 gotoxy(arg2, arg1); | |
5606 else if (*p == 'r') | |
5607 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1); | |
5608 } | |
5609 else if (*p == 'A') | |
5610 { | |
5611 /* move cursor up arg1 lines in same column */ | |
5612 gotoxy(g_coord.X + 1, | |
5613 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1); | |
5614 } | |
5615 else if (*p == 'C') | |
5616 { | |
5617 /* move cursor right arg1 columns in same line */ | |
5618 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1, | |
5619 g_coord.Y + 1); | |
5620 } | |
5621 else if (*p == 'H') | |
5622 { | |
5623 gotoxy(1, arg1); | |
5624 } | |
5625 else if (*p == 'L') | |
5626 { | |
5627 insert_lines(arg1); | |
5628 } | |
5629 else if (*p == 'm') | |
5630 { | |
5631 if (arg1 == 0) | |
5632 normvideo(); | |
5633 else | |
5634 textattr((WORD) arg1); | |
5635 } | |
5636 else if (*p == 'f') | |
5637 { | |
5638 textcolor((WORD) arg1); | |
5639 } | |
5640 else if (*p == 'b') | |
5641 { | |
5642 textbackground((WORD) arg1); | |
5643 } | |
5644 else if (*p == 'M') | |
5645 { | |
5646 delete_lines(arg1); | |
5647 } | |
5648 | |
835 | 5649 len -= (int)(p - s); |
7 | 5650 s = p + 1; |
5651 break; | |
5652 | |
5653 | |
5654 /* Three-character escape sequences */ | |
5655 | |
5656 case 'A': | |
5657 /* move cursor up one line in same column */ | |
5658 gotoxy(g_coord.X + 1, | |
5659 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1); | |
5660 goto got3; | |
5661 | |
5662 case 'B': | |
5663 visual_bell(); | |
5664 goto got3; | |
5665 | |
5666 case 'C': | |
5667 /* move cursor right one column in same line */ | |
5668 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1, | |
5669 g_coord.Y + 1); | |
5670 goto got3; | |
5671 | |
5672 case 'E': | |
5673 termcap_mode_end(); | |
5674 goto got3; | |
5675 | |
5676 case 'F': | |
5677 standout(); | |
5678 goto got3; | |
5679 | |
5680 case 'f': | |
5681 standend(); | |
5682 goto got3; | |
5683 | |
5684 case 'H': | |
5685 gotoxy(1, 1); | |
5686 goto got3; | |
5687 | |
5688 case 'j': | |
5689 clear_to_end_of_display(); | |
5690 goto got3; | |
5691 | |
5692 case 'J': | |
5693 clear_screen(); | |
5694 goto got3; | |
5695 | |
5696 case 'K': | |
5697 clear_to_end_of_line(); | |
5698 goto got3; | |
5699 | |
5700 case 'L': | |
5701 insert_lines(1); | |
5702 goto got3; | |
5703 | |
5704 case 'M': | |
5705 delete_lines(1); | |
5706 goto got3; | |
5707 | |
5708 case 'S': | |
5709 termcap_mode_start(); | |
5710 goto got3; | |
5711 | |
5712 case 'V': | |
5713 cursor_visible(TRUE); | |
5714 goto got3; | |
5715 | |
5716 case 'v': | |
5717 cursor_visible(FALSE); | |
5718 goto got3; | |
5719 | |
5720 got3: | |
5721 s += 3; | |
5722 len -= 2; | |
5723 } | |
5724 | |
5725 #ifdef MCH_WRITE_DUMP | |
5726 if (fdDump) | |
5727 { | |
5728 fputs("ESC | ", fdDump); | |
5729 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump); | |
5730 fputc('\n', fdDump); | |
5731 } | |
5732 #endif | |
5733 } | |
5734 else | |
5735 { | |
5736 /* Write a single character */ | |
5737 DWORD nWritten; | |
5738 | |
5739 nWritten = write_chars(s, 1); | |
5740 #ifdef MCH_WRITE_DUMP | |
5741 if (fdDump) | |
5742 { | |
5743 fputc('>', fdDump); | |
5744 fwrite(s, sizeof(char_u), nWritten, fdDump); | |
5745 fputs("<\n", fdDump); | |
5746 } | |
5747 #endif | |
5748 | |
5749 len -= (nWritten - 1); | |
5750 s += nWritten; | |
5751 } | |
5752 } | |
5753 | |
5754 #ifdef MCH_WRITE_DUMP | |
5755 if (fdDump) | |
5756 fflush(fdDump); | |
5757 #endif | |
5758 } | |
5759 | |
5760 #endif /* FEAT_GUI_W32 */ | |
5761 | |
5762 | |
5763 /* | |
5764 * Delay for half a second. | |
5765 */ | |
323 | 5766 /*ARGSUSED*/ |
7 | 5767 void |
5768 mch_delay( | |
5769 long msec, | |
5770 int ignoreinput) | |
5771 { | |
5772 #ifdef FEAT_GUI_W32 | |
5773 Sleep((int)msec); /* never wait for input */ | |
14 | 5774 #else /* Console */ |
7 | 5775 if (ignoreinput) |
14 | 5776 # ifdef FEAT_MZSCHEME |
5777 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq) | |
5778 { | |
5779 int towait = p_mzq; | |
5780 | |
5781 /* if msec is large enough, wait by portions in p_mzq */ | |
5782 while (msec > 0) | |
5783 { | |
5784 mzvim_check_threads(); | |
5785 if (msec < towait) | |
5786 towait = msec; | |
5787 Sleep(towait); | |
5788 msec -= towait; | |
5789 } | |
5790 } | |
5791 else | |
5792 # endif | |
5793 Sleep((int)msec); | |
7 | 5794 else |
5795 WaitForChar(msec); | |
5796 #endif | |
5797 } | |
5798 | |
5799 | |
5800 /* | |
5801 * this version of remove is not scared by a readonly (backup) file | |
5802 * Return 0 for success, -1 for failure. | |
5803 */ | |
5804 int | |
5805 mch_remove(char_u *name) | |
5806 { | |
5807 #ifdef FEAT_MBYTE | |
5808 WCHAR *wn = NULL; | |
5809 int n; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5810 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5811 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5812 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5813 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5814 #ifdef FEAT_MBYTE |
7 | 5815 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
5816 { | |
1752 | 5817 wn = enc_to_utf16(name, NULL); |
7 | 5818 if (wn != NULL) |
5819 { | |
5820 n = DeleteFileW(wn) ? 0 : -1; | |
5821 vim_free(wn); | |
5822 if (n == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5823 return n; | |
5824 /* Retry with non-wide function (for Windows 98). */ | |
5825 } | |
5826 } | |
5827 #endif | |
5828 return DeleteFile(name) ? 0 : -1; | |
5829 } | |
5830 | |
5831 | |
5832 /* | |
5833 * check for an "interrupt signal": CTRL-break or CTRL-C | |
5834 */ | |
5835 void | |
26 | 5836 mch_breakcheck(void) |
7 | 5837 { |
5838 #ifndef FEAT_GUI_W32 /* never used */ | |
5839 if (g_fCtrlCPressed || g_fCBrkPressed) | |
5840 { | |
5841 g_fCtrlCPressed = g_fCBrkPressed = FALSE; | |
5842 got_int = TRUE; | |
5843 } | |
5844 #endif | |
5845 } | |
5846 | |
5847 | |
5848 #ifdef FEAT_MBYTE | |
5849 /* | |
5850 * Same code as below, but with wide functions and no comments. | |
5851 * Return 0 for success, non-zero for failure. | |
5852 */ | |
5853 int | |
5854 mch_wrename(WCHAR *wold, WCHAR *wnew) | |
5855 { | |
5856 WCHAR *p; | |
5857 int i; | |
5858 WCHAR szTempFile[_MAX_PATH + 1]; | |
5859 WCHAR szNewPath[_MAX_PATH + 1]; | |
5860 HANDLE hf; | |
5861 | |
5862 if (!mch_windows95()) | |
5863 { | |
5864 p = wold; | |
5865 for (i = 0; wold[i] != NUL; ++i) | |
5866 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':') | |
5867 && wold[i + 1] != 0) | |
5868 p = wold + i + 1; | |
5869 if ((int)(wold + i - p) < 8 || p[6] != '~') | |
5870 return (MoveFileW(wold, wnew) == 0); | |
5871 } | |
5872 | |
5873 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL) | |
5874 return -1; | |
5875 *p = NUL; | |
5876 | |
5877 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0) | |
5878 return -2; | |
5879 | |
5880 if (!DeleteFileW(szTempFile)) | |
5881 return -3; | |
5882 | |
5883 if (!MoveFileW(wold, szTempFile)) | |
5884 return -4; | |
5885 | |
5886 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW, | |
5887 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) | |
5888 return -5; | |
5889 if (!CloseHandle(hf)) | |
5890 return -6; | |
5891 | |
5892 if (!MoveFileW(szTempFile, wnew)) | |
5893 { | |
5894 (void)MoveFileW(szTempFile, wold); | |
5895 return -7; | |
5896 } | |
5897 | |
5898 DeleteFileW(szTempFile); | |
5899 | |
5900 if (!DeleteFileW(wold)) | |
5901 return -8; | |
5902 | |
5903 return 0; | |
5904 } | |
5905 #endif | |
5906 | |
5907 | |
5908 /* | |
5909 * mch_rename() works around a bug in rename (aka MoveFile) in | |
5910 * Windows 95: rename("foo.bar", "foo.bar~") will generate a | |
5911 * file whose short file name is "FOO.BAR" (its long file name will | |
5912 * be correct: "foo.bar~"). Because a file can be accessed by | |
5913 * either its SFN or its LFN, "foo.bar" has effectively been | |
5914 * renamed to "foo.bar", which is not at all what was wanted. This | |
5915 * seems to happen only when renaming files with three-character | |
5916 * extensions by appending a suffix that does not include ".". | |
5917 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR". | |
5918 * | |
5919 * There is another problem, which isn't really a bug but isn't right either: | |
5920 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be | |
5921 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with | |
5922 * service pack 6. Doesn't seem to happen on Windows 98. | |
5923 * | |
5924 * Like rename(), returns 0 upon success, non-zero upon failure. | |
5925 * Should probably set errno appropriately when errors occur. | |
5926 */ | |
5927 int | |
5928 mch_rename( | |
5929 const char *pszOldFile, | |
5930 const char *pszNewFile) | |
5931 { | |
5932 char szTempFile[_MAX_PATH+1]; | |
5933 char szNewPath[_MAX_PATH+1]; | |
5934 char *pszFilePart; | |
5935 HANDLE hf; | |
5936 #ifdef FEAT_MBYTE | |
5937 WCHAR *wold = NULL; | |
5938 WCHAR *wnew = NULL; | |
5939 int retval = -1; | |
5940 | |
5941 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
5942 { | |
1752 | 5943 wold = enc_to_utf16((char_u *)pszOldFile, NULL); |
5944 wnew = enc_to_utf16((char_u *)pszNewFile, NULL); | |
7 | 5945 if (wold != NULL && wnew != NULL) |
5946 retval = mch_wrename(wold, wnew); | |
5947 vim_free(wold); | |
5948 vim_free(wnew); | |
5949 if (retval == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5950 return retval; | |
5951 /* Retry with non-wide function (for Windows 98). */ | |
5952 } | |
5953 #endif | |
5954 | |
5955 /* | |
5956 * No need to play tricks if not running Windows 95, unless the file name | |
5957 * contains a "~" as the seventh character. | |
5958 */ | |
5959 if (!mch_windows95()) | |
5960 { | |
5961 pszFilePart = (char *)gettail((char_u *)pszOldFile); | |
5962 if (STRLEN(pszFilePart) < 8 || pszFilePart[6] != '~') | |
5963 return rename(pszOldFile, pszNewFile); | |
5964 } | |
5965 | |
5966 /* Get base path of new file name. Undocumented feature: If pszNewFile is | |
5967 * a directory, no error is returned and pszFilePart will be NULL. */ | |
5968 if (GetFullPathName(pszNewFile, _MAX_PATH, szNewPath, &pszFilePart) == 0 | |
5969 || pszFilePart == NULL) | |
5970 return -1; | |
5971 *pszFilePart = NUL; | |
5972 | |
5973 /* Get (and create) a unique temporary file name in directory of new file */ | |
5974 if (GetTempFileName(szNewPath, "VIM", 0, szTempFile) == 0) | |
5975 return -2; | |
5976 | |
5977 /* blow the temp file away */ | |
5978 if (!DeleteFile(szTempFile)) | |
5979 return -3; | |
5980 | |
5981 /* rename old file to the temp file */ | |
5982 if (!MoveFile(pszOldFile, szTempFile)) | |
5983 return -4; | |
5984 | |
5985 /* now create an empty file called pszOldFile; this prevents the operating | |
5986 * system using pszOldFile as an alias (SFN) if we're renaming within the | |
5987 * same directory. For example, we're editing a file called | |
5988 * filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt | |
5989 * to filena~1.txt~ (i.e., we're making a backup while writing it), the | |
5990 * SFN for filena~1.txt~ will be filena~1.txt, by default, which will | |
39 | 5991 * cause all sorts of problems later in buf_write(). So, we create an |
5992 * empty file called filena~1.txt and the system will have to find some | |
5993 * other SFN for filena~1.txt~, such as filena~2.txt | |
7 | 5994 */ |
5995 if ((hf = CreateFile(pszOldFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, | |
5996 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) | |
5997 return -5; | |
5998 if (!CloseHandle(hf)) | |
5999 return -6; | |
6000 | |
6001 /* rename the temp file to the new file */ | |
6002 if (!MoveFile(szTempFile, pszNewFile)) | |
6003 { | |
6004 /* Renaming failed. Rename the file back to its old name, so that it | |
6005 * looks like nothing happened. */ | |
6006 (void)MoveFile(szTempFile, pszOldFile); | |
6007 | |
6008 return -7; | |
6009 } | |
6010 | |
6011 /* Seems to be left around on Novell filesystems */ | |
6012 DeleteFile(szTempFile); | |
6013 | |
6014 /* finally, remove the empty old file */ | |
6015 if (!DeleteFile(pszOldFile)) | |
6016 return -8; | |
6017 | |
6018 return 0; /* success */ | |
6019 } | |
6020 | |
6021 /* | |
6022 * Get the default shell for the current hardware platform | |
6023 */ | |
6024 char * | |
26 | 6025 default_shell(void) |
7 | 6026 { |
6027 char* psz = NULL; | |
6028 | |
6029 PlatformId(); | |
6030 | |
6031 if (g_PlatformId == VER_PLATFORM_WIN32_NT) /* Windows NT */ | |
6032 psz = "cmd.exe"; | |
6033 else if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) /* Windows 95 */ | |
6034 psz = "command.com"; | |
6035 | |
6036 return psz; | |
6037 } | |
6038 | |
6039 /* | |
6040 * mch_access() extends access() to do more detailed check on network drives. | |
6041 * Returns 0 if file "n" has access rights according to "p", -1 otherwise. | |
6042 */ | |
6043 int | |
6044 mch_access(char *n, int p) | |
6045 { | |
6046 HANDLE hFile; | |
6047 DWORD am; | |
6048 int retval = -1; /* default: fail */ | |
6049 #ifdef FEAT_MBYTE | |
6050 WCHAR *wn = NULL; | |
6051 | |
6052 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
1752 | 6053 wn = enc_to_utf16(n, NULL); |
7 | 6054 #endif |
6055 | |
6056 if (mch_isdir(n)) | |
6057 { | |
6058 char TempName[_MAX_PATH + 16] = ""; | |
6059 #ifdef FEAT_MBYTE | |
6060 WCHAR TempNameW[_MAX_PATH + 16] = L""; | |
6061 #endif | |
6062 | |
6063 if (p & R_OK) | |
6064 { | |
6065 /* Read check is performed by seeing if we can do a find file on | |
6066 * the directory for any file. */ | |
6067 #ifdef FEAT_MBYTE | |
6068 if (wn != NULL) | |
6069 { | |
6070 int i; | |
6071 WIN32_FIND_DATAW d; | |
6072 | |
6073 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i) | |
6074 TempNameW[i] = wn[i]; | |
6075 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/') | |
6076 TempNameW[i++] = '\\'; | |
6077 TempNameW[i++] = '*'; | |
6078 TempNameW[i++] = 0; | |
6079 | |
6080 hFile = FindFirstFileW(TempNameW, &d); | |
6081 if (hFile == INVALID_HANDLE_VALUE) | |
6082 { | |
6083 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
6084 goto getout; | |
6085 | |
6086 /* Retry with non-wide function (for Windows 98). */ | |
6087 vim_free(wn); | |
6088 wn = NULL; | |
6089 } | |
6090 else | |
6091 (void)FindClose(hFile); | |
6092 } | |
6093 if (wn == NULL) | |
6094 #endif | |
6095 { | |
6096 char *pch; | |
6097 WIN32_FIND_DATA d; | |
6098 | |
417 | 6099 vim_strncpy(TempName, n, _MAX_PATH); |
7 | 6100 pch = TempName + STRLEN(TempName) - 1; |
6101 if (*pch != '\\' && *pch != '/') | |
6102 *++pch = '\\'; | |
6103 *++pch = '*'; | |
6104 *++pch = NUL; | |
6105 | |
6106 hFile = FindFirstFile(TempName, &d); | |
6107 if (hFile == INVALID_HANDLE_VALUE) | |
6108 goto getout; | |
6109 (void)FindClose(hFile); | |
6110 } | |
6111 } | |
6112 | |
6113 if (p & W_OK) | |
6114 { | |
6115 /* Trying to create a temporary file in the directory should catch | |
6116 * directories on read-only network shares. However, in | |
6117 * directories whose ACL allows writes but denies deletes will end | |
6118 * up keeping the temporary file :-(. */ | |
6119 #ifdef FEAT_MBYTE | |
6120 if (wn != NULL) | |
6121 { | |
6122 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW)) | |
6123 { | |
6124 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
6125 goto getout; | |
6126 | |
6127 /* Retry with non-wide function (for Windows 98). */ | |
6128 vim_free(wn); | |
6129 wn = NULL; | |
6130 } | |
6131 else | |
6132 DeleteFileW(TempNameW); | |
6133 } | |
6134 if (wn == NULL) | |
6135 #endif | |
6136 { | |
6137 if (!GetTempFileName(n, "VIM", 0, TempName)) | |
6138 goto getout; | |
6139 mch_remove((char_u *)TempName); | |
6140 } | |
6141 } | |
6142 } | |
6143 else | |
6144 { | |
6145 /* Trying to open the file for the required access does ACL, read-only | |
6146 * network share, and file attribute checks. */ | |
6147 am = ((p & W_OK) ? GENERIC_WRITE : 0) | |
6148 | ((p & R_OK) ? GENERIC_READ : 0); | |
6149 #ifdef FEAT_MBYTE | |
6150 if (wn != NULL) | |
6151 { | |
6152 hFile = CreateFileW(wn, am, 0, NULL, OPEN_EXISTING, 0, NULL); | |
6153 if (hFile == INVALID_HANDLE_VALUE | |
6154 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
6155 { | |
6156 /* Retry with non-wide function (for Windows 98). */ | |
6157 vim_free(wn); | |
6158 wn = NULL; | |
6159 } | |
6160 } | |
6161 if (wn == NULL) | |
6162 #endif | |
6163 hFile = CreateFile(n, am, 0, NULL, OPEN_EXISTING, 0, NULL); | |
6164 if (hFile == INVALID_HANDLE_VALUE) | |
6165 goto getout; | |
6166 CloseHandle(hFile); | |
6167 } | |
6168 | |
6169 retval = 0; /* success */ | |
6170 getout: | |
6171 #ifdef FEAT_MBYTE | |
6172 vim_free(wn); | |
6173 #endif | |
6174 return retval; | |
6175 } | |
6176 | |
6177 #if defined(FEAT_MBYTE) || defined(PROTO) | |
6178 /* | |
1752 | 6179 * Version of open() that may use UTF-16 file name. |
7 | 6180 */ |
6181 int | |
6182 mch_open(char *name, int flags, int mode) | |
6183 { | |
39 | 6184 /* _wopen() does not work with Borland C 5.5: creates a read-only file. */ |
6185 # ifndef __BORLANDC__ | |
7 | 6186 WCHAR *wn; |
6187 int f; | |
6188 | |
39 | 6189 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
7 | 6190 { |
1752 | 6191 wn = enc_to_utf16(name, NULL); |
7 | 6192 if (wn != NULL) |
6193 { | |
6194 f = _wopen(wn, flags, mode); | |
6195 vim_free(wn); | |
5657 | 6196 if (f >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT) |
7 | 6197 return f; |
6198 /* Retry with non-wide function (for Windows 98). Can't use | |
6199 * GetLastError() here and it's unclear what errno gets set to if | |
6200 * the _wopen() fails for missing wide functions. */ | |
6201 } | |
6202 } | |
39 | 6203 # endif |
7 | 6204 |
6345 | 6205 /* open() can open a file which name is longer than _MAX_PATH bytes |
6206 * and shorter than _MAX_PATH characters successfully, but sometimes it | |
6207 * causes unexpected error in another part. We make it an error explicitly | |
6208 * here. */ | |
6209 if (strlen(name) >= _MAX_PATH) | |
6210 return -1; | |
6211 | |
7 | 6212 return open(name, flags, mode); |
6213 } | |
6214 | |
6215 /* | |
1752 | 6216 * Version of fopen() that may use UTF-16 file name. |
7 | 6217 */ |
6218 FILE * | |
6219 mch_fopen(char *name, char *mode) | |
6220 { | |
6221 WCHAR *wn, *wm; | |
6222 FILE *f = NULL; | |
6223 | |
6224 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage | |
6225 # ifdef __BORLANDC__ | |
6226 /* Wide functions of Borland C 5.5 do not work on Windows 98. */ | |
6227 && g_PlatformId == VER_PLATFORM_WIN32_NT | |
6228 # endif | |
6229 ) | |
6230 { | |
1686 | 6231 # if defined(DEBUG) && _MSC_VER >= 1400 |
1569 | 6232 /* Work around an annoying assertion in the Microsoft debug CRT |
6233 * when mode's text/binary setting doesn't match _get_fmode(). */ | |
6234 char newMode = mode[strlen(mode) - 1]; | |
6235 int oldMode = 0; | |
6236 | |
6237 _get_fmode(&oldMode); | |
6238 if (newMode == 't') | |
6239 _set_fmode(_O_TEXT); | |
6240 else if (newMode == 'b') | |
6241 _set_fmode(_O_BINARY); | |
6242 # endif | |
1752 | 6243 wn = enc_to_utf16(name, NULL); |
6244 wm = enc_to_utf16(mode, NULL); | |
7 | 6245 if (wn != NULL && wm != NULL) |
6246 f = _wfopen(wn, wm); | |
6247 vim_free(wn); | |
6248 vim_free(wm); | |
1569 | 6249 |
1686 | 6250 # if defined(DEBUG) && _MSC_VER >= 1400 |
1569 | 6251 _set_fmode(oldMode); |
6252 # endif | |
6253 | |
5657 | 6254 if (f != NULL || g_PlatformId == VER_PLATFORM_WIN32_NT) |
7 | 6255 return f; |
6256 /* Retry with non-wide function (for Windows 98). Can't use | |
6257 * GetLastError() here and it's unclear what errno gets set to if | |
6258 * the _wfopen() fails for missing wide functions. */ | |
6259 } | |
6260 | |
6345 | 6261 /* fopen() can open a file which name is longer than _MAX_PATH bytes |
6262 * and shorter than _MAX_PATH characters successfully, but sometimes it | |
6263 * causes unexpected error in another part. We make it an error explicitly | |
6264 * here. */ | |
6265 if (strlen(name) >= _MAX_PATH) | |
6266 return NULL; | |
6267 | |
7 | 6268 return fopen(name, mode); |
6269 } | |
6270 #endif | |
6271 | |
6272 #ifdef FEAT_MBYTE | |
6273 /* | |
6274 * SUB STREAM (aka info stream) handling: | |
6275 * | |
6276 * NTFS can have sub streams for each file. Normal contents of file is | |
6277 * stored in the main stream, and extra contents (author information and | |
6278 * title and so on) can be stored in sub stream. After Windows 2000, user | |
6279 * can access and store those informations in sub streams via explorer's | |
6280 * property menuitem in right click menu. Those informations in sub streams | |
6281 * were lost when copying only the main stream. So we have to copy sub | |
6282 * streams. | |
6283 * | |
6284 * Incomplete explanation: | |
6285 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp | |
6286 * More useful info and an example: | |
6287 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams | |
6288 */ | |
6289 | |
6290 /* | |
6291 * Copy info stream data "substream". Read from the file with BackupRead(sh) | |
6292 * and write to stream "substream" of file "to". | |
6293 * Errors are ignored. | |
6294 */ | |
6295 static void | |
6296 copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len) | |
6297 { | |
6298 HANDLE hTo; | |
6299 WCHAR *to_name; | |
6300 | |
6301 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR)); | |
6302 wcscpy(to_name, to); | |
6303 wcscat(to_name, substream); | |
6304 | |
6305 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, | |
6306 FILE_ATTRIBUTE_NORMAL, NULL); | |
6307 if (hTo != INVALID_HANDLE_VALUE) | |
6308 { | |
6309 long done; | |
6310 DWORD todo; | |
6311 DWORD readcnt, written; | |
6312 char buf[4096]; | |
6313 | |
6314 /* Copy block of bytes at a time. Abort when something goes wrong. */ | |
6315 for (done = 0; done < len; done += written) | |
6316 { | |
6317 /* (size_t) cast for Borland C 5.5 */ | |
835 | 6318 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf) |
6319 : (size_t)(len - done)); | |
7 | 6320 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt, |
6321 FALSE, FALSE, context) | |
6322 || readcnt != todo | |
6323 || !WriteFile(hTo, buf, todo, &written, NULL) | |
6324 || written != todo) | |
6325 break; | |
6326 } | |
6327 CloseHandle(hTo); | |
6328 } | |
6329 | |
6330 free(to_name); | |
6331 } | |
6332 | |
6333 /* | |
6334 * Copy info streams from file "from" to file "to". | |
6335 */ | |
6336 static void | |
6337 copy_infostreams(char_u *from, char_u *to) | |
6338 { | |
6339 WCHAR *fromw; | |
6340 WCHAR *tow; | |
6341 HANDLE sh; | |
6342 WIN32_STREAM_ID sid; | |
6343 int headersize; | |
6344 WCHAR streamname[_MAX_PATH]; | |
6345 DWORD readcount; | |
6346 void *context = NULL; | |
6347 DWORD lo, hi; | |
6348 int len; | |
6349 | |
6350 /* Convert the file names to wide characters. */ | |
1752 | 6351 fromw = enc_to_utf16(from, NULL); |
6352 tow = enc_to_utf16(to, NULL); | |
7 | 6353 if (fromw != NULL && tow != NULL) |
6354 { | |
6355 /* Open the file for reading. */ | |
6356 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL, | |
6357 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
6358 if (sh != INVALID_HANDLE_VALUE) | |
6359 { | |
6360 /* Use BackupRead() to find the info streams. Repeat until we | |
6361 * have done them all.*/ | |
6362 for (;;) | |
6363 { | |
6364 /* Get the header to find the length of the stream name. If | |
6365 * the "readcount" is zero we have done all info streams. */ | |
6366 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID)); | |
835 | 6367 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId); |
7 | 6368 if (!BackupRead(sh, (LPBYTE)&sid, headersize, |
6369 &readcount, FALSE, FALSE, &context) | |
6370 || readcount == 0) | |
6371 break; | |
6372 | |
6373 /* We only deal with streams that have a name. The normal | |
6374 * file data appears to be without a name, even though docs | |
6375 * suggest it is called "::$DATA". */ | |
6376 if (sid.dwStreamNameSize > 0) | |
6377 { | |
6378 /* Read the stream name. */ | |
6379 if (!BackupRead(sh, (LPBYTE)streamname, | |
6380 sid.dwStreamNameSize, | |
6381 &readcount, FALSE, FALSE, &context)) | |
6382 break; | |
6383 | |
6384 /* Copy an info stream with a name ":anything:$DATA". | |
6385 * Skip "::$DATA", it has no stream name (examples suggest | |
6386 * it might be used for the normal file contents). | |
6387 * Note that BackupRead() counts bytes, but the name is in | |
6388 * wide characters. */ | |
6389 len = readcount / sizeof(WCHAR); | |
6390 streamname[len] = 0; | |
6391 if (len > 7 && wcsicmp(streamname + len - 6, | |
6392 L":$DATA") == 0) | |
6393 { | |
6394 streamname[len - 6] = 0; | |
6395 copy_substream(sh, &context, tow, streamname, | |
10 | 6396 (long)sid.Size.u.LowPart); |
7 | 6397 } |
6398 } | |
6399 | |
6400 /* Advance to the next stream. We might try seeking too far, | |
6401 * but BackupSeek() doesn't skip over stream borders, thus | |
6402 * that's OK. */ | |
323 | 6403 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart, |
7 | 6404 &lo, &hi, &context); |
6405 } | |
6406 | |
6407 /* Clear the context. */ | |
6408 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context); | |
6409 | |
6410 CloseHandle(sh); | |
6411 } | |
6412 } | |
6413 vim_free(fromw); | |
6414 vim_free(tow); | |
6415 } | |
6416 #endif | |
6417 | |
6418 /* | |
6419 * Copy file attributes from file "from" to file "to". | |
6420 * For Windows NT and later we copy info streams. | |
6421 * Always returns zero, errors are ignored. | |
6422 */ | |
6423 int | |
6424 mch_copy_file_attribute(char_u *from, char_u *to) | |
6425 { | |
6426 #ifdef FEAT_MBYTE | |
6427 /* File streams only work on Windows NT and later. */ | |
6428 PlatformId(); | |
6429 if (g_PlatformId == VER_PLATFORM_WIN32_NT) | |
6430 copy_infostreams(from, to); | |
6431 #endif | |
6432 return 0; | |
6433 } | |
6434 | |
6435 #if defined(MYRESETSTKOFLW) || defined(PROTO) | |
6436 /* | |
6437 * Recreate a destroyed stack guard page in win32. | |
6438 * Written by Benjamin Peterson. | |
6439 */ | |
6440 | |
6441 /* These magic numbers are from the MS header files */ | |
6442 #define MIN_STACK_WIN9X 17 | |
6443 #define MIN_STACK_WINNT 2 | |
6444 | |
6445 /* | |
6446 * This function does the same thing as _resetstkoflw(), which is only | |
6447 * available in DevStudio .net and later. | |
6448 * Returns 0 for failure, 1 for success. | |
6449 */ | |
6450 int | |
6451 myresetstkoflw(void) | |
6452 { | |
6453 BYTE *pStackPtr; | |
6454 BYTE *pGuardPage; | |
6455 BYTE *pStackBase; | |
6456 BYTE *pLowestPossiblePage; | |
6457 MEMORY_BASIC_INFORMATION mbi; | |
6458 SYSTEM_INFO si; | |
6459 DWORD nPageSize; | |
6460 DWORD dummy; | |
6461 | |
6462 /* This code will not work on win32s. */ | |
6463 PlatformId(); | |
6464 if (g_PlatformId == VER_PLATFORM_WIN32s) | |
6465 return 0; | |
6466 | |
6467 /* We need to know the system page size. */ | |
6468 GetSystemInfo(&si); | |
6469 nPageSize = si.dwPageSize; | |
6470 | |
6471 /* ...and the current stack pointer */ | |
6472 pStackPtr = (BYTE*)_alloca(1); | |
6473 | |
6474 /* ...and the base of the stack. */ | |
6475 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0) | |
6476 return 0; | |
6477 pStackBase = (BYTE*)mbi.AllocationBase; | |
6478 | |
6479 /* ...and the page thats min_stack_req pages away from stack base; this is | |
6480 * the lowest page we could use. */ | |
6481 pLowestPossiblePage = pStackBase + ((g_PlatformId == VER_PLATFORM_WIN32_NT) | |
6482 ? MIN_STACK_WINNT : MIN_STACK_WIN9X) * nPageSize; | |
6483 | |
6484 /* On Win95, we want the next page down from the end of the stack. */ | |
6485 if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) | |
6486 { | |
6487 /* Find the page that's only 1 page down from the page that the stack | |
6488 * ptr is in. */ | |
6489 pGuardPage = (BYTE*)((DWORD)nPageSize * (((DWORD)pStackPtr | |
6490 / (DWORD)nPageSize) - 1)); | |
6491 if (pGuardPage < pLowestPossiblePage) | |
6492 return 0; | |
6493 | |
6494 /* Apply the noaccess attribute to the page -- there's no guard | |
6495 * attribute in win95-type OSes. */ | |
6496 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_NOACCESS, &dummy)) | |
6497 return 0; | |
6498 } | |
6499 else | |
6500 { | |
6501 /* On NT, however, we want the first committed page in the stack Start | |
6502 * at the stack base and move forward through memory until we find a | |
6503 * committed block. */ | |
6504 BYTE *pBlock = pStackBase; | |
6505 | |
406 | 6506 for (;;) |
7 | 6507 { |
6508 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0) | |
6509 return 0; | |
6510 | |
6511 pBlock += mbi.RegionSize; | |
6512 | |
6513 if (mbi.State & MEM_COMMIT) | |
6514 break; | |
6515 } | |
6516 | |
6517 /* mbi now describes the first committed block in the stack. */ | |
6518 if (mbi.Protect & PAGE_GUARD) | |
6519 return 1; | |
6520 | |
6521 /* decide where the guard page should start */ | |
6522 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage) | |
6523 pGuardPage = pLowestPossiblePage; | |
6524 else | |
6525 pGuardPage = (BYTE*)mbi.BaseAddress; | |
6526 | |
6527 /* allocate the guard page */ | |
6528 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE)) | |
6529 return 0; | |
6530 | |
6531 /* apply the guard attribute to the page */ | |
6532 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD, | |
6533 &dummy)) | |
6534 return 0; | |
6535 } | |
6536 | |
6537 return 1; | |
6538 } | |
6539 #endif | |
26 | 6540 |
6541 | |
6542 #if defined(FEAT_MBYTE) || defined(PROTO) | |
6543 /* | |
6544 * The command line arguments in UCS2 | |
6545 */ | |
344 | 6546 static int nArgsW = 0; |
26 | 6547 static LPWSTR *ArglistW = NULL; |
6548 static int global_argc = 0; | |
6549 static char **global_argv; | |
6550 | |
6551 static int used_file_argc = 0; /* last argument in global_argv[] used | |
6552 for the argument list. */ | |
6553 static int *used_file_indexes = NULL; /* indexes in global_argv[] for | |
6554 command line arguments added to | |
6555 the argument list */ | |
6556 static int used_file_count = 0; /* nr of entries in used_file_indexes */ | |
6557 static int used_file_literal = FALSE; /* take file names literally */ | |
6558 static int used_file_full_path = FALSE; /* file name was full path */ | |
819 | 6559 static int used_file_diff_mode = FALSE; /* file name was with diff mode */ |
26 | 6560 static int used_alist_count = 0; |
6561 | |
6562 | |
6563 /* | |
6564 * Get the command line arguments. Unicode version. | |
6565 * Returns argc. Zero when something fails. | |
6566 */ | |
6567 int | |
6568 get_cmd_argsW(char ***argvp) | |
6569 { | |
6570 char **argv = NULL; | |
6571 int argc = 0; | |
6572 int i; | |
6573 | |
6193 | 6574 free_cmd_argsW(); |
26 | 6575 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW); |
6576 if (ArglistW != NULL) | |
6577 { | |
6578 argv = malloc((nArgsW + 1) * sizeof(char *)); | |
6579 if (argv != NULL) | |
6580 { | |
6581 argc = nArgsW; | |
6582 argv[argc] = NULL; | |
6583 for (i = 0; i < argc; ++i) | |
6584 { | |
6585 int len; | |
6586 | |
6587 /* Convert each Unicode argument to the current codepage. */ | |
6588 WideCharToMultiByte_alloc(GetACP(), 0, | |
835 | 6589 ArglistW[i], (int)wcslen(ArglistW[i]) + 1, |
26 | 6590 (LPSTR *)&argv[i], &len, 0, 0); |
6591 if (argv[i] == NULL) | |
6592 { | |
6593 /* Out of memory, clear everything. */ | |
6594 while (i > 0) | |
6595 free(argv[--i]); | |
6596 free(argv); | |
5529 | 6597 argv = NULL; |
26 | 6598 argc = 0; |
6599 } | |
6600 } | |
6601 } | |
6602 } | |
6603 | |
6604 global_argc = argc; | |
6605 global_argv = argv; | |
6606 if (argc > 0) | |
6193 | 6607 { |
6608 if (used_file_indexes != NULL) | |
6609 free(used_file_indexes); | |
26 | 6610 used_file_indexes = malloc(argc * sizeof(int)); |
6193 | 6611 } |
26 | 6612 |
6613 if (argvp != NULL) | |
6614 *argvp = argv; | |
6615 return argc; | |
6616 } | |
6617 | |
6618 void | |
6619 free_cmd_argsW(void) | |
6620 { | |
6621 if (ArglistW != NULL) | |
6622 { | |
6623 GlobalFree(ArglistW); | |
6624 ArglistW = NULL; | |
6625 } | |
6626 } | |
6627 | |
6628 /* | |
6629 * Remember "name" is an argument that was added to the argument list. | |
6630 * This avoids that we have to re-parse the argument list when fix_arg_enc() | |
6631 * is called. | |
6632 */ | |
6633 void | |
819 | 6634 used_file_arg(char *name, int literal, int full_path, int diff_mode) |
26 | 6635 { |
6636 int i; | |
6637 | |
6638 if (used_file_indexes == NULL) | |
6639 return; | |
6640 for (i = used_file_argc + 1; i < global_argc; ++i) | |
6641 if (STRCMP(global_argv[i], name) == 0) | |
6642 { | |
6643 used_file_argc = i; | |
6644 used_file_indexes[used_file_count++] = i; | |
6645 break; | |
6646 } | |
6647 used_file_literal = literal; | |
6648 used_file_full_path = full_path; | |
819 | 6649 used_file_diff_mode = diff_mode; |
26 | 6650 } |
6651 | |
6652 /* | |
6653 * Remember the length of the argument list as it was. If it changes then we | |
6654 * leave it alone when 'encoding' is set. | |
6655 */ | |
6656 void | |
6657 set_alist_count(void) | |
6658 { | |
6659 used_alist_count = GARGCOUNT; | |
6660 } | |
6661 | |
6662 /* | |
6663 * Fix the encoding of the command line arguments. Invoked when 'encoding' | |
6664 * has been changed while starting up. Use the UCS-2 command line arguments | |
6665 * and convert them to 'encoding'. | |
6666 */ | |
6667 void | |
6668 fix_arg_enc(void) | |
6669 { | |
6670 int i; | |
6671 int idx; | |
6672 char_u *str; | |
41 | 6673 int *fnum_list; |
26 | 6674 |
6675 /* Safety checks: | |
6676 * - if argument count differs between the wide and non-wide argument | |
6677 * list, something must be wrong. | |
6678 * - the file name arguments must have been located. | |
6679 * - the length of the argument list wasn't changed by the user. | |
6680 */ | |
344 | 6681 if (global_argc != nArgsW |
26 | 6682 || ArglistW == NULL |
6683 || used_file_indexes == NULL | |
6684 || used_file_count == 0 | |
6685 || used_alist_count != GARGCOUNT) | |
6686 return; | |
6687 | |
41 | 6688 /* Remember the buffer numbers for the arguments. */ |
6689 fnum_list = (int *)alloc((int)sizeof(int) * GARGCOUNT); | |
6690 if (fnum_list == NULL) | |
6691 return; /* out of memory */ | |
6692 for (i = 0; i < GARGCOUNT; ++i) | |
6693 fnum_list[i] = GARGLIST[i].ae_fnum; | |
6694 | |
26 | 6695 /* Clear the argument list. Make room for the new arguments. */ |
6696 alist_clear(&global_alist); | |
6697 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL) | |
41 | 6698 return; /* out of memory */ |
26 | 6699 |
6700 for (i = 0; i < used_file_count; ++i) | |
6701 { | |
6702 idx = used_file_indexes[i]; | |
1752 | 6703 str = utf16_to_enc(ArglistW[idx], NULL); |
26 | 6704 if (str != NULL) |
41 | 6705 { |
819 | 6706 #ifdef FEAT_DIFF |
6707 /* When using diff mode may need to concatenate file name to | |
6708 * directory name. Just like it's done in main(). */ | |
6709 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0 | |
6710 && !mch_isdir(alist_name(&GARGLIST[0]))) | |
6711 { | |
6712 char_u *r; | |
6713 | |
6714 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE); | |
6715 if (r != NULL) | |
6716 { | |
6717 vim_free(str); | |
6718 str = r; | |
6719 } | |
6720 } | |
6721 #endif | |
41 | 6722 /* Re-use the old buffer by renaming it. When not using literal |
6723 * names it's done by alist_expand() below. */ | |
6724 if (used_file_literal) | |
6725 buf_set_name(fnum_list[i], str); | |
6726 | |
26 | 6727 alist_add(&global_alist, str, used_file_literal ? 2 : 0); |
41 | 6728 } |
26 | 6729 } |
6730 | |
6731 if (!used_file_literal) | |
6732 { | |
6733 /* Now expand wildcards in the arguments. */ | |
6734 /* Temporarily add '(' and ')' to 'isfname'. These are valid | |
6735 * filename characters but are excluded from 'isfname' to make | |
6736 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */ | |
6737 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)"); | |
41 | 6738 alist_expand(fnum_list, used_alist_count); |
26 | 6739 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF"); |
6740 } | |
6741 | |
6742 /* If wildcard expansion failed, we are editing the first file of the | |
6743 * arglist and there is no file name: Edit the first argument now. */ | |
6744 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL) | |
6745 { | |
6746 do_cmdline_cmd((char_u *)":rewind"); | |
6747 if (GARGCOUNT == 1 && used_file_full_path) | |
6748 (void)vim_chdirfile(alist_name(&GARGLIST[0])); | |
6749 } | |
41 | 6750 |
6751 set_alist_count(); | |
26 | 6752 } |
6753 #endif |