diff --git a/src/bin/pkgman/DecisionProvider.cpp b/src/bin/pkgman/DecisionProvider.cpp index 1c5e2d42ab..7f1387bfbb 100644 --- a/src/bin/pkgman/DecisionProvider.cpp +++ b/src/bin/pkgman/DecisionProvider.cpp @@ -10,6 +10,13 @@ #include "DecisionProvider.h" +DecisionProvider::DecisionProvider(bool interactive) + : + fInteractive(interactive) +{ +} + + bool DecisionProvider::YesNoDecisionNeeded(const BString& description, const BString& question, const BString& yes, const BString& no, @@ -25,6 +32,11 @@ DecisionProvider::YesNoDecisionNeeded(const BString& description, haveDefault ? (BString(" (") << defaultChoice << ") ").String() : ""); + if (!fInteractive) { + printf("%s\n", yes.String()); + return true; + } + char buffer[32]; if (fgets(buffer, 32, stdin)) { if (haveDefault && (buffer[0] == '\n' || buffer[0] == '\0')) diff --git a/src/bin/pkgman/DecisionProvider.h b/src/bin/pkgman/DecisionProvider.h index 74de90f592..7eeee0cf36 100644 --- a/src/bin/pkgman/DecisionProvider.h +++ b/src/bin/pkgman/DecisionProvider.h @@ -9,10 +9,20 @@ #include -struct DecisionProvider : public BPackageKit::BDecisionProvider { - virtual bool YesNoDecisionNeeded(const BString& description, - const BString& question, const BString& yes, const BString& no, - const BString& defaultChoice); +class DecisionProvider : public BPackageKit::BDecisionProvider { +public: + DecisionProvider(bool interactive = true); + + void SetInteractive(bool interactive) + { fInteractive = interactive; } + + virtual bool YesNoDecisionNeeded(const BString& description, + const BString& question, const BString& yes, + const BString& no, + const BString& defaultChoice); + +private: + bool fInteractive; }; diff --git a/src/bin/pkgman/PackageManager.cpp b/src/bin/pkgman/PackageManager.cpp index a3ac077fe3..38565a78c3 100644 --- a/src/bin/pkgman/PackageManager.cpp +++ b/src/bin/pkgman/PackageManager.cpp @@ -22,13 +22,15 @@ using namespace BPackageKit::BPrivate; -PackageManager::PackageManager(BPackageInstallationLocation location) +PackageManager::PackageManager(BPackageInstallationLocation location, + bool interactive) : BPackageManager(location, &fClientInstallationInterface, this), BPackageManager::UserInteractionHandler(), - fDecisionProvider(), + fDecisionProvider(interactive), fClientInstallationInterface(), - fPreviousDownloadPercentage(0) + fPreviousDownloadPercentage(0), + fInteractive(interactive) { } @@ -38,6 +40,14 @@ PackageManager::~PackageManager() } +void +PackageManager::SetInteractive(bool interactive) +{ + fInteractive = interactive; + fDecisionProvider.SetInteractive(interactive); +} + + void PackageManager::JobFailed(BJob* job) { @@ -80,6 +90,9 @@ PackageManager::HandleProblems() } } + if (!fInteractive) + continue; + // let the user choose a solution printf("Please select a solution, skip the problem for now or quit.\n"); for (;;) { @@ -112,6 +125,9 @@ PackageManager::HandleProblems() break; } } + + if (problemCount > 0 && !fInteractive) + exit(1); } diff --git a/src/bin/pkgman/PackageManager.h b/src/bin/pkgman/PackageManager.h index 0162046d19..e56b27dbbe 100644 --- a/src/bin/pkgman/PackageManager.h +++ b/src/bin/pkgman/PackageManager.h @@ -25,9 +25,12 @@ class PackageManager : public BPackageManager, private BPackageManager::UserInteractionHandler { public: PackageManager( - BPackageInstallationLocation location); + BPackageInstallationLocation location, + bool interactive = true); ~PackageManager(); + void SetInteractive(bool interactive); + virtual void JobFailed(BJob* job); virtual void JobAborted(BJob* job); @@ -68,6 +71,7 @@ private: BPackageManager::ClientInstallationInterface fClientInstallationInterface; int32 fPreviousDownloadPercentage; + bool fInteractive; }; diff --git a/src/bin/pkgman/command_install.cpp b/src/bin/pkgman/command_install.cpp index e7b55db10a..7e4ba58a6f 100644 --- a/src/bin/pkgman/command_install.cpp +++ b/src/bin/pkgman/command_install.cpp @@ -35,6 +35,9 @@ static const char* const kLongUsage = " -H, --home\n" " Install the packages in the user's home directory. Default is to\n" " install in the system directory.\n" + " -y\n" + " Non-interactive mode. Automatically confirm changes, but fail when\n" + " encountering problems.\n" "\n"; @@ -47,6 +50,7 @@ InstallCommand::Execute(int argc, const char* const* argv) { BPackageInstallationLocation location = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM; + bool interactive = true; while (true) { static struct option sLongOptions[] = { @@ -56,7 +60,7 @@ InstallCommand::Execute(int argc, const char* const* argv) }; opterr = 0; // don't print errors - int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL); + int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL); if (c == -1) break; @@ -69,6 +73,10 @@ InstallCommand::Execute(int argc, const char* const* argv) location = B_PACKAGE_INSTALLATION_LOCATION_HOME; break; + case 'y': + interactive = false; + break; + default: PrintUsageAndExit(true); break; @@ -83,7 +91,7 @@ InstallCommand::Execute(int argc, const char* const* argv) const char* const* packages = argv + optind; // perform the installation - PackageManager packageManager(location); + PackageManager packageManager(location, interactive); packageManager.Install(packages, packageCount); return 0; diff --git a/src/bin/pkgman/command_uninstall.cpp b/src/bin/pkgman/command_uninstall.cpp index be0a42e21f..9c9e422841 100644 --- a/src/bin/pkgman/command_uninstall.cpp +++ b/src/bin/pkgman/command_uninstall.cpp @@ -35,6 +35,9 @@ static const char* const kLongUsage = " -H, --home\n" " Uninstall the packages from the user's home directory. Default is to\n" " uninstall from the system directory.\n" + " -y\n" + " Non-interactive mode. Automatically confirm changes, but fail when\n" + " encountering problems.\n" "\n"; @@ -47,6 +50,7 @@ UninstallCommand::Execute(int argc, const char* const* argv) { BPackageInstallationLocation location = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM; + bool interactive = true; while (true) { static struct option sLongOptions[] = { @@ -56,7 +60,7 @@ UninstallCommand::Execute(int argc, const char* const* argv) }; opterr = 0; // don't print errors - int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL); + int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL); if (c == -1) break; @@ -69,6 +73,10 @@ UninstallCommand::Execute(int argc, const char* const* argv) location = B_PACKAGE_INSTALLATION_LOCATION_HOME; break; + case 'y': + interactive = false; + break; + default: PrintUsageAndExit(true); break; @@ -83,7 +91,7 @@ UninstallCommand::Execute(int argc, const char* const* argv) const char* const* packages = argv + optind; // perform the installation - PackageManager packageManager(location); + PackageManager packageManager(location, interactive); packageManager.Uninstall(packages, packageCount); return 0; diff --git a/src/bin/pkgman/command_update.cpp b/src/bin/pkgman/command_update.cpp index 81802f1c10..62f3641b0d 100644 --- a/src/bin/pkgman/command_update.cpp +++ b/src/bin/pkgman/command_update.cpp @@ -36,6 +36,9 @@ static const char* const kLongUsage = " -H, --home\n" " Update the packages in the user's home directory. Default is to\n" " update in the system directory.\n" + " -y\n" + " Non-interactive mode. Automatically confirm changes, but fail when\n" + " encountering problems.\n" "\n"; @@ -48,6 +51,7 @@ UpdateCommand::Execute(int argc, const char* const* argv) { BPackageInstallationLocation location = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM; + bool interactive = true; while (true) { static struct option sLongOptions[] = { @@ -57,7 +61,7 @@ UpdateCommand::Execute(int argc, const char* const* argv) }; opterr = 0; // don't print errors - int c = getopt_long(argc, (char**)argv, "hH", sLongOptions, NULL); + int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL); if (c == -1) break; @@ -70,6 +74,10 @@ UpdateCommand::Execute(int argc, const char* const* argv) location = B_PACKAGE_INSTALLATION_LOCATION_HOME; break; + case 'y': + interactive = false; + break; + default: PrintUsageAndExit(true); break; @@ -81,7 +89,7 @@ UpdateCommand::Execute(int argc, const char* const* argv) const char* const* packages = argv + optind; // perform the update - PackageManager packageManager(location); + PackageManager packageManager(location, interactive); packageManager.Update(packages, packageCount); return 0;