apt.cc 5.2 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 systems packages\n"
  73. "\n"
  74. " edit-sources - edit the source information file\n"
  75. );
  76. return true;
  77. }
  78. // figure out what kind of upgrade the user wants
  79. bool DoAptUpgrade(CommandLine &CmdL)
  80. {
  81. if (_config->FindB("Apt::Cmd::Dist-Upgrade"))
  82. return DoDistUpgrade(CmdL);
  83. else
  84. return DoUpgradeWithAllowNewPackages(CmdL);
  85. }
  86. int main(int argc, const char *argv[]) /*{{{*/
  87. {
  88. CommandLine::Dispatch Cmds[] = {{"list",&List},
  89. {"search", &FullTextSearch},
  90. {"show", &APT::Cmd::ShowPackage},
  91. // package stuff
  92. {"install",&DoInstall},
  93. {"remove", &DoInstall},
  94. {"purge", &DoInstall},
  95. // system wide stuff
  96. {"update",&DoUpdate},
  97. {"upgrade",&DoAptUpgrade},
  98. // misc
  99. {"edit-sources",&EditSources},
  100. // helper
  101. {"moo",&DoMoo},
  102. {"help",&ShowHelp},
  103. {0,0}};
  104. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  105. InitOutput();
  106. // Set up gettext support
  107. setlocale(LC_ALL,"");
  108. textdomain(PACKAGE);
  109. if(pkgInitConfig(*_config) == false)
  110. {
  111. _error->DumpErrors();
  112. return 100;
  113. }
  114. // FIXME: move into a new libprivate/private-install.cc:Install()
  115. _config->Set("DPkgPM::Progress", "1");
  116. _config->Set("Apt::Color", "1");
  117. // Parse the command line and initialize the package library
  118. CommandLine CmdL(Args.data(), _config);
  119. if (CmdL.Parse(argc, argv) == false ||
  120. pkgInitSystem(*_config, _system) == false)
  121. {
  122. _error->DumpErrors();
  123. return 100;
  124. }
  125. if(!isatty(STDOUT_FILENO) &&
  126. _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
  127. {
  128. std::cerr << std::endl
  129. << "WARNING: " << argv[0] << " "
  130. << "does not have a stable CLI interface yet. "
  131. << "Use with caution in scripts."
  132. << std::endl
  133. << std::endl;
  134. }
  135. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  136. _config->Set("quiet","1");
  137. // See if the help should be shown
  138. if (_config->FindB("help") == true ||
  139. _config->FindB("version") == true ||
  140. CmdL.FileSize() == 0)
  141. {
  142. ShowHelp(CmdL);
  143. return 0;
  144. }
  145. // see if we are in simulate mode
  146. CheckSimulateMode(CmdL);
  147. // parse args
  148. CmdL.DispatchArg(Cmds);
  149. // Print any errors or warnings found during parsing
  150. bool const Errors = _error->PendingError();
  151. if (_config->FindI("quiet",0) > 0)
  152. _error->DumpErrors();
  153. else
  154. _error->DumpErrors(GlobalError::DEBUG);
  155. return Errors == true ? 100 : 0;
  156. }
  157. /*}}}*/