-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathos_mswin.c
3028 lines (2657 loc) · 69.4 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:
*
* 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 common to both Win16 and Win32.
*/
#ifdef WIN16
# ifdef __BORLANDC__
# pragma warn -par
# pragma warn -ucp
# pragma warn -use
# pragma warn -aus
# endif
#endif
#include "vim.h"
#ifdef WIN16
# define SHORT_FNAME /* always 8.3 file name */
/* cproto fails on missing include files */
# ifndef PROTO
# include <dos.h>
# endif
# include <string.h>
#endif
#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_TITLE) && !defined(FEAT_GUI_W32)
# include <shellapi.h>
# endif
# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
# include <dlgs.h>
# ifdef WIN3264
# include <winspool.h>
# else
# include <print.h>
# endif
# 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 ENUMLOGFONT;
typedef int HANDLE;
typedef int HDC;
typedef int HFONT;
typedef int HICON;
typedef int HWND;
typedef int INPUT_RECORD;
typedef int KEY_EVENT_RECORD;
typedef int LOGFONT;
typedef int LPARAM;
typedef int LPBOOL;
typedef int LPCSTR;
typedef int LPCWSTR;
typedef int LPSTR;
typedef int LPTSTR;
typedef int LPWSTR;
typedef int LRESULT;
typedef int MOUSE_EVENT_RECORD;
typedef int NEWTEXTMETRIC;
typedef int PACL;
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 UINT;
typedef int WCHAR;
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
#ifdef WIN3264
extern DWORD g_PlatformId;
#endif
#ifndef FEAT_GUI_MSWIN
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
extern int WSInitialized;
/* 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
#else
# ifdef __BORLANDC__
/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
* but it does in BC 5.02! */
# if __BORLANDC__ < 0x502
int _stricoll(char *a, char *b)
{
# if 1
// this is fast but not correct:
return stricmp(a, b);
# else
// 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);
# endif
}
# endif
# endif
#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(int r)
{
display_errors();
ml_close_all(TRUE); /* remove all memfiles */
# ifdef FEAT_OLE
UninitOLE();
# endif
# ifdef FEAT_NETBEANS_INTG
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;
#ifdef WIN3264
PlatformId();
#endif
/* Init the tables for toupper() and tolower() */
for (i = 0; i < 256; ++i)
toupper_tab[i] = tolower_tab[i] = i;
#ifdef WIN3264
CharUpperBuff(toupper_tab, 256);
CharLowerBuff(tolower_tab, 256);
#else
AnsiUpperBuff(toupper_tab, 256);
AnsiLowerBuff(tolower_tab, 256);
#endif
#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
(void)get_cmd_argsW(NULL);
#endif
}
/*
* Return TRUE if the input comes from a terminal, FALSE otherwise.
*/
int
mch_input_isatty()
{
#ifdef FEAT_GUI_MSWIN
return OK; /* GUI always has a tty */
#else
if (isatty(read_cmd_fd))
return TRUE;
return FALSE;
#endif
}
#ifdef FEAT_TITLE
/*
* mch_settitle(): set titlebar of our window
*/
void
mch_settitle(
char_u *title,
char_u *icon)
{
# ifdef FEAT_GUI_MSWIN
gui_mch_settitle(title, icon);
# else
if (title != NULL)
{
# ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
{
/* Convert the title from 'encoding' to the active codepage. */
WCHAR *wp = enc_to_utf16(title, NULL);
int n;
if (wp != NULL)
{
n = SetConsoleTitleW(wp);
vim_free(wp);
if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
return;
}
}
# endif
SetConsoleTitle(title);
}
# endif
}
/*
* Restore the window/icon title.
* which is one of:
* 1: Just restore title
* 2: Just restore icon (which we don't have)
* 3: Restore title and icon (which we don't have)
*/
/*ARGSUSED*/
void
mch_restore_title(
int which)
{
#ifndef FEAT_GUI_MSWIN
mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
#endif
}
/*
* Return TRUE if we can restore the title (we can)
*/
int
mch_can_restore_title()
{
return TRUE;
}
/*
* Return TRUE if we can restore the icon title (we can't)
*/
int
mch_can_restore_icon()
{
return FALSE;
}
#endif /* FEAT_TITLE */
/*
* 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.
*/
/*ARGSUSED*/
int
mch_FullName(
char_u *fname,
char_u *buf,
int len,
int force)
{
int nResult = FAIL;
#ifdef __BORLANDC__
if (*fname == NUL) /* Borland behaves badly here - make it consistent */
nResult = mch_dirname(buf, len);
else
#endif
{
#ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
# ifdef __BORLANDC__
/* Wide functions of Borland C 5.5 do not work on Windows 98. */
&& g_PlatformId == VER_PLATFORM_WIN32_NT
# endif
)
{
WCHAR *wname;
WCHAR wbuf[MAX_PATH];
char_u *cname = NULL;
/* Use the wide function:
* - convert the fname from 'encoding' to UCS2.
* - invoke _wfullpath()
* - convert the result from UCS2 to 'encoding'.
*/
wname = enc_to_utf16(fname, NULL);
if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != 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);
}
if (nResult == FAIL) /* fall back to non-wide function */
#endif
{
if (_fullpath(buf, fname, len - 1) == NULL)
{
/* failed, use relative path name */
vim_strncpy(buf, fname, len - 1);
}
else
nResult = OK;
}
}
#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)
{
#ifdef FEAT_MBYTE
/* 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 szName[_MAX_PATH * 3 + 1];
#else
char szName[_MAX_PATH + 1];
#endif
/* A name like "d:/foo" and "//server/share" is absolute */
if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
|| (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
return TRUE;
/* A name that can't be made absolute probably isn't absolute. */
if (mch_FullName(fname, szName, sizeof(szName) - 1, FALSE) == FAIL)
return FALSE;
return pathcmp(fname, szName, -1) == 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.
*/
void
slash_adjust(p)
char_u *p;
{
while (*p)
{
if (*p == psepcN)
*p = psepc;
mb_ptr_adv(p);
}
}
#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
# define OPEN_OH_ARGTYPE intptr_t
#else
# define OPEN_OH_ARGTYPE long
#endif
static int
stat_symlink_aware(const char *name, struct stat *stp)
{
#if defined(_MSC_VER) && _MSC_VER < 1700
/* Work around for VC10 or earlier. stat() can't handle symlinks properly.
* VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
* status of a symlink itself.
* VC10: stat() supports a symlink to a normal file, but it doesn't support
* a symlink to a directory (always returns an error). */
WIN32_FIND_DATA findData;
HANDLE hFind, h;
DWORD attr = 0;
BOOL is_symlink = FALSE;
hFind = FindFirstFile(name, &findData);
if (hFind != INVALID_HANDLE_VALUE)
{
attr = findData.dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
&& (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
is_symlink = TRUE;
FindClose(hFind);
}
if (is_symlink)
{
h = CreateFile(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, n;
fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
n = _fstat(fd, (struct _stat*)stp);
_close(fd);
return n;
}
}
#endif
return stat(name, stp);
}
#ifdef FEAT_MBYTE
static int
wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
{
# if defined(_MSC_VER) && _MSC_VER < 1700
/* Work around for VC10 or earlier. _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). */
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((OPEN_OH_ARGTYPE)h, _O_RDONLY);
n = _fstat(fd, stp);
_close(fd);
return n;
}
}
# endif
return _wstat(name, stp);
}
#endif
/*
* stat() can't handle a trailing '/' or '\', remove it first.
*/
int
vim_stat(const char *name, struct stat *stp)
{
#ifdef FEAT_MBYTE
/* 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 buf[_MAX_PATH * 3 + 1];
#else
char buf[_MAX_PATH + 1];
#endif
char *p;
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, "\\/");
if (p != NULL)
{
p = vim_strpbrk(p + 1, "\\/");
if (p == NULL)
STRCAT(buf, "\\");
}
}
#ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
# ifdef __BORLANDC__
/* Wide functions of Borland C 5.5 do not work on Windows 98. */
&& g_PlatformId == VER_PLATFORM_WIN32_NT
# endif
)
{
WCHAR *wp = enc_to_utf16(buf, NULL);
int n;
if (wp != NULL)
{
n = wstat_symlink_aware(wp, (struct _stat *)stp);
vim_free(wp);
if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
return n;
/* Retry with non-wide function (for Windows 98). Can't use
* GetLastError() here and it's unclear what errno gets set to if
* the _wstat() fails for missing wide functions. */
}
}
#endif
return stat_symlink_aware(buf, stp);
}
#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
/*ARGSUSED*/
void
mch_settmode(int tmode)
{
/* 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()
{
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()
{
char *p;
if (error_ga.ga_data != NULL)
{
/* avoid putting up a message box with blanks only */
for (p = (char *)error_ga.ga_data; *p; ++p)
if (!isspace(*p))
{
(void)gui_mch_dialog(
#ifdef FEAT_GUI
gui.starting ? VIM_INFO :
#endif
VIM_ERROR,
#ifdef FEAT_GUI
gui.starting ? (char_u *)_("Message") :
#endif
(char_u *)_("Error"),
p, (char_u *)_("&Ok"), 1, NULL, FALSE);
break;
}
ga_clear(&error_ga);
}
}
#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)
{
if (path[0] == NUL) /* just checking... */
return -1;
if (p_verbose >= 5)
{
verbose_enter();
smsg((char_u *)"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;
#ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
{
WCHAR *p = enc_to_utf16(path, NULL);
int n;
if (p != NULL)
{
n = _wchdir(p);
vim_free(p);
if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
return n;
/* Retry with non-wide function (for Windows 98). */
}
}
#endif
return chdir(path); /* let the normal chdir() do the rest */
}
/*
* Switching off termcap mode is only allowed when Columns is 80, otherwise a
* crash may result. It's always allowed on NT or when running the GUI.
*/
/*ARGSUSED*/
int
can_end_termcap_mode(
int give_msg)
{
#ifdef FEAT_GUI_MSWIN
return TRUE; /* GUI starts a new console anyway */
#else
if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
return TRUE;
if (give_msg)
msg(_("'columns' is not 80, cannot execute external commands"));
return FALSE;
#endif
}
#ifdef FEAT_GUI_MSWIN
/*
* return non-zero if a character is available
*/
int
mch_char_avail()
{
/* never used */
return TRUE;
}
#endif
/*
* set screen mode, always fails.
*/
/*ARGSUSED*/
int
mch_screenmode(
char_u *arg)
{
EMSG(_(e_screenmode));
return FAIL;
}
#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.
*/
# ifdef WIN3264
typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
typedef LPTSTR (*MYINTPROCSTR)(int);
typedef int (*MYSTRPROCINT)(LPTSTR);
typedef int (*MYINTPROCINT)(int);
# else
typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
typedef LPSTR (*MYINTPROCSTR)(int);
typedef int (*MYSTRPROCINT)(LPSTR);
typedef int (*MYINTPROCINT)(int);
# endif
# ifndef WIN16
/*
* 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 *p;
/* get page size */
GetSystemInfo(&si);
/* get memory information */
if (VirtualQuery(str, &mbi, sizeof(mbi)))
{
/* 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;
}
# endif
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.
# ifdef WIN16
hinstLib = LoadLibrary(libname);
# else
hinstLib = vimLoadLib(libname);
# endif
// 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, funcname);
if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
else
retval_str = (ProcAdd)(argstring);
}
}
else
{
/* Call with number argument */
ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYINTPROCINT)ProcAddI)(argint);
else
retval_str = (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
# ifdef WIN16
&& retval_str != (char_u *)1
&& retval_str != (char_u *)-1
&& !IsBadStringPtr(retval_str, INT_MAX)