You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

571 lines
23 KiB

  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cmndline.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/error.h>
  6. #include <apt-pkg/fileutl.h>
  7. #include <apt-pkg/init.h>
  8. #include <apt-pkg/pkgsystem.h>
  9. #include <apt-pkg/strutl.h>
  10. #include <apt-private/private-cmndline.h>
  11. #include <apt-private/private-main.h>
  12. #include <stdarg.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <iomanip>
  16. #include <vector>
  17. #include <apti18n.h>
  18. /*}}}*/
  19. APT_NONNULL(1, 2)
  20. static bool CmdMatches_fn(char const *const Cmd, char const *const Match)
  21. {
  22. return strcmp(Cmd, Match) == 0;
  23. }
  24. template <typename... Tail>
  25. APT_NONNULL(1, 2)
  26. static bool CmdMatches_fn(char const *const Cmd, char const *const Match, Tail... MoreMatches)
  27. {
  28. return CmdMatches_fn(Cmd, Match) || CmdMatches_fn(Cmd, MoreMatches...);
  29. }
  30. #define addArg(w, x, y, z) Args.emplace_back(CommandLine::MakeArgs(w, x, y, z))
  31. #define CmdMatches(...) (Cmd != nullptr && CmdMatches_fn(Cmd, __VA_ARGS__))
  32. static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  33. {
  34. if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
  35. {
  36. addArg('i', "important", "APT::Cache::Important", 0);
  37. addArg(0, "installed", "APT::Cache::Installed", 0);
  38. addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
  39. addArg(0, "depends", "APT::Cache::ShowDepends", 0);
  40. addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
  41. addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
  42. addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
  43. addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
  44. addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
  45. addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
  46. addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
  47. addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
  48. }
  49. else if (CmdMatches("search"))
  50. {
  51. addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
  52. addArg('f', "full", "APT::Cache::ShowFull", 0);
  53. }
  54. else if (CmdMatches("show"))
  55. {
  56. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  57. }
  58. else if (CmdMatches("pkgnames"))
  59. {
  60. addArg(0, "all-names", "APT::Cache::AllNames", 0);
  61. }
  62. else if (CmdMatches("unmet"))
  63. {
  64. addArg('i', "important", "APT::Cache::Important", 0);
  65. }
  66. else if (CmdMatches("showsrc"))
  67. {
  68. addArg(0,"only-source","APT::Cache::Only-Source",0);
  69. }
  70. else if (CmdMatches("gencaches", "showpkg", "stats", "dump",
  71. "dumpavail", "showauto", "policy", "madison"))
  72. ;
  73. else
  74. return false;
  75. bool const found_something = Args.empty() == false;
  76. // FIXME: move to the correct command(s)
  77. addArg('g', "generate", "APT::Cache::Generate", 0);
  78. addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
  79. addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
  80. addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
  81. addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
  82. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  83. return found_something;
  84. }
  85. /*}}}*/
  86. static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  87. {
  88. if (CmdMatches("add", "ident") == false)
  89. return false;
  90. // FIXME: move to the correct command(s)
  91. addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
  92. addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
  93. addArg('r', "rename", "APT::CDROM::Rename", 0);
  94. addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
  95. addArg('f', "fast", "APT::CDROM::Fast", 0);
  96. addArg('n', "just-print", "APT::CDROM::NoAct", 0);
  97. addArg('n', "recon", "APT::CDROM::NoAct", 0);
  98. addArg('n', "no-act", "APT::CDROM::NoAct", 0);
  99. addArg('a', "thorough", "APT::CDROM::Thorough", 0);
  100. return true;
  101. }
  102. /*}}}*/
  103. static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  104. {
  105. if (CmdMatches("dump"))
  106. {
  107. addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
  108. addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
  109. }
  110. else if (CmdMatches("shell"))
  111. ;
  112. else
  113. return false;
  114. return true;
  115. }
  116. /*}}}*/
  117. static bool addArgumentsAPTDumpSolver(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  118. {
  119. addArg(0,"user","APT::Solver::RunAsUser",CommandLine::HasArg);
  120. return true;
  121. }
  122. /*}}}*/
  123. static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  124. {
  125. addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
  126. return true;
  127. }
  128. /*}}}*/
  129. static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  130. {
  131. addArg(0,"md5","APT::FTPArchive::MD5",0);
  132. addArg(0,"sha1","APT::FTPArchive::SHA1",0);
  133. addArg(0,"sha256","APT::FTPArchive::SHA256",0);
  134. addArg(0,"sha512","APT::FTPArchive::SHA512",0);
  135. addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
  136. addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
  137. addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
  138. addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
  139. addArg(0,"contents","APT::FTPArchive::Contents",0);
  140. addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
  141. return true;
  142. }
  143. /*}}}*/
  144. static bool addArgumentsAPTInternalPlanner(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
  145. {
  146. return true;
  147. }
  148. /*}}}*/
  149. static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
  150. {
  151. return true;
  152. }
  153. /*}}}*/
  154. static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  155. {
  156. if (CmdMatches("cat-file"))
  157. {
  158. addArg('C', "compress", "Apt-Helper::Cat-File::Compress",CommandLine::HasArg);
  159. }
  160. return true;
  161. }
  162. /*}}}*/
  163. static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  164. {
  165. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  166. "dselect-upgrade", "autoremove", "autopurge", "full-upgrade"))
  167. {
  168. addArg(0, "show-progress", "DpkgPM::Progress", 0);
  169. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  170. addArg(0, "purge", "APT::Get::Purge", 0);
  171. addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
  172. addArg(0, "autoremove", "APT::Get::AutomaticRemove", 0);
  173. addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
  174. addArg(0, "reinstall", "APT::Get::ReInstall", 0);
  175. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  176. addArg(0, "planner", "APT::Planner", CommandLine::HasArg);
  177. if (CmdMatches("upgrade"))
  178. {
  179. addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
  180. CommandLine::Boolean);
  181. }
  182. }
  183. else if (CmdMatches("update"))
  184. {
  185. addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
  186. addArg(0, "allow-insecure-repositories", "Acquire::AllowInsecureRepositories", 0);
  187. addArg(0, "allow-weak-repositories", "Acquire::AllowWeakRepositories", 0);
  188. addArg(0, "allow-releaseinfo-change", "Acquire::AllowReleaseInfoChange", 0);
  189. addArg(0, "allow-releaseinfo-change-origin", "Acquire::AllowReleaseInfoChange::Origin", 0);
  190. addArg(0, "allow-releaseinfo-change-label", "Acquire::AllowReleaseInfoChange::Label", 0);
  191. addArg(0, "allow-releaseinfo-change-version", "Acquire::AllowReleaseInfoChange::Version", 0);
  192. addArg(0, "allow-releaseinfo-change-codename", "Acquire::AllowReleaseInfoChange::Codename", 0);
  193. addArg(0, "allow-releaseinfo-change-suite", "Acquire::AllowReleaseInfoChange::Suite", 0);
  194. addArg(0, "allow-releaseinfo-change-defaultpin", "Acquire::AllowReleaseInfoChange::DefaultPin", 0);
  195. }
  196. else if (CmdMatches("source"))
  197. {
  198. addArg('b', "compile", "APT::Get::Compile", 0);
  199. addArg('b', "build", "APT::Get::Compile", 0);
  200. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  201. addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
  202. addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
  203. addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
  204. addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
  205. }
  206. else if (CmdMatches("build-dep"))
  207. {
  208. addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
  209. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  210. addArg(0, "purge", "APT::Get::Purge", 0);
  211. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  212. addArg(0,"arch-only","APT::Get::Arch-Only",0);
  213. addArg(0,"indep-only","APT::Get::Indep-Only",0);
  214. // this has no effect *but* sbuild is using it (see LP: #1255806)
  215. // once sbuild is fixed, this option can be removed
  216. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  217. }
  218. else if (CmdMatches("indextargets"))
  219. {
  220. addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
  221. addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
  222. }
  223. else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
  224. CmdMatches("markauto", "unmarkauto")) // deprecated commands
  225. ;
  226. else if (CmdMatches("moo"))
  227. addArg(0, "color", "APT::Moo::Color", 0);
  228. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  229. "dselect-upgrade", "autoremove", "auto-remove", "autopurge", "clean", "autoclean", "auto-clean", "check",
  230. "build-dep", "full-upgrade", "source"))
  231. {
  232. addArg('s', "simulate", "APT::Get::Simulate", 0);
  233. addArg('s', "just-print", "APT::Get::Simulate", 0);
  234. addArg('s', "recon", "APT::Get::Simulate", 0);
  235. addArg('s', "dry-run", "APT::Get::Simulate", 0);
  236. addArg('s', "no-act", "APT::Get::Simulate", 0);
  237. }
  238. bool const found_something = Args.empty() == false;
  239. // FIXME: move to the correct command(s)
  240. addArg('d',"download-only","APT::Get::Download-Only",0);
  241. addArg('y',"yes","APT::Get::Assume-Yes",0);
  242. addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
  243. addArg(0,"assume-no","APT::Get::Assume-No",0);
  244. addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
  245. addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
  246. addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
  247. addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
  248. addArg(0,"download","APT::Get::Download",0);
  249. addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
  250. addArg(0,"ignore-hold","APT::Ignore-Hold",0);
  251. addArg(0,"upgrade","APT::Get::upgrade",0);
  252. addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
  253. addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
  254. addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
  255. addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
  256. addArg(0,"force-yes","APT::Get::force-yes",0);
  257. addArg(0,"print-uris","APT::Get::Print-URIs",0);
  258. addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
  259. addArg(0,"remove","APT::Get::Remove",0);
  260. addArg(0,"only-source","APT::Get::Only-Source",0);
  261. addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
  262. addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
  263. addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
  264. addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
  265. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  266. return found_something;
  267. }
  268. /*}}}*/
  269. static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  270. {
  271. if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
  272. "showmanual", "showhold", "showholds",
  273. "markauto", "unmarkauto"))
  274. {
  275. addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
  276. }
  277. else if (CmdMatches("install", "remove", "deinstall", "purge",
  278. "showinstall", "showinstalls", "showremove", "showremoves",
  279. "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
  280. ;
  281. else
  282. return false;
  283. if (CmdMatches("markauto", "unmarkauto"))
  284. {
  285. addArg('v',"verbose","APT::MarkAuto::Verbose",0);
  286. }
  287. if (Cmd != nullptr && strncmp(Cmd, "show", strlen("show")) != 0)
  288. {
  289. addArg('s',"simulate","APT::Mark::Simulate",0);
  290. addArg('s',"just-print","APT::Mark::Simulate",0);
  291. addArg('s',"recon","APT::Mark::Simulate",0);
  292. addArg('s',"dry-run","APT::Mark::Simulate",0);
  293. addArg('s',"no-act","APT::Mark::Simulate",0);
  294. }
  295. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  296. return true;
  297. }
  298. /*}}}*/
  299. static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  300. {
  301. addArg('s',"source","APT::SortPkgs::Source",0);
  302. return true;
  303. }
  304. /*}}}*/
  305. static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  306. {
  307. if (CmdMatches("list"))
  308. {
  309. addArg('i',"installed","APT::Cmd::Installed",0);
  310. addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
  311. addArg('u',"upgradable","APT::Cmd::Upgradable",0);
  312. addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
  313. addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
  314. addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
  315. }
  316. else if (CmdMatches("show"))
  317. {
  318. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  319. }
  320. else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
  321. {
  322. // we have no (supported) command-name overlaps so far, so we call
  323. // specifics in order until we find one which adds arguments
  324. }
  325. else
  326. return false;
  327. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  328. return true;
  329. }
  330. /*}}}*/
  331. std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
  332. {
  333. std::vector<CommandLine::Args> Args;
  334. Args.reserve(50);
  335. if (Cmd != nullptr && strcmp(Cmd, "help") == 0)
  336. ; // no options for help so no need to implement it in each
  337. else
  338. switch (Program)
  339. {
  340. case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
  341. case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
  342. case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
  343. case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
  344. case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
  345. case APT_CMD::APT_DUMP_SOLVER: addArgumentsAPTDumpSolver(Args, Cmd); break;
  346. case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
  347. case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
  348. case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
  349. case APT_CMD::APT_INTERNAL_PLANNER: addArgumentsAPTInternalPlanner(Args, Cmd); break;
  350. case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
  351. case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
  352. case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
  353. }
  354. // options without a command
  355. addArg('h', "help", "help", 0);
  356. addArg('v', "version", "version", 0);
  357. // general options
  358. addArg('q', "quiet", "quiet", CommandLine::IntLevel);
  359. addArg('q', "silent", "quiet", CommandLine::IntLevel);
  360. addArg('c', "config-file", 0, CommandLine::ConfigFile);
  361. addArg('o', "option", 0, CommandLine::ArbItem);
  362. addArg(0, NULL, NULL, 0);
  363. return Args;
  364. }
  365. /*}}}*/
  366. #undef addArg
  367. static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
  368. {
  369. if (Cmds.empty() || Cmds[0].Match == nullptr)
  370. return;
  371. std::cout << std::endl << _("Most used commands:") << std::endl;
  372. for (auto const &c: Cmds)
  373. {
  374. if (c.Help == nullptr)
  375. continue;
  376. std::cout << " " << c.Match << " - " << c.Help << std::endl;
  377. }
  378. }
  379. /*}}}*/
  380. static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
  381. bool (*ShowHelp)(CommandLine &))
  382. {
  383. std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
  384. if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
  385. return true;
  386. if (ShowHelp(CmdL) == false)
  387. return false;
  388. if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
  389. return true;
  390. ShowHelpListCommands(Cmds);
  391. std::cout << std::endl;
  392. char const * cmd = nullptr;
  393. switch (Binary)
  394. {
  395. case APT_CMD::APT: cmd = "apt(8)"; break;
  396. case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
  397. case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
  398. case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
  399. case APT_CMD::APT_DUMP_SOLVER: cmd = nullptr; break;
  400. case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
  401. case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
  402. case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
  403. case APT_CMD::APT_HELPER: cmd = nullptr; break;
  404. case APT_CMD::APT_INTERNAL_PLANNER: cmd = nullptr; break;
  405. case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
  406. case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
  407. case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
  408. }
  409. if (cmd != nullptr)
  410. ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
  411. if (Binary != APT_CMD::APT_DUMP_SOLVER && Binary != APT_CMD::APT_INTERNAL_SOLVER &&
  412. Binary != APT_CMD::APT_INTERNAL_PLANNER)
  413. std::cout << std::endl <<
  414. _("Configuration options and syntax is detailed in apt.conf(5).\n"
  415. "Information about how to configure sources can be found in sources.list(5).\n"
  416. "Package and version choices can be expressed via apt_preferences(5).\n"
  417. "Security details are available in apt-secure(8).\n");
  418. if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
  419. std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
  420. else if (Binary == APT_CMD::APT_HELPER || Binary == APT_CMD::APT_DUMP_SOLVER)
  421. std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
  422. return true;
  423. }
  424. /*}}}*/
  425. static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
  426. {
  427. std::string const binary = flNotDir(Binary);
  428. if (binary == "apt" || binary == "apt-config")
  429. {
  430. _config->CndSet("Binary::apt::APT::Color", true);
  431. _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
  432. _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
  433. _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
  434. _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
  435. _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
  436. _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
  437. _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
  438. _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
  439. _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
  440. _config->CndSet("Binary::apt::APT::Keep-Downloaded-Packages", false);
  441. _config->CndSet("Binary::apt::APT::Get::Update::InteractiveReleaseInfoChanges", true);
  442. }
  443. _config->Set("Binary", binary);
  444. }
  445. /*}}}*/
  446. static void BinaryCommandSpecificConfiguration(char const * const Binary, char const * const Cmd)/*{{{*/
  447. {
  448. std::string const binary = flNotDir(Binary);
  449. if ((binary == "apt" || binary == "apt-get") && CmdMatches("upgrade", "dist-upgrade", "full-upgrade"))
  450. {
  451. //FIXME: the option is documented to apply only for install/remove, so
  452. // we force it false for configuration files where users can be confused if
  453. // we support it anyhow, but allow it on the commandline to take effect
  454. // even through it isn't documented as a user who doesn't want it wouldn't
  455. // ask for it
  456. _config->Set("Binary::apt-get::APT::Get::AutomaticRemove", false);
  457. _config->Set("Binary::apt::APT::Get::AutomaticRemove", false);
  458. }
  459. }
  460. #undef CmdMatches
  461. /*}}}*/
  462. std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
  463. Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
  464. bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
  465. {
  466. InitLocale(Binary);
  467. if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
  468. {
  469. _error->DumpErrors();
  470. exit(100);
  471. }
  472. if (likely(argc != 0 && argv[0] != NULL))
  473. BinarySpecificConfiguration(argv[0]);
  474. std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
  475. std::vector<CommandLine::Dispatch> Cmds;
  476. if (CmdsWithHelp.empty() == false)
  477. {
  478. CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
  479. Cmds.push_back(std::move(help));
  480. }
  481. for (auto const& cmd : CmdsWithHelp)
  482. Cmds.push_back({cmd.Match, cmd.Handler});
  483. char const * CmdCalled = nullptr;
  484. if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
  485. CmdCalled = CommandLine::GetCommand(Cmds.data(), argc, argv);
  486. if (CmdCalled != nullptr)
  487. BinaryCommandSpecificConfiguration(argv[0], CmdCalled);
  488. std::string const conf = "Binary::" + _config->Find("Binary");
  489. _config->MoveSubTree(conf.c_str(), nullptr);
  490. // Args running out of scope invalidates the pointer stored in CmdL,
  491. // but we don't use the pointer after this function, so we ignore
  492. // this problem for now and figure something out if we have to.
  493. auto Args = getCommandArgs(Binary, CmdCalled);
  494. CmdL = CommandLine(Args.data(), _config);
  495. if (CmdL.Parse(argc,argv) == false ||
  496. (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
  497. {
  498. if (_config->FindB("version") == true)
  499. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  500. _error->DumpErrors();
  501. exit(100);
  502. }
  503. if (_config->FindB("APT::Get::Force-Yes", false) == true)
  504. {
  505. _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
  506. }
  507. // See if the help should be shown
  508. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  509. (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
  510. {
  511. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  512. exit(0);
  513. }
  514. if (Cmds.empty() == false && CmdL.FileSize() == 0)
  515. {
  516. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  517. exit(1);
  518. }
  519. return Cmds;
  520. }
  521. /*}}}*/
  522. unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
  523. {
  524. // Match the operation
  525. bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
  526. // Print any errors or warnings found during parsing
  527. bool const Errors = _error->PendingError();
  528. if (_config->FindI("quiet",0) > 0)
  529. _error->DumpErrors();
  530. else
  531. _error->DumpErrors(GlobalError::DEBUG);
  532. if (returned == false)
  533. return 100;
  534. return Errors == true ? 100 : 0;
  535. }
  536. /*}}}*/