private-cmndline.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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, "auto-remove", "APT::Get::AutomaticRemove", 0);
  181. addArg(0, "reinstall", "APT::Get::ReInstall", 0);
  182. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  183. addArg(0, "planner", "APT::Planner", CommandLine::HasArg);
  184. if (CmdMatches("upgrade"))
  185. {
  186. addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
  187. CommandLine::Boolean);
  188. }
  189. }
  190. else if (CmdMatches("update"))
  191. {
  192. addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
  193. }
  194. else if (CmdMatches("source"))
  195. {
  196. addArg('b', "compile", "APT::Get::Compile", 0);
  197. addArg('b', "build", "APT::Get::Compile", 0);
  198. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  199. addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
  200. addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
  201. addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
  202. addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
  203. }
  204. else if (CmdMatches("build-dep"))
  205. {
  206. addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
  207. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  208. addArg(0, "purge", "APT::Get::Purge", 0);
  209. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  210. // this has no effect *but* sbuild is using it (see LP: #1255806)
  211. // once sbuild is fixed, this option can be removed
  212. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  213. }
  214. else if (CmdMatches("indextargets"))
  215. {
  216. addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
  217. addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
  218. }
  219. else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
  220. CmdMatches("markauto", "unmarkauto")) // deprecated commands
  221. ;
  222. else if (CmdMatches("moo"))
  223. addArg(0, "color", "APT::Moo::Color", 0);
  224. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  225. "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
  226. "build-dep", "full-upgrade", "source"))
  227. {
  228. addArg('s', "simulate", "APT::Get::Simulate", 0);
  229. addArg('s', "just-print", "APT::Get::Simulate", 0);
  230. addArg('s', "recon", "APT::Get::Simulate", 0);
  231. addArg('s', "dry-run", "APT::Get::Simulate", 0);
  232. addArg('s', "no-act", "APT::Get::Simulate", 0);
  233. }
  234. bool const found_something = Args.empty() == false;
  235. // FIXME: move to the correct command(s)
  236. addArg('d',"download-only","APT::Get::Download-Only",0);
  237. addArg('y',"yes","APT::Get::Assume-Yes",0);
  238. addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
  239. addArg(0,"assume-no","APT::Get::Assume-No",0);
  240. addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
  241. addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
  242. addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
  243. addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
  244. addArg(0,"download","APT::Get::Download",0);
  245. addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
  246. addArg(0,"ignore-hold","APT::Ignore-Hold",0);
  247. addArg(0,"upgrade","APT::Get::upgrade",0);
  248. addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
  249. addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
  250. addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
  251. addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
  252. addArg(0,"force-yes","APT::Get::force-yes",0);
  253. addArg(0,"print-uris","APT::Get::Print-URIs",0);
  254. addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
  255. addArg(0,"remove","APT::Get::Remove",0);
  256. addArg(0,"only-source","APT::Get::Only-Source",0);
  257. addArg(0,"arch-only","APT::Get::Arch-Only",0);
  258. addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
  259. addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
  260. addArg(0,"allow-weak-repositories","Acquire::AllowWeakRepositories",0);
  261. addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
  262. addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
  263. addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
  264. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  265. return found_something;
  266. }
  267. /*}}}*/
  268. static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  269. {
  270. if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
  271. "showmanual", "showhold", "showholds",
  272. "markauto", "unmarkauto"))
  273. {
  274. addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
  275. }
  276. else if (CmdMatches("install", "remove", "deinstall", "purge",
  277. "showinstall", "showinstalls", "showremove", "showremoves",
  278. "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
  279. ;
  280. else
  281. return false;
  282. if (CmdMatches("markauto", "unmarkauto"))
  283. {
  284. addArg('v',"verbose","APT::MarkAuto::Verbose",0);
  285. }
  286. if (strncmp(Cmd, "show", strlen("show")) != 0)
  287. {
  288. addArg('s',"simulate","APT::Mark::Simulate",0);
  289. addArg('s',"just-print","APT::Mark::Simulate",0);
  290. addArg('s',"recon","APT::Mark::Simulate",0);
  291. addArg('s',"dry-run","APT::Mark::Simulate",0);
  292. addArg('s',"no-act","APT::Mark::Simulate",0);
  293. }
  294. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  295. return true;
  296. }
  297. /*}}}*/
  298. static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  299. {
  300. addArg('s',"source","APT::SortPkgs::Source",0);
  301. return true;
  302. }
  303. /*}}}*/
  304. static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  305. {
  306. if (CmdMatches("list"))
  307. {
  308. addArg('i',"installed","APT::Cmd::Installed",0);
  309. addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
  310. addArg('u',"upgradable","APT::Cmd::Upgradable",0);
  311. addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
  312. addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
  313. addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
  314. }
  315. else if (CmdMatches("show"))
  316. {
  317. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  318. }
  319. else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
  320. {
  321. // we have no (supported) command-name overlaps so far, so we call
  322. // specifics in order until we find one which adds arguments
  323. }
  324. else
  325. return false;
  326. addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
  327. return true;
  328. }
  329. /*}}}*/
  330. std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
  331. {
  332. std::vector<CommandLine::Args> Args;
  333. Args.reserve(50);
  334. if (Cmd != nullptr && strcmp(Cmd, "help") == 0)
  335. ; // no options for help so no need to implement it in each
  336. else
  337. switch (Program)
  338. {
  339. case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
  340. case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
  341. case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
  342. case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
  343. case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
  344. case APT_CMD::APT_DUMP_SOLVER: addArgumentsAPTDumpSolver(Args, Cmd); break;
  345. case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
  346. case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
  347. case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
  348. case APT_CMD::APT_INTERNAL_PLANNER: addArgumentsAPTInternalPlanner(Args, Cmd); break;
  349. case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
  350. case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
  351. case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
  352. }
  353. // options without a command
  354. addArg('h', "help", "help", 0);
  355. addArg('v', "version", "version", 0);
  356. // general options
  357. addArg('q', "quiet", "quiet", CommandLine::IntLevel);
  358. addArg('q', "silent", "quiet", CommandLine::IntLevel);
  359. addArg('c', "config-file", 0, CommandLine::ConfigFile);
  360. addArg('o', "option", 0, CommandLine::ArbItem);
  361. addArg(0, NULL, NULL, 0);
  362. return Args;
  363. }
  364. /*}}}*/
  365. #undef addArg
  366. static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
  367. {
  368. if (Cmds.empty() || Cmds[0].Match == nullptr)
  369. return;
  370. std::cout << std::endl << _("Most used commands:") << std::endl;
  371. for (auto const &c: Cmds)
  372. {
  373. if (c.Help == nullptr)
  374. continue;
  375. std::cout << " " << c.Match << " - " << c.Help << std::endl;
  376. }
  377. }
  378. /*}}}*/
  379. static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
  380. bool (*ShowHelp)(CommandLine &))
  381. {
  382. std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
  383. if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
  384. return true;
  385. if (ShowHelp(CmdL) == false)
  386. return false;
  387. if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
  388. return true;
  389. ShowHelpListCommands(Cmds);
  390. std::cout << std::endl;
  391. char const * cmd = nullptr;
  392. switch (Binary)
  393. {
  394. case APT_CMD::APT: cmd = "apt(8)"; break;
  395. case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
  396. case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
  397. case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
  398. case APT_CMD::APT_DUMP_SOLVER: cmd = nullptr; break;
  399. case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
  400. case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
  401. case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
  402. case APT_CMD::APT_HELPER: cmd = nullptr; break;
  403. case APT_CMD::APT_INTERNAL_PLANNER: cmd = nullptr; break;
  404. case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
  405. case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
  406. case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
  407. }
  408. if (cmd != nullptr)
  409. ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
  410. if (Binary != APT_CMD::APT_DUMP_SOLVER && Binary != APT_CMD::APT_INTERNAL_SOLVER &&
  411. Binary != APT_CMD::APT_INTERNAL_PLANNER)
  412. std::cout << std::endl <<
  413. _("Configuration options and syntax is detailed in apt.conf(5).\n"
  414. "Information about how to configure sources can be found in sources.list(5).\n"
  415. "Package and version choices can be expressed via apt_preferences(5).\n"
  416. "Security details are available in apt-secure(8).\n");
  417. if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
  418. std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
  419. else if (Binary == APT_CMD::APT_HELPER || Binary == APT_CMD::APT_DUMP_SOLVER)
  420. std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
  421. return true;
  422. }
  423. /*}}}*/
  424. static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
  425. {
  426. std::string const binary = flNotDir(Binary);
  427. if (binary == "apt" || binary == "apt-config")
  428. {
  429. _config->CndSet("Binary::apt::APT::Color", true);
  430. _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
  431. _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
  432. _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
  433. _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
  434. _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
  435. _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
  436. _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
  437. _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
  438. _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
  439. _config->CndSet("Binary::apt::APT::Keep-Downloaded-Packages", false);
  440. }
  441. if (binary == "apt-config")
  442. _config->CndSet("Binary::apt-get::Acquire::AllowInsecureRepositories", true);
  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-get" && CmdMatches("update"))
  450. _config->CndSet("Binary::apt-get::Acquire::AllowInsecureRepositories", true);
  451. }
  452. #undef CmdMatches
  453. /*}}}*/
  454. std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
  455. Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
  456. bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
  457. {
  458. InitLocale(Binary);
  459. if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
  460. {
  461. _error->DumpErrors();
  462. exit(100);
  463. }
  464. if (likely(argc != 0 && argv[0] != NULL))
  465. BinarySpecificConfiguration(argv[0]);
  466. std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
  467. std::vector<CommandLine::Dispatch> Cmds;
  468. if (CmdsWithHelp.empty() == false)
  469. {
  470. CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
  471. Cmds.push_back(std::move(help));
  472. }
  473. for (auto const& cmd : CmdsWithHelp)
  474. Cmds.push_back({cmd.Match, cmd.Handler});
  475. // Args running out of scope invalidates the pointer stored in CmdL,
  476. // but we don't use the pointer after this function, so we ignore
  477. // this problem for now and figure something out if we have to.
  478. char const * CmdCalled = nullptr;
  479. if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
  480. CmdCalled = CommandLine::GetCommand(Cmds.data(), argc, argv);
  481. if (CmdCalled != nullptr)
  482. BinaryCommandSpecificConfiguration(argv[0], CmdCalled);
  483. std::string const conf = "Binary::" + _config->Find("Binary");
  484. _config->MoveSubTree(conf.c_str(), nullptr);
  485. auto Args = getCommandArgs(Binary, CmdCalled);
  486. CmdL = CommandLine(Args.data(), _config);
  487. if (CmdL.Parse(argc,argv) == false ||
  488. (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
  489. {
  490. if (_config->FindB("version") == true)
  491. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  492. _error->DumpErrors();
  493. exit(100);
  494. }
  495. // See if the help should be shown
  496. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  497. (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
  498. {
  499. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  500. exit(0);
  501. }
  502. if (Cmds.empty() == false && CmdL.FileSize() == 0)
  503. {
  504. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  505. exit(1);
  506. }
  507. return Cmds;
  508. }
  509. /*}}}*/
  510. unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
  511. {
  512. // Match the operation
  513. bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
  514. // Print any errors or warnings found during parsing
  515. bool const Errors = _error->PendingError();
  516. if (_config->FindI("quiet",0) > 0)
  517. _error->DumpErrors();
  518. else
  519. _error->DumpErrors(GlobalError::DEBUG);
  520. if (returned == false)
  521. return 100;
  522. return Errors == true ? 100 : 0;
  523. }
  524. /*}}}*/