forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dosinst.c
2810 lines (2541 loc) · 73.2 KB
/
dosinst.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.
*/
/*
* dosinst.c: Install program for Vim on MS-DOS and MS-Windows
*
* Compile with Make_mvc.mak, Make_cyg.mak or Make_ming.mak.
*/
/*
* Include common code for dosinst.c and uninstall.c.
*/
#define DOSINST
#include "dosinst.h"
#include <io.h>
#define GVIMEXT64_PATH "GvimExt64\\gvimext.dll"
#define GVIMEXT32_PATH "GvimExt32\\gvimext.dll"
// Macro to do an error check I was typing over and over
#define CHECK_REG_ERROR(code) \
do { \
if (code != ERROR_SUCCESS) \
{ \
printf("%ld error number: %ld\n", (long)__LINE__, (long)code); \
return 1; \
} \
} while (0)
int has_vim = 0; // installable vim.exe exists
int has_gvim = 0; // installable gvim.exe exists
char oldvimrc[BUFSIZE]; // name of existing vimrc file
char vimrc[BUFSIZE]; // name of vimrc file to create
char *default_bat_dir = NULL; // when not NULL, use this as the default
// directory to write .bat files in
char *default_vim_dir = NULL; // when not NULL, use this as the default
// install dir for NSIS
/*
* Structure used for each choice the user can make.
*/
struct choice
{
int active; // non-zero when choice is active
char *text; // text displayed for this choice
void (*changefunc)(int idx); // function to change this choice
int arg; // argument for function
void (*installfunc)(int idx); // function to install this choice
};
struct choice choices[30]; // choices the user can make
int choice_count = 0; // number of choices available
#define TABLE_SIZE(s) (int)ARRAYSIZE(s)
enum
{
compat_vi = 1,
compat_vim,
compat_some_enhancements,
compat_all_enhancements
};
char *(compat_choices[]) =
{
"\nChoose the default way to run Vim:",
"Vi compatible",
"Vim default",
"with some Vim enhancements",
"with syntax highlighting and other features switched on",
};
int compat_choice = (int)compat_all_enhancements;
char *compat_text = "- run Vim %s";
enum
{
remap_no = 1,
remap_win
};
char *(remap_choices[]) =
{
"\nChoose:",
"Do not remap keys for Windows behavior",
"Remap a few keys for Windows behavior (CTRL-V, CTRL-C, CTRL-F, etc)",
};
int remap_choice = (int)remap_no;
char *remap_text = "- %s";
enum
{
mouse_xterm = 1,
mouse_mswin,
mouse_default
};
char *(mouse_choices[]) =
{
"\nChoose the way how Vim uses the mouse:",
"right button extends selection (the Unix way)",
"right button has a popup menu, left button starts select mode (the Windows way)",
"right button has a popup menu, left button starts visual mode",
};
int mouse_choice = (int)mouse_default;
char *mouse_text = "- The mouse %s";
enum
{
vimfiles_dir_none = 1,
vimfiles_dir_vim,
vimfiles_dir_home
};
static char *(vimfiles_dir_choices[]) =
{
"\nCreate plugin directories:",
"No",
"In the VIM directory",
"In your HOME directory",
};
// non-zero when selected to install the popup menu entry.
static int install_popup = 0;
// non-zero when selected to install the "Open with" entry.
static int install_openwith = 0;
// non-zero when need to add an uninstall entry in the registry
static int need_uninstall_entry = 0;
/*
* Definitions of the directory name (under $VIM) of the vimfiles directory
* and its subdirectories:
*/
static char *(vimfiles_subdirs[]) =
{
"colors",
"compiler",
"doc",
"ftdetect",
"ftplugin",
"indent",
"keymap",
"plugin",
"syntax",
};
/*
* Obtain a choice from a table.
* First entry is a question, others are choices.
*/
static int
get_choice(char **table, int entries)
{
int answer;
int idx;
char dummy[100];
do
{
for (idx = 0; idx < entries; ++idx)
{
if (idx)
printf("%2d ", idx);
puts(table[idx]);
}
printf("Choice: ");
if (scanf("%d", &answer) != 1)
{
scanf("%99s", dummy);
answer = 0;
}
}
while (answer < 1 || answer >= entries);
return answer;
}
/*
* Check if the user unpacked the archives properly.
* Sets "runtimeidx".
*/
static void
check_unpack(void)
{
char buf[BUFSIZE];
FILE *fd;
struct stat st;
// check for presence of the correct version number in installdir[]
runtimeidx = strlen(installdir) - strlen(VIM_VERSION_NODOT);
if (runtimeidx <= 0
|| stricmp(installdir + runtimeidx, VIM_VERSION_NODOT) != 0
|| (installdir[runtimeidx - 1] != '/'
&& installdir[runtimeidx - 1] != '\\'))
{
printf("ERROR: Install program not in directory \"%s\"\n",
VIM_VERSION_NODOT);
printf("This program can only work when it is located in its original directory\n");
myexit(1);
}
// check if filetype.vim is present, which means the runtime archive has
// been unpacked
sprintf(buf, "%s\\filetype.vim", installdir);
if (stat(buf, &st) < 0)
{
printf("ERROR: Cannot find filetype.vim in \"%s\"\n", installdir);
printf("It looks like you did not unpack the runtime archive.\n");
printf("You must unpack the runtime archive \"%srt.zip\" before installing.\n",
VIM_VERSION_NODOT);
myexit(1);
}
// Check if vim.exe or gvim.exe is in the current directory.
if ((fd = fopen("gvim.exe", "r")) != NULL)
{
fclose(fd);
has_gvim = 1;
}
if ((fd = fopen("vim.exe", "r")) != NULL)
{
fclose(fd);
has_vim = 1;
}
if (!has_gvim && !has_vim)
{
printf("ERROR: Cannot find any Vim executables in \"%s\"\n\n",
installdir);
myexit(1);
}
}
/*
* Compare paths "p[plen]" to "q[qlen]". Return 0 if they match.
* Ignores case and differences between '/' and '\'.
* "plen" and "qlen" can be negative, strlen() is used then.
*/
static int
pathcmp(char *p, int plen, char *q, int qlen)
{
int i;
if (plen < 0)
plen = strlen(p);
if (qlen < 0)
qlen = strlen(q);
for (i = 0; ; ++i)
{
// End of "p": check if "q" also ends or just has a slash.
if (i == plen)
{
if (i == qlen) // match
return 0;
if (i == qlen - 1 && (q[i] == '\\' || q[i] == '/'))
return 0; // match with trailing slash
return 1; // no match
}
// End of "q": check if "p" also ends or just has a slash.
if (i == qlen)
{
if (i == plen) // match
return 0;
if (i == plen - 1 && (p[i] == '\\' || p[i] == '/'))
return 0; // match with trailing slash
return 1; // no match
}
if (!(mytoupper(p[i]) == mytoupper(q[i])
|| ((p[i] == '/' || p[i] == '\\')
&& (q[i] == '/' || q[i] == '\\'))))
return 1; // no match
}
//NOTREACHED
}
/*
* If the executable "**destination" is in the install directory, find another
* one in $PATH.
* On input "**destination" is the path of an executable in allocated memory
* (or NULL).
* "*destination" is set to NULL or the location of the file.
*/
static void
findoldfile(char **destination)
{
char *bp = *destination;
size_t indir_l = strlen(installdir);
char *cp;
char *tmpname;
char *farname;
/*
* No action needed if exe not found or not in this directory.
*/
if (bp == NULL || strnicmp(bp, installdir, indir_l) != 0)
return;
cp = bp + indir_l;
if (strchr("/\\", *cp++) == NULL
|| strchr(cp, '\\') != NULL
|| strchr(cp, '/') != NULL)
return;
tmpname = alloc(strlen(cp) + 1);
strcpy(tmpname, cp);
tmpname[strlen(tmpname) - 1] = 'x'; // .exe -> .exx
if (access(tmpname, 0) == 0)
{
printf("\nERROR: %s and %s clash. Remove or rename %s.\n",
tmpname, cp, tmpname);
myexit(1);
}
if (rename(cp, tmpname) != 0)
{
printf("\nERROR: failed to rename %s to %s: %s\n",
cp, tmpname, strerror(0));
myexit(1);
}
farname = searchpath_save(cp);
if (rename(tmpname, cp) != 0)
{
printf("\nERROR: failed to rename %s back to %s: %s\n",
tmpname, cp, strerror(0));
myexit(1);
}
free(*destination);
free(tmpname);
*destination = farname;
}
/*
* Check if there is a vim.[exe|bat|, gvim.[exe|bat|, etc. in the path.
* When "check_bat_only" is TRUE, only find "default_bat_dir".
*/
static void
find_bat_exe(int check_bat_only)
{
int i;
// avoid looking in the "installdir" by chdir to system root
mch_chdir(sysdrive);
mch_chdir("\\");
for (i = 1; i < TARGET_COUNT; ++i)
{
targets[i].oldbat = searchpath_save(targets[i].batname);
if (!check_bat_only)
targets[i].oldexe = searchpath_save(targets[i].exename);
if (default_bat_dir == NULL && targets[i].oldbat != NULL)
{
default_bat_dir = alloc(strlen(targets[i].oldbat) + 1);
strcpy(default_bat_dir, targets[i].oldbat);
remove_tail(default_bat_dir);
}
if (check_bat_only && targets[i].oldbat != NULL)
{
free(targets[i].oldbat);
targets[i].oldbat = NULL;
}
}
mch_chdir(installdir);
}
/*
* Get the value of $VIMRUNTIME or $VIM and write it in $TEMP/vimini.ini, so
* that NSIS can read it.
* When not set, use the directory of a previously installed Vim.
*/
static void
get_vim_env(void)
{
char *vim;
char buf[BUFSIZE];
FILE *fd;
char fname[BUFSIZE];
// First get $VIMRUNTIME. If it's set, remove the tail.
vim = getenv("VIMRUNTIME");
if (vim != NULL && *vim != 0 && strlen(vim) < sizeof(buf))
{
strcpy(buf, vim);
remove_tail(buf);
vim = buf;
}
else
{
vim = getenv("VIM");
if (vim == NULL || *vim == 0)
{
// Use the directory from an old uninstall entry.
if (default_vim_dir != NULL)
vim = default_vim_dir;
else
// Let NSIS know there is no default, it should use
// $PROGRAMFILES.
vim = "";
}
}
// NSIS also uses GetTempPath(), thus we should get the same directory
// name as where NSIS will look for vimini.ini.
GetTempPath(sizeof(fname) - 12, fname);
add_pathsep(fname);
strcat(fname, "vimini.ini");
fd = fopen(fname, "w");
if (fd != NULL)
{
// Make it look like an .ini file, so that NSIS can read it with a
// ReadINIStr command.
fprintf(fd, "[vimini]\n");
fprintf(fd, "dir=\"%s\"\n", vim);
fclose(fd);
}
else
{
printf("Failed to open %s\n", fname);
sleep(2);
}
}
static int num_windows;
/*
* Callback used for EnumWindows():
* Count the window if the title looks like it is for the uninstaller.
*/
//ARGSUSED
static BOOL CALLBACK
window_cb(HWND hwnd, LPARAM lparam UNUSED)
{
char title[256];
title[0] = 0;
GetWindowText(hwnd, title, 256);
if (strstr(title, "Vim ") != NULL && strstr(title, " Uninstall") != NULL)
++num_windows;
return TRUE;
}
/*
* Run the uninstaller silently.
*/
static int
run_silent_uninstall(char *uninst_exe)
{
char vimrt_dir[BUFSIZE];
char temp_uninst[BUFSIZE];
char temp_dir[MAX_PATH];
char buf[BUFSIZE * 2 + 10];
int i;
DWORD tick;
strcpy(vimrt_dir, uninst_exe);
remove_tail(vimrt_dir);
if (!GetTempPath(sizeof(temp_dir), temp_dir))
return FAIL;
// Copy the uninstaller to a temporary exe.
tick = GetTickCount();
for (i = 0; ; i++)
{
sprintf(temp_uninst, "%s\\vimun%04X.exe", temp_dir,
(unsigned int)((i + tick) & 0xFFFF));
if (CopyFile(uninst_exe, temp_uninst, TRUE))
break;
if (GetLastError() != ERROR_FILE_EXISTS)
return FAIL;
if (i == 65535)
return FAIL;
}
// Run the copied uninstaller silently.
if (strchr(temp_uninst, ' ') != NULL)
sprintf(buf, "\"%s\" /S _?=%s", temp_uninst, vimrt_dir);
else
sprintf(buf, "%s /S _?=%s", temp_uninst, vimrt_dir);
run_command(buf);
DeleteFile(temp_uninst);
return OK;
}
/*
* Check for already installed Vims.
* Return non-zero when found one.
*/
static int
uninstall_check(int skip_question)
{
HKEY key_handle;
HKEY uninstall_key_handle;
char *uninstall_key = "software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
char subkey_name_buff[BUFSIZE];
char temp_string_buffer[BUFSIZE-2];
DWORD local_bufsize;
FILETIME temp_pfiletime;
DWORD key_index;
char input;
long code;
DWORD value_type;
DWORD orig_num_keys;
DWORD new_num_keys;
DWORD allow_silent;
int foundone = 0;
code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, uninstall_key, 0,
KEY_WOW64_64KEY | KEY_READ, &key_handle);
CHECK_REG_ERROR(code);
key_index = 0;
while (TRUE)
{
local_bufsize = sizeof(subkey_name_buff);
if (RegEnumKeyEx(key_handle, key_index, subkey_name_buff, &local_bufsize,
NULL, NULL, NULL, &temp_pfiletime) == ERROR_NO_MORE_ITEMS)
break;
if (strncmp("Vim", subkey_name_buff, 3) == 0)
{
// Open the key named Vim*
code = RegOpenKeyEx(key_handle, subkey_name_buff, 0,
KEY_WOW64_64KEY | KEY_READ, &uninstall_key_handle);
CHECK_REG_ERROR(code);
// get the DisplayName out of it to show the user
local_bufsize = sizeof(temp_string_buffer);
code = RegQueryValueEx(uninstall_key_handle, "displayname", 0,
&value_type, (LPBYTE)temp_string_buffer,
&local_bufsize);
CHECK_REG_ERROR(code);
allow_silent = 0;
if (skip_question)
{
DWORD varsize = sizeof(DWORD);
RegQueryValueEx(uninstall_key_handle, "AllowSilent", 0,
&value_type, (LPBYTE)&allow_silent,
&varsize);
}
foundone = 1;
printf("\n*********************************************************\n");
printf("Vim Install found what looks like an existing Vim version.\n");
printf("The name of the entry is:\n");
printf("\n \"%s\"\n\n", temp_string_buffer);
printf("Installing the new version will disable part of the existing version.\n");
printf("(The batch files used in a console and the \"Edit with Vim\" entry in\n");
printf("the popup menu will use the new version)\n");
if (skip_question)
printf("\nRunning uninstall program for \"%s\"\n", temp_string_buffer);
else
printf("\nDo you want to uninstall \"%s\" now?\n(y)es/(n)o) ", temp_string_buffer);
fflush(stdout);
// get the UninstallString
local_bufsize = sizeof(temp_string_buffer);
code = RegQueryValueEx(uninstall_key_handle, "uninstallstring", 0,
&value_type, (LPBYTE)temp_string_buffer, &local_bufsize);
CHECK_REG_ERROR(code);
// Remember the directory, it is used as the default for NSIS.
default_vim_dir = alloc(strlen(temp_string_buffer) + 1);
strcpy(default_vim_dir, temp_string_buffer);
remove_tail(default_vim_dir);
remove_tail(default_vim_dir);
input = 'n';
do
{
if (input != 'n')
printf("%c is an invalid reply. Please enter either 'y' or 'n'\n", input);
if (skip_question)
input = 'y';
else
{
rewind(stdin);
scanf("%c", &input);
}
switch (input)
{
case 'y':
case 'Y':
// save the number of uninstall keys so we can know if
// it changed
RegQueryInfoKey(key_handle, NULL, NULL, NULL,
&orig_num_keys, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
// Find existing .bat files before deleting them.
find_bat_exe(TRUE);
if (allow_silent)
{
if (run_silent_uninstall(temp_string_buffer)
== FAIL)
allow_silent = 0; // Retry with non silent.
}
if (!allow_silent)
{
// Execute the uninstall program. Put it in double
// quotes if there is an embedded space.
{
char buf[BUFSIZE];
if (strchr(temp_string_buffer, ' ') != NULL)
sprintf(buf, "\"%s\"", temp_string_buffer);
else
strcpy(buf, temp_string_buffer);
run_command(buf);
}
// Count the number of windows with a title that
// match the installer, so that we can check when
// it's done. The uninstaller copies itself,
// executes the copy and exits, thus we can't wait
// for the process to finish.
sleep(1); // wait for uninstaller to start up
num_windows = 0;
EnumWindows(window_cb, 0);
if (num_windows == 0)
{
// Did not find the uninstaller, ask user to
// press Enter when done. Just in case.
printf("Press Enter when the uninstaller is finished\n");
rewind(stdin);
(void)getchar();
}
else
{
printf("Waiting for the uninstaller to finish (press CTRL-C to abort).");
do
{
printf(".");
fflush(stdout);
sleep(1); // wait for the uninstaller to
// finish
num_windows = 0;
EnumWindows(window_cb, 0);
} while (num_windows > 0);
}
}
printf("\nDone!\n");
// Check if an uninstall reg key was deleted.
// if it was, we want to decrement key_index.
// if we don't do this, we will skip the key
// immediately after any key that we delete.
RegQueryInfoKey(key_handle, NULL, NULL, NULL,
&new_num_keys, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
if (new_num_keys < orig_num_keys)
key_index--;
input = 'y';
break;
case 'n':
case 'N':
// Do not uninstall
input = 'n';
break;
default: // just drop through and redo the loop
break;
}
} while (input != 'n' && input != 'y');
RegCloseKey(uninstall_key_handle);
}
key_index++;
}
RegCloseKey(key_handle);
return foundone;
}
/*
* Find out information about the system.
*/
static void
inspect_system(void)
{
char *p;
char buf[BUFSIZE];
FILE *fd;
int i;
int foundone;
// This may take a little while, let the user know what we're doing.
printf("Inspecting system...\n");
/*
* If $VIM is set, check that it's pointing to our directory.
*/
p = getenv("VIM");
if (p != NULL && pathcmp(p, -1, installdir, runtimeidx - 1) != 0)
{
printf("------------------------------------------------------\n");
printf("$VIM is set to \"%s\".\n", p);
printf("This is different from where this version of Vim is:\n");
strcpy(buf, installdir);
*(buf + runtimeidx - 1) = NUL;
printf("\"%s\"\n", buf);
printf("You must adjust or remove the setting of $VIM,\n");
if (interactive)
{
printf("to be able to use this install program.\n");
myexit(1);
}
printf("otherwise Vim WILL NOT WORK properly!\n");
printf("------------------------------------------------------\n");
}
/*
* If $VIMRUNTIME is set, check that it's pointing to our runtime directory.
*/
p = getenv("VIMRUNTIME");
if (p != NULL && pathcmp(p, -1, installdir, -1) != 0)
{
printf("------------------------------------------------------\n");
printf("$VIMRUNTIME is set to \"%s\".\n", p);
printf("This is different from where this version of Vim is:\n");
printf("\"%s\"\n", installdir);
printf("You must adjust or remove the setting of $VIMRUNTIME,\n");
if (interactive)
{
printf("to be able to use this install program.\n");
myexit(1);
}
printf("otherwise Vim WILL NOT WORK properly!\n");
printf("------------------------------------------------------\n");
}
/*
* Check if there is a vim.[exe|bat|, gvim.[exe|bat|, etc. in the path.
*/
find_bat_exe(FALSE);
/*
* A .exe in the install directory may be found anyway on Windows 2000.
* Check for this situation and find another executable if necessary.
* w.briscoe@ponl.com 2001-01-20
*/
foundone = 0;
for (i = 1; i < TARGET_COUNT; ++i)
{
findoldfile(&(targets[i].oldexe));
if (targets[i].oldexe != NULL)
foundone = 1;
}
if (foundone)
{
printf("Warning: Found Vim executable(s) in your $PATH:\n");
for (i = 1; i < TARGET_COUNT; ++i)
if (targets[i].oldexe != NULL)
printf("%s\n", targets[i].oldexe);
printf("It will be used instead of the version you are installing.\n");
printf("Please delete or rename it, or adjust your $PATH setting.\n");
}
/*
* Check if there is an existing ../_vimrc or ../.vimrc file.
*/
strcpy(oldvimrc, installdir);
strcpy(oldvimrc + runtimeidx, "_vimrc");
if ((fd = fopen(oldvimrc, "r")) == NULL)
{
strcpy(oldvimrc + runtimeidx, "vimrc~1"); // short version of .vimrc
if ((fd = fopen(oldvimrc, "r")) == NULL)
{
strcpy(oldvimrc + runtimeidx, ".vimrc");
fd = fopen(oldvimrc, "r");
}
}
if (fd != NULL)
fclose(fd);
else
*oldvimrc = NUL;
}
/*
* Add a dummy choice to avoid that the numbering changes depending on items
* in the environment. The user may type a number he remembered without
* looking.
*/
static void
add_dummy_choice(void)
{
choices[choice_count].installfunc = NULL;
choices[choice_count].active = 0;
choices[choice_count].changefunc = NULL;
choices[choice_count].text = NULL;
choices[choice_count].arg = 0;
++choice_count;
}
////////////////////////////////////////////////
// stuff for creating the batch files.
/*
* Install the vim.bat, gvim.bat, etc. files.
*/
static void
install_bat_choice(int idx)
{
char *batpath = targets[choices[idx].arg].batpath;
char *oldname = targets[choices[idx].arg].oldbat;
char *exename = targets[choices[idx].arg].exenamearg;
char *vimarg = targets[choices[idx].arg].exearg;
FILE *fd;
if (*batpath != NUL)
{
fd = fopen(batpath, "w");
if (fd == NULL)
printf("\nERROR: Cannot open \"%s\" for writing.\n", batpath);
else
{
need_uninstall_entry = 1;
fprintf(fd, "@echo off\n");
fprintf(fd, "rem -- Run Vim --\n");
fprintf(fd, VIMBAT_UNINSTKEY "\n");
fprintf(fd, "\n");
fprintf(fd, "setlocal\n");
/*
* Don't use double quotes for the "set" argument, also when it
* contains a space. The quotes would be included in the value.
* The order of preference is:
* 1. $VIMRUNTIME/vim.exe (user preference)
* 2. $VIM/vim81/vim.exe (hard coded version)
* 3. installdir/vim.exe (hard coded install directory)
*/
fprintf(fd, "set VIM_EXE_DIR=%s\n", installdir);
fprintf(fd, "if exist \"%%VIM%%\\%s\\%s\" set VIM_EXE_DIR=%%VIM%%\\%s\n",
VIM_VERSION_NODOT, exename, VIM_VERSION_NODOT);
fprintf(fd, "if exist \"%%VIMRUNTIME%%\\%s\" set VIM_EXE_DIR=%%VIMRUNTIME%%\n", exename);
fprintf(fd, "\n");
// Give an error message when the executable could not be found.
fprintf(fd, "if not exist \"%%VIM_EXE_DIR%%\\%s\" (\n", exename);
fprintf(fd, " echo \"%%VIM_EXE_DIR%%\\%s\" not found\n", exename);
fprintf(fd, " goto :eof\n");
fprintf(fd, ")\n");
fprintf(fd, "\n");
if (*exename == 'g')
{
fprintf(fd, "rem check --nofork argument\n");
fprintf(fd, "set VIMNOFORK=\n");
fprintf(fd, ":loopstart\n");
fprintf(fd, "if .%%1==. goto loopend\n");
fprintf(fd, "if .%%1==.--nofork (\n");
fprintf(fd, " set VIMNOFORK=1\n");
fprintf(fd, ") else if .%%1==.-f (\n");
fprintf(fd, " set VIMNOFORK=1\n");
fprintf(fd, ")\n");
fprintf(fd, "shift\n");
fprintf(fd, "goto loopstart\n");
fprintf(fd, ":loopend\n");
fprintf(fd, "\n");
}
if (*exename == 'g')
{
// For gvim.exe use "start /b" to avoid that the console window
// stays open.
fprintf(fd, "if .%%VIMNOFORK%%==.1 (\n");
fprintf(fd, " start \"dummy\" /b /wait ");
// Always use quotes, $VIM or $VIMRUNTIME might have a space.
fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%*\n",
exename, vimarg);
fprintf(fd, ") else (\n");
fprintf(fd, " start \"dummy\" /b ");
// Always use quotes, $VIM or $VIMRUNTIME might have a space.
fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%*\n",
exename, vimarg);
fprintf(fd, ")\n");
}
else
{
// Always use quotes, $VIM or $VIMRUNTIME might have a space.
fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%*\n",
exename, vimarg);
}
fclose(fd);
printf("%s has been %s\n", batpath,
oldname == NULL ? "created" : "overwritten");
}
}
}
/*
* Make the text string for choice "idx".
* The format "fmt" is must have one %s item, which "arg" is used for.
*/
static void
alloc_text(int idx, char *fmt, char *arg)
{
if (choices[idx].text != NULL)
free(choices[idx].text);
choices[idx].text = alloc(strlen(fmt) + strlen(arg) - 1);
sprintf(choices[idx].text, fmt, arg);
}
/*
* Toggle the "Overwrite .../vim.bat" to "Don't overwrite".
*/
static void
toggle_bat_choice(int idx)
{
char *batname = targets[choices[idx].arg].batpath;
char *oldname = targets[choices[idx].arg].oldbat;
if (*batname == NUL)
{
alloc_text(idx, " Overwrite %s", oldname);
strcpy(batname, oldname);
}
else
{
alloc_text(idx, " Do NOT overwrite %s", oldname);
*batname = NUL;
}
}
/*
* Do some work for a batch file entry: Append the batch file name to the path
* and set the text for the choice.
*/
static void
set_bat_text(int idx, char *batpath, char *name)
{
strcat(batpath, name);
alloc_text(idx, " Create %s", batpath);
}
/*
* Select a directory to write the batch file line.
*/
static void
change_bat_choice(int idx)
{
char *path;
char *batpath;
char *name;
int n;
char *s;
char *p;
int count;
char **names = NULL;
int i;
int target = choices[idx].arg;
name = targets[target].batname;
batpath = targets[target].batpath;
path = getenv("PATH");
if (path == NULL)
{
printf("\nERROR: The variable $PATH is not set\n");
return;
}
/*
* first round: count number of names in path;
* second round: save names to names[].
*/
for (;;)
{
count = 1;
for (p = path; *p; )
{
s = strchr(p, ';');
if (s == NULL)