1 /* 2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "pkgman.h" 8 9 #include <errno.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include <package/manager/Exceptions.h> 15 16 #include "Command.h" 17 18 19 using namespace BPackageKit::BManager::BPrivate; 20 21 22 extern const char* __progname; 23 const char* kProgramName = __progname; 24 25 26 const BString kCommandCategoryPackages("packages"); 27 const BString kCommandCategoryRepositories("repositories"); 28 const BString kCommandCategoryOther("other"); 29 30 31 static const char* const kUsage = 32 "Usage: %s <command> <command args>\n" 33 "Manages packages and package repositories.\n" 34 "\n" 35 "Package management commands:\n" 36 "%s" 37 "Repository management commands:\n" 38 "%s" 39 "Other commands:\n" 40 "%s" 41 "Common options:\n" 42 " -h, --help - Print usage info for a specific command.\n" 43 ; 44 45 46 static BString 47 get_commands_usage_for_category(const char* category) 48 { 49 BString commandsUsage; 50 CommandList commands; 51 CommandManager::Default()->GetCommandsForCategory(category, commands); 52 for (int32 i = 0; Command* command = commands.ItemAt(i); i++) 53 commandsUsage << command->ShortUsage() << '\n'; 54 return commandsUsage; 55 } 56 57 58 void 59 print_usage_and_exit(bool error) 60 { 61 BString packageCommandsUsage 62 = get_commands_usage_for_category(kCommandCategoryPackages); 63 BString repositoryCommandsUsage 64 = get_commands_usage_for_category(kCommandCategoryRepositories); 65 BString otherCommandsUsage 66 = get_commands_usage_for_category(kCommandCategoryOther); 67 68 fprintf(error ? stderr : stdout, kUsage, kProgramName, 69 packageCommandsUsage.String(), repositoryCommandsUsage.String(), 70 otherCommandsUsage.String()); 71 72 exit(error ? 1 : 0); 73 } 74 75 76 int 77 main(int argc, const char* const* argv) 78 { 79 CommandManager::Default()->InitCommands(kProgramName); 80 81 if (argc < 2) 82 print_usage_and_exit(true); 83 84 const char* command = argv[1]; 85 if (strcmp(command, "help") == 0) 86 print_usage_and_exit(false); 87 88 CommandList commands; 89 CommandManager::Default()->GetCommands(command, commands); 90 if (commands.CountItems() != 1) 91 print_usage_and_exit(true); 92 93 try { 94 return commands.ItemAt(0)->Execute(argc - 1, argv + 1); 95 } catch (BNothingToDoException&) { 96 fprintf(stderr, "Nothing to do.\n"); 97 return 0; 98 } catch (std::bad_alloc&) { 99 fprintf(stderr, "Out of memory!\n"); 100 return 1; 101 } catch (BFatalErrorException& exception) { 102 if (!exception.Details().IsEmpty()) 103 fprintf(stderr, "%s", exception.Details().String()); 104 if (exception.Error() == B_OK) { 105 fprintf(stderr, "*** %s\n", exception.Message().String()); 106 } else { 107 fprintf(stderr, "*** %s: %s\n", exception.Message().String(), 108 strerror(exception.Error())); 109 } 110 return 1; 111 } catch (BAbortedByUserException&) { 112 return 0; 113 } 114 } 115