private-cmndline.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. return found_something;
  91. }
  92. /*}}}*/
  93. static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  94. {
  95. if (CmdMatches("add", "ident") == false)
  96. return false;
  97. // FIXME: move to the correct command(s)
  98. addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
  99. addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
  100. addArg('r', "rename", "APT::CDROM::Rename", 0);
  101. addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
  102. addArg('f', "fast", "APT::CDROM::Fast", 0);
  103. addArg('n', "just-print", "APT::CDROM::NoAct", 0);
  104. addArg('n', "recon", "APT::CDROM::NoAct", 0);
  105. addArg('n', "no-act", "APT::CDROM::NoAct", 0);
  106. addArg('a', "thorough", "APT::CDROM::Thorough", 0);
  107. return true;
  108. }
  109. /*}}}*/
  110. static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  111. {
  112. if (CmdMatches("dump"))
  113. {
  114. addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
  115. addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
  116. }
  117. else if (CmdMatches("shell"))
  118. ;
  119. else
  120. return false;
  121. return true;
  122. }
  123. /*}}}*/
  124. static bool addArgumentsAPTDumpSolver(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  125. {
  126. addArg(0,"user","APT::Solver::RunAsUser",CommandLine::HasArg);
  127. return true;
  128. }
  129. /*}}}*/
  130. static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  131. {
  132. addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
  133. return true;
  134. }
  135. /*}}}*/
  136. static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  137. {
  138. addArg(0,"md5","APT::FTPArchive::MD5",0);
  139. addArg(0,"sha1","APT::FTPArchive::SHA1",0);
  140. addArg(0,"sha256","APT::FTPArchive::SHA256",0);
  141. addArg(0,"sha512","APT::FTPArchive::SHA512",0);
  142. addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
  143. addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
  144. addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
  145. addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
  146. addArg(0,"contents","APT::FTPArchive::Contents",0);
  147. addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
  148. return true;
  149. }
  150. /*}}}*/
  151. static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
  152. {
  153. return true;
  154. }
  155. /*}}}*/
  156. static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  157. {
  158. if (CmdMatches("cat-file"))
  159. {
  160. addArg('C', "compress", "Apt-Helper::Cat-File::Compress",CommandLine::HasArg);
  161. }
  162. return true;
  163. }
  164. /*}}}*/
  165. static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  166. {
  167. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  168. "dselect-upgrade", "autoremove", "full-upgrade"))
  169. {
  170. addArg(0, "show-progress", "DpkgPM::Progress", 0);
  171. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  172. addArg(0, "purge", "APT::Get::Purge", 0);
  173. addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
  174. addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
  175. addArg(0, "reinstall", "APT::Get::ReInstall", 0);
  176. addArg(0, "solver", "APT::Solver", 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. }
  187. else if (CmdMatches("source"))
  188. {
  189. addArg('b', "compile", "APT::Get::Compile", 0);
  190. addArg('b', "build", "APT::Get::Compile", 0);
  191. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  192. addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
  193. addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
  194. addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
  195. addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
  196. }
  197. else if (CmdMatches("build-dep"))
  198. {
  199. addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
  200. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  201. addArg(0, "purge", "APT::Get::Purge", 0);
  202. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  203. // this has no effect *but* sbuild is using it (see LP: #1255806)
  204. // once sbuild is fixed, this option can be removed
  205. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  206. }
  207. else if (CmdMatches("indextargets"))
  208. {
  209. addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
  210. addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
  211. }
  212. else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
  213. CmdMatches("markauto", "unmarkauto")) // deprecated commands
  214. ;
  215. else if (CmdMatches("moo"))
  216. addArg(0, "color", "APT::Moo::Color", 0);
  217. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  218. "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
  219. "build-dep", "full-upgrade", "source"))
  220. {
  221. addArg('s', "simulate", "APT::Get::Simulate", 0);
  222. addArg('s', "just-print", "APT::Get::Simulate", 0);
  223. addArg('s', "recon", "APT::Get::Simulate", 0);
  224. addArg('s', "dry-run", "APT::Get::Simulate", 0);
  225. addArg('s', "no-act", "APT::Get::Simulate", 0);
  226. }
  227. bool const found_something = Args.empty() == false;
  228. // FIXME: move to the correct command(s)
  229. addArg('d',"download-only","APT::Get::Download-Only",0);
  230. addArg('y',"yes","APT::Get::Assume-Yes",0);
  231. addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
  232. addArg(0,"assume-no","APT::Get::Assume-No",0);
  233. addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
  234. addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
  235. addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
  236. addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
  237. addArg(0,"download","APT::Get::Download",0);
  238. addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
  239. addArg(0,"ignore-hold","APT::Ignore-Hold",0);
  240. addArg(0,"upgrade","APT::Get::upgrade",0);
  241. addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
  242. addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
  243. addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
  244. addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
  245. addArg(0,"force-yes","APT::Get::force-yes",0);
  246. addArg(0,"print-uris","APT::Get::Print-URIs",0);
  247. addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
  248. addArg(0,"remove","APT::Get::Remove",0);
  249. addArg(0,"only-source","APT::Get::Only-Source",0);
  250. addArg(0,"arch-only","APT::Get::Arch-Only",0);
  251. addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
  252. addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
  253. addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
  254. addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
  255. addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
  256. return found_something;
  257. }
  258. /*}}}*/
  259. static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  260. {
  261. if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
  262. "showmanual", "showhold", "showholds",
  263. "markauto", "unmarkauto"))
  264. {
  265. addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
  266. }
  267. else if (CmdMatches("install", "remove", "deinstall", "purge",
  268. "showinstall", "showinstalls", "showremove", "showremoves",
  269. "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
  270. ;
  271. else
  272. return false;
  273. if (CmdMatches("markauto", "unmarkauto"))
  274. {
  275. addArg('v',"verbose","APT::MarkAuto::Verbose",0);
  276. }
  277. if (strncmp(Cmd, "show", strlen("show")) != 0)
  278. {
  279. addArg('s',"simulate","APT::Mark::Simulate",0);
  280. addArg('s',"just-print","APT::Mark::Simulate",0);
  281. addArg('s',"recon","APT::Mark::Simulate",0);
  282. addArg('s',"dry-run","APT::Mark::Simulate",0);
  283. addArg('s',"no-act","APT::Mark::Simulate",0);
  284. }
  285. return true;
  286. }
  287. /*}}}*/
  288. static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
  289. {
  290. addArg('s',"source","APT::SortPkgs::Source",0);
  291. return true;
  292. }
  293. /*}}}*/
  294. static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  295. {
  296. if (CmdMatches("list"))
  297. {
  298. addArg('i',"installed","APT::Cmd::Installed",0);
  299. addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
  300. addArg('u',"upgradable","APT::Cmd::Upgradable",0);
  301. addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
  302. addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
  303. addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
  304. }
  305. else if (CmdMatches("show"))
  306. {
  307. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  308. }
  309. else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
  310. {
  311. // we have no (supported) command-name overlaps so far, so we call
  312. // specifics in order until we find one which adds arguments
  313. }
  314. else
  315. return false;
  316. return true;
  317. }
  318. /*}}}*/
  319. std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
  320. {
  321. std::vector<CommandLine::Args> Args;
  322. Args.reserve(50);
  323. if (Cmd != nullptr && strcmp(Cmd, "help") == 0)
  324. ; // no options for help so no need to implement it in each
  325. else
  326. switch (Program)
  327. {
  328. case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
  329. case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
  330. case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
  331. case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
  332. case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
  333. case APT_CMD::APT_DUMP_SOLVER: addArgumentsAPTDumpSolver(Args, Cmd); break;
  334. case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
  335. case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
  336. case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
  337. case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
  338. case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
  339. case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
  340. }
  341. // options without a command
  342. addArg('h', "help", "help", 0);
  343. addArg('v', "version", "version", 0);
  344. // general options
  345. addArg('q', "quiet", "quiet", CommandLine::IntLevel);
  346. addArg('q', "silent", "quiet", CommandLine::IntLevel);
  347. addArg('c', "config-file", 0, CommandLine::ConfigFile);
  348. addArg('o', "option", 0, CommandLine::ArbItem);
  349. addArg(0, NULL, NULL, 0);
  350. return Args;
  351. }
  352. /*}}}*/
  353. #undef CmdMatches
  354. #undef addArg
  355. static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
  356. {
  357. if (Cmds.empty() || Cmds[0].Match == nullptr)
  358. return;
  359. std::cout << std::endl << _("Most used commands:") << std::endl;
  360. for (auto const &c: Cmds)
  361. {
  362. if (c.Help == nullptr)
  363. continue;
  364. std::cout << " " << c.Match << " - " << c.Help << std::endl;
  365. }
  366. }
  367. /*}}}*/
  368. static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
  369. bool (*ShowHelp)(CommandLine &))
  370. {
  371. std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
  372. if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
  373. return true;
  374. if (ShowHelp(CmdL) == false)
  375. return false;
  376. if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
  377. return true;
  378. ShowHelpListCommands(Cmds);
  379. std::cout << std::endl;
  380. char const * cmd = nullptr;
  381. switch (Binary)
  382. {
  383. case APT_CMD::APT: cmd = "apt(8)"; break;
  384. case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
  385. case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
  386. case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
  387. case APT_CMD::APT_DUMP_SOLVER: cmd = nullptr; break;
  388. case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
  389. case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
  390. case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
  391. case APT_CMD::APT_HELPER: cmd = nullptr; break;
  392. case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
  393. case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
  394. case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
  395. }
  396. if (cmd != nullptr)
  397. ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
  398. if (Binary != APT_CMD::APT_DUMP_SOLVER && Binary != APT_CMD::APT_INTERNAL_SOLVER)
  399. std::cout << std::endl <<
  400. _("Configuration options and syntax is detailed in apt.conf(5).\n"
  401. "Information about how to configure sources can be found in sources.list(5).\n"
  402. "Package and version choices can be expressed via apt_preferences(5).\n"
  403. "Security details are available in apt-secure(8).\n");
  404. if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
  405. std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
  406. else if (Binary == APT_CMD::APT_HELPER || Binary == APT_CMD::APT_DUMP_SOLVER)
  407. std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
  408. return true;
  409. }
  410. /*}}}*/
  411. static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
  412. {
  413. std::string const binary = flNotDir(Binary);
  414. if (binary == "apt" || binary == "apt-config")
  415. {
  416. _config->CndSet("Binary::apt::APT::Color", true);
  417. _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
  418. _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
  419. _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
  420. _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
  421. _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
  422. _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
  423. _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
  424. _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
  425. _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
  426. _config->CndSet("Binary::apt::Acquire::AllowInsecureRepositories", false);
  427. _config->CndSet("Binary::apt::APT::Keep-Downloaded-Packages", false);
  428. }
  429. _config->Set("Binary", binary);
  430. std::string const conf = "Binary::" + binary;
  431. _config->MoveSubTree(conf.c_str(), NULL);
  432. }
  433. /*}}}*/
  434. std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
  435. Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
  436. bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
  437. {
  438. InitLocale(Binary);
  439. if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
  440. {
  441. _error->DumpErrors();
  442. exit(100);
  443. }
  444. if (likely(argc != 0 && argv[0] != NULL))
  445. BinarySpecificConfiguration(argv[0]);
  446. std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
  447. std::vector<CommandLine::Dispatch> Cmds;
  448. if (CmdsWithHelp.empty() == false)
  449. {
  450. CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
  451. Cmds.push_back(std::move(help));
  452. }
  453. for (auto const& cmd : CmdsWithHelp)
  454. Cmds.push_back({cmd.Match, cmd.Handler});
  455. // Args running out of scope invalidates the pointer stored in CmdL,
  456. // but we don't use the pointer after this function, so we ignore
  457. // this problem for now and figure something out if we have to.
  458. std::vector<CommandLine::Args> Args;
  459. if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
  460. Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds.data(), argc, argv));
  461. else
  462. Args = getCommandArgs(Binary, nullptr);
  463. CmdL = CommandLine(Args.data(), _config);
  464. if (CmdL.Parse(argc,argv) == false ||
  465. (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
  466. {
  467. if (_config->FindB("version") == true)
  468. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  469. _error->DumpErrors();
  470. exit(100);
  471. }
  472. // See if the help should be shown
  473. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  474. (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
  475. {
  476. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  477. exit(0);
  478. }
  479. if (Cmds.empty() == false && CmdL.FileSize() == 0)
  480. {
  481. ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
  482. exit(1);
  483. }
  484. return Cmds;
  485. }
  486. /*}}}*/
  487. unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
  488. {
  489. // Match the operation
  490. bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
  491. // Print any errors or warnings found during parsing
  492. bool const Errors = _error->PendingError();
  493. if (_config->FindI("quiet",0) > 0)
  494. _error->DumpErrors();
  495. else
  496. _error->DumpErrors(GlobalError::DEBUG);
  497. if (returned == false)
  498. return 100;
  499. return Errors == true ? 100 : 0;
  500. }
  501. /*}}}*/