apt.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. apt - CLI UI for apt
  5. Returns 100 on failure, 0 on success.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include<config.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/init.h>
  13. #include <apt-pkg/pkgsystem.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-private/private-list.h>
  17. #include <apt-private/private-search.h>
  18. #include <apt-private/private-install.h>
  19. #include <apt-private/private-output.h>
  20. #include <apt-private/private-update.h>
  21. #include <apt-private/private-cmndline.h>
  22. #include <apt-private/private-moo.h>
  23. #include <apt-private/private-upgrade.h>
  24. #include <apt-private/private-show.h>
  25. #include <apt-private/private-main.h>
  26. #include <apt-private/private-sources.h>
  27. #include <unistd.h>
  28. #include <iostream>
  29. #include <vector>
  30. #include <apti18n.h>
  31. /*}}}*/
  32. static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
  33. {
  34. ioprintf(c1out, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  35. // FIXME: generate from CommandLine
  36. c1out <<
  37. _("Usage: apt [options] command\n"
  38. "\n"
  39. "CLI for apt.\n")
  40. << std::endl
  41. << _("Commands:") << std::endl;
  42. for (; Cmds->Handler != nullptr; ++Cmds)
  43. {
  44. if (Cmds->Help == nullptr)
  45. continue;
  46. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  47. }
  48. return true;
  49. }
  50. int main(int argc, const char *argv[]) /*{{{*/
  51. {
  52. CommandLine::DispatchWithHelp Cmds[] = {
  53. // query
  54. {"list", &DoList, _("list packages based on package names")},
  55. {"search", &DoSearch, _("search in package descriptions")},
  56. {"show", &ShowPackage, _("show package details")},
  57. // package stuff
  58. {"install", &DoInstall, _("install packages")},
  59. {"remove", &DoInstall, _("remove packages")},
  60. {"autoremove", &DoInstall, _("Remove automatically all unused packages")},
  61. {"auto-remove", &DoInstall, nullptr},
  62. {"purge", &DoInstall, nullptr},
  63. // system wide stuff
  64. {"update", &DoUpdate, _("update list of available packages")},
  65. {"upgrade", &DoUpgrade, _("upgrade the system by installing/upgrading packages")},
  66. {"full-upgrade", &DoDistUpgrade, _("upgrade the system by removing/installing/upgrading packages")},
  67. {"dist-upgrade", &DoDistUpgrade, nullptr}, // for compat with muscle memory
  68. // misc
  69. {"edit-sources", &EditSources, _("edit the source information file")},
  70. {"moo", &DoMoo, nullptr},
  71. {nullptr, nullptr, nullptr}
  72. };
  73. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  74. // Init the signals
  75. InitSignals();
  76. // Init the output
  77. InitOutput();
  78. // Set up gettext support
  79. setlocale(LC_ALL,"");
  80. textdomain(PACKAGE);
  81. if(pkgInitConfig(*_config) == false)
  82. {
  83. _error->DumpErrors();
  84. return 100;
  85. }
  86. // Parse the command line and initialize the package library
  87. CommandLine CmdL;
  88. ParseCommandLine(CmdL, Cmds, Args.data(), NULL, &_system, argc, argv, ShowHelp);
  89. if(!isatty(STDOUT_FILENO) &&
  90. _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
  91. {
  92. std::cerr << std::endl
  93. << "WARNING: " << argv[0] << " "
  94. << "does not have a stable CLI interface yet. "
  95. << "Use with caution in scripts."
  96. << std::endl
  97. << std::endl;
  98. }
  99. // see if we are in simulate mode
  100. CheckSimulateMode(CmdL);
  101. // parse args
  102. CmdL.DispatchArg(Cmds);
  103. // Print any errors or warnings found during parsing
  104. bool const Errors = _error->PendingError();
  105. if (_config->FindI("quiet",0) > 0)
  106. _error->DumpErrors();
  107. else
  108. _error->DumpErrors(GlobalError::DEBUG);
  109. return Errors == true ? 100 : 0;
  110. }
  111. /*}}}*/