apt.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <cassert>
  11. #include <locale.h>
  12. #include <iostream>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <regex.h>
  16. #include <stdio.h>
  17. #include <iomanip>
  18. #include <algorithm>
  19. #include <apt-pkg/error.h>
  20. #include <apt-pkg/cachefile.h>
  21. #include <apt-pkg/cacheset.h>
  22. #include <apt-pkg/init.h>
  23. #include <apt-pkg/progress.h>
  24. #include <apt-pkg/sourcelist.h>
  25. #include <apt-pkg/cmndline.h>
  26. #include <apt-pkg/strutl.h>
  27. #include <apt-pkg/fileutl.h>
  28. #include <apt-pkg/pkgrecords.h>
  29. #include <apt-pkg/srcrecords.h>
  30. #include <apt-pkg/version.h>
  31. #include <apt-pkg/policy.h>
  32. #include <apt-pkg/tagfile.h>
  33. #include <apt-pkg/algorithms.h>
  34. #include <apt-pkg/sptr.h>
  35. #include <apt-pkg/pkgsystem.h>
  36. #include <apt-pkg/indexfile.h>
  37. #include <apt-pkg/metaindex.h>
  38. #include <apt-pkg/hashes.h>
  39. #include <apti18n.h>
  40. #include <apt-private/private-list.h>
  41. #include <apt-private/private-search.h>
  42. #include <apt-private/private-install.h>
  43. #include <apt-private/private-output.h>
  44. #include <apt-private/private-update.h>
  45. #include <apt-private/private-cmndline.h>
  46. #include <apt-private/private-moo.h>
  47. #include <apt-private/private-upgrade.h>
  48. #include <apt-private/private-show.h>
  49. #include <apt-private/private-main.h>
  50. #include <apt-private/private-utils.h>
  51. #include <apt-private/private-sources.h>
  52. /*}}}*/
  53. bool ShowHelp(CommandLine &CmdL)
  54. {
  55. ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  56. COMMON_ARCH,__DATE__,__TIME__);
  57. // FIXME: generate from CommandLine
  58. c1out <<
  59. _("Usage: apt [options] command\n"
  60. "\n"
  61. "CLI for apt.\n"
  62. "Basic commands: \n"
  63. " list - list packages based on package names\n"
  64. " search - search in package descriptions\n"
  65. " show - show package details\n"
  66. "\n"
  67. " update - update list of available packages\n"
  68. "\n"
  69. " install - install packages\n"
  70. " remove - remove packages\n"
  71. "\n"
  72. " upgrade - upgrade the system by installing/upgrading packages\n"
  73. " full-upgrade - upgrade the system by removing/installing/upgrading packages\n"
  74. "\n"
  75. " edit-sources - edit the source information file\n"
  76. );
  77. return true;
  78. }
  79. int main(int argc, const char *argv[]) /*{{{*/
  80. {
  81. CommandLine::Dispatch Cmds[] = {
  82. // query
  83. {"list",&List},
  84. {"search", &FullTextSearch},
  85. {"show", &APT::Cmd::ShowPackage},
  86. // package stuff
  87. {"install",&DoInstall},
  88. {"remove", &DoInstall},
  89. {"purge", &DoInstall},
  90. // system wide stuff
  91. {"update",&DoUpdate},
  92. {"upgrade",&DoUpgrade},
  93. {"full-upgrade",&DoDistUpgrade},
  94. // for compat with muscle memory
  95. {"dist-upgrade",&DoDistUpgrade},
  96. // misc
  97. {"edit-sources",&EditSources},
  98. // helper
  99. {"moo",&DoMoo},
  100. {"help",&ShowHelp},
  101. {0,0}};
  102. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  103. InitOutput();
  104. // Set up gettext support
  105. setlocale(LC_ALL,"");
  106. textdomain(PACKAGE);
  107. if(pkgInitConfig(*_config) == false)
  108. {
  109. _error->DumpErrors();
  110. return 100;
  111. }
  112. // some different defaults
  113. _config->CndSet("DPkgPM::Progress", "1");
  114. _config->CndSet("Apt::Color", "1");
  115. _config->CndSet("APT::Get::Upgrade-Allow-New", true);
  116. // Parse the command line and initialize the package library
  117. CommandLine CmdL(Args.data(), _config);
  118. if (CmdL.Parse(argc, argv) == false ||
  119. pkgInitSystem(*_config, _system) == false)
  120. {
  121. _error->DumpErrors();
  122. return 100;
  123. }
  124. if(!isatty(STDOUT_FILENO) &&
  125. _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
  126. {
  127. std::cerr << std::endl
  128. << "WARNING: " << argv[0] << " "
  129. << "does not have a stable CLI interface yet. "
  130. << "Use with caution in scripts."
  131. << std::endl
  132. << std::endl;
  133. }
  134. // See if the help should be shown
  135. if (_config->FindB("help") == true ||
  136. _config->FindB("version") == true ||
  137. CmdL.FileSize() == 0)
  138. {
  139. ShowHelp(CmdL);
  140. return 0;
  141. }
  142. // see if we are in simulate mode
  143. CheckSimulateMode(CmdL);
  144. // parse args
  145. CmdL.DispatchArg(Cmds);
  146. // Print any errors or warnings found during parsing
  147. bool const Errors = _error->PendingError();
  148. if (_config->FindI("quiet",0) > 0)
  149. _error->DumpErrors();
  150. else
  151. _error->DumpErrors(GlobalError::DEBUG);
  152. return Errors == true ? 100 : 0;
  153. }
  154. /*}}}*/