apt.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include <apt-private/private-utils.h>
  50. /*}}}*/
  51. // EditSource - EditSourcesList /*{{{*/
  52. // ---------------------------------------------------------------------
  53. bool EditSources(CommandLine &CmdL)
  54. {
  55. // FIXME: suport CmdL.FileList to specify sources.list.d files
  56. std::string sourceslist = _config->FindFile("Dir::Etc::sourcelist");
  57. // FIXME: take hash before,
  58. // when changed display message to apt update
  59. bool res;
  60. pkgSourceList sl;
  61. do {
  62. EditFileInSensibleEditor(sourceslist);
  63. _error->PushToStack();
  64. res = sl.Read(sourceslist);
  65. if (!res) {
  66. std::string outs;
  67. strprintf(outs, _("Failed to parse %s. Edit again? "),
  68. sourceslist.c_str());
  69. std::cout << outs;
  70. res = !YnPrompt(true);
  71. }
  72. _error->RevertToStack();
  73. } while (res == false);
  74. return true;
  75. }
  76. /*}}}*/
  77. bool ShowHelp(CommandLine &CmdL)
  78. {
  79. ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  80. COMMON_ARCH,__DATE__,__TIME__);
  81. // FIXME: generate from CommandLine
  82. c1out <<
  83. _("Usage: apt [options] command\n"
  84. "\n"
  85. "CLI for apt.\n"
  86. "Commands: \n"
  87. " list - list packages based on package names\n"
  88. " search - search in package descriptions\n"
  89. " show - show package details\n"
  90. "\n"
  91. " update - update list of available packages\n"
  92. " install - install packages\n"
  93. " upgrade - upgrade the systems packages\n"
  94. "\n"
  95. " edit-sources - edit the source information file\n"
  96. );
  97. return true;
  98. }
  99. int main(int argc, const char *argv[]) /*{{{*/
  100. {
  101. CommandLine::Dispatch Cmds[] = {{"list",&List},
  102. {"search", &FullTextSearch},
  103. {"show", &APT::Cmd::ShowPackage},
  104. // needs root
  105. {"install",&DoInstall},
  106. {"remove", &DoInstall},
  107. {"update",&DoUpdate},
  108. {"upgrade",&DoUpgradeWithAllowNewPackages},
  109. // misc
  110. {"edit-sources",&EditSources},
  111. // helper
  112. {"moo",&DoMoo},
  113. {"help",&ShowHelp},
  114. {0,0}};
  115. std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
  116. if(!isatty(1))
  117. {
  118. std::cerr << std::endl
  119. << "WARNING WARNING "
  120. << argv[0]
  121. << " is *NOT* intended for scripts "
  122. << "use at your own peril^Wrisk"
  123. << std::endl
  124. << std::endl;
  125. }
  126. InitOutput();
  127. // Set up gettext support
  128. setlocale(LC_ALL,"");
  129. textdomain(PACKAGE);
  130. if(pkgInitConfig(*_config) == false)
  131. {
  132. _error->DumpErrors();
  133. return 100;
  134. }
  135. // FIXME: move into a new libprivate/private-install.cc:Install()
  136. _config->Set("DPkgPM::Progress", "1");
  137. _config->Set("Apt::Color", "1");
  138. // Parse the command line and initialize the package library
  139. CommandLine CmdL(Args.data(), _config);
  140. if (CmdL.Parse(argc, argv) == false ||
  141. pkgInitSystem(*_config, _system) == false)
  142. {
  143. _error->DumpErrors();
  144. return 100;
  145. }
  146. // See if the help should be shown
  147. if (_config->FindB("help") == true ||
  148. _config->FindB("version") == true ||
  149. CmdL.FileSize() == 0)
  150. {
  151. ShowHelp(CmdL);
  152. return 0;
  153. }
  154. // see if we are in simulate mode
  155. CheckSimulateMode(CmdL);
  156. // parse args
  157. CmdL.DispatchArg(Cmds);
  158. // Print any errors or warnings found during parsing
  159. bool const Errors = _error->PendingError();
  160. if (_config->FindI("quiet",0) > 0)
  161. _error->DumpErrors();
  162. else
  163. _error->DumpErrors(GlobalError::DEBUG);
  164. return Errors == true ? 100 : 0;
  165. }
  166. /*}}}*/