1 /* 2 * Copyright 2013, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold <ingo_weinhold@gmx.de> 7 */ 8 9 10 #include <getopt.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 #include "Command.h" 15 #include "pkgman.h" 16 #include "PackageManager.h" 17 18 19 // TODO: internationalization! 20 21 22 using namespace BPackageKit; 23 using namespace BPackageKit::BPrivate; 24 25 26 static const char* const kShortUsage = 27 " %command% <package> ...\n" 28 " Uninstalls one or more packages.\n"; 29 30 static const char* const kLongUsage = 31 "Usage: %program% %command% <package> ...\n" 32 "Uninstalls the specified packages.\n" 33 "\n" 34 "Options:\n" 35 " -H, --home\n" 36 " Uninstall the packages from the user's home directory. Default is to\n" 37 " uninstall from the system directory.\n" 38 "\n"; 39 40 41 DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage, 42 kCommandCategoryPackages) 43 44 45 int 46 UninstallCommand::Execute(int argc, const char* const* argv) 47 { 48 BPackageInstallationLocation location 49 = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM; 50 51 while (true) { 52 static struct option sLongOptions[] = { 53 { "help", no_argument, 0, 'h' }, 54 { "home", no_argument, 0, 'H' }, 55 { 0, 0, 0, 0 } 56 }; 57 58 opterr = 0; // don't print errors 59 int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL); 60 if (c == -1) 61 break; 62 63 switch (c) { 64 case 'h': 65 PrintUsageAndExit(false); 66 break; 67 68 case 'H': 69 location = B_PACKAGE_INSTALLATION_LOCATION_HOME; 70 break; 71 72 default: 73 PrintUsageAndExit(true); 74 break; 75 } 76 } 77 78 // The remaining arguments are the packages to be uninstalled. 79 if (argc < optind + 1) 80 PrintUsageAndExit(true); 81 82 int packageCount = argc - optind; 83 const char* const* packages = argv + optind; 84 85 // perform the installation 86 PackageManager packageManager(location); 87 packageManager.Uninstall(packages, packageCount); 88 89 return 0; 90 } 91