apt.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 &)
  33. {
  34. ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  35. COMMON_ARCH,__DATE__,__TIME__);
  36. // FIXME: generate from CommandLine
  37. c1out <<
  38. _("Usage: apt [options] command\n"
  39. "\n"
  40. "CLI for apt.\n"
  41. "Basic commands: \n"
  42. " list - list packages based on package names\n"
  43. " search - search in package descriptions\n"
  44. " show - show package details\n"
  45. "\n"
  46. " update - update list of available packages\n"
  47. "\n"
  48. " install - install packages\n"
  49. " remove - remove packages\n"
  50. "\n"
  51. " upgrade - upgrade the system by installing/upgrading packages\n"
  52. " full-upgrade - upgrade the system by removing/installing/upgrading packages\n"
  53. "\n"
  54. " edit-sources - edit the source information file\n"
  55. );
  56. return true;
  57. }
  58. int main(int argc, const char *argv[]) /*{{{*/
  59. {
  60. CommandLine::Dispatch Cmds[] = {
  61. // query
  62. {"list",&List},
  63. {"search", &FullTextSearch},
  64. {"show", &APT::Cmd::ShowPackage},
  65. // package stuff
  66. {"install",&DoInstall},
  67. {"remove", &DoInstall},
  68. {"purge", &DoInstall},
  69. // system wide stuff
  70. {"update",&DoUpdate},
  71. {"upgrade",&DoUpgrade},
  72. {"full-upgrade",&DoDistUpgrade},
  73. // for compat with muscle memory
  74. {"dist-upgrade",&DoDistUpgrade},
  75. // misc
  76. {"edit-sources",&EditSources},
  77. // helper
  78. {"moo",&DoMoo},
  79. {"help",&ShowHelp},
  80. {0,0}};
  81. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  82. InitOutput();
  83. // Set up gettext support
  84. setlocale(LC_ALL,"");
  85. textdomain(PACKAGE);
  86. if(pkgInitConfig(*_config) == false)
  87. {
  88. _error->DumpErrors();
  89. return 100;
  90. }
  91. // some different defaults
  92. _config->CndSet("DPkg::Progress-Fancy", "1");
  93. _config->CndSet("Apt::Color", "1");
  94. _config->CndSet("APT::Get::Upgrade-Allow-New", true);
  95. // Parse the command line and initialize the package library
  96. CommandLine CmdL(Args.data(), _config);
  97. if (CmdL.Parse(argc, argv) == false ||
  98. pkgInitSystem(*_config, _system) == false)
  99. {
  100. _error->DumpErrors();
  101. return 100;
  102. }
  103. if(!isatty(STDOUT_FILENO) &&
  104. _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
  105. {
  106. std::cerr << std::endl
  107. << "WARNING: " << argv[0] << " "
  108. << "does not have a stable CLI interface yet. "
  109. << "Use with caution in scripts."
  110. << std::endl
  111. << std::endl;
  112. }
  113. // See if the help should be shown
  114. if (_config->FindB("help") == true ||
  115. _config->FindB("version") == true ||
  116. CmdL.FileSize() == 0)
  117. {
  118. ShowHelp(CmdL);
  119. return 0;
  120. }
  121. // see if we are in simulate mode
  122. CheckSimulateMode(CmdL);
  123. // parse args
  124. CmdL.DispatchArg(Cmds);
  125. // Print any errors or warnings found during parsing
  126. bool const Errors = _error->PendingError();
  127. if (_config->FindI("quiet",0) > 0)
  128. _error->DumpErrors();
  129. else
  130. _error->DumpErrors(GlobalError::DEBUG);
  131. return Errors == true ? 100 : 0;
  132. }
  133. /*}}}*/