-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathos_mswin.c
3101 lines (2688 loc) · 72.9 KB
/
os_mswin.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_mswin.c
*
* Routines for Win32.
*/
#include "vim.h"
#include <sys/types.h>
#include <signal.h>
#include <limits.h>
#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_GUI_MSWIN)
# include <shellapi.h>
# endif
# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
# include <dlgs.h>
# include <winspool.h>
# include <commdlg.h>
# endif
#endif // PROTO
#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
/*
* 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
# define WINBASEAPI
typedef int BOOL;
typedef int CALLBACK;
typedef int COLORREF;
typedef int CONSOLE_CURSOR_INFO;
typedef int COORD;
typedef int DWORD;
typedef int ENUMLOGFONTW;
typedef int HANDLE;
typedef int HDC;
typedef int HFONT;
typedef int HICON;
typedef int HWND;
typedef int INPUT_RECORD;
typedef int INT_PTR;
typedef int KEY_EVENT_RECORD;
typedef int LOGFONTW;
typedef int LPARAM;
typedef int LPBOOL;
typedef int LPCSTR;
typedef int LPCWSTR;
typedef int LPDWORD;
typedef int LPSTR;
typedef int LPTSTR;
typedef int LPVOID;
typedef int LPWSTR;
typedef int LRESULT;
typedef int MOUSE_EVENT_RECORD;
typedef int NEWTEXTMETRICW;
typedef int PACL;
typedef int PRINTDLGW;
typedef int PSECURITY_DESCRIPTOR;
typedef int PSID;
typedef int SECURITY_INFORMATION;
typedef int SHORT;
typedef int SMALL_RECT;
typedef int TEXTMETRIC;
typedef int UINT;
typedef int WCHAR;
typedef int WNDENUMPROC;
typedef int WORD;
typedef int WPARAM;
typedef void VOID;
#endif
// Record all output and all keyboard & mouse input
// #define MCH_WRITE_DUMP
#ifdef MCH_WRITE_DUMP
FILE* fdDump = NULL;
#endif
#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
extern char g_szOrigTitle[];
#endif
#ifdef FEAT_GUI
extern HWND s_hwnd;
#else
static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
#endif
#ifdef FEAT_JOB_CHANNEL
int WSInitialized = FALSE; // WinSock is initialized
#endif
// Don't generate prototypes here, because some systems do have these
// functions.
#if defined(__GNUC__) && !defined(PROTO)
# ifndef __MINGW32__
int _stricoll(char *a, char *b)
{
// the ANSI-ish correct way is to use strxfrm():
char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
strxfrm(a_buff, a, 512);
strxfrm(b_buff, b, 512);
return strcoll(a_buff, b_buff);
}
char * _fullpath(char *buf, char *fname, int len)
{
LPTSTR toss;
return (char *)GetFullPathName(fname, len, buf, &toss);
}
# endif
# if !defined(__MINGW32__) || (__GNUC__ < 4)
int _chdrive(int drive)
{
char temp [3] = "-:";
temp[0] = drive + 'A' - 1;
return !SetCurrentDirectory(temp);
}
# endif
#endif
#ifndef PROTO
/*
* Save the instance handle of the exe/dll.
*/
void
SaveInst(HINSTANCE hInst)
{
g_hinst = hInst;
}
#endif
#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
/*
* GUI version of mch_exit().
* Shut down and exit with status `r'
* Careful: mch_exit() may be called before mch_init()!
*/
void
mch_exit_g(int r)
{
exiting = TRUE;
display_errors();
ml_close_all(TRUE); // remove all memfiles
# ifdef FEAT_OLE
UninitOLE();
# endif
# ifdef FEAT_JOB_CHANNEL
if (WSInitialized)
{
WSInitialized = FALSE;
WSACleanup();
}
# endif
# ifdef DYNAMIC_GETTEXT
dyn_libintl_end();
# endif
if (gui.in_use)
gui_exit(r);
# ifdef EXITFREE
free_all_mem();
# endif
exit(r);
}
#endif // FEAT_GUI_MSWIN
/*
* Init the tables for toupper() and tolower().
*/
void
mch_early_init(void)
{
int i;
PlatformId();
// Init the tables for toupper() and tolower()
for (i = 0; i < 256; ++i)
toupper_tab[i] = tolower_tab[i] = i;
CharUpperBuff((LPSTR)toupper_tab, 256);
CharLowerBuff((LPSTR)tolower_tab, 256);
}
/*
* Return TRUE if the input comes from a terminal, FALSE otherwise.
*/
int
mch_input_isatty(void)
{
#ifdef FEAT_GUI_MSWIN
# ifdef VIMDLL
if (gui.in_use)
# endif
return TRUE; // GUI always has a tty
#endif
#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
if (isatty(read_cmd_fd))
return TRUE;
return FALSE;
#endif
}
/*
* mch_settitle(): set titlebar of our window
*/
void
mch_settitle(
char_u *title,
char_u *icon UNUSED)
{
#ifdef FEAT_GUI_MSWIN
# ifdef VIMDLL
if (gui.in_use)
# endif
{
gui_mch_settitle(title, icon);
return;
}
#endif
#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
if (title != NULL)
{
WCHAR *wp = enc_to_utf16(title, NULL);
if (wp == NULL)
return;
SetConsoleTitleW(wp);
vim_free(wp);
return;
}
#endif
}
/*
* Restore the window/icon title.
* which is one of:
* SAVE_RESTORE_TITLE: Just restore title
* SAVE_RESTORE_ICON: Just restore icon (which we don't have)
* SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
*/
void
mch_restore_title(int which UNUSED)
{
#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
# ifdef VIMDLL
if (!gui.in_use)
# endif
SetConsoleTitle(g_szOrigTitle);
#endif
}
/*
* Return TRUE if we can restore the title (we can)
*/
int
mch_can_restore_title(void)
{
return TRUE;
}
/*
* Return TRUE if we can restore the icon title (we can't)
*/
int
mch_can_restore_icon(void)
{
return FALSE;
}
/*
* Get absolute file name into buffer "buf" of length "len" bytes,
* turning all '/'s into '\\'s and getting the correct case of each component
* of the file name. Append a (back)slash to a directory name.
* When 'shellslash' set do it the other way around.
* Return OK or FAIL.
*/
int
mch_FullName(
char_u *fname,
char_u *buf,
int len,
int force UNUSED)
{
int nResult = FAIL;
WCHAR *wname;
WCHAR wbuf[MAX_PATH];
char_u *cname = NULL;
wname = enc_to_utf16(fname, NULL);
if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
{
cname = utf16_to_enc((short_u *)wbuf, NULL);
if (cname != NULL)
{
vim_strncpy(buf, cname, len - 1);
nResult = OK;
}
}
vim_free(wname);
vim_free(cname);
#ifdef USE_FNAME_CASE
fname_case(buf, len);
#else
slash_adjust(buf);
#endif
return nResult;
}
/*
* Return TRUE if "fname" does not depend on the current directory.
*/
int
mch_isFullName(char_u *fname)
{
// A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
// Another way to check is to use mch_FullName() and see if the result is
// the same as the name or mch_FullName() fails. However, this has quite a
// bit of overhead, so let's not do that.
if (*fname == NUL)
return FALSE;
return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
&& (fname[2] == '/' || fname[2] == '\\'))
|| (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
}
/*
* Replace all slashes by backslashes.
* This used to be the other way around, but MS-DOS sometimes has problems
* with slashes (e.g. in a command name). We can't have mixed slashes and
* backslashes, because comparing file names will not work correctly. The
* commands that use a file name should try to avoid the need to type a
* backslash twice.
* When 'shellslash' set do it the other way around.
* When the path looks like a URL leave it unmodified.
*/
void
slash_adjust(char_u *p)
{
if (path_with_url(p))
return;
if (*p == '`')
{
size_t len = STRLEN(p);
// don't replace backslash in backtick quoted strings
if (len > 2 && *(p + len - 1) == '`')
return;
}
while (*p)
{
if (*p == psepcN)
*p = psepc;
MB_PTR_ADV(p);
}
}
// Use 64-bit stat functions.
#undef stat
#undef _stat
#undef _wstat
#undef _fstat
#define stat _stat64
#define _stat _stat64
#define _wstat _wstat64
#define _fstat _fstat64
static int
read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
{
HANDLE h;
BOOL ok;
h = CreateFileW(name, FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
NULL);
if (h == INVALID_HANDLE_VALUE)
return FAIL;
ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
buf_len, NULL);
CloseHandle(h);
return ok ? OK : FAIL;
}
static int
wstat_symlink_aware(const WCHAR *name, stat_T *stp)
{
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
// Work around for VC12 or earlier (and MinGW). _wstat() can't handle
// symlinks properly.
// VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
// status of a symlink itself.
// VC10: _wstat() supports a symlink to a normal file, but it doesn't
// support a symlink to a directory (always returns an error).
// VC11 and VC12: _wstat() doesn't return an error for a symlink to a
// directory, but it doesn't set S_IFDIR flag.
// MinGW: Same as VC9.
int n;
BOOL is_symlink = FALSE;
HANDLE hFind, h;
DWORD attr = 0;
WIN32_FIND_DATAW findDataW;
hFind = FindFirstFileW(name, &findDataW);
if (hFind != INVALID_HANDLE_VALUE)
{
attr = findDataW.dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
&& (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
is_symlink = TRUE;
FindClose(hFind);
}
if (is_symlink)
{
h = CreateFileW(name, FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING,
(attr & FILE_ATTRIBUTE_DIRECTORY)
? FILE_FLAG_BACKUP_SEMANTICS : 0,
NULL);
if (h != INVALID_HANDLE_VALUE)
{
int fd;
fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
n = _fstat(fd, (struct _stat *)stp);
if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
_close(fd);
return n;
}
}
#endif
return _wstat(name, (struct _stat *)stp);
}
char_u *
resolve_appexeclink(char_u *fname)
{
DWORD attr = 0;
int idx;
WCHAR *p, *end, *wname;
// The buffer size is arbitrarily chosen to be "big enough" (TM), the
// ceiling should be around 16k.
char_u buf[4096];
DWORD buf_len = sizeof(buf);
REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
wname = enc_to_utf16(fname, NULL);
if (wname == NULL)
return NULL;
attr = GetFileAttributesW(wname);
if (attr == INVALID_FILE_ATTRIBUTES ||
(attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
{
vim_free(wname);
return NULL;
}
// The applinks are similar to symlinks but with a huge difference: they can
// only be executed, any other I/O operation on them is bound to fail with
// ERROR_FILE_NOT_FOUND even though the file exists.
if (read_reparse_point(wname, buf, &buf_len) == FAIL)
{
vim_free(wname);
return NULL;
}
vim_free(wname);
if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
return NULL;
// The (undocumented) reparse buffer contains a set of N null-terminated
// Unicode strings, the application path is stored in the third one.
if (rb->AppExecLinkReparseBuffer.StringCount < 3)
return NULL;
p = rb->AppExecLinkReparseBuffer.StringList;
end = p + rb->ReparseDataLength / sizeof(WCHAR);
for (idx = 0; p < end
&& idx < (int)rb->AppExecLinkReparseBuffer.StringCount
&& idx != 2; )
{
if (*p++ == L'\0')
++idx;
}
return utf16_to_enc(p, NULL);
}
/*
* stat() can't handle a trailing '/' or '\', remove it first.
*/
int
vim_stat(const char *name, stat_T *stp)
{
// WinNT and later can use _MAX_PATH wide characters for a pathname, which
// means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
// UTF-8.
char_u buf[_MAX_PATH * 3 + 1];
char_u *p;
WCHAR *wp;
int n;
vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
p = buf + STRLEN(buf);
if (p > buf)
MB_PTR_BACK(buf, p);
// Remove trailing '\\' except root path.
if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
*p = NUL;
if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
{
// UNC root path must be followed by '\\'.
p = vim_strpbrk(buf + 2, (char_u *)"\\/");
if (p != NULL)
{
p = vim_strpbrk(p + 1, (char_u *)"\\/");
if (p == NULL)
STRCAT(buf, "\\");
}
}
wp = enc_to_utf16(buf, NULL);
if (wp == NULL)
return -1;
n = wstat_symlink_aware(wp, stp);
vim_free(wp);
return n;
}
#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
void
mch_settmode(tmode_T tmode UNUSED)
{
// nothing to do
}
int
mch_get_shellsize(void)
{
// never used
return OK;
}
void
mch_set_shellsize(void)
{
// never used
}
/*
* Rows and/or Columns has changed.
*/
void
mch_new_shellsize(void)
{
// never used
}
#endif
/*
* We have no job control, so fake it by starting a new shell.
*/
void
mch_suspend(void)
{
suspend_shell();
}
#if defined(USE_MCH_ERRMSG) || defined(PROTO)
# ifdef display_errors
# undef display_errors
# endif
/*
* Display the saved error message(s).
*/
void
display_errors(void)
{
# ifdef FEAT_GUI
char_u *p;
# ifdef VIMDLL
if (gui.in_use || gui.starting)
# endif
{
if (error_ga.ga_data != NULL)
{
// avoid putting up a message box with blanks only
for (p = (char_u *)error_ga.ga_data; *p; ++p)
if (!isspace(*p))
{
// Only use a dialog when not using --gui-dialog-file:
// write text to a file.
if (!gui_dialog_log((char_u *)"Errors", p))
(void)gui_mch_dialog(
gui.starting ? VIM_INFO :
VIM_ERROR,
gui.starting ? (char_u *)_("Message") :
(char_u *)_("Error"),
p, (char_u *)_("&Ok"),
1, NULL, FALSE);
break;
}
ga_clear(&error_ga);
}
return;
}
# endif
# if !defined(FEAT_GUI) || defined(VIMDLL)
FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
# endif
}
#endif
/*
* Return TRUE if "p" contain a wildcard that can be expanded by
* dos_expandpath().
*/
int
mch_has_exp_wildcard(char_u *p)
{
for ( ; *p; MB_PTR_ADV(p))
{
if (vim_strchr((char_u *)"?*[", *p) != NULL
|| (*p == '~' && p[1] != NUL))
return TRUE;
}
return FALSE;
}
/*
* Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
* shortened file name).
*/
int
mch_has_wildcard(char_u *p)
{
for ( ; *p; MB_PTR_ADV(p))
{
if (vim_strchr((char_u *)
#ifdef VIM_BACKTICK
"?*$[`"
#else
"?*$["
#endif
, *p) != NULL
|| (*p == '~' && p[1] != NUL))
return TRUE;
}
return FALSE;
}
/*
* The normal _chdir() does not change the default drive. This one does.
* Returning 0 implies success; -1 implies failure.
*/
int
mch_chdir(char *path)
{
WCHAR *p;
int n;
if (path[0] == NUL) // just checking...
return -1;
if (p_verbose >= 5)
{
verbose_enter();
smsg("chdir(%s)", path);
verbose_leave();
}
if (isalpha(path[0]) && path[1] == ':') // has a drive name
{
// If we can change to the drive, skip that part of the path. If we
// can't then the current directory may be invalid, try using chdir()
// with the whole path.
if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
path += 2;
}
if (*path == NUL) // drive name only
return 0;
p = enc_to_utf16((char_u *)path, NULL);
if (p == NULL)
return -1;
n = _wchdir(p);
vim_free(p);
return n;
}
#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
/*
* return non-zero if a character is available
*/
int
mch_char_avail(void)
{
// never used
return TRUE;
}
# if defined(FEAT_TERMINAL) || defined(PROTO)
/*
* Check for any pending input or messages.
*/
int
mch_check_messages(void)
{
// TODO: check for messages
return TRUE;
}
# endif
#endif
#if defined(FEAT_LIBCALL) || defined(PROTO)
/*
* Call a DLL routine which takes either a string or int param
* and returns an allocated string.
* Return OK if it worked, FAIL if not.
*/
typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
typedef LPTSTR (*MYINTPROCSTR)(int);
typedef int (*MYSTRPROCINT)(LPTSTR);
typedef int (*MYINTPROCINT)(int);
/*
* Check if a pointer points to a valid NUL terminated string.
* Return the length of the string, including terminating NUL.
* Returns 0 for an invalid pointer, 1 for an empty string.
*/
static size_t
check_str_len(char_u *str)
{
SYSTEM_INFO si;
MEMORY_BASIC_INFORMATION mbi;
size_t length = 0;
size_t i;
const char_u *p;
// get page size
GetSystemInfo(&si);
// get memory information
if (!VirtualQuery(str, &mbi, sizeof(mbi)))
return 0;
// pre cast these (typing savers)
long_u dwStr = (long_u)str;
long_u dwBaseAddress = (long_u)mbi.BaseAddress;
// get start address of page that str is on
long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
// get length from str to end of page
long_u pageLength = si.dwPageSize - (dwStr - strPage);
for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
p += pageLength, pageLength = si.dwPageSize)
for (i = 0; i < pageLength; ++i, ++length)
if (p[i] == NUL)
return length + 1;
return 0;
}
/*
* Passed to do_in_runtimepath() to load a vim.ico file.
*/
static void
mch_icon_load_cb(char_u *fname, void *cookie)
{
HANDLE *h = (HANDLE *)cookie;
*h = LoadImage(NULL,
(LPSTR)fname,
IMAGE_ICON,
64,
64,
LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
}
/*
* Try loading an icon file from 'runtimepath'.
*/
int
mch_icon_load(HANDLE *iconp)
{
return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
0, mch_icon_load_cb, iconp);
}
int
mch_libcall(
char_u *libname,
char_u *funcname,
char_u *argstring, // NULL when using a argint
int argint,
char_u **string_result,// NULL when using number_result
int *number_result)
{
HINSTANCE hinstLib;
MYSTRPROCSTR ProcAdd;
MYINTPROCSTR ProcAddI;
char_u *retval_str = NULL;
int retval_int = 0;
size_t len;
BOOL fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = vimLoadLib((char *)libname);
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
# ifdef HAVE_TRY_EXCEPT
__try
{
# endif
if (argstring != NULL)
{
// Call with string argument
ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
else
retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
}
}
else
{
// Call with number argument
ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYINTPROCINT)ProcAddI)(argint);
else
retval_str = (char_u *)(ProcAddI)(argint);
}
}
// Save the string before we free the library.
// Assume that a "1" result is an illegal pointer.
if (string_result == NULL)
*number_result = retval_int;
else if (retval_str != NULL
&& (len = check_str_len(retval_str)) > 0)
{
*string_result = alloc(len);
if (*string_result != NULL)
mch_memmove(*string_result, retval_str, len);
}
# ifdef HAVE_TRY_EXCEPT
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
_resetstkoflw();
fRunTimeLinkSuccess = 0;
}
# endif
// Free the DLL module.
(void)FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess)
{
semsg(_(e_library_call_failed_for_str), funcname);
return FAIL;
}
return OK;
}
#endif
/*
* Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
*/
void
DumpPutS(const char *psz UNUSED)
{
#ifdef MCH_WRITE_DUMP
if (fdDump)
{
fputs(psz, fdDump);
if (psz[strlen(psz) - 1] != '\n')
fputc('\n', fdDump);
fflush(fdDump);
}
#endif
}
#ifdef _DEBUG
void __cdecl
Trace(
char *pszFormat,
...)
{
CHAR szBuff[2048];
va_list args;