-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_win32.c
7234 lines (6383 loc) · 170 KB
/
os_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* os_win32.c
*
* Used for both the console version and the Win32 GUI. A lot of code is for
* the console version only, so there is a lot of "#ifndef FEAT_GUI_W32".
*
* Win32 (Windows NT and Windows 95) system-dependent routines.
* Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
* NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
*
* George V. Reilly <[email protected]> wrote most of this.
* Roger Knobbe <[email protected]> did the initial port of Vim 3.0.
*/
#include "vim.h"
#ifdef FEAT_MZSCHEME
# include "if_mzsch.h"
#endif
#include <sys/types.h>
#include <signal.h>
#include <limits.h>
/* cproto fails on missing include files */
#ifndef PROTO
# include <process.h>
#endif
#undef chdir
#ifdef __GNUC__
# ifndef __MINGW32__
# include <dirent.h>
# endif
#else
# include <direct.h>
#endif
#ifndef PROTO
# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
# include <shellapi.h>
# endif
#endif
#ifdef FEAT_JOB_CHANNEL
# include <tlhelp32.h>
#endif
#ifdef __MINGW32__
# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
# endif
# ifndef RIGHTMOST_BUTTON_PRESSED
# define RIGHTMOST_BUTTON_PRESSED 0x0002
# endif
# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
# endif
# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
# endif
# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
# endif
/*
* EventFlags
*/
# ifndef MOUSE_MOVED
# define MOUSE_MOVED 0x0001
# endif
# ifndef DOUBLE_CLICK
# define DOUBLE_CLICK 0x0002
# endif
#endif
/* Record all output and all keyboard & mouse input */
/* #define MCH_WRITE_DUMP */
#ifdef MCH_WRITE_DUMP
FILE* fdDump = NULL;
#endif
/*
* When generating prototypes for Win32 on Unix, these lines make the syntax
* errors disappear. They do not need to be correct.
*/
#ifdef PROTO
#define WINAPI
typedef char * LPCSTR;
typedef char * LPWSTR;
typedef int ACCESS_MASK;
typedef int BOOL;
typedef int COLORREF;
typedef int CONSOLE_CURSOR_INFO;
typedef int COORD;
typedef int DWORD;
typedef int HANDLE;
typedef int LPHANDLE;
typedef int HDC;
typedef int HFONT;
typedef int HICON;
typedef int HINSTANCE;
typedef int HWND;
typedef int INPUT_RECORD;
typedef int INT;
typedef int KEY_EVENT_RECORD;
typedef int LOGFONT;
typedef int LPBOOL;
typedef int LPCTSTR;
typedef int LPDWORD;
typedef int LPSTR;
typedef int LPTSTR;
typedef int LPVOID;
typedef int MOUSE_EVENT_RECORD;
typedef int PACL;
typedef int PDWORD;
typedef int PHANDLE;
typedef int PRINTDLG;
typedef int PSECURITY_DESCRIPTOR;
typedef int PSID;
typedef int SECURITY_INFORMATION;
typedef int SHORT;
typedef int SMALL_RECT;
typedef int TEXTMETRIC;
typedef int TOKEN_INFORMATION_CLASS;
typedef int TRUSTEE;
typedef int WORD;
typedef int WCHAR;
typedef void VOID;
typedef int BY_HANDLE_FILE_INFORMATION;
typedef int SE_OBJECT_TYPE;
typedef int PSNSECINFO;
typedef int PSNSECINFOW;
typedef int STARTUPINFO;
typedef int PROCESS_INFORMATION;
typedef int LPSECURITY_ATTRIBUTES;
# define __stdcall /* empty */
#endif
#if defined(__BORLANDC__)
/* Strangely Borland uses a non-standard name. */
# define wcsicmp(a, b) wcscmpi((a), (b))
#endif
#ifndef FEAT_GUI_W32
/* Win32 Console handles for input and output */
static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
/* Win32 Screen buffer,coordinate,console I/O information */
static SMALL_RECT g_srScrollRegion;
static COORD g_coord; /* 0-based, but external coords are 1-based */
/* The attribute of the screen when the editor was started */
static WORD g_attrDefault = 7; /* lightgray text on black background */
static WORD g_attrCurrent;
static int g_fCBrkPressed = FALSE; /* set by ctrl-break interrupt */
static int g_fCtrlCPressed = FALSE; /* set when ctrl-C or ctrl-break detected */
static int g_fForceExit = FALSE; /* set when forcefully exiting */
static void termcap_mode_start(void);
static void termcap_mode_end(void);
static void clear_chars(COORD coord, DWORD n);
static void clear_screen(void);
static void clear_to_end_of_display(void);
static void clear_to_end_of_line(void);
static void scroll(unsigned cLines);
static void set_scroll_region(unsigned left, unsigned top,
unsigned right, unsigned bottom);
static void insert_lines(unsigned cLines);
static void delete_lines(unsigned cLines);
static void gotoxy(unsigned x, unsigned y);
static void normvideo(void);
static void textattr(WORD wAttr);
static void textcolor(WORD wAttr);
static void textbackground(WORD wAttr);
static void standout(void);
static void standend(void);
static void visual_bell(void);
static void cursor_visible(BOOL fVisible);
static DWORD write_chars(char_u *pchBuf, DWORD cbToWrite);
static void create_conin(void);
static int s_cursor_visible = TRUE;
static int did_create_conin = FALSE;
#else
static int s_dont_use_vimrun = TRUE;
static int need_vimrun_warning = FALSE;
static char *vimrun_path = "vimrun ";
#endif
static int win32_getattrs(char_u *name);
static int win32_setattrs(char_u *name, int attrs);
static int win32_set_archive(char_u *name);
#ifndef FEAT_GUI_W32
static int suppress_winsize = 1; /* don't fiddle with console */
#endif
static char_u *exe_path = NULL;
static BOOL win8_or_later = FALSE;
#ifndef FEAT_GUI_W32
/*
* Version of ReadConsoleInput() that works with IME.
* Works around problems on Windows 8.
*/
static BOOL
read_console_input(
HANDLE hInput,
INPUT_RECORD *lpBuffer,
DWORD nLength,
LPDWORD lpEvents)
{
enum
{
IRSIZE = 10
};
static INPUT_RECORD s_irCache[IRSIZE];
static DWORD s_dwIndex = 0;
static DWORD s_dwMax = 0;
DWORD dwEvents;
int head;
int tail;
int i;
if (nLength == -2)
return (s_dwMax > 0) ? TRUE : FALSE;
if (!win8_or_later)
{
if (nLength == -1)
return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
}
if (s_dwMax == 0)
{
if (nLength == -1)
return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
return FALSE;
s_dwIndex = 0;
s_dwMax = dwEvents;
if (dwEvents == 0)
{
*lpEvents = 0;
return TRUE;
}
if (s_dwMax > 1)
{
head = 0;
tail = s_dwMax - 1;
while (head != tail)
{
if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
&& s_irCache[head + 1].EventType
== WINDOW_BUFFER_SIZE_EVENT)
{
/* Remove duplicate event to avoid flicker. */
for (i = head; i < tail; ++i)
s_irCache[i] = s_irCache[i + 1];
--tail;
continue;
}
head++;
}
s_dwMax = tail + 1;
}
}
*lpBuffer = s_irCache[s_dwIndex];
if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
s_dwMax = 0;
*lpEvents = 1;
return TRUE;
}
/*
* Version of PeekConsoleInput() that works with IME.
*/
static BOOL
peek_console_input(
HANDLE hInput,
INPUT_RECORD *lpBuffer,
DWORD nLength,
LPDWORD lpEvents)
{
return read_console_input(hInput, lpBuffer, -1, lpEvents);
}
# ifdef FEAT_CLIENTSERVER
static DWORD
msg_wait_for_multiple_objects(
DWORD nCount,
LPHANDLE pHandles,
BOOL fWaitAll,
DWORD dwMilliseconds,
DWORD dwWakeMask)
{
if (read_console_input(NULL, NULL, -2, NULL))
return WAIT_OBJECT_0;
return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
dwMilliseconds, dwWakeMask);
}
# endif
# ifndef FEAT_CLIENTSERVER
static DWORD
wait_for_single_object(
HANDLE hHandle,
DWORD dwMilliseconds)
{
if (read_console_input(NULL, NULL, -2, NULL))
return WAIT_OBJECT_0;
return WaitForSingleObject(hHandle, dwMilliseconds);
}
# endif
#endif
static void
get_exe_name(void)
{
/* Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
* as the maximum length that works (plus a NUL byte). */
#define MAX_ENV_PATH_LEN 8192
char temp[MAX_ENV_PATH_LEN];
char_u *p;
if (exe_name == NULL)
{
/* store the name of the executable, may be used for $VIM */
GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
if (*temp != NUL)
exe_name = FullName_save((char_u *)temp, FALSE);
}
if (exe_path == NULL && exe_name != NULL)
{
exe_path = vim_strnsave(exe_name,
(int)(gettail_sep(exe_name) - exe_name));
if (exe_path != NULL)
{
/* Append our starting directory to $PATH, so that when doing
* "!xxd" it's found in our starting directory. Needed because
* SearchPath() also looks there. */
p = mch_getenv("PATH");
if (p == NULL
|| STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
{
if (p == NULL || *p == NUL)
temp[0] = NUL;
else
{
STRCPY(temp, p);
STRCAT(temp, ";");
}
STRCAT(temp, exe_path);
vim_setenv((char_u *)"PATH", (char_u *)temp);
}
}
}
}
/*
* Unescape characters in "p" that appear in "escaped".
*/
static void
unescape_shellxquote(char_u *p, char_u *escaped)
{
int l = (int)STRLEN(p);
int n;
while (*p != NUL)
{
if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
mch_memmove(p, p + 1, l--);
#ifdef FEAT_MBYTE
n = (*mb_ptr2len)(p);
#else
n = 1;
#endif
p += n;
l -= n;
}
}
/*
* Load library "name".
*/
HINSTANCE
vimLoadLib(char *name)
{
HINSTANCE dll = NULL;
/* NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
* vimLoadLib() recursively, which causes a stack overflow. */
if (exe_path == NULL)
get_exe_name();
if (exe_path != NULL)
{
WCHAR old_dirw[MAXPATHL];
if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
{
/* Change directory to where the executable is, both to make
* sure we find a .dll there and to avoid looking for a .dll
* in the current directory. */
SetCurrentDirectory((LPCSTR)exe_path);
dll = LoadLibrary(name);
SetCurrentDirectoryW(old_dirw);
return dll;
}
}
return dll;
}
#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
/*
* Get related information about 'funcname' which is imported by 'hInst'.
* If 'info' is 0, return the function address.
* If 'info' is 1, return the module name which the function is imported from.
*/
static void *
get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
{
PBYTE pImage = (PBYTE)hInst;
PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
PIMAGE_NT_HEADERS pPE;
PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
PIMAGE_THUNK_DATA pIAT; /* Import Address Table */
PIMAGE_THUNK_DATA pINT; /* Import Name Table */
PIMAGE_IMPORT_BY_NAME pImpName;
if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
return NULL;
pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
if (pPE->Signature != IMAGE_NT_SIGNATURE)
return NULL;
pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
+ pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
.VirtualAddress);
for (; pImpDesc->FirstThunk; ++pImpDesc)
{
if (!pImpDesc->OriginalFirstThunk)
continue;
pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
for (; pIAT->u1.Function; ++pIAT, ++pINT)
{
if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
continue;
pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
+ (UINT_PTR)(pINT->u1.AddressOfData));
if (strcmp((char *)pImpName->Name, funcname) == 0)
{
switch (info)
{
case 0:
return (void *)pIAT->u1.Function;
case 1:
return (void *)(pImage + pImpDesc->Name);
default:
return NULL;
}
}
}
}
return NULL;
}
/*
* Get the module handle which 'funcname' in 'hInst' is imported from.
*/
HINSTANCE
find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
{
char *modulename;
modulename = (char *)get_imported_func_info(hInst, funcname, 1);
if (modulename != NULL)
return GetModuleHandleA(modulename);
return NULL;
}
/*
* Get the address of 'funcname' which is imported by 'hInst' DLL.
*/
void *
get_dll_import_func(HINSTANCE hInst, const char *funcname)
{
return get_imported_func_info(hInst, funcname, 0);
}
#endif
#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
# ifndef GETTEXT_DLL
# define GETTEXT_DLL "libintl.dll"
# define GETTEXT_DLL_ALT "libintl-8.dll"
# endif
/* Dummy functions */
static char *null_libintl_gettext(const char *);
static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
static char *null_libintl_textdomain(const char *);
static char *null_libintl_bindtextdomain(const char *, const char *);
static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
static int null_libintl_putenv(const char *);
static int null_libintl_wputenv(const wchar_t *);
static HINSTANCE hLibintlDLL = NULL;
char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
= null_libintl_ngettext;
char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
= null_libintl_bindtextdomain;
char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
= null_libintl_bind_textdomain_codeset;
int (*dyn_libintl_putenv)(const char *) = null_libintl_putenv;
int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
int
dyn_libintl_init(void)
{
int i;
static struct
{
char *name;
FARPROC *ptr;
} libintl_entry[] =
{
{"gettext", (FARPROC*)&dyn_libintl_gettext},
{"ngettext", (FARPROC*)&dyn_libintl_ngettext},
{"textdomain", (FARPROC*)&dyn_libintl_textdomain},
{"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
{NULL, NULL}
};
HINSTANCE hmsvcrt;
/* No need to initialize twice. */
if (hLibintlDLL)
return 1;
/* Load gettext library (libintl.dll) */
hLibintlDLL = vimLoadLib(GETTEXT_DLL);
#ifdef GETTEXT_DLL_ALT
if (!hLibintlDLL)
hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT);
#endif
if (!hLibintlDLL)
{
if (p_verbose > 0)
{
verbose_enter();
EMSG2(_(e_loadlib), GETTEXT_DLL);
verbose_leave();
}
return 0;
}
for (i = 0; libintl_entry[i].name != NULL
&& libintl_entry[i].ptr != NULL; ++i)
{
if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
libintl_entry[i].name)) == NULL)
{
dyn_libintl_end();
if (p_verbose > 0)
{
verbose_enter();
EMSG2(_(e_loadfunc), libintl_entry[i].name);
verbose_leave();
}
return 0;
}
}
/* The bind_textdomain_codeset() function is optional. */
dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
"bind_textdomain_codeset");
if (dyn_libintl_bind_textdomain_codeset == NULL)
dyn_libintl_bind_textdomain_codeset =
null_libintl_bind_textdomain_codeset;
/* _putenv() function for the libintl.dll is optional. */
hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
if (hmsvcrt != NULL)
{
dyn_libintl_putenv = (void *)GetProcAddress(hmsvcrt, "_putenv");
dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
}
if (dyn_libintl_putenv == NULL || dyn_libintl_putenv == _putenv)
dyn_libintl_putenv = null_libintl_putenv;
if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
dyn_libintl_wputenv = null_libintl_wputenv;
return 1;
}
void
dyn_libintl_end(void)
{
if (hLibintlDLL)
FreeLibrary(hLibintlDLL);
hLibintlDLL = NULL;
dyn_libintl_gettext = null_libintl_gettext;
dyn_libintl_ngettext = null_libintl_ngettext;
dyn_libintl_textdomain = null_libintl_textdomain;
dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
dyn_libintl_putenv = null_libintl_putenv;
dyn_libintl_wputenv = null_libintl_wputenv;
}
static char *
null_libintl_gettext(const char *msgid)
{
return (char*)msgid;
}
static char *
null_libintl_ngettext(
const char *msgid,
const char *msgid_plural,
unsigned long n)
{
return (char *)(n == 1 ? msgid : msgid_plural);
}
static char *
null_libintl_bindtextdomain(
const char *domainname UNUSED,
const char *dirname UNUSED)
{
return NULL;
}
static char *
null_libintl_bind_textdomain_codeset(
const char *domainname UNUSED,
const char *codeset UNUSED)
{
return NULL;
}
static char *
null_libintl_textdomain(const char *domainname UNUSED)
{
return NULL;
}
static int
null_libintl_putenv(const char *envstring UNUSED)
{
return 0;
}
static int
null_libintl_wputenv(const wchar_t *envstring UNUSED)
{
return 0;
}
#endif /* DYNAMIC_GETTEXT */
/* This symbol is not defined in older versions of the SDK or Visual C++ */
#ifndef VER_PLATFORM_WIN32_WINDOWS
# define VER_PLATFORM_WIN32_WINDOWS 1
#endif
DWORD g_PlatformId;
#ifdef HAVE_ACL
# ifndef PROTO
# include <aclapi.h>
# endif
# ifndef PROTECTED_DACL_SECURITY_INFORMATION
# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
# endif
#endif
#ifdef HAVE_ACL
/*
* Enables or disables the specified privilege.
*/
static BOOL
win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
{
BOOL bResult;
LUID luid;
HANDLE hToken;
TOKEN_PRIVILEGES tokenPrivileges;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
{
CloseHandle(hToken);
return FALSE;
}
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges[0].Luid = luid;
tokenPrivileges.Privileges[0].Attributes = bEnable ?
SE_PRIVILEGE_ENABLED : 0;
bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
sizeof(TOKEN_PRIVILEGES), NULL, NULL);
CloseHandle(hToken);
return bResult && GetLastError() == ERROR_SUCCESS;
}
#endif
/*
* Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or
* VER_PLATFORM_WIN32_WINDOWS (Win95).
*/
void
PlatformId(void)
{
static int done = FALSE;
if (!done)
{
OSVERSIONINFO ovi;
ovi.dwOSVersionInfoSize = sizeof(ovi);
GetVersionEx(&ovi);
g_PlatformId = ovi.dwPlatformId;
if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
|| ovi.dwMajorVersion > 6)
win8_or_later = TRUE;
#ifdef HAVE_ACL
/* Enable privilege for getting or setting SACLs. */
win32_enable_privilege(SE_SECURITY_NAME, TRUE);
#endif
done = TRUE;
}
}
#ifndef FEAT_GUI_W32
#define SHIFT (SHIFT_PRESSED)
#define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
#define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
#define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
/* When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
* We map function keys to their ANSI terminal equivalents, as produced
* by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
* ANSI key with a value >= '\300' is nonstandard, but provided anyway
* so that the user can have access to all SHIFT-, CTRL-, and ALT-
* combinations of function/arrow/etc keys.
*/
static const struct
{
WORD wVirtKey;
BOOL fAnsiKey;
int chAlone;
int chShift;
int chCtrl;
int chAlt;
} VirtKeyMap[] =
{
/* Key ANSI alone shift ctrl alt */
{ VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
{ VK_F1, TRUE, ';', 'T', '^', 'h', },
{ VK_F2, TRUE, '<', 'U', '_', 'i', },
{ VK_F3, TRUE, '=', 'V', '`', 'j', },
{ VK_F4, TRUE, '>', 'W', 'a', 'k', },
{ VK_F5, TRUE, '?', 'X', 'b', 'l', },
{ VK_F6, TRUE, '@', 'Y', 'c', 'm', },
{ VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
{ VK_F8, TRUE, 'B', '[', 'e', 'o', },
{ VK_F9, TRUE, 'C', '\\', 'f', 'p', },
{ VK_F10, TRUE, 'D', ']', 'g', 'q', },
{ VK_F11, TRUE, '\205', '\207', '\211', '\213', },
{ VK_F12, TRUE, '\206', '\210', '\212', '\214', },
{ VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
{ VK_UP, TRUE, 'H', '\304', '\305', '\306', },
{ VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, /*PgUp*/
{ VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
{ VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
{ VK_END, TRUE, 'O', '\315', 'u', '\316', },
{ VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
{ VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, /*PgDn*/
{ VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
{ VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
{ VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, /*PrtScrn*/
#if 0
/* Most people don't have F13-F20, but what the hell... */
{ VK_F13, TRUE, '\332', '\333', '\334', '\335', },
{ VK_F14, TRUE, '\336', '\337', '\340', '\341', },
{ VK_F15, TRUE, '\342', '\343', '\344', '\345', },
{ VK_F16, TRUE, '\346', '\347', '\350', '\351', },
{ VK_F17, TRUE, '\352', '\353', '\354', '\355', },
{ VK_F18, TRUE, '\356', '\357', '\360', '\361', },
{ VK_F19, TRUE, '\362', '\363', '\364', '\365', },
{ VK_F20, TRUE, '\366', '\367', '\370', '\371', },
#endif
{ VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, /* keyp '+' */
{ VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, /* keyp '-' */
/* { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, keyp '/' */
{ VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, /* keyp '*' */
{ VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
{ VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
{ VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
{ VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
{ VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
{ VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
{ VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
{ VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
{ VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
/* Sorry, out of number space! <negri>*/
{ VK_NUMPAD9,TRUE, '\376', '\377', '\377', '\367', },
};
#ifdef _MSC_VER
// The ToAscii bug destroys several registers. Need to turn off optimization
// or the GetConsoleKeyboardLayoutName hack will fail in non-debug versions
# pragma warning(push)
# pragma warning(disable: 4748)
# pragma optimize("", off)
#endif
#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
# define UChar UnicodeChar
#else
# define UChar uChar.UnicodeChar
#endif
/* The return code indicates key code size. */
static int
#ifdef __BORLANDC__
__stdcall
#endif
win32_kbd_patch_key(
KEY_EVENT_RECORD *pker)
{
UINT uMods = pker->dwControlKeyState;
static int s_iIsDead = 0;
static WORD awAnsiCode[2];
static BYTE abKeystate[256];
if (s_iIsDead == 2)
{
pker->UChar = (WCHAR) awAnsiCode[1];
s_iIsDead = 0;
return 1;
}
if (pker->UChar != 0)
return 1;
vim_memset(abKeystate, 0, sizeof (abKeystate));
/* Clear any pending dead keys */
ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
if (uMods & SHIFT_PRESSED)
abKeystate[VK_SHIFT] = 0x80;
if (uMods & CAPSLOCK_ON)
abKeystate[VK_CAPITAL] = 1;
if ((uMods & ALT_GR) == ALT_GR)
{
abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
}
s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
abKeystate, awAnsiCode, 2, 0);
if (s_iIsDead > 0)
pker->UChar = (WCHAR) awAnsiCode[0];
return s_iIsDead;
}
#ifdef _MSC_VER
/* MUST switch optimization on again here, otherwise a call to
* decode_key_event() may crash (e.g. when hitting caps-lock) */
# pragma optimize("", on)
# pragma warning(pop)
# if (_MSC_VER < 1100)
/* MUST turn off global optimisation for this next function, or
* pressing ctrl-minus in insert mode crashes Vim when built with
* VC4.1. -- negri. */
# pragma optimize("g", off)
# endif
#endif
static BOOL g_fJustGotFocus = FALSE;
/*
* Decode a KEY_EVENT into one or two keystrokes
*/
static BOOL
decode_key_event(
KEY_EVENT_RECORD *pker,
WCHAR *pch,
WCHAR *pch2,
int *pmodifiers,
BOOL fDoPost)
{
int i;
const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
*pch = *pch2 = NUL;
g_fJustGotFocus = FALSE;
/* ignore key up events */
if (!pker->bKeyDown)
return FALSE;
/* ignore some keystrokes */
switch (pker->wVirtualKeyCode)
{
/* modifiers */
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU: /* Alt key */
return FALSE;
default:
break;
}
/* special cases */
if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
{
/* Ctrl-6 is Ctrl-^ */
if (pker->wVirtualKeyCode == '6')
{
*pch = Ctrl_HAT;
return TRUE;
}
/* Ctrl-2 is Ctrl-@ */
else if (pker->wVirtualKeyCode == '2')
{
*pch = NUL;
return TRUE;
}
/* Ctrl-- is Ctrl-_ */
else if (pker->wVirtualKeyCode == 0xBD)
{
*pch = Ctrl__;
return TRUE;
}
}
/* Shift-TAB */
if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
{
*pch = K_NUL;
*pch2 = '\017';
return TRUE;
}
for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
{
if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
{
if (nModifs == 0)
*pch = VirtKeyMap[i].chAlone;
else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
*pch = VirtKeyMap[i].chShift;
else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
*pch = VirtKeyMap[i].chCtrl;
else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
*pch = VirtKeyMap[i].chAlt;