Merge branch 'vim'
[MacVim.git] / src / uninstal.c
blob23c246b30458170578ccbada3bcf7222aaddd837
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * uninstal.c: Minimalistic uninstall program for Vim on MS-Windows
12 * Removes:
13 * - the "Edit with Vim" popup menu entry
14 * - the Vim "Open With..." popup menu entry
15 * - any Vim Batch files in the path
16 * - icons for Vim on the Desktop
17 * - the Vim entry in the Start Menu
20 /* Include common code for dosinst.c and uninstal.c. */
21 #include "dosinst.h"
24 * Return TRUE if the user types a 'y' or 'Y', FALSE otherwise.
26 static int
27 confirm(void)
29 char answer[10];
31 fflush(stdout);
32 return (scanf(" %c", answer) == 1 && toupper(answer[0]) == 'Y');
35 #ifdef WIN3264
37 * Check if the popup menu entry exists and what gvim it refers to.
38 * Returns non-zero when it's found.
40 static int
41 popup_gvim_path(char *buf)
43 HKEY key_handle;
44 DWORD value_type;
45 DWORD bufsize = BUFSIZE;
46 int r;
48 /* Open the key where the path to gvim.exe is stored. */
49 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0, KEY_READ,
50 &key_handle) != ERROR_SUCCESS)
51 return 0;
53 /* get the DisplayName out of it to show the user */
54 r = RegQueryValueEx(key_handle, "path", 0,
55 &value_type, (LPBYTE)buf, &bufsize);
56 RegCloseKey(key_handle);
58 return (r == ERROR_SUCCESS);
62 * Check if the "Open With..." menu entry exists and what gvim it refers to.
63 * Returns non-zero when it's found.
65 static int
66 openwith_gvim_path(char *buf)
68 HKEY key_handle;
69 DWORD value_type;
70 DWORD bufsize = BUFSIZE;
71 int r;
73 /* Open the key where the path to gvim.exe is stored. */
74 if (RegOpenKeyEx(HKEY_CLASSES_ROOT,
75 "Applications\\gvim.exe\\shell\\edit\\command", 0, KEY_READ,
76 &key_handle) != ERROR_SUCCESS)
77 return 0;
79 /* get the DisplayName out of it to show the user */
80 r = RegQueryValueEx(key_handle, "", 0, &value_type, (LPBYTE)buf, &bufsize);
81 RegCloseKey(key_handle);
83 return (r == ERROR_SUCCESS);
86 static void
87 remove_popup(void)
89 int fail = 0;
90 HKEY kh;
92 if (RegDeleteKey(HKEY_CLASSES_ROOT, "CLSID\\{51EEE242-AD87-11d3-9C1E-0090278BBD99}\\InProcServer32") != ERROR_SUCCESS)
93 ++fail;
94 if (RegDeleteKey(HKEY_CLASSES_ROOT, "CLSID\\{51EEE242-AD87-11d3-9C1E-0090278BBD99}") != ERROR_SUCCESS)
95 ++fail;
96 if (RegDeleteKey(HKEY_CLASSES_ROOT, "*\\shellex\\ContextMenuHandlers\\gvim") != ERROR_SUCCESS)
97 ++fail;
98 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", 0, KEY_ALL_ACCESS, &kh) != ERROR_SUCCESS)
99 ++fail;
100 else
102 if (RegDeleteValue(kh, "{51EEE242-AD87-11d3-9C1E-0090278BBD99}") != ERROR_SUCCESS)
103 ++fail;
104 RegCloseKey(kh);
106 if (RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim") != ERROR_SUCCESS)
107 ++fail;
108 if (RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Vim") != ERROR_SUCCESS)
109 ++fail;
111 if (fail == 6)
112 printf("No Vim popup registry entries could be removed\n");
113 else if (fail > 0)
114 printf("Some Vim popup registry entries could not be removed\n");
115 else
116 printf("The Vim popup registry entries have been removed\n");
119 static void
120 remove_openwith(void)
122 int fail = 0;
124 if (RegDeleteKey(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell\\edit\\command") != ERROR_SUCCESS)
125 ++fail;
126 if (RegDeleteKey(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell\\edit") != ERROR_SUCCESS)
127 ++fail;
128 if (RegDeleteKey(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell") != ERROR_SUCCESS)
129 ++fail;
130 if (RegDeleteKey(HKEY_CLASSES_ROOT, "Applications\\gvim.exe") != ERROR_SUCCESS)
131 ++fail;
132 if (RegDeleteKey(HKEY_CLASSES_ROOT, ".htm\\OpenWithList\\gvim.exe") != ERROR_SUCCESS)
133 ++fail;
134 if (RegDeleteKey(HKEY_CLASSES_ROOT, ".vim\\OpenWithList\\gvim.exe") != ERROR_SUCCESS)
135 ++fail;
136 if (RegDeleteKey(HKEY_CLASSES_ROOT, "*\\OpenWithList\\gvim.exe") != ERROR_SUCCESS)
137 ++fail;
139 if (fail == 7)
140 printf("No Vim open-with registry entries could be removed\n");
141 else if (fail > 0)
142 printf("Some Vim open-with registry entries could not be removed\n");
143 else
144 printf("The Vim open-with registry entries have been removed\n");
146 #endif
149 * Check if a batch file is really for the current version. Don't delete a
150 * batch file that was written for another (possibly newer) version.
152 static int
153 batfile_thisversion(char *path)
155 FILE *fd;
156 char line[BUFSIZE];
157 char *p;
158 int ver_len = strlen(VIM_VERSION_NODOT);
159 int found = FALSE;
161 fd = fopen(path, "r");
162 if (fd != NULL)
164 while (fgets(line, BUFSIZE, fd) != NULL)
166 for (p = line; *p != 0; ++p)
167 /* don't accept "vim60an" when looking for "vim60". */
168 if (strnicmp(p, VIM_VERSION_NODOT, ver_len) == 0
169 && !isdigit(p[ver_len])
170 && !isalpha(p[ver_len]))
172 found = TRUE;
173 break;
175 if (found)
176 break;
178 fclose(fd);
180 return found;
183 static int
184 remove_batfiles(int doit)
186 char *batfile_path;
187 int i;
188 int found = 0;
190 for (i = 1; i < TARGET_COUNT; ++i)
192 batfile_path = searchpath_save(targets[i].batname);
193 if (batfile_path != NULL && batfile_thisversion(batfile_path))
195 ++found;
196 if (doit)
198 printf("removing %s\n", batfile_path);
199 remove(batfile_path);
201 else
202 printf(" - the batch file %s\n", batfile_path);
203 free(batfile_path);
206 return found;
209 #ifdef WIN3264
210 static void
211 remove_if_exists(char *path, char *filename)
213 char buf[BUFSIZE];
214 FILE *fd;
216 sprintf(buf, "%s\\%s", path, filename);
218 fd = fopen(buf, "r");
219 if (fd != NULL)
221 fclose(fd);
222 printf("removing %s\n", buf);
223 remove(buf);
227 static void
228 remove_icons(void)
230 char path[BUFSIZE];
231 int i;
233 if (get_shell_folder_path(path, "desktop"))
234 for (i = 0; i < ICON_COUNT; ++i)
235 remove_if_exists(path, icon_link_names[i]);
238 static void
239 remove_start_menu(void)
241 char path[BUFSIZE];
242 int i;
243 struct stat st;
245 if (get_shell_folder_path(path, VIM_STARTMENU))
247 for (i = 1; i < TARGET_COUNT; ++i)
248 remove_if_exists(path, targets[i].lnkname);
249 remove_if_exists(path, "uninstall.lnk");
250 remove_if_exists(path, "Help.lnk");
251 /* Win95 uses .pif, WinNT uses .lnk */
252 remove_if_exists(path, "Vim tutor.pif");
253 remove_if_exists(path, "Vim tutor.lnk");
254 remove_if_exists(path, "Vim online.url");
255 if (stat(path, &st) == 0)
257 printf("removing %s\n", path);
258 rmdir(path);
262 #endif
264 #if 0 /* currently not used */
266 * Return TRUE when we're on Windows 95/98/ME.
268 static int
269 win95(void)
271 static int done = FALSE;
272 static DWORD PlatformId;
274 if (!done)
276 OSVERSIONINFO ovi;
278 ovi.dwOSVersionInfoSize = sizeof(ovi);
279 GetVersionEx(&ovi);
280 PlatformId = ovi.dwPlatformId;
281 done = TRUE;
283 /* Win NT/2000/XP is VER_PLATFORM_WIN32_NT */
284 return PlatformId == VER_PLATFORM_WIN32_WINDOWS;
286 #endif
288 static void
289 delete_uninstall_key(void)
291 #ifdef WIN3264
292 RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Vim " VIM_VERSION_SHORT);
293 #else
294 FILE *fd;
295 char buf[BUFSIZE];
298 * On DJGPP we delete registry entries by creating a .inf file and
299 * installing it.
301 fd = fopen("vim.inf", "w");
302 if (fd != NULL)
304 fprintf(fd, "[version]\n");
305 fprintf(fd, "signature=\"$CHICAGO$\"\n\n");
306 fprintf(fd, "[DefaultInstall]\n");
307 fprintf(fd, "DelReg=DeleteMe\n\n");
308 fprintf(fd, "[DeleteMe]\n");
309 fprintf(fd, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Vim " VIM_VERSION_SHORT "\"\n");
310 fclose(fd);
312 /* Don't know how to detect Win NT with DJGPP. Hack: Just try the Win
313 * 95/98/ME method, since the DJGPP version can't use long filenames
314 * on Win NT anyway. */
315 sprintf(buf, "rundll setupx.dll,InstallHinfSection DefaultInstall 132 %s\\vim.inf", installdir);
316 run_command(buf);
317 #if 0
318 /* Windows NT method (untested). */
319 sprintf(buf, "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 128 %s\\vim.inf", installdir);
320 run_command(buf);
321 #endif
323 remove("vim.inf");
325 #endif
329 main(int argc, char *argv[])
331 int found = 0;
332 FILE *fd;
333 #ifdef WIN3264
334 int i;
335 struct stat st;
336 char icon[BUFSIZE];
337 char path[BUFSIZE];
338 char popup_path[BUFSIZE];
340 /* The nsis uninstaller calls us with a "-nsis" argument. */
341 if (argc == 2 && stricmp(argv[1], "-nsis") == 0)
342 interactive = FALSE;
343 else
344 #endif
345 interactive = TRUE;
347 /* Initialize this program. */
348 do_inits(argv);
350 printf("This program will remove the following items:\n");
352 #ifdef WIN3264
353 if (popup_gvim_path(popup_path))
355 printf(" - the \"Edit with Vim\" entry in the popup menu\n");
356 printf(" which uses \"%s\"\n", popup_path);
357 printf("\nRemove it (y/n)? ");
358 if (confirm())
360 remove_popup();
361 /* Assume the "Open With" entry can be removed as well, don't
362 * bother the user with asking him again. */
363 remove_openwith();
366 else if (openwith_gvim_path(popup_path))
368 printf(" - the Vim \"Open With...\" entry in the popup menu\n");
369 printf(" which uses \"%s\"\n", popup_path);
370 printf("\nRemove it (y/n)? ");
371 if (confirm())
372 remove_openwith();
375 if (get_shell_folder_path(path, "desktop"))
377 printf("\n");
378 for (i = 0; i < ICON_COUNT; ++i)
380 sprintf(icon, "%s\\%s", path, icon_link_names[i]);
381 if (stat(icon, &st) == 0)
383 printf(" - the \"%s\" icon on the desktop\n", icon_names[i]);
384 ++found;
387 if (found > 0)
389 if (interactive)
390 printf("\nRemove %s (y/n)? ", found > 1 ? "them" : "it");
391 if (!interactive || confirm())
392 remove_icons();
396 if (get_shell_folder_path(path, VIM_STARTMENU)
397 && stat(path, &st) == 0)
399 printf("\n - the \"%s\" entry in the Start Menu\n", VIM_STARTMENU);
400 if (interactive)
401 printf("\nRemove it (y/n)? ");
402 if (!interactive || confirm())
403 remove_start_menu();
405 #endif
407 printf("\n");
408 found = remove_batfiles(0);
409 if (found > 0)
411 if (interactive)
412 printf("\nRemove %s (y/n)? ", found > 1 ? "them" : "it");
413 if (!interactive || confirm())
414 remove_batfiles(1);
417 fd = fopen("gvim.exe", "r");
418 if (fd != NULL)
420 fclose(fd);
421 printf("gvim.exe detected. Attempting to unregister gvim with OLE\n");
422 system("gvim.exe -silent -unregister");
425 delete_uninstall_key();
427 if (interactive)
429 printf("\nYou may now want to delete the Vim executables and runtime files.\n");
430 printf("(They are still where you unpacked them.)\n");
433 rewind(stdin);
434 printf("\nPress Enter to exit...");
435 (void)getchar();
437 return 0;