Mercurial > vim
annotate src/os_win32.c @ 4930:7155782d94fb v7.3.1210
updated for version 7.3.1210
Problem: 'backupcopy' default on MS-Windows is wrong when 'encoding' equals
the current codepage.
Solution: Change the #else block. (Ken Takata)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 16 Jun 2013 16:34:56 +0200 |
parents | fa98c2b030ed |
children | cabdcfe72dc3 |
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 } |
4930
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2792 else |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2793 #endif |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2794 { |
4930
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2795 hFind = FindFirstFile(fname, &findDataA); |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2796 if (hFind != INVALID_HANDLE_VALUE) |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2797 { |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2798 fileFlags = findDataA.dwFileAttributes; |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2799 reparseTag = findDataA.dwReserved0; |
7155782d94fb
updated for version 7.3.1210
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
2800 } |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2801 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2802 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2803 if (hFind != INVALID_HANDLE_VALUE) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2804 FindClose(hFind); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2805 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2806 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2807 && reparseTag == IO_REPARSE_TAG_SYMLINK) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2808 res = TRUE; |
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 return res; |
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 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2813 /* |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2814 * 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
|
2815 * link. |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2816 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2817 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2818 mch_is_linked(char_u *fname) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2819 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2820 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
|
2821 return TRUE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2822 return FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2823 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2824 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2825 /* |
2793 | 2826 * Get the by-handle-file-information for "fname". |
2827 * Returns FILEINFO_OK when OK. | |
2828 * returns FILEINFO_ENC_FAIL when enc_to_utf16() failed. | |
2829 * Returns FILEINFO_READ_FAIL when CreateFile() failed. | |
2830 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed. | |
2831 */ | |
2832 int | |
2833 win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info) | |
2834 { | |
696 | 2835 HANDLE hFile; |
2793 | 2836 int res = FILEINFO_READ_FAIL; |
696 | 2837 #ifdef FEAT_MBYTE |
2838 WCHAR *wn = NULL; | |
2839 | |
2840 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
2793 | 2841 { |
1752 | 2842 wn = enc_to_utf16(fname, NULL); |
2793 | 2843 if (wn == NULL) |
2844 res = FILEINFO_ENC_FAIL; | |
2845 } | |
696 | 2846 if (wn != NULL) |
2847 { | |
2848 hFile = CreateFileW(wn, /* file name */ | |
2849 GENERIC_READ, /* access mode */ | |
2793 | 2850 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
696 | 2851 NULL, /* security descriptor */ |
2852 OPEN_EXISTING, /* creation disposition */ | |
2793 | 2853 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
696 | 2854 NULL); /* handle to template file */ |
2855 if (hFile == INVALID_HANDLE_VALUE | |
2793 | 2856 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
696 | 2857 { |
2858 /* Retry with non-wide function (for Windows 98). */ | |
2859 vim_free(wn); | |
2860 wn = NULL; | |
2861 } | |
2862 } | |
2863 if (wn == NULL) | |
2864 #endif | |
2865 hFile = CreateFile(fname, /* file name */ | |
2866 GENERIC_READ, /* access mode */ | |
2793 | 2867 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
696 | 2868 NULL, /* security descriptor */ |
2869 OPEN_EXISTING, /* creation disposition */ | |
2793 | 2870 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
696 | 2871 NULL); /* handle to template file */ |
2872 | |
2873 if (hFile != INVALID_HANDLE_VALUE) | |
2874 { | |
2793 | 2875 if (GetFileInformationByHandle(hFile, info) != 0) |
2876 res = FILEINFO_OK; | |
2877 else | |
2878 res = FILEINFO_INFO_FAIL; | |
696 | 2879 CloseHandle(hFile); |
2880 } | |
2881 | |
2882 #ifdef FEAT_MBYTE | |
2883 vim_free(wn); | |
2884 #endif | |
2885 return res; | |
2886 } | |
2887 | |
2888 /* | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2889 * get file attributes for `name' |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2890 * -1 : error |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2891 * else FILE_ATTRIBUTE_* defined in winnt.h |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2892 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2893 static |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2894 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2895 win32_getattrs(char_u *name) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2896 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2897 int attr; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2898 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2899 WCHAR *p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2900 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2901 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2902 p = enc_to_utf16(name, 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 if (p != NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2905 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2906 attr = GetFileAttributesW(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2907 if (attr < 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2908 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2909 /* Retry with non-wide function (for Windows 98). */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2910 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2911 p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2912 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2913 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2914 if (p == NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2915 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2916 attr = GetFileAttributes((char *)name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2917 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2918 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2919 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2920 return attr; |
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 |
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 * set file attributes for `name' to `attrs' |
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 * return -1 for failure, 0 otherwise |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2927 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2928 static |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2929 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2930 win32_setattrs(char_u *name, int attrs) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2931 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2932 int res; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2933 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2934 WCHAR *p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2935 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2936 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2937 p = enc_to_utf16(name, 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 if (p != NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2940 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2941 res = SetFileAttributesW(p, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2942 if (res == FALSE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2943 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2944 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2945 /* Retry with non-wide function (for Windows 98). */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2946 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2947 p = NULL; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2948 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2949 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2950 if (p == NULL) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2951 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2952 res = SetFileAttributes((char *)name, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2953 #ifdef FEAT_MBYTE |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2954 vim_free(p); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2955 #endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2956 return res ? 0 : -1; |
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 |
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 * Set archive flag for "name". |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2961 */ |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2962 static |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2963 int |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2964 win32_set_archive(char_u *name) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2965 { |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2966 int attrs = win32_getattrs(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2967 if (attrs == -1) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2968 return -1; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2969 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2970 attrs |= FILE_ATTRIBUTE_ARCHIVE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2971 return win32_setattrs(name, attrs); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2972 } |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2973 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2974 /* |
7 | 2975 * Return TRUE if file or directory "name" is writable (not readonly). |
2976 * Strange semantics of Win32: a readonly directory is writable, but you can't | |
2977 * delete a file. Let's say this means it is writable. | |
2978 */ | |
2979 int | |
2980 mch_writable(char_u *name) | |
2981 { | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2982 int attrs = win32_getattrs(name); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2983 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2984 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
2985 || (attrs & FILE_ATTRIBUTE_DIRECTORY))); |
7 | 2986 } |
2987 | |
2988 /* | |
2989 * Return 1 if "name" can be executed, 0 if not. | |
2990 * Return -1 if unknown. | |
2991 */ | |
2992 int | |
2993 mch_can_exe(char_u *name) | |
2994 { | |
10 | 2995 char_u buf[_MAX_PATH]; |
835 | 2996 int len = (int)STRLEN(name); |
10 | 2997 char_u *p; |
2998 | |
2999 if (len >= _MAX_PATH) /* safety check */ | |
3000 return FALSE; | |
3001 | |
3002 /* If there already is an extension try using the name directly. Also do | |
3003 * this with a Unix-shell like 'shell'. */ | |
3004 if (vim_strchr(gettail(name), '.') != NULL | |
3005 || strstr((char *)gettail(p_sh), "sh") != NULL) | |
3006 if (executable_exists((char *)name)) | |
3007 return TRUE; | |
3008 | |
3009 /* | |
3010 * Loop over all extensions in $PATHEXT. | |
3011 */ | |
417 | 3012 vim_strncpy(buf, name, _MAX_PATH - 1); |
10 | 3013 p = mch_getenv("PATHEXT"); |
3014 if (p == NULL) | |
3015 p = (char_u *)".com;.exe;.bat;.cmd"; | |
3016 while (*p) | |
3017 { | |
3018 if (p[0] == '.' && (p[1] == NUL || p[1] == ';')) | |
3019 { | |
3020 /* A single "." means no extension is added. */ | |
3021 buf[len] = NUL; | |
3022 ++p; | |
3023 if (*p) | |
3024 ++p; | |
3025 } | |
3026 else | |
3027 copy_option_part(&p, buf + len, _MAX_PATH - len, ";"); | |
3028 if (executable_exists((char *)buf)) | |
3029 return TRUE; | |
3030 } | |
3031 return FALSE; | |
7 | 3032 } |
3033 | |
3034 /* | |
3035 * Check what "name" is: | |
3036 * NODE_NORMAL: file or directory (or doesn't exist) | |
3037 * NODE_WRITABLE: writable device, socket, fifo, etc. | |
3038 * NODE_OTHER: non-writable things | |
3039 */ | |
3040 int | |
3041 mch_nodetype(char_u *name) | |
3042 { | |
3043 HANDLE hFile; | |
3044 int type; | |
3045 | |
1004 | 3046 /* We can't open a file with a name "\\.\con" or "\\.\prn" and trying to |
3047 * read from it later will cause Vim to hang. Thus return NODE_WRITABLE | |
3048 * here. */ | |
3049 if (STRNCMP(name, "\\\\.\\", 4) == 0) | |
3050 return NODE_WRITABLE; | |
3051 | |
7 | 3052 hFile = CreateFile(name, /* file name */ |
3053 GENERIC_WRITE, /* access mode */ | |
3054 0, /* share mode */ | |
3055 NULL, /* security descriptor */ | |
3056 OPEN_EXISTING, /* creation disposition */ | |
3057 0, /* file attributes */ | |
3058 NULL); /* handle to template file */ | |
3059 | |
3060 if (hFile == INVALID_HANDLE_VALUE) | |
3061 return NODE_NORMAL; | |
3062 | |
3063 type = GetFileType(hFile); | |
3064 CloseHandle(hFile); | |
3065 if (type == FILE_TYPE_CHAR) | |
3066 return NODE_WRITABLE; | |
3067 if (type == FILE_TYPE_DISK) | |
3068 return NODE_NORMAL; | |
3069 return NODE_OTHER; | |
3070 } | |
3071 | |
3072 #ifdef HAVE_ACL | |
3073 struct my_acl | |
3074 { | |
3075 PSECURITY_DESCRIPTOR pSecurityDescriptor; | |
3076 PSID pSidOwner; | |
3077 PSID pSidGroup; | |
3078 PACL pDacl; | |
3079 PACL pSacl; | |
3080 }; | |
3081 #endif | |
3082 | |
3083 /* | |
3084 * Return a pointer to the ACL of file "fname" in allocated memory. | |
3085 * Return NULL if the ACL is not available for whatever reason. | |
3086 */ | |
3087 vim_acl_T | |
26 | 3088 mch_get_acl(char_u *fname) |
7 | 3089 { |
3090 #ifndef HAVE_ACL | |
3091 return (vim_acl_T)NULL; | |
3092 #else | |
3093 struct my_acl *p = NULL; | |
3094 | |
3095 /* This only works on Windows NT and 2000. */ | |
3096 if (g_PlatformId == VER_PLATFORM_WIN32_NT && advapi_lib != NULL) | |
3097 { | |
3098 p = (struct my_acl *)alloc_clear((unsigned)sizeof(struct my_acl)); | |
3099 if (p != NULL) | |
3100 { | |
3101 if (pGetNamedSecurityInfo( | |
3102 (LPTSTR)fname, // Abstract filename | |
3103 SE_FILE_OBJECT, // File Object | |
3104 // Retrieve the entire security descriptor. | |
3105 OWNER_SECURITY_INFORMATION | | |
3106 GROUP_SECURITY_INFORMATION | | |
3107 DACL_SECURITY_INFORMATION | | |
3108 SACL_SECURITY_INFORMATION, | |
3109 &p->pSidOwner, // Ownership information. | |
3110 &p->pSidGroup, // Group membership. | |
3111 &p->pDacl, // Discretionary information. | |
3112 &p->pSacl, // For auditing purposes. | |
3113 &p->pSecurityDescriptor | |
3114 ) != ERROR_SUCCESS) | |
3115 { | |
3116 mch_free_acl((vim_acl_T)p); | |
3117 p = NULL; | |
3118 } | |
3119 } | |
3120 } | |
3121 | |
3122 return (vim_acl_T)p; | |
3123 #endif | |
3124 } | |
3125 | |
3126 /* | |
3127 * Set the ACL of file "fname" to "acl" (unless it's NULL). | |
3128 * Errors are ignored. | |
3129 * This must only be called with "acl" equal to what mch_get_acl() returned. | |
3130 */ | |
3131 void | |
26 | 3132 mch_set_acl(char_u *fname, vim_acl_T acl) |
7 | 3133 { |
3134 #ifdef HAVE_ACL | |
3135 struct my_acl *p = (struct my_acl *)acl; | |
3136 | |
3137 if (p != NULL && advapi_lib != NULL) | |
3138 (void)pSetNamedSecurityInfo( | |
3139 (LPTSTR)fname, // Abstract filename | |
3140 SE_FILE_OBJECT, // File Object | |
3141 // Retrieve the entire security descriptor. | |
3142 OWNER_SECURITY_INFORMATION | | |
3143 GROUP_SECURITY_INFORMATION | | |
3144 DACL_SECURITY_INFORMATION | | |
3145 SACL_SECURITY_INFORMATION, | |
3146 p->pSidOwner, // Ownership information. | |
3147 p->pSidGroup, // Group membership. | |
3148 p->pDacl, // Discretionary information. | |
3149 p->pSacl // For auditing purposes. | |
3150 ); | |
3151 #endif | |
3152 } | |
3153 | |
3154 void | |
26 | 3155 mch_free_acl(vim_acl_T acl) |
7 | 3156 { |
3157 #ifdef HAVE_ACL | |
3158 struct my_acl *p = (struct my_acl *)acl; | |
3159 | |
3160 if (p != NULL) | |
3161 { | |
3162 LocalFree(p->pSecurityDescriptor); // Free the memory just in case | |
3163 vim_free(p); | |
3164 } | |
3165 #endif | |
3166 } | |
3167 | |
3168 #ifndef FEAT_GUI_W32 | |
3169 | |
3170 /* | |
3171 * handler for ctrl-break, ctrl-c interrupts, and fatal events. | |
3172 */ | |
3173 static BOOL WINAPI | |
3174 handler_routine( | |
3175 DWORD dwCtrlType) | |
3176 { | |
3177 switch (dwCtrlType) | |
3178 { | |
3179 case CTRL_C_EVENT: | |
3180 if (ctrl_c_interrupts) | |
3181 g_fCtrlCPressed = TRUE; | |
3182 return TRUE; | |
3183 | |
3184 case CTRL_BREAK_EVENT: | |
3185 g_fCBrkPressed = TRUE; | |
3186 return TRUE; | |
3187 | |
3188 /* fatal events: shut down gracefully */ | |
3189 case CTRL_CLOSE_EVENT: | |
3190 case CTRL_LOGOFF_EVENT: | |
3191 case CTRL_SHUTDOWN_EVENT: | |
3192 windgoto((int)Rows - 1, 0); | |
3193 g_fForceExit = TRUE; | |
3194 | |
1569 | 3195 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"), |
7 | 3196 (dwCtrlType == CTRL_CLOSE_EVENT |
3197 ? _("close") | |
3198 : dwCtrlType == CTRL_LOGOFF_EVENT | |
3199 ? _("logoff") | |
3200 : _("shutdown"))); | |
3201 #ifdef DEBUG | |
3202 OutputDebugString(IObuff); | |
3203 #endif | |
3204 | |
3205 preserve_exit(); /* output IObuff, preserve files and exit */ | |
3206 | |
3207 return TRUE; /* not reached */ | |
3208 | |
3209 default: | |
3210 return FALSE; | |
3211 } | |
3212 } | |
3213 | |
3214 | |
3215 /* | |
3216 * set the tty in (raw) ? "raw" : "cooked" mode | |
3217 */ | |
3218 void | |
26 | 3219 mch_settmode(int tmode) |
7 | 3220 { |
3221 DWORD cmodein; | |
3222 DWORD cmodeout; | |
3223 BOOL bEnableHandler; | |
3224 | |
3225 GetConsoleMode(g_hConIn, &cmodein); | |
3226 GetConsoleMode(g_hConOut, &cmodeout); | |
3227 if (tmode == TMODE_RAW) | |
3228 { | |
3229 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | | |
3230 ENABLE_ECHO_INPUT); | |
3231 #ifdef FEAT_MOUSE | |
3232 if (g_fMouseActive) | |
3233 cmodein |= ENABLE_MOUSE_INPUT; | |
3234 #endif | |
3235 cmodeout &= ~(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); | |
3236 bEnableHandler = TRUE; | |
3237 } | |
3238 else /* cooked */ | |
3239 { | |
3240 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | | |
3241 ENABLE_ECHO_INPUT); | |
3242 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); | |
3243 bEnableHandler = FALSE; | |
3244 } | |
3245 SetConsoleMode(g_hConIn, cmodein); | |
3246 SetConsoleMode(g_hConOut, cmodeout); | |
3247 SetConsoleCtrlHandler(handler_routine, bEnableHandler); | |
3248 | |
3249 #ifdef MCH_WRITE_DUMP | |
3250 if (fdDump) | |
3251 { | |
3252 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n", | |
3253 tmode == TMODE_RAW ? "raw" : | |
3254 tmode == TMODE_COOK ? "cooked" : "normal", | |
3255 cmodein, cmodeout); | |
3256 fflush(fdDump); | |
3257 } | |
3258 #endif | |
3259 } | |
3260 | |
3261 | |
3262 /* | |
3263 * Get the size of the current window in `Rows' and `Columns' | |
3264 * Return OK when size could be determined, FAIL otherwise. | |
3265 */ | |
3266 int | |
26 | 3267 mch_get_shellsize(void) |
7 | 3268 { |
3269 CONSOLE_SCREEN_BUFFER_INFO csbi; | |
3270 | |
3271 if (!g_fTermcapMode && g_cbTermcap.IsValid) | |
3272 { | |
3273 /* | |
3274 * For some reason, we are trying to get the screen dimensions | |
3275 * even though we are not in termcap mode. The 'Rows' and 'Columns' | |
3276 * variables are really intended to mean the size of Vim screen | |
3277 * while in termcap mode. | |
3278 */ | |
3279 Rows = g_cbTermcap.Info.dwSize.Y; | |
3280 Columns = g_cbTermcap.Info.dwSize.X; | |
3281 } | |
3282 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
3283 { | |
3284 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; | |
3285 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; | |
3286 } | |
3287 else | |
3288 { | |
3289 Rows = 25; | |
3290 Columns = 80; | |
3291 } | |
3292 return OK; | |
3293 } | |
3294 | |
3295 /* | |
3296 * Set a console window to `xSize' * `ySize' | |
3297 */ | |
3298 static void | |
3299 ResizeConBufAndWindow( | |
26 | 3300 HANDLE hConsole, |
3301 int xSize, | |
3302 int ySize) | |
7 | 3303 { |
3304 CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */ | |
3305 SMALL_RECT srWindowRect; /* hold the new console size */ | |
3306 COORD coordScreen; | |
3307 | |
3308 #ifdef MCH_WRITE_DUMP | |
3309 if (fdDump) | |
3310 { | |
3311 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize); | |
3312 fflush(fdDump); | |
3313 } | |
3314 #endif | |
3315 | |
3316 /* get the largest size we can size the console window to */ | |
3317 coordScreen = GetLargestConsoleWindowSize(hConsole); | |
3318 | |
3319 /* define the new console window size and scroll position */ | |
3320 srWindowRect.Left = srWindowRect.Top = (SHORT) 0; | |
3321 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1); | |
3322 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1); | |
3323 | |
3324 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) | |
3325 { | |
3326 int sx, sy; | |
3327 | |
3328 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1; | |
3329 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; | |
3330 if (sy < ySize || sx < xSize) | |
3331 { | |
3332 /* | |
3333 * Increasing number of lines/columns, do buffer first. | |
3334 * Use the maximal size in x and y direction. | |
3335 */ | |
3336 if (sy < ySize) | |
3337 coordScreen.Y = ySize; | |
3338 else | |
3339 coordScreen.Y = sy; | |
3340 if (sx < xSize) | |
3341 coordScreen.X = xSize; | |
3342 else | |
3343 coordScreen.X = sx; | |
3344 SetConsoleScreenBufferSize(hConsole, coordScreen); | |
3345 } | |
3346 } | |
3347 | |
3348 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &srWindowRect)) | |
3349 { | |
3350 #ifdef MCH_WRITE_DUMP | |
3351 if (fdDump) | |
3352 { | |
3353 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n", | |
3354 GetLastError()); | |
3355 fflush(fdDump); | |
3356 } | |
3357 #endif | |
3358 } | |
3359 | |
3360 /* define the new console buffer size */ | |
3361 coordScreen.X = xSize; | |
3362 coordScreen.Y = ySize; | |
3363 | |
3364 if (!SetConsoleScreenBufferSize(hConsole, coordScreen)) | |
3365 { | |
3366 #ifdef MCH_WRITE_DUMP | |
3367 if (fdDump) | |
3368 { | |
3369 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n", | |
3370 GetLastError()); | |
3371 fflush(fdDump); | |
3372 } | |
3373 #endif | |
3374 } | |
3375 } | |
3376 | |
3377 | |
3378 /* | |
3379 * Set the console window to `Rows' * `Columns' | |
3380 */ | |
3381 void | |
26 | 3382 mch_set_shellsize(void) |
7 | 3383 { |
3384 COORD coordScreen; | |
3385 | |
3386 /* Don't change window size while still starting up */ | |
3387 if (suppress_winsize != 0) | |
3388 { | |
3389 suppress_winsize = 2; | |
3390 return; | |
3391 } | |
3392 | |
3393 if (term_console) | |
3394 { | |
3395 coordScreen = GetLargestConsoleWindowSize(g_hConOut); | |
3396 | |
3397 /* Clamp Rows and Columns to reasonable values */ | |
3398 if (Rows > coordScreen.Y) | |
3399 Rows = coordScreen.Y; | |
3400 if (Columns > coordScreen.X) | |
3401 Columns = coordScreen.X; | |
3402 | |
3403 ResizeConBufAndWindow(g_hConOut, Columns, Rows); | |
3404 } | |
3405 } | |
3406 | |
3407 /* | |
3408 * Rows and/or Columns has changed. | |
3409 */ | |
3410 void | |
26 | 3411 mch_new_shellsize(void) |
7 | 3412 { |
3413 set_scroll_region(0, 0, Columns - 1, Rows - 1); | |
3414 } | |
3415 | |
3416 | |
3417 /* | |
3418 * Called when started up, to set the winsize that was delayed. | |
3419 */ | |
3420 void | |
26 | 3421 mch_set_winsize_now(void) |
7 | 3422 { |
3423 if (suppress_winsize == 2) | |
3424 { | |
3425 suppress_winsize = 0; | |
3426 mch_set_shellsize(); | |
3427 shell_resized(); | |
3428 } | |
3429 suppress_winsize = 0; | |
3430 } | |
3431 #endif /* FEAT_GUI_W32 */ | |
3432 | |
3433 | |
3434 | |
3435 #if defined(FEAT_GUI_W32) || defined(PROTO) | |
3436 | |
3437 /* | |
3438 * Specialised version of system() for Win32 GUI mode. | |
3439 * This version proceeds as follows: | |
3440 * 1. Create a console window for use by the subprocess | |
3441 * 2. Run the subprocess (it gets the allocated console by default) | |
3442 * 3. Wait for the subprocess to terminate and get its exit code | |
3443 * 4. Prompt the user to press a key to close the console window | |
3444 */ | |
3445 static int | |
2935 | 3446 mch_system_classic(char *cmd, int options) |
7 | 3447 { |
3448 STARTUPINFO si; | |
3449 PROCESS_INFORMATION pi; | |
3450 DWORD ret = 0; | |
3451 HWND hwnd = GetFocus(); | |
3452 | |
3453 si.cb = sizeof(si); | |
3454 si.lpReserved = NULL; | |
3455 si.lpDesktop = NULL; | |
3456 si.lpTitle = NULL; | |
3457 si.dwFlags = STARTF_USESHOWWINDOW; | |
3458 /* | |
3459 * It's nicer to run a filter command in a minimized window, but in | |
3460 * Windows 95 this makes the command MUCH slower. We can't do it under | |
3461 * Win32s either as it stops the synchronous spawn workaround working. | |
2643 | 3462 * Don't activate the window to keep focus on Vim. |
7 | 3463 */ |
3464 if ((options & SHELL_DOOUT) && !mch_windows95() && !gui_is_win32s()) | |
2643 | 3465 si.wShowWindow = SW_SHOWMINNOACTIVE; |
7 | 3466 else |
3467 si.wShowWindow = SW_SHOWNORMAL; | |
3468 si.cbReserved2 = 0; | |
3469 si.lpReserved2 = NULL; | |
3470 | |
3471 /* There is a strange error on Windows 95 when using "c:\\command.com". | |
3472 * When the "c:\\" is left out it works OK...? */ | |
3473 if (mch_windows95() | |
3474 && (STRNICMP(cmd, "c:/command.com", 14) == 0 | |
3475 || STRNICMP(cmd, "c:\\command.com", 14) == 0)) | |
3476 cmd += 3; | |
3477 | |
3478 /* Now, run the command */ | |
3479 CreateProcess(NULL, /* Executable name */ | |
3480 cmd, /* Command to execute */ | |
3481 NULL, /* Process security attributes */ | |
3482 NULL, /* Thread security attributes */ | |
3483 FALSE, /* Inherit handles */ | |
3484 CREATE_DEFAULT_ERROR_MODE | /* Creation flags */ | |
3485 CREATE_NEW_CONSOLE, | |
3486 NULL, /* Environment */ | |
3487 NULL, /* Current directory */ | |
3488 &si, /* Startup information */ | |
3489 &pi); /* Process information */ | |
3490 | |
3491 | |
3492 /* Wait for the command to terminate before continuing */ | |
3493 if (g_PlatformId != VER_PLATFORM_WIN32s) | |
3494 { | |
3495 #ifdef FEAT_GUI | |
3496 int delay = 1; | |
3497 | |
3498 /* Keep updating the window while waiting for the shell to finish. */ | |
3499 for (;;) | |
3500 { | |
3501 MSG msg; | |
3502 | |
3010 | 3503 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
7 | 3504 { |
3505 TranslateMessage(&msg); | |
3010 | 3506 pDispatchMessage(&msg); |
3720 | 3507 delay = 1; |
3508 continue; | |
7 | 3509 } |
3510 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) | |
3511 break; | |
3512 | |
3513 /* We start waiting for a very short time and then increase it, so | |
3514 * that we respond quickly when the process is quick, and don't | |
3515 * consume too much overhead when it's slow. */ | |
3516 if (delay < 50) | |
3517 delay += 10; | |
3518 } | |
3519 #else | |
3520 WaitForSingleObject(pi.hProcess, INFINITE); | |
3521 #endif | |
3522 | |
3523 /* Get the command exit code */ | |
3524 GetExitCodeProcess(pi.hProcess, &ret); | |
3525 } | |
3526 else | |
3527 { | |
3528 /* | |
3529 * This ugly code is the only quick way of performing | |
3530 * a synchronous spawn under Win32s. Yuk. | |
3531 */ | |
3532 num_windows = 0; | |
3533 EnumWindows(win32ssynch_cb, 0); | |
3534 old_num_windows = num_windows; | |
3535 do | |
3536 { | |
3537 Sleep(1000); | |
3538 num_windows = 0; | |
3539 EnumWindows(win32ssynch_cb, 0); | |
3540 } while (num_windows == old_num_windows); | |
3541 ret = 0; | |
3542 } | |
3543 | |
3544 /* Close the handles to the subprocess, so that it goes away */ | |
3545 CloseHandle(pi.hThread); | |
3546 CloseHandle(pi.hProcess); | |
3547 | |
3548 /* Try to get input focus back. Doesn't always work though. */ | |
3549 PostMessage(hwnd, WM_SETFOCUS, 0, 0); | |
3550 | |
3551 return ret; | |
3552 } | |
2935 | 3553 |
3554 /* | |
3555 * Thread launched by the gui to send the current buffer data to the | |
3556 * process. This way avoid to hang up vim totally if the children | |
3557 * process take a long time to process the lines. | |
3558 */ | |
3559 static DWORD WINAPI | |
3560 sub_process_writer(LPVOID param) | |
3561 { | |
3562 HANDLE g_hChildStd_IN_Wr = param; | |
3563 linenr_T lnum = curbuf->b_op_start.lnum; | |
3564 DWORD len = 0; | |
3565 DWORD l; | |
3566 char_u *lp = ml_get(lnum); | |
3567 char_u *s; | |
3568 int written = 0; | |
3569 | |
3570 for (;;) | |
3571 { | |
3572 l = (DWORD)STRLEN(lp + written); | |
3573 if (l == 0) | |
3574 len = 0; | |
3575 else if (lp[written] == NL) | |
3576 { | |
3577 /* NL -> NUL translation */ | |
3578 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL); | |
3579 } | |
3580 else | |
3581 { | |
3582 s = vim_strchr(lp + written, NL); | |
3583 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written, | |
3584 s == NULL ? l : (DWORD)(s - (lp + written)), | |
3585 &len, NULL); | |
3586 } | |
3587 if (len == (int)l) | |
3588 { | |
3589 /* Finished a line, add a NL, unless this line should not have | |
3590 * one. */ | |
3591 if (lnum != curbuf->b_op_end.lnum | |
3592 || !curbuf->b_p_bin | |
3593 || (lnum != curbuf->b_no_eol_lnum | |
3594 && (lnum != curbuf->b_ml.ml_line_count | |
3595 || curbuf->b_p_eol))) | |
3596 { | |
4238 | 3597 WriteFile(g_hChildStd_IN_Wr, "\n", 1, (LPDWORD)&ignored, NULL); |
2935 | 3598 } |
3599 | |
3600 ++lnum; | |
3601 if (lnum > curbuf->b_op_end.lnum) | |
3602 break; | |
3603 | |
3604 lp = ml_get(lnum); | |
3605 written = 0; | |
3606 } | |
3607 else if (len > 0) | |
3608 written += len; | |
3609 } | |
3610 | |
3611 /* finished all the lines, close pipe */ | |
3612 CloseHandle(g_hChildStd_IN_Wr); | |
3613 ExitThread(0); | |
3614 } | |
3615 | |
3616 | |
3617 # define BUFLEN 100 /* length for buffer, stolen from unix version */ | |
3618 | |
3619 /* | |
3620 * This function read from the children's stdout and write the | |
3621 * data on screen or in the buffer accordingly. | |
3622 */ | |
3623 static void | |
3624 dump_pipe(int options, | |
3625 HANDLE g_hChildStd_OUT_Rd, | |
3626 garray_T *ga, | |
3627 char_u buffer[], | |
3628 DWORD *buffer_off) | |
3629 { | |
3630 DWORD availableBytes = 0; | |
3631 DWORD i; | |
3632 int ret; | |
3633 DWORD len; | |
3634 DWORD toRead; | |
3635 int repeatCount; | |
3636 | |
3637 /* we query the pipe to see if there is any data to read | |
3638 * to avoid to perform a blocking read */ | |
3639 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, /* pipe to query */ | |
3640 NULL, /* optional buffer */ | |
3622 | 3641 0, /* buffer size */ |
2935 | 3642 NULL, /* number of read bytes */ |
3643 &availableBytes, /* available bytes total */ | |
3644 NULL); /* byteLeft */ | |
3645 | |
3646 repeatCount = 0; | |
3647 /* We got real data in the pipe, read it */ | |
3622 | 3648 while (ret != 0 && availableBytes > 0) |
2935 | 3649 { |
3650 repeatCount++; | |
3651 toRead = | |
3652 # ifdef FEAT_MBYTE | |
3653 (DWORD)(BUFLEN - *buffer_off); | |
3654 # else | |
3655 (DWORD)BUFLEN; | |
3656 # endif | |
3657 toRead = availableBytes < toRead ? availableBytes : toRead; | |
3658 ReadFile(g_hChildStd_OUT_Rd, buffer | |
3659 # ifdef FEAT_MBYTE | |
3660 + *buffer_off, toRead | |
3661 # else | |
3662 , toRead | |
3663 # endif | |
3664 , &len, NULL); | |
3665 | |
3666 /* If we haven't read anything, there is a problem */ | |
3667 if (len == 0) | |
3668 break; | |
3669 | |
3670 availableBytes -= len; | |
3671 | |
3672 if (options & SHELL_READ) | |
3673 { | |
3674 /* Do NUL -> NL translation, append NL separated | |
3675 * lines to the current buffer. */ | |
3676 for (i = 0; i < len; ++i) | |
3677 { | |
3678 if (buffer[i] == NL) | |
3679 append_ga_line(ga); | |
3680 else if (buffer[i] == NUL) | |
3681 ga_append(ga, NL); | |
3682 else | |
3683 ga_append(ga, buffer[i]); | |
3684 } | |
3685 } | |
3686 # ifdef FEAT_MBYTE | |
3687 else if (has_mbyte) | |
3688 { | |
3689 int l; | |
3030 | 3690 int c; |
3691 char_u *p; | |
2935 | 3692 |
3693 len += *buffer_off; | |
3694 buffer[len] = NUL; | |
3695 | |
3696 /* Check if the last character in buffer[] is | |
3697 * incomplete, keep these bytes for the next | |
3698 * round. */ | |
3699 for (p = buffer; p < buffer + len; p += l) | |
3700 { | |
3701 l = mb_cptr2len(p); | |
3702 if (l == 0) | |
3703 l = 1; /* NUL byte? */ | |
3704 else if (MB_BYTE2LEN(*p) != l) | |
3705 break; | |
3706 } | |
3707 if (p == buffer) /* no complete character */ | |
3708 { | |
3709 /* avoid getting stuck at an illegal byte */ | |
3710 if (len >= 12) | |
3711 ++p; | |
3712 else | |
3713 { | |
3714 *buffer_off = len; | |
3715 return; | |
3716 } | |
3717 } | |
3718 c = *p; | |
3719 *p = NUL; | |
3720 msg_puts(buffer); | |
3721 if (p < buffer + len) | |
3722 { | |
3723 *p = c; | |
3724 *buffer_off = (DWORD)((buffer + len) - p); | |
3725 mch_memmove(buffer, p, *buffer_off); | |
3726 return; | |
3727 } | |
3728 *buffer_off = 0; | |
3729 } | |
3730 # endif /* FEAT_MBYTE */ | |
3731 else | |
3732 { | |
3733 buffer[len] = NUL; | |
3734 msg_puts(buffer); | |
3735 } | |
3736 | |
3737 windgoto(msg_row, msg_col); | |
3738 cursor_on(); | |
3739 out_flush(); | |
3740 } | |
3741 } | |
3742 | |
3743 /* | |
3744 * Version of system to use for windows NT > 5.0 (Win2K), use pipe | |
3745 * for communication and doesn't open any new window. | |
3746 */ | |
3747 static int | |
3748 mch_system_piped(char *cmd, int options) | |
3749 { | |
3750 STARTUPINFO si; | |
3751 PROCESS_INFORMATION pi; | |
3752 DWORD ret = 0; | |
3753 | |
3754 HANDLE g_hChildStd_IN_Rd = NULL; | |
3755 HANDLE g_hChildStd_IN_Wr = NULL; | |
3756 HANDLE g_hChildStd_OUT_Rd = NULL; | |
3757 HANDLE g_hChildStd_OUT_Wr = NULL; | |
3758 | |
3759 char_u buffer[BUFLEN + 1]; /* reading buffer + size */ | |
3760 DWORD len; | |
3761 | |
3762 /* buffer used to receive keys */ | |
3763 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ | |
3764 int ta_len = 0; /* valid bytes in ta_buf[] */ | |
3765 | |
3766 DWORD i; | |
3767 int c; | |
3768 int noread_cnt = 0; | |
3769 garray_T ga; | |
3770 int delay = 1; | |
3771 DWORD buffer_off = 0; /* valid bytes in buffer[] */ | |
3361 | 3772 char *p = NULL; |
2935 | 3773 |
3774 SECURITY_ATTRIBUTES saAttr; | |
3775 | |
3776 /* Set the bInheritHandle flag so pipe handles are inherited. */ | |
3777 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); | |
3778 saAttr.bInheritHandle = TRUE; | |
3779 saAttr.lpSecurityDescriptor = NULL; | |
3780 | |
3781 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) | |
3782 /* Ensure the read handle to the pipe for STDOUT is not inherited. */ | |
3783 || ! pSetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) | |
3784 /* Create a pipe for the child process's STDIN. */ | |
3785 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0) | |
3786 /* Ensure the write handle to the pipe for STDIN is not inherited. */ | |
3787 || ! pSetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) | |
3788 { | |
3789 CloseHandle(g_hChildStd_IN_Rd); | |
3790 CloseHandle(g_hChildStd_IN_Wr); | |
3791 CloseHandle(g_hChildStd_OUT_Rd); | |
3792 CloseHandle(g_hChildStd_OUT_Wr); | |
3793 MSG_PUTS(_("\nCannot create pipes\n")); | |
3794 } | |
3795 | |
3796 si.cb = sizeof(si); | |
3797 si.lpReserved = NULL; | |
3798 si.lpDesktop = NULL; | |
3799 si.lpTitle = NULL; | |
3800 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; | |
3801 | |
3802 /* set-up our file redirection */ | |
3803 si.hStdError = g_hChildStd_OUT_Wr; | |
3804 si.hStdOutput = g_hChildStd_OUT_Wr; | |
3805 si.hStdInput = g_hChildStd_IN_Rd; | |
3806 si.wShowWindow = SW_HIDE; | |
3807 si.cbReserved2 = 0; | |
3808 si.lpReserved2 = NULL; | |
3809 | |
3810 if (options & SHELL_READ) | |
3811 ga_init2(&ga, 1, BUFLEN); | |
3812 | |
3361 | 3813 if (cmd != NULL) |
3814 { | |
3815 p = (char *)vim_strsave((char_u *)cmd); | |
3816 if (p != NULL) | |
3817 unescape_shellxquote((char_u *)p, p_sxe); | |
3818 else | |
3819 p = cmd; | |
3820 } | |
3821 | |
2935 | 3822 /* Now, run the command */ |
3823 CreateProcess(NULL, /* Executable name */ | |
3361 | 3824 p, /* Command to execute */ |
2935 | 3825 NULL, /* Process security attributes */ |
3826 NULL, /* Thread security attributes */ | |
3827 | |
3622 | 3828 // this command can be litigious, handle inheritance was |
2935 | 3829 // deactivated for pending temp file, but, if we deactivate |
3830 // it, the pipes don't work for some reason. | |
3831 TRUE, /* Inherit handles, first deactivated, | |
3832 * but needed */ | |
3833 CREATE_DEFAULT_ERROR_MODE, /* Creation flags */ | |
3834 NULL, /* Environment */ | |
3835 NULL, /* Current directory */ | |
3836 &si, /* Startup information */ | |
3837 &pi); /* Process information */ | |
3838 | |
3361 | 3839 if (p != cmd) |
3840 vim_free(p); | |
2935 | 3841 |
3842 /* Close our unused side of the pipes */ | |
3843 CloseHandle(g_hChildStd_IN_Rd); | |
3844 CloseHandle(g_hChildStd_OUT_Wr); | |
3845 | |
3846 if (options & SHELL_WRITE) | |
3847 { | |
3848 HANDLE thread = | |
3849 CreateThread(NULL, /* security attributes */ | |
3850 0, /* default stack size */ | |
3851 sub_process_writer, /* function to be executed */ | |
3852 g_hChildStd_IN_Wr, /* parameter */ | |
3853 0, /* creation flag, start immediately */ | |
3854 NULL); /* we don't care about thread id */ | |
3855 CloseHandle(thread); | |
3856 g_hChildStd_IN_Wr = NULL; | |
3857 } | |
3858 | |
3859 /* Keep updating the window while waiting for the shell to finish. */ | |
3860 for (;;) | |
3861 { | |
3862 MSG msg; | |
3863 | |
3864 if (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) | |
3865 { | |
3866 TranslateMessage(&msg); | |
3867 DispatchMessage(&msg); | |
3868 } | |
3869 | |
3870 /* write pipe information in the window */ | |
3871 if ((options & (SHELL_READ|SHELL_WRITE)) | |
3872 # ifdef FEAT_GUI | |
3873 || gui.in_use | |
3874 # endif | |
3875 ) | |
3876 { | |
3877 len = 0; | |
3878 if (!(options & SHELL_EXPAND) | |
3879 && ((options & | |
3880 (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) | |
3881 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) | |
3882 # ifdef FEAT_GUI | |
3883 || gui.in_use | |
3884 # endif | |
3885 ) | |
3886 && (ta_len > 0 || noread_cnt > 4)) | |
3887 { | |
3888 if (ta_len == 0) | |
3889 { | |
3890 /* Get extra characters when we don't have any. Reset the | |
3891 * counter and timer. */ | |
3892 noread_cnt = 0; | |
3893 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
3894 gettimeofday(&start_tv, NULL); | |
3895 # endif | |
3896 len = ui_inchar(ta_buf, BUFLEN, 10L, 0); | |
3897 } | |
3898 if (ta_len > 0 || len > 0) | |
3899 { | |
3900 /* | |
3901 * For pipes: Check for CTRL-C: send interrupt signal to | |
3902 * child. Check for CTRL-D: EOF, close pipe to child. | |
3903 */ | |
3904 if (len == 1 && cmd != NULL) | |
3905 { | |
3906 if (ta_buf[ta_len] == Ctrl_C) | |
3907 { | |
3908 /* Learn what exit code is expected, for | |
3909 * now put 9 as SIGKILL */ | |
3910 TerminateProcess(pi.hProcess, 9); | |
3911 } | |
3912 if (ta_buf[ta_len] == Ctrl_D) | |
3913 { | |
3914 CloseHandle(g_hChildStd_IN_Wr); | |
3915 g_hChildStd_IN_Wr = NULL; | |
3916 } | |
3917 } | |
3918 | |
3919 /* replace K_BS by <BS> and K_DEL by <DEL> */ | |
3920 for (i = ta_len; i < ta_len + len; ++i) | |
3921 { | |
3922 if (ta_buf[i] == CSI && len - i > 2) | |
3923 { | |
3924 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); | |
3925 if (c == K_DEL || c == K_KDEL || c == K_BS) | |
3926 { | |
3927 mch_memmove(ta_buf + i + 1, ta_buf + i + 3, | |
3928 (size_t)(len - i - 2)); | |
3929 if (c == K_DEL || c == K_KDEL) | |
3930 ta_buf[i] = DEL; | |
3931 else | |
3932 ta_buf[i] = Ctrl_H; | |
3933 len -= 2; | |
3934 } | |
3935 } | |
3936 else if (ta_buf[i] == '\r') | |
3937 ta_buf[i] = '\n'; | |
3938 # ifdef FEAT_MBYTE | |
3939 if (has_mbyte) | |
3940 i += (*mb_ptr2len_len)(ta_buf + i, | |
3941 ta_len + len - i) - 1; | |
3942 # endif | |
3943 } | |
3944 | |
3945 /* | |
3946 * For pipes: echo the typed characters. For a pty this | |
3947 * does not seem to work. | |
3948 */ | |
3949 for (i = ta_len; i < ta_len + len; ++i) | |
3950 { | |
3951 if (ta_buf[i] == '\n' || ta_buf[i] == '\b') | |
3952 msg_putchar(ta_buf[i]); | |
3953 # ifdef FEAT_MBYTE | |
3954 else if (has_mbyte) | |
3955 { | |
3956 int l = (*mb_ptr2len)(ta_buf + i); | |
3957 | |
3958 msg_outtrans_len(ta_buf + i, l); | |
3959 i += l - 1; | |
3960 } | |
3961 # endif | |
3962 else | |
3963 msg_outtrans_len(ta_buf + i, 1); | |
3964 } | |
3965 windgoto(msg_row, msg_col); | |
3966 out_flush(); | |
3967 | |
3968 ta_len += len; | |
3969 | |
3970 /* | |
3971 * Write the characters to the child, unless EOF has been | |
3972 * typed for pipes. Write one character at a time, to | |
3973 * avoid losing too much typeahead. When writing buffer | |
3974 * lines, drop the typed characters (only check for | |
3975 * CTRL-C). | |
3976 */ | |
3977 if (options & SHELL_WRITE) | |
3978 ta_len = 0; | |
3979 else if (g_hChildStd_IN_Wr != NULL) | |
3980 { | |
3981 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf, | |
3982 1, &len, NULL); | |
3983 // if we are typing in, we want to keep things reactive | |
3984 delay = 1; | |
3985 if (len > 0) | |
3986 { | |
3987 ta_len -= len; | |
3988 mch_memmove(ta_buf, ta_buf + len, ta_len); | |
3989 } | |
3990 } | |
3991 } | |
3992 } | |
3993 } | |
3994 | |
3995 if (ta_len) | |
3996 ui_inchar_undo(ta_buf, ta_len); | |
3997 | |
3998 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) | |
3999 { | |
3030 | 4000 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
2935 | 4001 break; |
4002 } | |
4003 | |
4004 ++noread_cnt; | |
3030 | 4005 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off); |
2935 | 4006 |
4007 /* We start waiting for a very short time and then increase it, so | |
4008 * that we respond quickly when the process is quick, and don't | |
4009 * consume too much overhead when it's slow. */ | |
4010 if (delay < 50) | |
4011 delay += 10; | |
4012 } | |
4013 | |
4014 /* Close the pipe */ | |
4015 CloseHandle(g_hChildStd_OUT_Rd); | |
4016 if (g_hChildStd_IN_Wr != NULL) | |
4017 CloseHandle(g_hChildStd_IN_Wr); | |
4018 | |
4019 WaitForSingleObject(pi.hProcess, INFINITE); | |
4020 | |
4021 /* Get the command exit code */ | |
4022 GetExitCodeProcess(pi.hProcess, &ret); | |
4023 | |
4024 if (options & SHELL_READ) | |
4025 { | |
4026 if (ga.ga_len > 0) | |
4027 { | |
4028 append_ga_line(&ga); | |
4029 /* remember that the NL was missing */ | |
4030 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; | |
4031 } | |
4032 else | |
4033 curbuf->b_no_eol_lnum = 0; | |
4034 ga_clear(&ga); | |
4035 } | |
4036 | |
4037 /* Close the handles to the subprocess, so that it goes away */ | |
4038 CloseHandle(pi.hThread); | |
4039 CloseHandle(pi.hProcess); | |
4040 | |
4041 return ret; | |
4042 } | |
4043 | |
4044 static int | |
4045 mch_system(char *cmd, int options) | |
4046 { | |
4047 /* if we can pipe and the shelltemp option is off */ | |
4048 if (allowPiping && !p_stmp) | |
4049 return mch_system_piped(cmd, options); | |
4050 else | |
4051 return mch_system_classic(cmd, options); | |
4052 } | |
7 | 4053 #else |
4054 | |
4055 # define mch_system(c, o) system(c) | |
4056 | |
4057 #endif | |
4058 | |
4059 /* | |
4060 * Either execute a command by calling the shell or start a new shell | |
4061 */ | |
4062 int | |
4063 mch_call_shell( | |
26 | 4064 char_u *cmd, |
4065 int options) /* SHELL_*, see vim.h */ | |
7 | 4066 { |
4067 int x = 0; | |
4068 int tmode = cur_tmode; | |
4069 #ifdef FEAT_TITLE | |
4070 char szShellTitle[512]; | |
4071 | |
4072 /* Change the title to reflect that we are in a subshell. */ | |
4073 if (GetConsoleTitle(szShellTitle, sizeof(szShellTitle) - 4) > 0) | |
4074 { | |
4075 if (cmd == NULL) | |
4076 strcat(szShellTitle, " :sh"); | |
4077 else | |
4078 { | |
4079 strcat(szShellTitle, " - !"); | |
4080 if ((strlen(szShellTitle) + strlen(cmd) < sizeof(szShellTitle))) | |
4081 strcat(szShellTitle, cmd); | |
4082 } | |
4083 mch_settitle(szShellTitle, NULL); | |
4084 } | |
4085 #endif | |
4086 | |
4087 out_flush(); | |
4088 | |
4089 #ifdef MCH_WRITE_DUMP | |
4090 if (fdDump) | |
4091 { | |
4092 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options); | |
4093 fflush(fdDump); | |
4094 } | |
4095 #endif | |
4096 | |
4097 /* | |
4098 * Catch all deadly signals while running the external command, because a | |
4099 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us. | |
4100 */ | |
4101 signal(SIGINT, SIG_IGN); | |
4102 #if defined(__GNUC__) && !defined(__MINGW32__) | |
4103 signal(SIGKILL, SIG_IGN); | |
4104 #else | |
4105 signal(SIGBREAK, SIG_IGN); | |
4106 #endif | |
4107 signal(SIGILL, SIG_IGN); | |
4108 signal(SIGFPE, SIG_IGN); | |
4109 signal(SIGSEGV, SIG_IGN); | |
4110 signal(SIGTERM, SIG_IGN); | |
4111 signal(SIGABRT, SIG_IGN); | |
4112 | |
4113 if (options & SHELL_COOKED) | |
4114 settmode(TMODE_COOK); /* set to normal mode */ | |
4115 | |
4116 if (cmd == NULL) | |
4117 { | |
4118 x = mch_system(p_sh, options); | |
4119 } | |
4120 else | |
4121 { | |
4122 /* we use "command" or "cmd" to start the shell; slow but easy */ | |
3363 | 4123 char_u *newcmd = NULL; |
4124 char_u *cmdbase = cmd; | |
4125 long_u cmdlen; | |
3361 | 4126 |
4127 /* Skip a leading ", ( and "(. */ | |
4128 if (*cmdbase == '"' ) | |
4129 ++cmdbase; | |
4130 if (*cmdbase == '(') | |
4131 ++cmdbase; | |
4132 | |
4133 if ((STRNICMP(cmdbase, "start", 5) == 0) && vim_iswhite(cmdbase[5])) | |
4134 { | |
4135 STARTUPINFO si; | |
4136 PROCESS_INFORMATION pi; | |
4137 DWORD flags = CREATE_NEW_CONSOLE; | |
4138 char_u *p; | |
4139 | |
4140 si.cb = sizeof(si); | |
4141 si.lpReserved = NULL; | |
4142 si.lpDesktop = NULL; | |
4143 si.lpTitle = NULL; | |
4144 si.dwFlags = 0; | |
4145 si.cbReserved2 = 0; | |
4146 si.lpReserved2 = NULL; | |
4147 | |
4148 cmdbase = skipwhite(cmdbase + 5); | |
4149 if ((STRNICMP(cmdbase, "/min", 4) == 0) | |
4150 && vim_iswhite(cmdbase[4])) | |
4151 { | |
4152 cmdbase = skipwhite(cmdbase + 4); | |
4153 si.dwFlags = STARTF_USESHOWWINDOW; | |
4154 si.wShowWindow = SW_SHOWMINNOACTIVE; | |
4155 } | |
4156 else if ((STRNICMP(cmdbase, "/b", 2) == 0) | |
4157 && vim_iswhite(cmdbase[2])) | |
4158 { | |
4159 cmdbase = skipwhite(cmdbase + 2); | |
4160 flags = CREATE_NO_WINDOW; | |
4161 si.dwFlags = STARTF_USESTDHANDLES; | |
4162 si.hStdInput = CreateFile("\\\\.\\NUL", // File name | |
3363 | 4163 GENERIC_READ, // Access flags |
3361 | 4164 0, // Share flags |
3363 | 4165 NULL, // Security att. |
4166 OPEN_EXISTING, // Open flags | |
4167 FILE_ATTRIBUTE_NORMAL, // File att. | |
4168 NULL); // Temp file | |
3361 | 4169 si.hStdOutput = si.hStdInput; |
4170 si.hStdError = si.hStdInput; | |
4171 } | |
4172 | |
4173 /* Remove a trailing ", ) and )" if they have a match | |
4174 * at the start of the command. */ | |
4175 if (cmdbase > cmd) | |
4176 { | |
4177 p = cmdbase + STRLEN(cmdbase); | |
4178 if (p > cmdbase && p[-1] == '"' && *cmd == '"') | |
4179 *--p = NUL; | |
4180 if (p > cmdbase && p[-1] == ')' | |
4181 && (*cmd =='(' || cmd[1] == '(')) | |
4182 *--p = NUL; | |
4183 } | |
4184 | |
3363 | 4185 newcmd = cmdbase; |
4186 unescape_shellxquote(cmdbase, p_sxe); | |
4187 | |
3361 | 4188 /* |
3363 | 4189 * If creating new console, arguments are passed to the |
4190 * 'cmd.exe' as-is. If it's not, arguments are not treated | |
4191 * correctly for current 'cmd.exe'. So unescape characters in | |
4192 * shellxescape except '|' for avoiding to be treated as | |
4193 * argument to them. Pass the arguments to sub-shell. | |
3361 | 4194 */ |
3363 | 4195 if (flags != CREATE_NEW_CONSOLE) |
4196 { | |
4197 char_u *subcmd; | |
3367 | 4198 char_u *cmd_shell = mch_getenv("COMSPEC"); |
4199 | |
4200 if (cmd_shell == NULL || *cmd_shell == NUL) | |
4201 cmd_shell = default_shell(); | |
3363 | 4202 |
4203 subcmd = vim_strsave_escaped_ext(cmdbase, "|", '^', FALSE); | |
4204 if (subcmd != NULL) | |
4205 { | |
4206 /* make "cmd.exe /c arguments" */ | |
4207 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5; | |
4208 newcmd = lalloc(cmdlen, TRUE); | |
4209 if (newcmd != NULL) | |
4210 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s", | |
3367 | 4211 cmd_shell, subcmd); |
3363 | 4212 else |
4213 newcmd = cmdbase; | |
3367 | 4214 vim_free(subcmd); |
3363 | 4215 } |
4216 } | |
3361 | 4217 |
4218 /* | |
4219 * Now, start the command as a process, so that it doesn't | |
4220 * inherit our handles which causes unpleasant dangling swap | |
4221 * files if we exit before the spawned process | |
4222 */ | |
4223 if (CreateProcess(NULL, // Executable name | |
3363 | 4224 newcmd, // Command to execute |
3361 | 4225 NULL, // Process security attributes |
4226 NULL, // Thread security attributes | |
4227 FALSE, // Inherit handles | |
4228 flags, // Creation flags | |
4229 NULL, // Environment | |
4230 NULL, // Current directory | |
4231 &si, // Startup information | |
4232 &pi)) // Process information | |
4233 x = 0; | |
4234 else | |
4235 { | |
4236 x = -1; | |
4237 #ifdef FEAT_GUI_W32 | |
4238 EMSG(_("E371: Command not found")); | |
4239 #endif | |
4240 } | |
3363 | 4241 |
4242 if (newcmd != cmdbase) | |
4243 vim_free(newcmd); | |
4244 | |
3361 | 4245 if (si.hStdInput != NULL) |
4246 { | |
4247 /* Close the handle to \\.\NUL */ | |
4248 CloseHandle(si.hStdInput); | |
4249 } | |
4250 /* Close the handles to the subprocess, so that it goes away */ | |
4251 CloseHandle(pi.hThread); | |
4252 CloseHandle(pi.hProcess); | |
4253 } | |
4254 else | |
4255 { | |
3363 | 4256 cmdlen = ( |
7 | 4257 #ifdef FEAT_GUI_W32 |
2935 | 4258 (allowPiping && !p_stmp ? 0 : STRLEN(vimrun_path)) + |
7 | 4259 #endif |
1569 | 4260 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10); |
4261 | |
3361 | 4262 newcmd = lalloc(cmdlen, TRUE); |
4263 if (newcmd != NULL) | |
7 | 4264 { |
4265 #if defined(FEAT_GUI_W32) | |
4266 if (need_vimrun_warning) | |
4267 { | |
4268 MessageBox(NULL, | |
4269 _("VIMRUN.EXE not found in your $PATH.\n" | |
4270 "External commands will not pause after completion.\n" | |
4271 "See :help win32-vimrun for more information."), | |
4272 _("Vim Warning"), | |
4273 MB_ICONWARNING); | |
4274 need_vimrun_warning = FALSE; | |
4275 } | |
2935 | 4276 if (!s_dont_use_vimrun && (!allowPiping || p_stmp)) |
7 | 4277 /* Use vimrun to execute the command. It opens a console |
4278 * 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
|
4279 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s", |
7 | 4280 vimrun_path, |
4281 (msg_silent != 0 || (options & SHELL_DOOUT)) | |
4282 ? "-s " : "", | |
4283 p_sh, p_shcf, cmd); | |
4284 else | |
4285 #endif | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4286 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", |
1569 | 4287 p_sh, p_shcf, cmd); |
7 | 4288 x = mch_system((char *)newcmd, options); |
3361 | 4289 vim_free(newcmd); |
7 | 4290 } |
4291 } | |
4292 } | |
4293 | |
4294 if (tmode == TMODE_RAW) | |
4295 settmode(TMODE_RAW); /* set to raw mode */ | |
4296 | |
4297 /* Print the return value, unless "vimrun" was used. */ | |
4298 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent | |
4299 #if defined(FEAT_GUI_W32) | |
2935 | 4300 && ((options & SHELL_DOOUT) || s_dont_use_vimrun |
4301 || (allowPiping && !p_stmp)) | |
7 | 4302 #endif |
4303 ) | |
4304 { | |
4305 smsg(_("shell returned %d"), x); | |
4306 msg_putchar('\n'); | |
4307 } | |
4308 #ifdef FEAT_TITLE | |
4309 resettitle(); | |
4310 #endif | |
4311 | |
4312 signal(SIGINT, SIG_DFL); | |
4313 #if defined(__GNUC__) && !defined(__MINGW32__) | |
4314 signal(SIGKILL, SIG_DFL); | |
4315 #else | |
4316 signal(SIGBREAK, SIG_DFL); | |
4317 #endif | |
4318 signal(SIGILL, SIG_DFL); | |
4319 signal(SIGFPE, SIG_DFL); | |
4320 signal(SIGSEGV, SIG_DFL); | |
4321 signal(SIGTERM, SIG_DFL); | |
4322 signal(SIGABRT, SIG_DFL); | |
4323 | |
4324 return x; | |
4325 } | |
4326 | |
4327 | |
4328 #ifndef FEAT_GUI_W32 | |
4329 | |
4330 /* | |
4331 * Start termcap mode | |
4332 */ | |
4333 static void | |
4334 termcap_mode_start(void) | |
4335 { | |
4336 DWORD cmodein; | |
4337 | |
4338 if (g_fTermcapMode) | |
4339 return; | |
4340 | |
4341 SaveConsoleBuffer(&g_cbNonTermcap); | |
4342 | |
4343 if (g_cbTermcap.IsValid) | |
4344 { | |
4345 /* | |
4346 * We've been in termcap mode before. Restore certain screen | |
4347 * characteristics, including the buffer size and the window | |
4348 * size. Since we will be redrawing the screen, we don't need | |
4349 * to restore the actual contents of the buffer. | |
4350 */ | |
4351 RestoreConsoleBuffer(&g_cbTermcap, FALSE); | |
4352 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow); | |
4353 Rows = g_cbTermcap.Info.dwSize.Y; | |
4354 Columns = g_cbTermcap.Info.dwSize.X; | |
4355 } | |
4356 else | |
4357 { | |
4358 /* | |
4359 * This is our first time entering termcap mode. Clear the console | |
4360 * screen buffer, and resize the buffer to match the current window | |
4361 * size. We will use this as the size of our editing environment. | |
4362 */ | |
4363 ClearConsoleBuffer(g_attrCurrent); | |
4364 ResizeConBufAndWindow(g_hConOut, Columns, Rows); | |
4365 } | |
4366 | |
4367 #ifdef FEAT_TITLE | |
4368 resettitle(); | |
4369 #endif | |
4370 | |
4371 GetConsoleMode(g_hConIn, &cmodein); | |
4372 #ifdef FEAT_MOUSE | |
4373 if (g_fMouseActive) | |
4374 cmodein |= ENABLE_MOUSE_INPUT; | |
4375 else | |
4376 cmodein &= ~ENABLE_MOUSE_INPUT; | |
4377 #endif | |
4378 cmodein |= ENABLE_WINDOW_INPUT; | |
4379 SetConsoleMode(g_hConIn, cmodein); | |
4380 | |
4381 redraw_later_clear(); | |
4382 g_fTermcapMode = TRUE; | |
4383 } | |
4384 | |
4385 | |
4386 /* | |
4387 * End termcap mode | |
4388 */ | |
4389 static void | |
4390 termcap_mode_end(void) | |
4391 { | |
4392 DWORD cmodein; | |
4393 ConsoleBuffer *cb; | |
4394 COORD coord; | |
4395 DWORD dwDummy; | |
4396 | |
4397 if (!g_fTermcapMode) | |
4398 return; | |
4399 | |
4400 SaveConsoleBuffer(&g_cbTermcap); | |
4401 | |
4402 GetConsoleMode(g_hConIn, &cmodein); | |
4403 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); | |
4404 SetConsoleMode(g_hConIn, cmodein); | |
4405 | |
4406 #ifdef FEAT_RESTORE_ORIG_SCREEN | |
4407 cb = exiting ? &g_cbOrig : &g_cbNonTermcap; | |
4408 #else | |
4409 cb = &g_cbNonTermcap; | |
4410 #endif | |
4411 RestoreConsoleBuffer(cb, p_rs); | |
4412 SetConsoleCursorInfo(g_hConOut, &g_cci); | |
4413 | |
4414 if (p_rs || exiting) | |
4415 { | |
4416 /* | |
4417 * Clear anything that happens to be on the current line. | |
4418 */ | |
4419 coord.X = 0; | |
4420 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1)); | |
4421 FillConsoleOutputCharacter(g_hConOut, ' ', | |
4422 cb->Info.dwSize.X, coord, &dwDummy); | |
4423 /* | |
4424 * The following is just for aesthetics. If we are exiting without | |
4425 * restoring the screen, then we want to have a prompt string | |
4426 * appear at the bottom line. However, the command interpreter | |
4427 * seems to always advance the cursor one line before displaying | |
4428 * the prompt string, which causes the screen to scroll. To | |
4429 * counter this, move the cursor up one line before exiting. | |
4430 */ | |
4431 if (exiting && !p_rs) | |
4432 coord.Y--; | |
4433 /* | |
4434 * Position the cursor at the leftmost column of the desired row. | |
4435 */ | |
4436 SetConsoleCursorPosition(g_hConOut, coord); | |
4437 } | |
4438 | |
4439 g_fTermcapMode = FALSE; | |
4440 } | |
4441 #endif /* FEAT_GUI_W32 */ | |
4442 | |
4443 | |
4444 #ifdef FEAT_GUI_W32 | |
323 | 4445 /*ARGSUSED*/ |
7 | 4446 void |
4447 mch_write( | |
4448 char_u *s, | |
4449 int len) | |
4450 { | |
4451 /* never used */ | |
4452 } | |
4453 | |
4454 #else | |
4455 | |
4456 /* | |
4457 * clear `n' chars, starting from `coord' | |
4458 */ | |
4459 static void | |
4460 clear_chars( | |
4461 COORD coord, | |
4462 DWORD n) | |
4463 { | |
4464 DWORD dwDummy; | |
4465 | |
4466 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy); | |
4467 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy); | |
4468 } | |
4469 | |
4470 | |
4471 /* | |
4472 * Clear the screen | |
4473 */ | |
4474 static void | |
4475 clear_screen(void) | |
4476 { | |
4477 g_coord.X = g_coord.Y = 0; | |
4478 clear_chars(g_coord, Rows * Columns); | |
4479 } | |
4480 | |
4481 | |
4482 /* | |
4483 * Clear to end of display | |
4484 */ | |
4485 static void | |
4486 clear_to_end_of_display(void) | |
4487 { | |
4488 clear_chars(g_coord, (Rows - g_coord.Y - 1) | |
4489 * Columns + (Columns - g_coord.X)); | |
4490 } | |
4491 | |
4492 | |
4493 /* | |
4494 * Clear to end of line | |
4495 */ | |
4496 static void | |
4497 clear_to_end_of_line(void) | |
4498 { | |
4499 clear_chars(g_coord, Columns - g_coord.X); | |
4500 } | |
4501 | |
4502 | |
4503 /* | |
4504 * Scroll the scroll region up by `cLines' lines | |
4505 */ | |
4506 static void | |
26 | 4507 scroll(unsigned cLines) |
7 | 4508 { |
4509 COORD oldcoord = g_coord; | |
4510 | |
4511 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1); | |
4512 delete_lines(cLines); | |
4513 | |
4514 g_coord = oldcoord; | |
4515 } | |
4516 | |
4517 | |
4518 /* | |
4519 * Set the scroll region | |
4520 */ | |
4521 static void | |
4522 set_scroll_region( | |
4523 unsigned left, | |
4524 unsigned top, | |
4525 unsigned right, | |
4526 unsigned bottom) | |
4527 { | |
4528 if (left >= right | |
4529 || top >= bottom | |
4530 || right > (unsigned) Columns - 1 | |
4531 || bottom > (unsigned) Rows - 1) | |
4532 return; | |
4533 | |
4534 g_srScrollRegion.Left = left; | |
4535 g_srScrollRegion.Top = top; | |
4536 g_srScrollRegion.Right = right; | |
4537 g_srScrollRegion.Bottom = bottom; | |
4538 } | |
4539 | |
4540 | |
4541 /* | |
4542 * Insert `cLines' lines at the current cursor position | |
4543 */ | |
4544 static void | |
26 | 4545 insert_lines(unsigned cLines) |
7 | 4546 { |
4547 SMALL_RECT source; | |
4548 COORD dest; | |
4549 CHAR_INFO fill; | |
4550 | |
4551 dest.X = 0; | |
4552 dest.Y = g_coord.Y + cLines; | |
4553 | |
4554 source.Left = 0; | |
4555 source.Top = g_coord.Y; | |
4556 source.Right = g_srScrollRegion.Right; | |
4557 source.Bottom = g_srScrollRegion.Bottom - cLines; | |
4558 | |
4559 fill.Char.AsciiChar = ' '; | |
4560 fill.Attributes = g_attrCurrent; | |
4561 | |
4562 ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); | |
4563 | |
4564 /* Here we have to deal with a win32 console flake: If the scroll | |
4565 * region looks like abc and we scroll c to a and fill with d we get | |
4566 * cbd... if we scroll block c one line at a time to a, we get cdd... | |
4567 * vim expects cdd consistently... So we have to deal with that | |
4568 * here... (this also occurs scrolling the same way in the other | |
4569 * direction). */ | |
4570 | |
4571 if (source.Bottom < dest.Y) | |
4572 { | |
4573 COORD coord; | |
4574 | |
4575 coord.X = 0; | |
4576 coord.Y = source.Bottom; | |
4577 clear_chars(coord, Columns * (dest.Y - source.Bottom)); | |
4578 } | |
4579 } | |
4580 | |
4581 | |
4582 /* | |
4583 * Delete `cLines' lines at the current cursor position | |
4584 */ | |
4585 static void | |
26 | 4586 delete_lines(unsigned cLines) |
7 | 4587 { |
4588 SMALL_RECT source; | |
4589 COORD dest; | |
4590 CHAR_INFO fill; | |
4591 int nb; | |
4592 | |
4593 dest.X = 0; | |
4594 dest.Y = g_coord.Y; | |
4595 | |
4596 source.Left = 0; | |
4597 source.Top = g_coord.Y + cLines; | |
4598 source.Right = g_srScrollRegion.Right; | |
4599 source.Bottom = g_srScrollRegion.Bottom; | |
4600 | |
4601 fill.Char.AsciiChar = ' '; | |
4602 fill.Attributes = g_attrCurrent; | |
4603 | |
4604 ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); | |
4605 | |
4606 /* Here we have to deal with a win32 console flake: If the scroll | |
4607 * region looks like abc and we scroll c to a and fill with d we get | |
4608 * cbd... if we scroll block c one line at a time to a, we get cdd... | |
4609 * vim expects cdd consistently... So we have to deal with that | |
4610 * here... (this also occurs scrolling the same way in the other | |
4611 * direction). */ | |
4612 | |
4613 nb = dest.Y + (source.Bottom - source.Top) + 1; | |
4614 | |
4615 if (nb < source.Top) | |
4616 { | |
4617 COORD coord; | |
4618 | |
4619 coord.X = 0; | |
4620 coord.Y = nb; | |
4621 clear_chars(coord, Columns * (source.Top - nb)); | |
4622 } | |
4623 } | |
4624 | |
4625 | |
4626 /* | |
4627 * Set the cursor position | |
4628 */ | |
4629 static void | |
4630 gotoxy( | |
4631 unsigned x, | |
4632 unsigned y) | |
4633 { | |
4634 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows) | |
4635 return; | |
4636 | |
4637 /* external cursor coords are 1-based; internal are 0-based */ | |
4638 g_coord.X = x - 1; | |
4639 g_coord.Y = y - 1; | |
4640 SetConsoleCursorPosition(g_hConOut, g_coord); | |
4641 } | |
4642 | |
4643 | |
4644 /* | |
4645 * Set the current text attribute = (foreground | background) | |
4646 * See ../doc/os_win32.txt for the numbers. | |
4647 */ | |
4648 static void | |
26 | 4649 textattr(WORD wAttr) |
7 | 4650 { |
4651 g_attrCurrent = wAttr; | |
4652 | |
4653 SetConsoleTextAttribute(g_hConOut, wAttr); | |
4654 } | |
4655 | |
4656 | |
4657 static void | |
26 | 4658 textcolor(WORD wAttr) |
7 | 4659 { |
4660 g_attrCurrent = (g_attrCurrent & 0xf0) + wAttr; | |
4661 | |
4662 SetConsoleTextAttribute(g_hConOut, g_attrCurrent); | |
4663 } | |
4664 | |
4665 | |
4666 static void | |
26 | 4667 textbackground(WORD wAttr) |
7 | 4668 { |
4669 g_attrCurrent = (g_attrCurrent & 0x0f) + (wAttr << 4); | |
4670 | |
4671 SetConsoleTextAttribute(g_hConOut, g_attrCurrent); | |
4672 } | |
4673 | |
4674 | |
4675 /* | |
4676 * restore the default text attribute (whatever we started with) | |
4677 */ | |
4678 static void | |
26 | 4679 normvideo(void) |
7 | 4680 { |
4681 textattr(g_attrDefault); | |
4682 } | |
4683 | |
4684 | |
4685 static WORD g_attrPreStandout = 0; | |
4686 | |
4687 /* | |
4688 * Make the text standout, by brightening it | |
4689 */ | |
4690 static void | |
4691 standout(void) | |
4692 { | |
4693 g_attrPreStandout = g_attrCurrent; | |
4694 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY)); | |
4695 } | |
4696 | |
4697 | |
4698 /* | |
4699 * Turn off standout mode | |
4700 */ | |
4701 static void | |
26 | 4702 standend(void) |
7 | 4703 { |
4704 if (g_attrPreStandout) | |
4705 { | |
4706 textattr(g_attrPreStandout); | |
4707 g_attrPreStandout = 0; | |
4708 } | |
4709 } | |
4710 | |
4711 | |
4712 /* | |
1199 | 4713 * Set normal fg/bg color, based on T_ME. Called when t_me has been set. |
7 | 4714 */ |
4715 void | |
26 | 4716 mch_set_normal_colors(void) |
7 | 4717 { |
4718 char_u *p; | |
4719 int n; | |
4720 | |
4721 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1; | |
4722 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1; | |
4723 if (T_ME[0] == ESC && T_ME[1] == '|') | |
4724 { | |
4725 p = T_ME + 2; | |
4726 n = getdigits(&p); | |
4727 if (*p == 'm' && n > 0) | |
4728 { | |
4729 cterm_normal_fg_color = (n & 0xf) + 1; | |
4730 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1; | |
4731 } | |
4732 } | |
4733 } | |
4734 | |
4735 | |
4736 /* | |
4737 * visual bell: flash the screen | |
4738 */ | |
4739 static void | |
26 | 4740 visual_bell(void) |
7 | 4741 { |
4742 COORD coordOrigin = {0, 0}; | |
4743 WORD attrFlash = ~g_attrCurrent & 0xff; | |
4744 | |
4745 DWORD dwDummy; | |
4746 LPWORD oldattrs = (LPWORD)alloc(Rows * Columns * sizeof(WORD)); | |
4747 | |
4748 if (oldattrs == NULL) | |
4749 return; | |
4750 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, | |
4751 coordOrigin, &dwDummy); | |
4752 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns, | |
4753 coordOrigin, &dwDummy); | |
4754 | |
4755 Sleep(15); /* wait for 15 msec */ | |
4756 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, | |
4757 coordOrigin, &dwDummy); | |
4758 vim_free(oldattrs); | |
4759 } | |
4760 | |
4761 | |
4762 /* | |
4763 * Make the cursor visible or invisible | |
4764 */ | |
4765 static void | |
26 | 4766 cursor_visible(BOOL fVisible) |
7 | 4767 { |
4768 s_cursor_visible = fVisible; | |
4769 #ifdef MCH_CURSOR_SHAPE | |
4770 mch_update_cursor(); | |
4771 #endif | |
4772 } | |
4773 | |
4774 | |
4775 /* | |
4776 * write `cchToWrite' characters in `pchBuf' to the screen | |
4777 * Returns the number of characters actually written (at least one). | |
4778 */ | |
4779 static BOOL | |
4780 write_chars( | |
4781 LPCSTR pchBuf, | |
4782 DWORD cchToWrite) | |
4783 { | |
4784 COORD coord = g_coord; | |
4785 DWORD written; | |
4786 | |
4787 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cchToWrite, | |
4788 coord, &written); | |
4789 /* When writing fails or didn't write a single character, pretend one | |
4790 * character was written, otherwise we get stuck. */ | |
4791 if (WriteConsoleOutputCharacter(g_hConOut, pchBuf, cchToWrite, | |
4792 coord, &written) == 0 | |
4793 || written == 0) | |
4794 written = 1; | |
4795 | |
4796 g_coord.X += (SHORT) written; | |
4797 | |
4798 while (g_coord.X > g_srScrollRegion.Right) | |
4799 { | |
4800 g_coord.X -= (SHORT) Columns; | |
4801 if (g_coord.Y < g_srScrollRegion.Bottom) | |
4802 g_coord.Y++; | |
4803 } | |
4804 | |
4805 gotoxy(g_coord.X + 1, g_coord.Y + 1); | |
4806 | |
4807 return written; | |
4808 } | |
4809 | |
4810 | |
4811 /* | |
4812 * mch_write(): write the output buffer to the screen, translating ESC | |
4813 * sequences into calls to console output routines. | |
4814 */ | |
4815 void | |
4816 mch_write( | |
4817 char_u *s, | |
4818 int len) | |
4819 { | |
4820 s[len] = NUL; | |
4821 | |
4822 if (!term_console) | |
4823 { | |
4824 write(1, s, (unsigned)len); | |
4825 return; | |
4826 } | |
4827 | |
4828 /* translate ESC | sequences into faked bios calls */ | |
4829 while (len--) | |
4830 { | |
4831 /* optimization: use one single write_chars for runs of text, | |
4832 * rather than once per character It ain't curses, but it helps. */ | |
835 | 4833 DWORD prefix = (DWORD)strcspn(s, "\n\r\b\a\033"); |
7 | 4834 |
4835 if (p_wd) | |
4836 { | |
4837 WaitForChar(p_wd); | |
4838 if (prefix != 0) | |
4839 prefix = 1; | |
4840 } | |
4841 | |
4842 if (prefix != 0) | |
4843 { | |
4844 DWORD nWritten; | |
4845 | |
4846 nWritten = write_chars(s, prefix); | |
4847 #ifdef MCH_WRITE_DUMP | |
4848 if (fdDump) | |
4849 { | |
4850 fputc('>', fdDump); | |
4851 fwrite(s, sizeof(char_u), nWritten, fdDump); | |
4852 fputs("<\n", fdDump); | |
4853 } | |
4854 #endif | |
4855 len -= (nWritten - 1); | |
4856 s += nWritten; | |
4857 } | |
4858 else if (s[0] == '\n') | |
4859 { | |
4860 /* \n, newline: go to the beginning of the next line or scroll */ | |
4861 if (g_coord.Y == g_srScrollRegion.Bottom) | |
4862 { | |
4863 scroll(1); | |
4864 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1); | |
4865 } | |
4866 else | |
4867 { | |
4868 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2); | |
4869 } | |
4870 #ifdef MCH_WRITE_DUMP | |
4871 if (fdDump) | |
4872 fputs("\\n\n", fdDump); | |
4873 #endif | |
4874 s++; | |
4875 } | |
4876 else if (s[0] == '\r') | |
4877 { | |
4878 /* \r, carriage return: go to beginning of line */ | |
4879 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1); | |
4880 #ifdef MCH_WRITE_DUMP | |
4881 if (fdDump) | |
4882 fputs("\\r\n", fdDump); | |
4883 #endif | |
4884 s++; | |
4885 } | |
4886 else if (s[0] == '\b') | |
4887 { | |
4888 /* \b, backspace: move cursor one position left */ | |
4889 if (g_coord.X > g_srScrollRegion.Left) | |
4890 g_coord.X--; | |
4891 else if (g_coord.Y > g_srScrollRegion.Top) | |
4892 { | |
4893 g_coord.X = g_srScrollRegion.Right; | |
4894 g_coord.Y--; | |
4895 } | |
4896 gotoxy(g_coord.X + 1, g_coord.Y + 1); | |
4897 #ifdef MCH_WRITE_DUMP | |
4898 if (fdDump) | |
4899 fputs("\\b\n", fdDump); | |
4900 #endif | |
4901 s++; | |
4902 } | |
4903 else if (s[0] == '\a') | |
4904 { | |
4905 /* \a, bell */ | |
4906 MessageBeep(0xFFFFFFFF); | |
4907 #ifdef MCH_WRITE_DUMP | |
4908 if (fdDump) | |
4909 fputs("\\a\n", fdDump); | |
4910 #endif | |
4911 s++; | |
4912 } | |
4913 else if (s[0] == ESC && len >= 3-1 && s[1] == '|') | |
4914 { | |
4915 #ifdef MCH_WRITE_DUMP | |
24 | 4916 char_u *old_s = s; |
7 | 4917 #endif |
24 | 4918 char_u *p; |
4919 int arg1 = 0, arg2 = 0; | |
7 | 4920 |
4921 switch (s[2]) | |
4922 { | |
4923 /* one or two numeric arguments, separated by ';' */ | |
4924 | |
4925 case '0': case '1': case '2': case '3': case '4': | |
4926 case '5': case '6': case '7': case '8': case '9': | |
4927 p = s + 2; | |
4928 arg1 = getdigits(&p); /* no check for length! */ | |
4929 if (p > s + len) | |
4930 break; | |
4931 | |
4932 if (*p == ';') | |
4933 { | |
4934 ++p; | |
4935 arg2 = getdigits(&p); /* no check for length! */ | |
4936 if (p > s + len) | |
4937 break; | |
4938 | |
4939 if (*p == 'H') | |
4940 gotoxy(arg2, arg1); | |
4941 else if (*p == 'r') | |
4942 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1); | |
4943 } | |
4944 else if (*p == 'A') | |
4945 { | |
4946 /* move cursor up arg1 lines in same column */ | |
4947 gotoxy(g_coord.X + 1, | |
4948 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1); | |
4949 } | |
4950 else if (*p == 'C') | |
4951 { | |
4952 /* move cursor right arg1 columns in same line */ | |
4953 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1, | |
4954 g_coord.Y + 1); | |
4955 } | |
4956 else if (*p == 'H') | |
4957 { | |
4958 gotoxy(1, arg1); | |
4959 } | |
4960 else if (*p == 'L') | |
4961 { | |
4962 insert_lines(arg1); | |
4963 } | |
4964 else if (*p == 'm') | |
4965 { | |
4966 if (arg1 == 0) | |
4967 normvideo(); | |
4968 else | |
4969 textattr((WORD) arg1); | |
4970 } | |
4971 else if (*p == 'f') | |
4972 { | |
4973 textcolor((WORD) arg1); | |
4974 } | |
4975 else if (*p == 'b') | |
4976 { | |
4977 textbackground((WORD) arg1); | |
4978 } | |
4979 else if (*p == 'M') | |
4980 { | |
4981 delete_lines(arg1); | |
4982 } | |
4983 | |
835 | 4984 len -= (int)(p - s); |
7 | 4985 s = p + 1; |
4986 break; | |
4987 | |
4988 | |
4989 /* Three-character escape sequences */ | |
4990 | |
4991 case 'A': | |
4992 /* move cursor up one line in same column */ | |
4993 gotoxy(g_coord.X + 1, | |
4994 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1); | |
4995 goto got3; | |
4996 | |
4997 case 'B': | |
4998 visual_bell(); | |
4999 goto got3; | |
5000 | |
5001 case 'C': | |
5002 /* move cursor right one column in same line */ | |
5003 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1, | |
5004 g_coord.Y + 1); | |
5005 goto got3; | |
5006 | |
5007 case 'E': | |
5008 termcap_mode_end(); | |
5009 goto got3; | |
5010 | |
5011 case 'F': | |
5012 standout(); | |
5013 goto got3; | |
5014 | |
5015 case 'f': | |
5016 standend(); | |
5017 goto got3; | |
5018 | |
5019 case 'H': | |
5020 gotoxy(1, 1); | |
5021 goto got3; | |
5022 | |
5023 case 'j': | |
5024 clear_to_end_of_display(); | |
5025 goto got3; | |
5026 | |
5027 case 'J': | |
5028 clear_screen(); | |
5029 goto got3; | |
5030 | |
5031 case 'K': | |
5032 clear_to_end_of_line(); | |
5033 goto got3; | |
5034 | |
5035 case 'L': | |
5036 insert_lines(1); | |
5037 goto got3; | |
5038 | |
5039 case 'M': | |
5040 delete_lines(1); | |
5041 goto got3; | |
5042 | |
5043 case 'S': | |
5044 termcap_mode_start(); | |
5045 goto got3; | |
5046 | |
5047 case 'V': | |
5048 cursor_visible(TRUE); | |
5049 goto got3; | |
5050 | |
5051 case 'v': | |
5052 cursor_visible(FALSE); | |
5053 goto got3; | |
5054 | |
5055 got3: | |
5056 s += 3; | |
5057 len -= 2; | |
5058 } | |
5059 | |
5060 #ifdef MCH_WRITE_DUMP | |
5061 if (fdDump) | |
5062 { | |
5063 fputs("ESC | ", fdDump); | |
5064 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump); | |
5065 fputc('\n', fdDump); | |
5066 } | |
5067 #endif | |
5068 } | |
5069 else | |
5070 { | |
5071 /* Write a single character */ | |
5072 DWORD nWritten; | |
5073 | |
5074 nWritten = write_chars(s, 1); | |
5075 #ifdef MCH_WRITE_DUMP | |
5076 if (fdDump) | |
5077 { | |
5078 fputc('>', fdDump); | |
5079 fwrite(s, sizeof(char_u), nWritten, fdDump); | |
5080 fputs("<\n", fdDump); | |
5081 } | |
5082 #endif | |
5083 | |
5084 len -= (nWritten - 1); | |
5085 s += nWritten; | |
5086 } | |
5087 } | |
5088 | |
5089 #ifdef MCH_WRITE_DUMP | |
5090 if (fdDump) | |
5091 fflush(fdDump); | |
5092 #endif | |
5093 } | |
5094 | |
5095 #endif /* FEAT_GUI_W32 */ | |
5096 | |
5097 | |
5098 /* | |
5099 * Delay for half a second. | |
5100 */ | |
323 | 5101 /*ARGSUSED*/ |
7 | 5102 void |
5103 mch_delay( | |
5104 long msec, | |
5105 int ignoreinput) | |
5106 { | |
5107 #ifdef FEAT_GUI_W32 | |
5108 Sleep((int)msec); /* never wait for input */ | |
14 | 5109 #else /* Console */ |
7 | 5110 if (ignoreinput) |
14 | 5111 # ifdef FEAT_MZSCHEME |
5112 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq) | |
5113 { | |
5114 int towait = p_mzq; | |
5115 | |
5116 /* if msec is large enough, wait by portions in p_mzq */ | |
5117 while (msec > 0) | |
5118 { | |
5119 mzvim_check_threads(); | |
5120 if (msec < towait) | |
5121 towait = msec; | |
5122 Sleep(towait); | |
5123 msec -= towait; | |
5124 } | |
5125 } | |
5126 else | |
5127 # endif | |
5128 Sleep((int)msec); | |
7 | 5129 else |
5130 WaitForChar(msec); | |
5131 #endif | |
5132 } | |
5133 | |
5134 | |
5135 /* | |
5136 * this version of remove is not scared by a readonly (backup) file | |
5137 * Return 0 for success, -1 for failure. | |
5138 */ | |
5139 int | |
5140 mch_remove(char_u *name) | |
5141 { | |
5142 #ifdef FEAT_MBYTE | |
5143 WCHAR *wn = NULL; | |
5144 int n; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5145 #endif |
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 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL); |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5148 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4789
diff
changeset
|
5149 #ifdef FEAT_MBYTE |
7 | 5150 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
5151 { | |
1752 | 5152 wn = enc_to_utf16(name, NULL); |
7 | 5153 if (wn != NULL) |
5154 { | |
5155 n = DeleteFileW(wn) ? 0 : -1; | |
5156 vim_free(wn); | |
5157 if (n == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5158 return n; | |
5159 /* Retry with non-wide function (for Windows 98). */ | |
5160 } | |
5161 } | |
5162 #endif | |
5163 return DeleteFile(name) ? 0 : -1; | |
5164 } | |
5165 | |
5166 | |
5167 /* | |
5168 * check for an "interrupt signal": CTRL-break or CTRL-C | |
5169 */ | |
5170 void | |
26 | 5171 mch_breakcheck(void) |
7 | 5172 { |
5173 #ifndef FEAT_GUI_W32 /* never used */ | |
5174 if (g_fCtrlCPressed || g_fCBrkPressed) | |
5175 { | |
5176 g_fCtrlCPressed = g_fCBrkPressed = FALSE; | |
5177 got_int = TRUE; | |
5178 } | |
5179 #endif | |
5180 } | |
5181 | |
5182 | |
5183 #ifdef FEAT_MBYTE | |
5184 /* | |
5185 * Same code as below, but with wide functions and no comments. | |
5186 * Return 0 for success, non-zero for failure. | |
5187 */ | |
5188 int | |
5189 mch_wrename(WCHAR *wold, WCHAR *wnew) | |
5190 { | |
5191 WCHAR *p; | |
5192 int i; | |
5193 WCHAR szTempFile[_MAX_PATH + 1]; | |
5194 WCHAR szNewPath[_MAX_PATH + 1]; | |
5195 HANDLE hf; | |
5196 | |
5197 if (!mch_windows95()) | |
5198 { | |
5199 p = wold; | |
5200 for (i = 0; wold[i] != NUL; ++i) | |
5201 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':') | |
5202 && wold[i + 1] != 0) | |
5203 p = wold + i + 1; | |
5204 if ((int)(wold + i - p) < 8 || p[6] != '~') | |
5205 return (MoveFileW(wold, wnew) == 0); | |
5206 } | |
5207 | |
5208 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL) | |
5209 return -1; | |
5210 *p = NUL; | |
5211 | |
5212 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0) | |
5213 return -2; | |
5214 | |
5215 if (!DeleteFileW(szTempFile)) | |
5216 return -3; | |
5217 | |
5218 if (!MoveFileW(wold, szTempFile)) | |
5219 return -4; | |
5220 | |
5221 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW, | |
5222 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) | |
5223 return -5; | |
5224 if (!CloseHandle(hf)) | |
5225 return -6; | |
5226 | |
5227 if (!MoveFileW(szTempFile, wnew)) | |
5228 { | |
5229 (void)MoveFileW(szTempFile, wold); | |
5230 return -7; | |
5231 } | |
5232 | |
5233 DeleteFileW(szTempFile); | |
5234 | |
5235 if (!DeleteFileW(wold)) | |
5236 return -8; | |
5237 | |
5238 return 0; | |
5239 } | |
5240 #endif | |
5241 | |
5242 | |
5243 /* | |
5244 * mch_rename() works around a bug in rename (aka MoveFile) in | |
5245 * Windows 95: rename("foo.bar", "foo.bar~") will generate a | |
5246 * file whose short file name is "FOO.BAR" (its long file name will | |
5247 * be correct: "foo.bar~"). Because a file can be accessed by | |
5248 * either its SFN or its LFN, "foo.bar" has effectively been | |
5249 * renamed to "foo.bar", which is not at all what was wanted. This | |
5250 * seems to happen only when renaming files with three-character | |
5251 * extensions by appending a suffix that does not include ".". | |
5252 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR". | |
5253 * | |
5254 * There is another problem, which isn't really a bug but isn't right either: | |
5255 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be | |
5256 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with | |
5257 * service pack 6. Doesn't seem to happen on Windows 98. | |
5258 * | |
5259 * Like rename(), returns 0 upon success, non-zero upon failure. | |
5260 * Should probably set errno appropriately when errors occur. | |
5261 */ | |
5262 int | |
5263 mch_rename( | |
5264 const char *pszOldFile, | |
5265 const char *pszNewFile) | |
5266 { | |
5267 char szTempFile[_MAX_PATH+1]; | |
5268 char szNewPath[_MAX_PATH+1]; | |
5269 char *pszFilePart; | |
5270 HANDLE hf; | |
5271 #ifdef FEAT_MBYTE | |
5272 WCHAR *wold = NULL; | |
5273 WCHAR *wnew = NULL; | |
5274 int retval = -1; | |
5275 | |
5276 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
5277 { | |
1752 | 5278 wold = enc_to_utf16((char_u *)pszOldFile, NULL); |
5279 wnew = enc_to_utf16((char_u *)pszNewFile, NULL); | |
7 | 5280 if (wold != NULL && wnew != NULL) |
5281 retval = mch_wrename(wold, wnew); | |
5282 vim_free(wold); | |
5283 vim_free(wnew); | |
5284 if (retval == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5285 return retval; | |
5286 /* Retry with non-wide function (for Windows 98). */ | |
5287 } | |
5288 #endif | |
5289 | |
5290 /* | |
5291 * No need to play tricks if not running Windows 95, unless the file name | |
5292 * contains a "~" as the seventh character. | |
5293 */ | |
5294 if (!mch_windows95()) | |
5295 { | |
5296 pszFilePart = (char *)gettail((char_u *)pszOldFile); | |
5297 if (STRLEN(pszFilePart) < 8 || pszFilePart[6] != '~') | |
5298 return rename(pszOldFile, pszNewFile); | |
5299 } | |
5300 | |
5301 /* Get base path of new file name. Undocumented feature: If pszNewFile is | |
5302 * a directory, no error is returned and pszFilePart will be NULL. */ | |
5303 if (GetFullPathName(pszNewFile, _MAX_PATH, szNewPath, &pszFilePart) == 0 | |
5304 || pszFilePart == NULL) | |
5305 return -1; | |
5306 *pszFilePart = NUL; | |
5307 | |
5308 /* Get (and create) a unique temporary file name in directory of new file */ | |
5309 if (GetTempFileName(szNewPath, "VIM", 0, szTempFile) == 0) | |
5310 return -2; | |
5311 | |
5312 /* blow the temp file away */ | |
5313 if (!DeleteFile(szTempFile)) | |
5314 return -3; | |
5315 | |
5316 /* rename old file to the temp file */ | |
5317 if (!MoveFile(pszOldFile, szTempFile)) | |
5318 return -4; | |
5319 | |
5320 /* now create an empty file called pszOldFile; this prevents the operating | |
5321 * system using pszOldFile as an alias (SFN) if we're renaming within the | |
5322 * same directory. For example, we're editing a file called | |
5323 * filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt | |
5324 * to filena~1.txt~ (i.e., we're making a backup while writing it), the | |
5325 * SFN for filena~1.txt~ will be filena~1.txt, by default, which will | |
39 | 5326 * cause all sorts of problems later in buf_write(). So, we create an |
5327 * empty file called filena~1.txt and the system will have to find some | |
5328 * other SFN for filena~1.txt~, such as filena~2.txt | |
7 | 5329 */ |
5330 if ((hf = CreateFile(pszOldFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, | |
5331 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) | |
5332 return -5; | |
5333 if (!CloseHandle(hf)) | |
5334 return -6; | |
5335 | |
5336 /* rename the temp file to the new file */ | |
5337 if (!MoveFile(szTempFile, pszNewFile)) | |
5338 { | |
5339 /* Renaming failed. Rename the file back to its old name, so that it | |
5340 * looks like nothing happened. */ | |
5341 (void)MoveFile(szTempFile, pszOldFile); | |
5342 | |
5343 return -7; | |
5344 } | |
5345 | |
5346 /* Seems to be left around on Novell filesystems */ | |
5347 DeleteFile(szTempFile); | |
5348 | |
5349 /* finally, remove the empty old file */ | |
5350 if (!DeleteFile(pszOldFile)) | |
5351 return -8; | |
5352 | |
5353 return 0; /* success */ | |
5354 } | |
5355 | |
5356 /* | |
5357 * Get the default shell for the current hardware platform | |
5358 */ | |
5359 char * | |
26 | 5360 default_shell(void) |
7 | 5361 { |
5362 char* psz = NULL; | |
5363 | |
5364 PlatformId(); | |
5365 | |
5366 if (g_PlatformId == VER_PLATFORM_WIN32_NT) /* Windows NT */ | |
5367 psz = "cmd.exe"; | |
5368 else if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) /* Windows 95 */ | |
5369 psz = "command.com"; | |
5370 | |
5371 return psz; | |
5372 } | |
5373 | |
5374 /* | |
5375 * mch_access() extends access() to do more detailed check on network drives. | |
5376 * Returns 0 if file "n" has access rights according to "p", -1 otherwise. | |
5377 */ | |
5378 int | |
5379 mch_access(char *n, int p) | |
5380 { | |
5381 HANDLE hFile; | |
5382 DWORD am; | |
5383 int retval = -1; /* default: fail */ | |
5384 #ifdef FEAT_MBYTE | |
5385 WCHAR *wn = NULL; | |
5386 | |
5387 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
1752 | 5388 wn = enc_to_utf16(n, NULL); |
7 | 5389 #endif |
5390 | |
5391 if (mch_isdir(n)) | |
5392 { | |
5393 char TempName[_MAX_PATH + 16] = ""; | |
5394 #ifdef FEAT_MBYTE | |
5395 WCHAR TempNameW[_MAX_PATH + 16] = L""; | |
5396 #endif | |
5397 | |
5398 if (p & R_OK) | |
5399 { | |
5400 /* Read check is performed by seeing if we can do a find file on | |
5401 * the directory for any file. */ | |
5402 #ifdef FEAT_MBYTE | |
5403 if (wn != NULL) | |
5404 { | |
5405 int i; | |
5406 WIN32_FIND_DATAW d; | |
5407 | |
5408 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i) | |
5409 TempNameW[i] = wn[i]; | |
5410 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/') | |
5411 TempNameW[i++] = '\\'; | |
5412 TempNameW[i++] = '*'; | |
5413 TempNameW[i++] = 0; | |
5414 | |
5415 hFile = FindFirstFileW(TempNameW, &d); | |
5416 if (hFile == INVALID_HANDLE_VALUE) | |
5417 { | |
5418 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5419 goto getout; | |
5420 | |
5421 /* Retry with non-wide function (for Windows 98). */ | |
5422 vim_free(wn); | |
5423 wn = NULL; | |
5424 } | |
5425 else | |
5426 (void)FindClose(hFile); | |
5427 } | |
5428 if (wn == NULL) | |
5429 #endif | |
5430 { | |
5431 char *pch; | |
5432 WIN32_FIND_DATA d; | |
5433 | |
417 | 5434 vim_strncpy(TempName, n, _MAX_PATH); |
7 | 5435 pch = TempName + STRLEN(TempName) - 1; |
5436 if (*pch != '\\' && *pch != '/') | |
5437 *++pch = '\\'; | |
5438 *++pch = '*'; | |
5439 *++pch = NUL; | |
5440 | |
5441 hFile = FindFirstFile(TempName, &d); | |
5442 if (hFile == INVALID_HANDLE_VALUE) | |
5443 goto getout; | |
5444 (void)FindClose(hFile); | |
5445 } | |
5446 } | |
5447 | |
5448 if (p & W_OK) | |
5449 { | |
5450 /* Trying to create a temporary file in the directory should catch | |
5451 * directories on read-only network shares. However, in | |
5452 * directories whose ACL allows writes but denies deletes will end | |
5453 * up keeping the temporary file :-(. */ | |
5454 #ifdef FEAT_MBYTE | |
5455 if (wn != NULL) | |
5456 { | |
5457 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW)) | |
5458 { | |
5459 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) | |
5460 goto getout; | |
5461 | |
5462 /* Retry with non-wide function (for Windows 98). */ | |
5463 vim_free(wn); | |
5464 wn = NULL; | |
5465 } | |
5466 else | |
5467 DeleteFileW(TempNameW); | |
5468 } | |
5469 if (wn == NULL) | |
5470 #endif | |
5471 { | |
5472 if (!GetTempFileName(n, "VIM", 0, TempName)) | |
5473 goto getout; | |
5474 mch_remove((char_u *)TempName); | |
5475 } | |
5476 } | |
5477 } | |
5478 else | |
5479 { | |
5480 /* Trying to open the file for the required access does ACL, read-only | |
5481 * network share, and file attribute checks. */ | |
5482 am = ((p & W_OK) ? GENERIC_WRITE : 0) | |
5483 | ((p & R_OK) ? GENERIC_READ : 0); | |
5484 #ifdef FEAT_MBYTE | |
5485 if (wn != NULL) | |
5486 { | |
5487 hFile = CreateFileW(wn, am, 0, NULL, OPEN_EXISTING, 0, NULL); | |
5488 if (hFile == INVALID_HANDLE_VALUE | |
5489 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
5490 { | |
5491 /* Retry with non-wide function (for Windows 98). */ | |
5492 vim_free(wn); | |
5493 wn = NULL; | |
5494 } | |
5495 } | |
5496 if (wn == NULL) | |
5497 #endif | |
5498 hFile = CreateFile(n, am, 0, NULL, OPEN_EXISTING, 0, NULL); | |
5499 if (hFile == INVALID_HANDLE_VALUE) | |
5500 goto getout; | |
5501 CloseHandle(hFile); | |
5502 } | |
5503 | |
5504 retval = 0; /* success */ | |
5505 getout: | |
5506 #ifdef FEAT_MBYTE | |
5507 vim_free(wn); | |
5508 #endif | |
5509 return retval; | |
5510 } | |
5511 | |
5512 #if defined(FEAT_MBYTE) || defined(PROTO) | |
5513 /* | |
1752 | 5514 * Version of open() that may use UTF-16 file name. |
7 | 5515 */ |
5516 int | |
5517 mch_open(char *name, int flags, int mode) | |
5518 { | |
39 | 5519 /* _wopen() does not work with Borland C 5.5: creates a read-only file. */ |
5520 # ifndef __BORLANDC__ | |
7 | 5521 WCHAR *wn; |
5522 int f; | |
5523 | |
39 | 5524 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
7 | 5525 { |
1752 | 5526 wn = enc_to_utf16(name, NULL); |
7 | 5527 if (wn != NULL) |
5528 { | |
5529 f = _wopen(wn, flags, mode); | |
5530 vim_free(wn); | |
5531 if (f >= 0) | |
5532 return f; | |
5533 /* Retry with non-wide function (for Windows 98). Can't use | |
5534 * GetLastError() here and it's unclear what errno gets set to if | |
5535 * the _wopen() fails for missing wide functions. */ | |
5536 } | |
5537 } | |
39 | 5538 # endif |
7 | 5539 |
5540 return open(name, flags, mode); | |
5541 } | |
5542 | |
5543 /* | |
1752 | 5544 * Version of fopen() that may use UTF-16 file name. |
7 | 5545 */ |
5546 FILE * | |
5547 mch_fopen(char *name, char *mode) | |
5548 { | |
5549 WCHAR *wn, *wm; | |
5550 FILE *f = NULL; | |
5551 | |
5552 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage | |
5553 # ifdef __BORLANDC__ | |
5554 /* Wide functions of Borland C 5.5 do not work on Windows 98. */ | |
5555 && g_PlatformId == VER_PLATFORM_WIN32_NT | |
5556 # endif | |
5557 ) | |
5558 { | |
1686 | 5559 # if defined(DEBUG) && _MSC_VER >= 1400 |
1569 | 5560 /* Work around an annoying assertion in the Microsoft debug CRT |
5561 * when mode's text/binary setting doesn't match _get_fmode(). */ | |
5562 char newMode = mode[strlen(mode) - 1]; | |
5563 int oldMode = 0; | |
5564 | |
5565 _get_fmode(&oldMode); | |
5566 if (newMode == 't') | |
5567 _set_fmode(_O_TEXT); | |
5568 else if (newMode == 'b') | |
5569 _set_fmode(_O_BINARY); | |
5570 # endif | |
1752 | 5571 wn = enc_to_utf16(name, NULL); |
5572 wm = enc_to_utf16(mode, NULL); | |
7 | 5573 if (wn != NULL && wm != NULL) |
5574 f = _wfopen(wn, wm); | |
5575 vim_free(wn); | |
5576 vim_free(wm); | |
1569 | 5577 |
1686 | 5578 # if defined(DEBUG) && _MSC_VER >= 1400 |
1569 | 5579 _set_fmode(oldMode); |
5580 # endif | |
5581 | |
7 | 5582 if (f != NULL) |
5583 return f; | |
5584 /* Retry with non-wide function (for Windows 98). Can't use | |
5585 * GetLastError() here and it's unclear what errno gets set to if | |
5586 * the _wfopen() fails for missing wide functions. */ | |
5587 } | |
5588 | |
5589 return fopen(name, mode); | |
5590 } | |
5591 #endif | |
5592 | |
5593 #ifdef FEAT_MBYTE | |
5594 /* | |
5595 * SUB STREAM (aka info stream) handling: | |
5596 * | |
5597 * NTFS can have sub streams for each file. Normal contents of file is | |
5598 * stored in the main stream, and extra contents (author information and | |
5599 * title and so on) can be stored in sub stream. After Windows 2000, user | |
5600 * can access and store those informations in sub streams via explorer's | |
5601 * property menuitem in right click menu. Those informations in sub streams | |
5602 * were lost when copying only the main stream. So we have to copy sub | |
5603 * streams. | |
5604 * | |
5605 * Incomplete explanation: | |
5606 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp | |
5607 * More useful info and an example: | |
5608 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams | |
5609 */ | |
5610 | |
5611 /* | |
5612 * Copy info stream data "substream". Read from the file with BackupRead(sh) | |
5613 * and write to stream "substream" of file "to". | |
5614 * Errors are ignored. | |
5615 */ | |
5616 static void | |
5617 copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len) | |
5618 { | |
5619 HANDLE hTo; | |
5620 WCHAR *to_name; | |
5621 | |
5622 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR)); | |
5623 wcscpy(to_name, to); | |
5624 wcscat(to_name, substream); | |
5625 | |
5626 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, | |
5627 FILE_ATTRIBUTE_NORMAL, NULL); | |
5628 if (hTo != INVALID_HANDLE_VALUE) | |
5629 { | |
5630 long done; | |
5631 DWORD todo; | |
5632 DWORD readcnt, written; | |
5633 char buf[4096]; | |
5634 | |
5635 /* Copy block of bytes at a time. Abort when something goes wrong. */ | |
5636 for (done = 0; done < len; done += written) | |
5637 { | |
5638 /* (size_t) cast for Borland C 5.5 */ | |
835 | 5639 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf) |
5640 : (size_t)(len - done)); | |
7 | 5641 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt, |
5642 FALSE, FALSE, context) | |
5643 || readcnt != todo | |
5644 || !WriteFile(hTo, buf, todo, &written, NULL) | |
5645 || written != todo) | |
5646 break; | |
5647 } | |
5648 CloseHandle(hTo); | |
5649 } | |
5650 | |
5651 free(to_name); | |
5652 } | |
5653 | |
5654 /* | |
5655 * Copy info streams from file "from" to file "to". | |
5656 */ | |
5657 static void | |
5658 copy_infostreams(char_u *from, char_u *to) | |
5659 { | |
5660 WCHAR *fromw; | |
5661 WCHAR *tow; | |
5662 HANDLE sh; | |
5663 WIN32_STREAM_ID sid; | |
5664 int headersize; | |
5665 WCHAR streamname[_MAX_PATH]; | |
5666 DWORD readcount; | |
5667 void *context = NULL; | |
5668 DWORD lo, hi; | |
5669 int len; | |
5670 | |
5671 /* Convert the file names to wide characters. */ | |
1752 | 5672 fromw = enc_to_utf16(from, NULL); |
5673 tow = enc_to_utf16(to, NULL); | |
7 | 5674 if (fromw != NULL && tow != NULL) |
5675 { | |
5676 /* Open the file for reading. */ | |
5677 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL, | |
5678 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
5679 if (sh != INVALID_HANDLE_VALUE) | |
5680 { | |
5681 /* Use BackupRead() to find the info streams. Repeat until we | |
5682 * have done them all.*/ | |
5683 for (;;) | |
5684 { | |
5685 /* Get the header to find the length of the stream name. If | |
5686 * the "readcount" is zero we have done all info streams. */ | |
5687 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID)); | |
835 | 5688 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId); |
7 | 5689 if (!BackupRead(sh, (LPBYTE)&sid, headersize, |
5690 &readcount, FALSE, FALSE, &context) | |
5691 || readcount == 0) | |
5692 break; | |
5693 | |
5694 /* We only deal with streams that have a name. The normal | |
5695 * file data appears to be without a name, even though docs | |
5696 * suggest it is called "::$DATA". */ | |
5697 if (sid.dwStreamNameSize > 0) | |
5698 { | |
5699 /* Read the stream name. */ | |
5700 if (!BackupRead(sh, (LPBYTE)streamname, | |
5701 sid.dwStreamNameSize, | |
5702 &readcount, FALSE, FALSE, &context)) | |
5703 break; | |
5704 | |
5705 /* Copy an info stream with a name ":anything:$DATA". | |
5706 * Skip "::$DATA", it has no stream name (examples suggest | |
5707 * it might be used for the normal file contents). | |
5708 * Note that BackupRead() counts bytes, but the name is in | |
5709 * wide characters. */ | |
5710 len = readcount / sizeof(WCHAR); | |
5711 streamname[len] = 0; | |
5712 if (len > 7 && wcsicmp(streamname + len - 6, | |
5713 L":$DATA") == 0) | |
5714 { | |
5715 streamname[len - 6] = 0; | |
5716 copy_substream(sh, &context, tow, streamname, | |
10 | 5717 (long)sid.Size.u.LowPart); |
7 | 5718 } |
5719 } | |
5720 | |
5721 /* Advance to the next stream. We might try seeking too far, | |
5722 * but BackupSeek() doesn't skip over stream borders, thus | |
5723 * that's OK. */ | |
323 | 5724 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart, |
7 | 5725 &lo, &hi, &context); |
5726 } | |
5727 | |
5728 /* Clear the context. */ | |
5729 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context); | |
5730 | |
5731 CloseHandle(sh); | |
5732 } | |
5733 } | |
5734 vim_free(fromw); | |
5735 vim_free(tow); | |
5736 } | |
5737 #endif | |
5738 | |
5739 /* | |
5740 * Copy file attributes from file "from" to file "to". | |
5741 * For Windows NT and later we copy info streams. | |
5742 * Always returns zero, errors are ignored. | |
5743 */ | |
5744 int | |
5745 mch_copy_file_attribute(char_u *from, char_u *to) | |
5746 { | |
5747 #ifdef FEAT_MBYTE | |
5748 /* File streams only work on Windows NT and later. */ | |
5749 PlatformId(); | |
5750 if (g_PlatformId == VER_PLATFORM_WIN32_NT) | |
5751 copy_infostreams(from, to); | |
5752 #endif | |
5753 return 0; | |
5754 } | |
5755 | |
5756 #if defined(MYRESETSTKOFLW) || defined(PROTO) | |
5757 /* | |
5758 * Recreate a destroyed stack guard page in win32. | |
5759 * Written by Benjamin Peterson. | |
5760 */ | |
5761 | |
5762 /* These magic numbers are from the MS header files */ | |
5763 #define MIN_STACK_WIN9X 17 | |
5764 #define MIN_STACK_WINNT 2 | |
5765 | |
5766 /* | |
5767 * This function does the same thing as _resetstkoflw(), which is only | |
5768 * available in DevStudio .net and later. | |
5769 * Returns 0 for failure, 1 for success. | |
5770 */ | |
5771 int | |
5772 myresetstkoflw(void) | |
5773 { | |
5774 BYTE *pStackPtr; | |
5775 BYTE *pGuardPage; | |
5776 BYTE *pStackBase; | |
5777 BYTE *pLowestPossiblePage; | |
5778 MEMORY_BASIC_INFORMATION mbi; | |
5779 SYSTEM_INFO si; | |
5780 DWORD nPageSize; | |
5781 DWORD dummy; | |
5782 | |
5783 /* This code will not work on win32s. */ | |
5784 PlatformId(); | |
5785 if (g_PlatformId == VER_PLATFORM_WIN32s) | |
5786 return 0; | |
5787 | |
5788 /* We need to know the system page size. */ | |
5789 GetSystemInfo(&si); | |
5790 nPageSize = si.dwPageSize; | |
5791 | |
5792 /* ...and the current stack pointer */ | |
5793 pStackPtr = (BYTE*)_alloca(1); | |
5794 | |
5795 /* ...and the base of the stack. */ | |
5796 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0) | |
5797 return 0; | |
5798 pStackBase = (BYTE*)mbi.AllocationBase; | |
5799 | |
5800 /* ...and the page thats min_stack_req pages away from stack base; this is | |
5801 * the lowest page we could use. */ | |
5802 pLowestPossiblePage = pStackBase + ((g_PlatformId == VER_PLATFORM_WIN32_NT) | |
5803 ? MIN_STACK_WINNT : MIN_STACK_WIN9X) * nPageSize; | |
5804 | |
5805 /* On Win95, we want the next page down from the end of the stack. */ | |
5806 if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) | |
5807 { | |
5808 /* Find the page that's only 1 page down from the page that the stack | |
5809 * ptr is in. */ | |
5810 pGuardPage = (BYTE*)((DWORD)nPageSize * (((DWORD)pStackPtr | |
5811 / (DWORD)nPageSize) - 1)); | |
5812 if (pGuardPage < pLowestPossiblePage) | |
5813 return 0; | |
5814 | |
5815 /* Apply the noaccess attribute to the page -- there's no guard | |
5816 * attribute in win95-type OSes. */ | |
5817 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_NOACCESS, &dummy)) | |
5818 return 0; | |
5819 } | |
5820 else | |
5821 { | |
5822 /* On NT, however, we want the first committed page in the stack Start | |
5823 * at the stack base and move forward through memory until we find a | |
5824 * committed block. */ | |
5825 BYTE *pBlock = pStackBase; | |
5826 | |
406 | 5827 for (;;) |
7 | 5828 { |
5829 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0) | |
5830 return 0; | |
5831 | |
5832 pBlock += mbi.RegionSize; | |
5833 | |
5834 if (mbi.State & MEM_COMMIT) | |
5835 break; | |
5836 } | |
5837 | |
5838 /* mbi now describes the first committed block in the stack. */ | |
5839 if (mbi.Protect & PAGE_GUARD) | |
5840 return 1; | |
5841 | |
5842 /* decide where the guard page should start */ | |
5843 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage) | |
5844 pGuardPage = pLowestPossiblePage; | |
5845 else | |
5846 pGuardPage = (BYTE*)mbi.BaseAddress; | |
5847 | |
5848 /* allocate the guard page */ | |
5849 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE)) | |
5850 return 0; | |
5851 | |
5852 /* apply the guard attribute to the page */ | |
5853 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD, | |
5854 &dummy)) | |
5855 return 0; | |
5856 } | |
5857 | |
5858 return 1; | |
5859 } | |
5860 #endif | |
26 | 5861 |
5862 | |
5863 #if defined(FEAT_MBYTE) || defined(PROTO) | |
5864 /* | |
5865 * The command line arguments in UCS2 | |
5866 */ | |
344 | 5867 static int nArgsW = 0; |
26 | 5868 static LPWSTR *ArglistW = NULL; |
5869 static int global_argc = 0; | |
5870 static char **global_argv; | |
5871 | |
5872 static int used_file_argc = 0; /* last argument in global_argv[] used | |
5873 for the argument list. */ | |
5874 static int *used_file_indexes = NULL; /* indexes in global_argv[] for | |
5875 command line arguments added to | |
5876 the argument list */ | |
5877 static int used_file_count = 0; /* nr of entries in used_file_indexes */ | |
5878 static int used_file_literal = FALSE; /* take file names literally */ | |
5879 static int used_file_full_path = FALSE; /* file name was full path */ | |
819 | 5880 static int used_file_diff_mode = FALSE; /* file name was with diff mode */ |
26 | 5881 static int used_alist_count = 0; |
5882 | |
5883 | |
5884 /* | |
5885 * Get the command line arguments. Unicode version. | |
5886 * Returns argc. Zero when something fails. | |
5887 */ | |
5888 int | |
5889 get_cmd_argsW(char ***argvp) | |
5890 { | |
5891 char **argv = NULL; | |
5892 int argc = 0; | |
5893 int i; | |
5894 | |
5895 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW); | |
5896 if (ArglistW != NULL) | |
5897 { | |
5898 argv = malloc((nArgsW + 1) * sizeof(char *)); | |
5899 if (argv != NULL) | |
5900 { | |
5901 argc = nArgsW; | |
5902 argv[argc] = NULL; | |
5903 for (i = 0; i < argc; ++i) | |
5904 { | |
5905 int len; | |
5906 | |
5907 /* Convert each Unicode argument to the current codepage. */ | |
5908 WideCharToMultiByte_alloc(GetACP(), 0, | |
835 | 5909 ArglistW[i], (int)wcslen(ArglistW[i]) + 1, |
26 | 5910 (LPSTR *)&argv[i], &len, 0, 0); |
5911 if (argv[i] == NULL) | |
5912 { | |
5913 /* Out of memory, clear everything. */ | |
5914 while (i > 0) | |
5915 free(argv[--i]); | |
5916 free(argv); | |
5917 argc = 0; | |
5918 } | |
5919 } | |
5920 } | |
5921 } | |
5922 | |
5923 global_argc = argc; | |
5924 global_argv = argv; | |
5925 if (argc > 0) | |
5926 used_file_indexes = malloc(argc * sizeof(int)); | |
5927 | |
5928 if (argvp != NULL) | |
5929 *argvp = argv; | |
5930 return argc; | |
5931 } | |
5932 | |
5933 void | |
5934 free_cmd_argsW(void) | |
5935 { | |
5936 if (ArglistW != NULL) | |
5937 { | |
5938 GlobalFree(ArglistW); | |
5939 ArglistW = NULL; | |
5940 } | |
5941 } | |
5942 | |
5943 /* | |
5944 * Remember "name" is an argument that was added to the argument list. | |
5945 * This avoids that we have to re-parse the argument list when fix_arg_enc() | |
5946 * is called. | |
5947 */ | |
5948 void | |
819 | 5949 used_file_arg(char *name, int literal, int full_path, int diff_mode) |
26 | 5950 { |
5951 int i; | |
5952 | |
5953 if (used_file_indexes == NULL) | |
5954 return; | |
5955 for (i = used_file_argc + 1; i < global_argc; ++i) | |
5956 if (STRCMP(global_argv[i], name) == 0) | |
5957 { | |
5958 used_file_argc = i; | |
5959 used_file_indexes[used_file_count++] = i; | |
5960 break; | |
5961 } | |
5962 used_file_literal = literal; | |
5963 used_file_full_path = full_path; | |
819 | 5964 used_file_diff_mode = diff_mode; |
26 | 5965 } |
5966 | |
5967 /* | |
5968 * Remember the length of the argument list as it was. If it changes then we | |
5969 * leave it alone when 'encoding' is set. | |
5970 */ | |
5971 void | |
5972 set_alist_count(void) | |
5973 { | |
5974 used_alist_count = GARGCOUNT; | |
5975 } | |
5976 | |
5977 /* | |
5978 * Fix the encoding of the command line arguments. Invoked when 'encoding' | |
5979 * has been changed while starting up. Use the UCS-2 command line arguments | |
5980 * and convert them to 'encoding'. | |
5981 */ | |
5982 void | |
5983 fix_arg_enc(void) | |
5984 { | |
5985 int i; | |
5986 int idx; | |
5987 char_u *str; | |
41 | 5988 int *fnum_list; |
26 | 5989 |
5990 /* Safety checks: | |
5991 * - if argument count differs between the wide and non-wide argument | |
5992 * list, something must be wrong. | |
5993 * - the file name arguments must have been located. | |
5994 * - the length of the argument list wasn't changed by the user. | |
5995 */ | |
344 | 5996 if (global_argc != nArgsW |
26 | 5997 || ArglistW == NULL |
5998 || used_file_indexes == NULL | |
5999 || used_file_count == 0 | |
6000 || used_alist_count != GARGCOUNT) | |
6001 return; | |
6002 | |
41 | 6003 /* Remember the buffer numbers for the arguments. */ |
6004 fnum_list = (int *)alloc((int)sizeof(int) * GARGCOUNT); | |
6005 if (fnum_list == NULL) | |
6006 return; /* out of memory */ | |
6007 for (i = 0; i < GARGCOUNT; ++i) | |
6008 fnum_list[i] = GARGLIST[i].ae_fnum; | |
6009 | |
26 | 6010 /* Clear the argument list. Make room for the new arguments. */ |
6011 alist_clear(&global_alist); | |
6012 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL) | |
41 | 6013 return; /* out of memory */ |
26 | 6014 |
6015 for (i = 0; i < used_file_count; ++i) | |
6016 { | |
6017 idx = used_file_indexes[i]; | |
1752 | 6018 str = utf16_to_enc(ArglistW[idx], NULL); |
26 | 6019 if (str != NULL) |
41 | 6020 { |
819 | 6021 #ifdef FEAT_DIFF |
6022 /* When using diff mode may need to concatenate file name to | |
6023 * directory name. Just like it's done in main(). */ | |
6024 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0 | |
6025 && !mch_isdir(alist_name(&GARGLIST[0]))) | |
6026 { | |
6027 char_u *r; | |
6028 | |
6029 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE); | |
6030 if (r != NULL) | |
6031 { | |
6032 vim_free(str); | |
6033 str = r; | |
6034 } | |
6035 } | |
6036 #endif | |
41 | 6037 /* Re-use the old buffer by renaming it. When not using literal |
6038 * names it's done by alist_expand() below. */ | |
6039 if (used_file_literal) | |
6040 buf_set_name(fnum_list[i], str); | |
6041 | |
26 | 6042 alist_add(&global_alist, str, used_file_literal ? 2 : 0); |
41 | 6043 } |
26 | 6044 } |
6045 | |
6046 if (!used_file_literal) | |
6047 { | |
6048 /* Now expand wildcards in the arguments. */ | |
6049 /* Temporarily add '(' and ')' to 'isfname'. These are valid | |
6050 * filename characters but are excluded from 'isfname' to make | |
6051 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */ | |
6052 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)"); | |
41 | 6053 alist_expand(fnum_list, used_alist_count); |
26 | 6054 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF"); |
6055 } | |
6056 | |
6057 /* If wildcard expansion failed, we are editing the first file of the | |
6058 * arglist and there is no file name: Edit the first argument now. */ | |
6059 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL) | |
6060 { | |
6061 do_cmdline_cmd((char_u *)":rewind"); | |
6062 if (GARGCOUNT == 1 && used_file_full_path) | |
6063 (void)vim_chdirfile(alist_name(&GARGLIST[0])); | |
6064 } | |
41 | 6065 |
6066 set_alist_count(); | |
26 | 6067 } |
6068 #endif |