You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

450 lines
17 KiB

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