private-cmndline.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cmndline.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/fileutl.h>
  6. #include <apt-pkg/pkgsystem.h>
  7. #include <apt-pkg/init.h>
  8. #include <apt-pkg/error.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 <string.h>
  14. #include <stdlib.h>
  15. #include <vector>
  16. #include <iomanip>
  17. #include <apti18n.h>
  18. /*}}}*/
  19. APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
  20. {
  21. if (Cmd == nullptr)
  22. return false;
  23. va_list args;
  24. bool found = false;
  25. va_start(args, Cmd);
  26. char const * Match = NULL;
  27. while ((Match = va_arg(args, char const *)) != NULL)
  28. {
  29. if (strcmp(Cmd, Match) != 0)
  30. continue;
  31. found = true;
  32. break;
  33. }
  34. va_end(args);
  35. return found;
  36. }
  37. /*}}}*/
  38. #define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z))
  39. #define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL)
  40. static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  41. {
  42. if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
  43. {
  44. addArg('i', "important", "APT::Cache::Important", 0);
  45. addArg(0, "installed", "APT::Cache::Installed", 0);
  46. addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
  47. addArg(0, "depends", "APT::Cache::ShowDepends", 0);
  48. addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
  49. addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
  50. addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
  51. addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
  52. addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
  53. addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
  54. addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
  55. addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
  56. }
  57. else if (CmdMatches("search"))
  58. {
  59. addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
  60. addArg('f', "full", "APT::Cache::ShowFull", 0);
  61. }
  62. else if (CmdMatches("show"))
  63. {
  64. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  65. }
  66. else if (CmdMatches("pkgnames"))
  67. {
  68. addArg(0, "all-names", "APT::Cache::AllNames", 0);
  69. }
  70. else if (CmdMatches("unmet"))
  71. {
  72. addArg('i', "important", "APT::Cache::Important", 0);
  73. }
  74. else if (CmdMatches("showsrc"))
  75. {
  76. addArg(0,"only-source","APT::Cache::Only-Source",0);
  77. }
  78. else if (CmdMatches("gencaches", "showpkg", "stats", "dump",
  79. "dumpavail", "showauto", "policy", "madison"))
  80. ;
  81. else
  82. return false;
  83. bool const found_something = Args.empty() == false;
  84. // FIXME: move to the correct command(s)
  85. addArg('g', "generate", "APT::Cache::Generate", 0);
  86. addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
  87. addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
  88. addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
  89. addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
  90. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  91. return found_something;
  92. }
  93. /*}}}*/
  94. static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  95. {
  96. if (CmdMatches("add", "ident") == false)
  97. return false;
  98. // FIXME: move to the correct command(s)
  99. addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
  100. addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
  101. addArg('r', "rename", "APT::CDROM::Rename", 0);
  102. addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
  103. addArg('f', "fast", "APT::CDROM::Fast", 0);
  104. addArg('n', "just-print", "APT::CDROM::NoAct", 0);
  105. addArg('n', "recon", "APT::CDROM::NoAct", 0);
  106. addArg('n', "no-act", "APT::CDROM::NoAct", 0);
  107. addArg('a', "thorough", "APT::CDROM::Thorough", 0);
  108. return true;
  109. }
  110. /*}}}*/
  111. static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  112. {
  113. if (CmdMatches("dump"))
  114. {
  115. addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
  116. addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
  117. }
  118. else if (CmdMatches("shell"))
  119. ;
  120. else
  121. return false;
  122. return true;
  123. }
  124. /*}}}*/
  125. static bool addArgumentsAPTDumpSolver(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  126. {
  127. addArg(0,"user","APT::Solver::RunAsUser",CommandLine::HasArg);
  128. return true;
  129. }
  130. /*}}}*/
  131. static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  132. {
  133. addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
  134. return true;
  135. }
  136. /*}}}*/
  137. static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  138. {
  139. addArg(0,"md5","APT::FTPArchive::MD5",0);
  140. addArg(0,"sha1","APT::FTPArchive::SHA1",0);
  141. addArg(0,"sha256","APT::FTPArchive::SHA256",0);
  142. addArg(0,"sha512","APT::FTPArchive::SHA512",0);
  143. addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
  144. addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
  145. addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
  146. addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
  147. addArg(0,"contents","APT::FTPArchive::Contents",0);
  148. addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
  149. return true;
  150. }
  151. /*}}}*/
  152. static bool addArgumentsAPTInternalPlanner(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
  153. {
  154. return true;
  155. }
  156. /*}}}*/
  157. static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
  158. {
  159. return true;
  160. }
  161. /*}}}*/
  162. static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  163. {
  164. if (CmdMatches("cat-file"))
  165. {
  166. addArg('C', "compress", "Apt-Helper::Cat-File::Compress",CommandLine::HasArg);
  167. }
  168. return true;
  169. }
  170. /*}}}*/
  171. static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  172. {
  173. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  174. "dselect-upgrade", "autoremove", "full-upgrade"))
  175. {
  176. addArg(0, "show-progress", "DpkgPM::Progress", 0);
  177. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  178. addArg(0, "purge", "APT::Get::Purge", 0);
  179. addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
  180. addArg(0, "autoremove", "APT::Get::AutomaticRemove", 0);
  181. addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
  182. addArg(0, "reinstall", "APT::Get::ReInstall", 0);
  183. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  184. addArg(0, "planner", "APT::Planner", CommandLine::HasArg);
  185. if (CmdMatches("upgrade"))
  186. {
  187. addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
  188. CommandLine::Boolean);
  189. }
  190. }
  191. else if (CmdMatches("update"))
  192. {
  193. addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
  194. }
  195. else if (CmdMatches("source"))
  196. {
  197. addArg('b', "compile", "APT::Get::Compile", 0);
  198. addArg('b', "build", "APT::Get::Compile", 0);
  199. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  200. addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
  201. addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
  202. addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
  203. addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
  204. }
  205. else if (CmdMatches("build-dep"))
  206. {
  207. addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
  208. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  209. addArg(0, "purge", "APT::Get::Purge", 0);
  210. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  211. // this has no effect *but* sbuild is using it (see LP: #1255806)
  212. // once sbuild is fixed, this option can be removed
  213. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  214. }
  215. else if (CmdMatches("indextargets"))
  216. {
  217. addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
  218. addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
  219. }
  220. else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
  221. CmdMatches("markauto", "unmarkauto")) // deprecated commands
  222. ;
  223. else if (CmdMatches("moo"))
  224. addArg(0, "color", "APT::Moo::Color", 0);
  225. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  226. "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
  227. "build-dep", "full-upgrade", "source"))
  228. {
  229. addArg('s', "simulate", "APT::Get::Simulate", 0);
  230. addArg('s', "just-print", "APT::Get::Simulate", 0);
  231. addArg('s', "recon", "APT::Get::Simulate", 0);
  232. addArg('s', "dry-run", "APT::Get::Simulate", 0);
  233. addArg('s', "no-act", "APT::Get::Simulate", 0);
  234. }
  235. bool const found_something = Args.empty() == false;
  236. // FIXME: move to the correct command(s)
  237. addArg('d',"download-only","APT::Get::Download-Only",0);
  238. addArg('y',"yes","APT::Get::Assume-Yes",0);
  239. addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
  240. addArg(0,"assume-no","APT::Get::Assume-No",0);
  241. addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
  242. addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
  243. addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
  244. addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
  245. addArg(0,"download","APT::Get::Download",0);
  246. addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
  247. addArg(0,"ignore-hold","APT::Ignore-Hold",0);
  248. addArg(0,"upgrade","APT::Get::upgrade",0);
  249. addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
  250. addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
  251. addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
  252. addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
  253. addArg(0,"force-yes","APT::Get::force-yes",0);
  254. addArg(0,"print-uris","APT::Get::Print-URIs",0);
  255. addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
  256. addArg(0,"remove","APT::Get::Remove",0);
  257. addArg(0,"only-source","APT::Get::Only-Source",0);
  258. addArg(0,"arch-only","APT::Get::Arch-Only",0);
  259. addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
  260. addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
  261. addArg(0,"allow-weak-repositories","Acquire::AllowWeakRepositories",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 (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. }
  442. if (binary == "apt-config")
  443. _config->CndSet("Binary::apt-get::Acquire::AllowInsecureRepositories", true);
  444. _config->Set("Binary", binary);
  445. }
  446. /*}}}*/
  447. static void BinaryCommandSpecificConfiguration(char const * const Binary, char const * const Cmd)/*{{{*/
  448. {
  449. std::string const binary = flNotDir(Binary);
  450. if (binary == "apt-get" && CmdMatches("update"))
  451. _config->CndSet("Binary::apt-get::Acquire::AllowInsecureRepositories", true);
  452. }
  453. #undef CmdMatches
  454. /*}}}*/
  455. std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
  456. Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
  457. bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
  458. {
  459. InitLocale(Binary);
  460. if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
  461. {
  462. _error->DumpErrors();
  463. exit(100);
  464. }
  465. if (likely(argc != 0 && argv[0] != NULL))
  466. BinarySpecificConfiguration(argv[0]);
  467. std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
  468. std::vector<CommandLine::Dispatch> Cmds;
  469. if (CmdsWithHelp.empty() == false)
  470. {
  471. CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
  472. Cmds.push_back(std::move(help));
  473. }
  474. for (auto const& cmd : CmdsWithHelp)
  475. Cmds.push_back({cmd.Match, cmd.Handler});
  476. // Args running out of scope invalidates the pointer stored in CmdL,
  477. // but we don't use the pointer after this function, so we ignore
  478. // this problem for now and figure something out if we have to.
  479. char const * CmdCalled = nullptr;
  480. if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
  481. CmdCalled = CommandLine::GetCommand(Cmds.data(), argc, argv);
  482. if (CmdCalled != nullptr)
  483. BinaryCommandSpecificConfiguration(argv[0], CmdCalled);
  484. std::string const conf = "Binary::" + _config->Find("Binary");
  485. _config->MoveSubTree(conf.c_str(), nullptr);
  486. auto Args = getCommandArgs(Binary, CmdCalled);
  487. CmdL = CommandLine(Args.data(), _config);
  488. if (CmdL.Parse(argc,argv) == false ||
  489. (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
  490. {
  491. if (_config->FindB("version") == true)
  492. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  493. _error->DumpErrors();
  494. exit(100);
  495. }
  496. // See if the help should be shown
  497. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  498. (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
  499. {
  500. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  501. exit(0);
  502. }
  503. if (Cmds.empty() == false && CmdL.FileSize() == 0)
  504. {
  505. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  506. exit(1);
  507. }
  508. return Cmds;
  509. }
  510. /*}}}*/
  511. unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
  512. {
  513. // Match the operation
  514. bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
  515. // Print any errors or warnings found during parsing
  516. bool const Errors = _error->PendingError();
  517. if (_config->FindI("quiet",0) > 0)
  518. _error->DumpErrors();
  519. else
  520. _error->DumpErrors(GlobalError::DEBUG);
  521. if (returned == false)
  522. return 100;
  523. return Errors == true ? 100 : 0;
  524. }
  525. /*}}}*/