apt-helper.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. apt-helper - cmdline helpers
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/configuration.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/init.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/pkgsystem.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/acquire.h>
  17. #include <apt-pkg/acquire-item.h>
  18. #include <apt-private/acqprogress.h>
  19. #include <apt-private/private-output.h>
  20. #include <apt-private/private-download.h>
  21. #include <apt-private/private-cmndline.h>
  22. #include <iostream>
  23. #include <string>
  24. #include <vector>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. static bool DoDownloadFile(CommandLine &CmdL)
  28. {
  29. if (CmdL.FileSize() <= 2)
  30. return _error->Error(_("Must specify at least one pair url/filename"));
  31. pkgAcquire Fetcher;
  32. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  33. Fetcher.Setup(&Stat);
  34. std::string download_uri = CmdL.FileList[1];
  35. std::string targetfile = CmdL.FileList[2];
  36. new pkgAcqFile(&Fetcher, download_uri, "", 0, "desc", "short-desc",
  37. "dest-dir-ignored", targetfile);
  38. Fetcher.Run();
  39. bool Failed = false;
  40. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == false ||
  41. FileExists(targetfile) == false)
  42. return _error->Error(_("Download Failed"));
  43. return true;
  44. }
  45. static bool ShowHelp(CommandLine &)
  46. {
  47. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  48. COMMON_ARCH,__DATE__,__TIME__);
  49. if (_config->FindB("version") == true)
  50. return true;
  51. std::cout <<
  52. _("Usage: apt-helper [options] command\n"
  53. " apt-helper [options] download-file uri target-path\n"
  54. "\n"
  55. "apt-helper is a internal helper for apt\n"
  56. "\n"
  57. "Commands:\n"
  58. " download-file - download the given uri to the target-path\n"
  59. "\n"
  60. " This APT helper has Super Meep Powers.\n");
  61. return true;
  62. }
  63. int main(int argc,const char *argv[]) /*{{{*/
  64. {
  65. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  66. {"download-file", &DoDownloadFile},
  67. {0,0}};
  68. std::vector<CommandLine::Args> Args = getCommandArgs(
  69. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  70. // Set up gettext support
  71. setlocale(LC_ALL,"");
  72. textdomain(PACKAGE);
  73. // Parse the command line and initialize the package library
  74. CommandLine CmdL(Args.data(),_config);
  75. if (pkgInitConfig(*_config) == false ||
  76. CmdL.Parse(argc,argv) == false ||
  77. pkgInitSystem(*_config,_system) == false)
  78. {
  79. if (_config->FindB("version") == true)
  80. ShowHelp(CmdL);
  81. _error->DumpErrors();
  82. return 100;
  83. }
  84. // See if the help should be shown
  85. if (_config->FindB("help") == true ||
  86. _config->FindB("version") == true ||
  87. CmdL.FileSize() == 0)
  88. {
  89. ShowHelp(CmdL);
  90. return 0;
  91. }
  92. InitOutput();
  93. // Match the operation
  94. CmdL.DispatchArg(Cmds);
  95. // Print any errors or warnings found during parsing
  96. bool const Errors = _error->PendingError();
  97. if (_config->FindI("quiet",0) > 0)
  98. _error->DumpErrors();
  99. else
  100. _error->DumpErrors(GlobalError::DEBUG);
  101. return Errors == true ? 100 : 0;
  102. }
  103. /*}}}*/