apt.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // do syntax check after save (like visudo)
  60. EditFileInSensibleEditor(sourceslist);
  61. return true;
  62. }
  63. /*}}}*/
  64. bool ShowHelp(CommandLine &CmdL)
  65. {
  66. ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  67. COMMON_ARCH,__DATE__,__TIME__);
  68. // FIXME: generate from CommandLine
  69. c1out <<
  70. _("Usage: apt [options] command\n"
  71. "\n"
  72. "CLI for apt.\n"
  73. "Commands: \n"
  74. " list - list packages based on package names\n"
  75. " search - search in package descriptions\n"
  76. " show - show package details\n"
  77. "\n"
  78. " update - update list of available packages\n"
  79. " install - install packages\n"
  80. " upgrade - upgrade the systems packages\n"
  81. "\n"
  82. " edit-sources - edit the source information file\n"
  83. );
  84. return true;
  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. // needs root
  92. {"install",&DoInstall},
  93. {"remove", &DoInstall},
  94. {"update",&DoUpdate},
  95. {"upgrade",&DoUpgradeWithAllowNewPackages},
  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. if(!isatty(1))
  104. {
  105. std::cerr << std::endl
  106. << "WARNING WARNING "
  107. << argv[0]
  108. << " is *NOT* intended for scripts "
  109. << "use at your own peril^Wrisk"
  110. << std::endl
  111. << std::endl;
  112. }
  113. InitOutput();
  114. // Set up gettext support
  115. setlocale(LC_ALL,"");
  116. textdomain(PACKAGE);
  117. if(pkgInitConfig(*_config) == false)
  118. {
  119. _error->DumpErrors();
  120. return 100;
  121. }
  122. // FIXME: move into a new libprivate/private-install.cc:Install()
  123. _config->Set("DPkgPM::Progress", "1");
  124. _config->Set("Apt::Color", "1");
  125. // Parse the command line and initialize the package library
  126. CommandLine CmdL(Args.data(), _config);
  127. if (CmdL.Parse(argc, argv) == false ||
  128. pkgInitSystem(*_config, _system) == false)
  129. {
  130. _error->DumpErrors();
  131. return 100;
  132. }
  133. // See if the help should be shown
  134. if (_config->FindB("help") == true ||
  135. _config->FindB("version") == true ||
  136. CmdL.FileSize() == 0)
  137. {
  138. ShowHelp(CmdL);
  139. return 0;
  140. }
  141. // see if we are in simulate mode
  142. CheckSimulateMode(CmdL);
  143. // parse args
  144. CmdL.DispatchArg(Cmds);
  145. // Print any errors or warnings found during parsing
  146. bool const Errors = _error->PendingError();
  147. if (_config->FindI("quiet",0) > 0)
  148. _error->DumpErrors();
  149. else
  150. _error->DumpErrors(GlobalError::DEBUG);
  151. return Errors == true ? 100 : 0;
  152. }
  153. /*}}}*/