xref: /haiku/src/bin/pkgman/command_uninstall.cpp (revision 372a66634410cf0450e426716c14ad42d40c0da4)
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 common directory.\n"
38 	"  -S, --system\n"
39 	"    Uninstall the packages from the system directory. Default is to\n"
40 	"    uninstall from the common directory.\n"
41 	"\n";
42 
43 
44 DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage,
45 	kCommandCategoryPackages)
46 
47 
48 int
49 UninstallCommand::Execute(int argc, const char* const* argv)
50 {
51 	BPackageInstallationLocation location
52 		= B_PACKAGE_INSTALLATION_LOCATION_COMMON;
53 
54 	while (true) {
55 		static struct option sLongOptions[] = {
56 			{ "help", no_argument, 0, 'h' },
57 			{ "home", no_argument, 0, 'H' },
58 			{ "system", no_argument, 0, 'S' },
59 			{ 0, 0, 0, 0 }
60 		};
61 
62 		opterr = 0; // don't print errors
63 		int c = getopt_long(argc, (char**)argv, "hHS", sLongOptions, NULL);
64 		if (c == -1)
65 			break;
66 
67 		switch (c) {
68 			case 'h':
69 				PrintUsageAndExit(false);
70 				break;
71 
72 			case 'H':
73 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
74 				break;
75 
76 			case 'S':
77 				location = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
78 				break;
79 
80 			default:
81 				PrintUsageAndExit(true);
82 				break;
83 		}
84 	}
85 
86 	// The remaining arguments are the packages to be uninstalled.
87 	if (argc < optind + 1)
88 		PrintUsageAndExit(true);
89 
90 	int packageCount = argc - optind;
91 	const char* const* packages = argv + optind;
92 
93 	// perform the installation
94 	PackageManager packageManager(location);
95 	packageManager.Uninstall(packages, packageCount);
96 
97 	return 0;
98 }
99