Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 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 | */ |
| 9 | |
| 10 | /* |
| 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 |
| 18 | */ |
| 19 | |
| 20 | /* Include common code for dosinst.c and uninstal.c. */ |
| 21 | #include "dosinst.h" |
| 22 | |
| 23 | /* |
| 24 | * Return TRUE if the user types a 'y' or 'Y', FALSE otherwise. |
| 25 | */ |
| 26 | static int |
| 27 | confirm(void) |
| 28 | { |
| 29 | char answer[10]; |
| 30 | |
| 31 | fflush(stdout); |
| 32 | return (scanf(" %c", answer) == 1 && toupper(answer[0]) == 'Y'); |
| 33 | } |
| 34 | |
| 35 | #ifdef WIN3264 |
| 36 | /* |
| 37 | * Check if the popup menu entry exists and what gvim it refers to. |
| 38 | * Returns non-zero when it's found. |
| 39 | */ |
| 40 | static int |
| 41 | popup_gvim_path(char *buf) |
| 42 | { |
| 43 | HKEY key_handle; |
| 44 | DWORD value_type; |
| 45 | DWORD bufsize = BUFSIZE; |
| 46 | int r; |
| 47 | |
| 48 | /* Open the key where the path to gvim.exe is stored. */ |
Bram Moolenaar | 760d14a | 2010-07-31 22:03:44 +0200 | [diff] [blame] | 49 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0, |
| 50 | KEY_WOW64_64KEY | KEY_READ, &key_handle) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | |
| 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); |
| 57 | |
| 58 | return (r == ERROR_SUCCESS); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Check if the "Open With..." menu entry exists and what gvim it refers to. |
| 63 | * Returns non-zero when it's found. |
| 64 | */ |
| 65 | static int |
| 66 | openwith_gvim_path(char *buf) |
| 67 | { |
| 68 | HKEY key_handle; |
| 69 | DWORD value_type; |
| 70 | DWORD bufsize = BUFSIZE; |
| 71 | int r; |
| 72 | |
| 73 | /* Open the key where the path to gvim.exe is stored. */ |
| 74 | if (RegOpenKeyEx(HKEY_CLASSES_ROOT, |
Bram Moolenaar | 760d14a | 2010-07-31 22:03:44 +0200 | [diff] [blame] | 75 | "Applications\\gvim.exe\\shell\\edit\\command", 0, |
| 76 | KEY_WOW64_64KEY | KEY_READ, &key_handle) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | |
| 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); |
| 82 | |
| 83 | return (r == ERROR_SUCCESS); |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | remove_popup(void) |
| 88 | { |
| 89 | int fail = 0; |
| 90 | HKEY kh; |
| 91 | |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 92 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "CLSID\\{51EEE242-AD87-11d3-9C1E-0090278BBD99}\\InProcServer32", |
| 93 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 95 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "CLSID\\{51EEE242-AD87-11d3-9C1E-0090278BBD99}", |
| 96 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 98 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "*\\shellex\\ContextMenuHandlers\\gvim", |
| 99 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | ++fail; |
Bram Moolenaar | 760d14a | 2010-07-31 22:03:44 +0200 | [diff] [blame] | 101 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", 0, |
| 102 | KEY_WOW64_64KEY | KEY_ALL_ACCESS, &kh) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | ++fail; |
| 104 | else |
| 105 | { |
| 106 | if (RegDeleteValue(kh, "{51EEE242-AD87-11d3-9C1E-0090278BBD99}") != ERROR_SUCCESS) |
| 107 | ++fail; |
| 108 | RegCloseKey(kh); |
| 109 | } |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 110 | if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", |
| 111 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 113 | if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim", |
| 114 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | ++fail; |
| 116 | |
| 117 | if (fail == 6) |
| 118 | printf("No Vim popup registry entries could be removed\n"); |
| 119 | else if (fail > 0) |
| 120 | printf("Some Vim popup registry entries could not be removed\n"); |
| 121 | else |
| 122 | printf("The Vim popup registry entries have been removed\n"); |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | remove_openwith(void) |
| 127 | { |
| 128 | int fail = 0; |
| 129 | |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 130 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell\\edit\\command", |
| 131 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 133 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell\\edit", |
| 134 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 135 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 136 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "Applications\\gvim.exe\\shell", |
| 137 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 139 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "Applications\\gvim.exe", |
| 140 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 142 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, ".htm\\OpenWithList\\gvim.exe", |
| 143 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 145 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, ".vim\\OpenWithList\\gvim.exe", |
| 146 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | ++fail; |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 148 | if (RegDeleteKeyEx(HKEY_CLASSES_ROOT, "*\\OpenWithList\\gvim.exe", |
| 149 | KEY_WOW64_64KEY, 0) != ERROR_SUCCESS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | ++fail; |
| 151 | |
| 152 | if (fail == 7) |
| 153 | printf("No Vim open-with registry entries could be removed\n"); |
| 154 | else if (fail > 0) |
| 155 | printf("Some Vim open-with registry entries could not be removed\n"); |
| 156 | else |
| 157 | printf("The Vim open-with registry entries have been removed\n"); |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | /* |
| 162 | * Check if a batch file is really for the current version. Don't delete a |
| 163 | * batch file that was written for another (possibly newer) version. |
| 164 | */ |
| 165 | static int |
| 166 | batfile_thisversion(char *path) |
| 167 | { |
| 168 | FILE *fd; |
| 169 | char line[BUFSIZE]; |
| 170 | char *p; |
| 171 | int ver_len = strlen(VIM_VERSION_NODOT); |
| 172 | int found = FALSE; |
| 173 | |
| 174 | fd = fopen(path, "r"); |
| 175 | if (fd != NULL) |
| 176 | { |
| 177 | while (fgets(line, BUFSIZE, fd) != NULL) |
| 178 | { |
| 179 | for (p = line; *p != 0; ++p) |
| 180 | /* don't accept "vim60an" when looking for "vim60". */ |
| 181 | if (strnicmp(p, VIM_VERSION_NODOT, ver_len) == 0 |
| 182 | && !isdigit(p[ver_len]) |
| 183 | && !isalpha(p[ver_len])) |
| 184 | { |
| 185 | found = TRUE; |
| 186 | break; |
| 187 | } |
| 188 | if (found) |
| 189 | break; |
| 190 | } |
| 191 | fclose(fd); |
| 192 | } |
| 193 | return found; |
| 194 | } |
| 195 | |
| 196 | static int |
| 197 | remove_batfiles(int doit) |
| 198 | { |
| 199 | char *batfile_path; |
| 200 | int i; |
| 201 | int found = 0; |
| 202 | |
| 203 | for (i = 1; i < TARGET_COUNT; ++i) |
| 204 | { |
| 205 | batfile_path = searchpath_save(targets[i].batname); |
| 206 | if (batfile_path != NULL && batfile_thisversion(batfile_path)) |
| 207 | { |
| 208 | ++found; |
| 209 | if (doit) |
| 210 | { |
| 211 | printf("removing %s\n", batfile_path); |
| 212 | remove(batfile_path); |
| 213 | } |
| 214 | else |
| 215 | printf(" - the batch file %s\n", batfile_path); |
| 216 | free(batfile_path); |
| 217 | } |
| 218 | } |
| 219 | return found; |
| 220 | } |
| 221 | |
| 222 | #ifdef WIN3264 |
| 223 | static void |
| 224 | remove_if_exists(char *path, char *filename) |
| 225 | { |
| 226 | char buf[BUFSIZE]; |
| 227 | FILE *fd; |
| 228 | |
| 229 | sprintf(buf, "%s\\%s", path, filename); |
| 230 | |
| 231 | fd = fopen(buf, "r"); |
| 232 | if (fd != NULL) |
| 233 | { |
| 234 | fclose(fd); |
| 235 | printf("removing %s\n", buf); |
| 236 | remove(buf); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | remove_icons(void) |
| 242 | { |
| 243 | char path[BUFSIZE]; |
| 244 | int i; |
| 245 | |
| 246 | if (get_shell_folder_path(path, "desktop")) |
| 247 | for (i = 0; i < ICON_COUNT; ++i) |
| 248 | remove_if_exists(path, icon_link_names[i]); |
| 249 | } |
| 250 | |
| 251 | static void |
| 252 | remove_start_menu(void) |
| 253 | { |
| 254 | char path[BUFSIZE]; |
| 255 | int i; |
| 256 | struct stat st; |
| 257 | |
| 258 | if (get_shell_folder_path(path, VIM_STARTMENU)) |
| 259 | { |
| 260 | for (i = 1; i < TARGET_COUNT; ++i) |
| 261 | remove_if_exists(path, targets[i].lnkname); |
| 262 | remove_if_exists(path, "uninstall.lnk"); |
| 263 | remove_if_exists(path, "Help.lnk"); |
| 264 | /* Win95 uses .pif, WinNT uses .lnk */ |
| 265 | remove_if_exists(path, "Vim tutor.pif"); |
| 266 | remove_if_exists(path, "Vim tutor.lnk"); |
| 267 | remove_if_exists(path, "Vim online.url"); |
| 268 | if (stat(path, &st) == 0) |
| 269 | { |
| 270 | printf("removing %s\n", path); |
| 271 | rmdir(path); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | #if 0 /* currently not used */ |
| 278 | /* |
| 279 | * Return TRUE when we're on Windows 95/98/ME. |
| 280 | */ |
| 281 | static int |
| 282 | win95(void) |
| 283 | { |
| 284 | static int done = FALSE; |
| 285 | static DWORD PlatformId; |
| 286 | |
| 287 | if (!done) |
| 288 | { |
| 289 | OSVERSIONINFO ovi; |
| 290 | |
| 291 | ovi.dwOSVersionInfoSize = sizeof(ovi); |
| 292 | GetVersionEx(&ovi); |
| 293 | PlatformId = ovi.dwPlatformId; |
| 294 | done = TRUE; |
| 295 | } |
| 296 | /* Win NT/2000/XP is VER_PLATFORM_WIN32_NT */ |
| 297 | return PlatformId == VER_PLATFORM_WIN32_WINDOWS; |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | static void |
| 302 | delete_uninstall_key(void) |
| 303 | { |
| 304 | #ifdef WIN3264 |
Bram Moolenaar | 3a0ae77 | 2010-08-01 21:15:45 +0200 | [diff] [blame] | 305 | RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Vim " VIM_VERSION_SHORT, |
| 306 | KEY_WOW64_64KEY, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | #else |
| 308 | FILE *fd; |
| 309 | char buf[BUFSIZE]; |
| 310 | |
| 311 | /* |
| 312 | * On DJGPP we delete registry entries by creating a .inf file and |
| 313 | * installing it. |
| 314 | */ |
| 315 | fd = fopen("vim.inf", "w"); |
| 316 | if (fd != NULL) |
| 317 | { |
| 318 | fprintf(fd, "[version]\n"); |
| 319 | fprintf(fd, "signature=\"$CHICAGO$\"\n\n"); |
| 320 | fprintf(fd, "[DefaultInstall]\n"); |
| 321 | fprintf(fd, "DelReg=DeleteMe\n\n"); |
| 322 | fprintf(fd, "[DeleteMe]\n"); |
| 323 | fprintf(fd, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Vim " VIM_VERSION_SHORT "\"\n"); |
| 324 | fclose(fd); |
| 325 | |
| 326 | /* Don't know how to detect Win NT with DJGPP. Hack: Just try the Win |
| 327 | * 95/98/ME method, since the DJGPP version can't use long filenames |
| 328 | * on Win NT anyway. */ |
| 329 | sprintf(buf, "rundll setupx.dll,InstallHinfSection DefaultInstall 132 %s\\vim.inf", installdir); |
| 330 | run_command(buf); |
| 331 | #if 0 |
| 332 | /* Windows NT method (untested). */ |
| 333 | sprintf(buf, "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 128 %s\\vim.inf", installdir); |
| 334 | run_command(buf); |
| 335 | #endif |
| 336 | |
| 337 | remove("vim.inf"); |
| 338 | } |
| 339 | #endif |
| 340 | } |
| 341 | |
| 342 | int |
| 343 | main(int argc, char *argv[]) |
| 344 | { |
| 345 | int found = 0; |
| 346 | FILE *fd; |
| 347 | #ifdef WIN3264 |
| 348 | int i; |
| 349 | struct stat st; |
| 350 | char icon[BUFSIZE]; |
| 351 | char path[BUFSIZE]; |
| 352 | char popup_path[BUFSIZE]; |
| 353 | |
| 354 | /* The nsis uninstaller calls us with a "-nsis" argument. */ |
| 355 | if (argc == 2 && stricmp(argv[1], "-nsis") == 0) |
| 356 | interactive = FALSE; |
| 357 | else |
| 358 | #endif |
| 359 | interactive = TRUE; |
| 360 | |
| 361 | /* Initialize this program. */ |
| 362 | do_inits(argv); |
| 363 | |
| 364 | printf("This program will remove the following items:\n"); |
| 365 | |
| 366 | #ifdef WIN3264 |
| 367 | if (popup_gvim_path(popup_path)) |
| 368 | { |
| 369 | printf(" - the \"Edit with Vim\" entry in the popup menu\n"); |
| 370 | printf(" which uses \"%s\"\n", popup_path); |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 371 | if (interactive) |
| 372 | printf("\nRemove it (y/n)? "); |
| 373 | if (!interactive || confirm()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | { |
| 375 | remove_popup(); |
| 376 | /* Assume the "Open With" entry can be removed as well, don't |
| 377 | * bother the user with asking him again. */ |
| 378 | remove_openwith(); |
| 379 | } |
| 380 | } |
| 381 | else if (openwith_gvim_path(popup_path)) |
| 382 | { |
| 383 | printf(" - the Vim \"Open With...\" entry in the popup menu\n"); |
| 384 | printf(" which uses \"%s\"\n", popup_path); |
| 385 | printf("\nRemove it (y/n)? "); |
| 386 | if (confirm()) |
| 387 | remove_openwith(); |
| 388 | } |
| 389 | |
| 390 | if (get_shell_folder_path(path, "desktop")) |
| 391 | { |
| 392 | printf("\n"); |
| 393 | for (i = 0; i < ICON_COUNT; ++i) |
| 394 | { |
| 395 | sprintf(icon, "%s\\%s", path, icon_link_names[i]); |
| 396 | if (stat(icon, &st) == 0) |
| 397 | { |
| 398 | printf(" - the \"%s\" icon on the desktop\n", icon_names[i]); |
| 399 | ++found; |
| 400 | } |
| 401 | } |
| 402 | if (found > 0) |
| 403 | { |
| 404 | if (interactive) |
| 405 | printf("\nRemove %s (y/n)? ", found > 1 ? "them" : "it"); |
| 406 | if (!interactive || confirm()) |
| 407 | remove_icons(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (get_shell_folder_path(path, VIM_STARTMENU) |
| 412 | && stat(path, &st) == 0) |
| 413 | { |
| 414 | printf("\n - the \"%s\" entry in the Start Menu\n", VIM_STARTMENU); |
| 415 | if (interactive) |
| 416 | printf("\nRemove it (y/n)? "); |
| 417 | if (!interactive || confirm()) |
| 418 | remove_start_menu(); |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | printf("\n"); |
| 423 | found = remove_batfiles(0); |
| 424 | if (found > 0) |
| 425 | { |
| 426 | if (interactive) |
| 427 | printf("\nRemove %s (y/n)? ", found > 1 ? "them" : "it"); |
| 428 | if (!interactive || confirm()) |
| 429 | remove_batfiles(1); |
| 430 | } |
| 431 | |
| 432 | fd = fopen("gvim.exe", "r"); |
| 433 | if (fd != NULL) |
| 434 | { |
| 435 | fclose(fd); |
| 436 | printf("gvim.exe detected. Attempting to unregister gvim with OLE\n"); |
| 437 | system("gvim.exe -silent -unregister"); |
| 438 | } |
| 439 | |
| 440 | delete_uninstall_key(); |
| 441 | |
| 442 | if (interactive) |
| 443 | { |
| 444 | printf("\nYou may now want to delete the Vim executables and runtime files.\n"); |
| 445 | printf("(They are still where you unpacked them.)\n"); |
| 446 | } |
| 447 | |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 448 | if (interactive) |
| 449 | { |
| 450 | rewind(stdin); |
| 451 | printf("\nPress Enter to exit..."); |
| 452 | (void)getchar(); |
| 453 | } |
| 454 | else |
Bram Moolenaar | ab8205e | 2010-07-07 15:14:03 +0200 | [diff] [blame] | 455 | sleep(3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |