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