private-cmndline.cc 20 KB

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