private-cmndline.cc 20 KB

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