apt.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <apti18n.h>
  39. #include <apt-private/private-list.h>
  40. #include <apt-private/private-search.h>
  41. #include <apt-private/private-install.h>
  42. #include <apt-private/private-output.h>
  43. #include <apt-private/private-update.h>
  44. #include <apt-private/private-cmndline.h>
  45. #include <apt-private/private-moo.h>
  46. #include <apt-private/private-upgrade.h>
  47. #include <apt-private/private-show.h>
  48. #include <apt-private/private-main.h>
  49. /*}}}*/
  50. bool ShowHelp(CommandLine &CmdL)
  51. {
  52. ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  53. COMMON_ARCH,__DATE__,__TIME__);
  54. // FIXME: generate from CommandLine
  55. c1out <<
  56. _("Usage: apt [options] command\n"
  57. "\n"
  58. "CLI for apt.\n"
  59. "Commands: \n"
  60. " list - list packages based on package names\n"
  61. " search - search in package descriptions\n"
  62. " show - show package details\n"
  63. "\n"
  64. " update - update list of available packages\n"
  65. " install - install packages\n"
  66. " upgrade - upgrade the systems packages\n"
  67. );
  68. return true;
  69. }
  70. int main(int argc, const char *argv[]) /*{{{*/
  71. {
  72. CommandLine::Dispatch Cmds[] = {{"list",&List},
  73. {"search", &FullTextSearch},
  74. {"show", &APT::Cmd::ShowPackage},
  75. // needs root
  76. {"install",&DoInstall},
  77. {"remove", &DoInstall},
  78. {"update",&DoUpdate},
  79. {"upgrade",&DoUpgradeWithAllowNewPackages},
  80. // helper
  81. {"moo",&DoMoo},
  82. {"help",&ShowHelp},
  83. {0,0}};
  84. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  85. if(!isatty(1))
  86. {
  87. std::cerr << std::endl
  88. << "WARNING WARNING "
  89. << argv[0]
  90. << " is *NOT* intended for scripts "
  91. << "use at your own peril^Wrisk"
  92. << std::endl
  93. << std::endl;
  94. }
  95. InitOutput();
  96. // Set up gettext support
  97. setlocale(LC_ALL,"");
  98. textdomain(PACKAGE);
  99. if(pkgInitConfig(*_config) == false)
  100. {
  101. _error->DumpErrors();
  102. return 100;
  103. }
  104. // FIXME: move into a new libprivate/private-install.cc:Install()
  105. _config->Set("DPkgPM::Progress-Fancy", "1");
  106. _config->Set("Apt::Color", "1");
  107. // Parse the command line and initialize the package library
  108. CommandLine CmdL(Args.data(), _config);
  109. if (CmdL.Parse(argc, argv) == false ||
  110. pkgInitSystem(*_config, _system) == false)
  111. {
  112. _error->DumpErrors();
  113. return 100;
  114. }
  115. // See if the help should be shown
  116. if (_config->FindB("help") == true ||
  117. _config->FindB("version") == true ||
  118. CmdL.FileSize() == 0)
  119. {
  120. ShowHelp(CmdL);
  121. return 0;
  122. }
  123. // see if we are in simulate mode
  124. CheckSimulateMode(CmdL);
  125. // parse args
  126. CmdL.DispatchArg(Cmds);
  127. // Print any errors or warnings found during parsing
  128. bool const Errors = _error->PendingError();
  129. if (_config->FindI("quiet",0) > 0)
  130. _error->DumpErrors();
  131. else
  132. _error->DumpErrors(GlobalError::DEBUG);
  133. return Errors == true ? 100 : 0;
  134. }
  135. /*}}}*/