private-cmndline.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cmndline.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/pkgsystem.h>
  6. #include <apt-pkg/init.h>
  7. #include <apt-pkg/error.h>
  8. #include <apt-private/private-cmndline.h>
  9. #include <vector>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <apti18n.h>
  14. /*}}}*/
  15. APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
  16. {
  17. va_list args;
  18. bool found = false;
  19. va_start(args, Cmd);
  20. char const * Match = NULL;
  21. while ((Match = va_arg(args, char const *)) != NULL)
  22. {
  23. if (strcmp(Cmd, Match) != 0)
  24. continue;
  25. found = true;
  26. break;
  27. }
  28. va_end(args);
  29. return found;
  30. }
  31. /*}}}*/
  32. #define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z))
  33. #define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL)
  34. static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  35. {
  36. if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
  37. {
  38. addArg('i', "important", "APT::Cache::Important", 0);
  39. addArg(0, "installed", "APT::Cache::Installed", 0);
  40. addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
  41. addArg(0, "depends", "APT::Cache::ShowDepends", 0);
  42. addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
  43. addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
  44. addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
  45. addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
  46. addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
  47. addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
  48. addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
  49. }
  50. else if (CmdMatches("search"))
  51. {
  52. addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
  53. addArg('f', "full", "APT::Cache::ShowFull", 0);
  54. }
  55. else if (CmdMatches("show"))
  56. {
  57. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  58. }
  59. else if (CmdMatches("pkgnames"))
  60. {
  61. addArg(0, "all-names", "APT::Cache::AllNames", 0);
  62. }
  63. else if (CmdMatches("unmet"))
  64. {
  65. addArg('i', "important", "APT::Cache::Important", 0);
  66. }
  67. else if (CmdMatches("gencaches", "showsrc", "showpkg", "stats", "dump",
  68. "dumpavail", "showauto", "policy", "madison"))
  69. ;
  70. else
  71. return false;
  72. bool const found_something = Args.empty() == false;
  73. // FIXME: move to the correct command(s)
  74. addArg('g', "generate", "APT::Cache::Generate", 0);
  75. addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
  76. addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
  77. addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
  78. addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
  79. return found_something;
  80. }
  81. /*}}}*/
  82. static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  83. {
  84. if (CmdMatches("add", "ident") == false)
  85. return false;
  86. // FIXME: move to the correct command(s)
  87. addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
  88. addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
  89. addArg('r', "rename", "APT::CDROM::Rename", 0);
  90. addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
  91. addArg('f', "fast", "APT::CDROM::Fast", 0);
  92. addArg('n', "just-print", "APT::CDROM::NoAct", 0);
  93. addArg('n', "recon", "APT::CDROM::NoAct", 0);
  94. addArg('n', "no-act", "APT::CDROM::NoAct", 0);
  95. addArg('a', "thorough", "APT::CDROM::Thorough", 0);
  96. return true;
  97. }
  98. /*}}}*/
  99. static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  100. {
  101. if (CmdMatches("dump"))
  102. {
  103. addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
  104. addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
  105. }
  106. else if (CmdMatches("shell"))
  107. ;
  108. else
  109. return false;
  110. return true;
  111. }
  112. /*}}}*/
  113. static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  114. {
  115. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  116. "dselect-upgrade", "autoremove", "full-upgrade"))
  117. {
  118. addArg(0, "show-progress", "DpkgPM::Progress", 0);
  119. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  120. addArg(0, "purge", "APT::Get::Purge", 0);
  121. addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
  122. addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
  123. addArg(0, "reinstall", "APT::Get::ReInstall", 0);
  124. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  125. if (CmdMatches("upgrade"))
  126. {
  127. addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
  128. CommandLine::Boolean);
  129. }
  130. }
  131. else if (CmdMatches("update"))
  132. {
  133. addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
  134. }
  135. else if (CmdMatches("source"))
  136. {
  137. addArg('b', "compile", "APT::Get::Compile", 0);
  138. addArg('b', "build", "APT::Get::Compile", 0);
  139. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  140. addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
  141. addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
  142. addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
  143. addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
  144. }
  145. else if (CmdMatches("build-dep"))
  146. {
  147. addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
  148. addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
  149. addArg(0, "purge", "APT::Get::Purge", 0);
  150. addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
  151. // this has no effect *but* sbuild is using it (see LP: #1255806)
  152. // once sbuild is fixed, this option can be removed
  153. addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
  154. }
  155. else if (CmdMatches("files"))
  156. {
  157. addArg(0,"format","APT::Get::Files::Format", CommandLine::HasArg);
  158. addArg(0,"release-info","APT::Get::Files::ReleaseInfo", 0);
  159. }
  160. else if (CmdMatches("clean", "autoclean", "check", "download", "changelog") ||
  161. CmdMatches("markauto", "unmarkauto")) // deprecated commands
  162. ;
  163. else if (CmdMatches("moo"))
  164. addArg(0, "color", "APT::Moo::Color", 0);
  165. if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
  166. "dselect-upgrade", "autoremove", "clean", "autoclean", "check",
  167. "build-dep", "full-upgrade", "source"))
  168. {
  169. addArg('s', "simulate", "APT::Get::Simulate", 0);
  170. addArg('s', "just-print", "APT::Get::Simulate", 0);
  171. addArg('s', "recon", "APT::Get::Simulate", 0);
  172. addArg('s', "dry-run", "APT::Get::Simulate", 0);
  173. addArg('s', "no-act", "APT::Get::Simulate", 0);
  174. }
  175. bool const found_something = Args.empty() == false;
  176. // FIXME: move to the correct command(s)
  177. addArg('d',"download-only","APT::Get::Download-Only",0);
  178. addArg('y',"yes","APT::Get::Assume-Yes",0);
  179. addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
  180. addArg(0,"assume-no","APT::Get::Assume-No",0);
  181. addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
  182. addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
  183. addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
  184. addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
  185. addArg(0,"download","APT::Get::Download",0);
  186. addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
  187. addArg(0,"ignore-hold","APT::Ignore-Hold",0);
  188. addArg(0,"upgrade","APT::Get::upgrade",0);
  189. addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
  190. addArg(0,"force-yes","APT::Get::force-yes",0);
  191. addArg(0,"print-uris","APT::Get::Print-URIs",0);
  192. addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
  193. addArg(0,"remove","APT::Get::Remove",0);
  194. addArg(0,"only-source","APT::Get::Only-Source",0);
  195. addArg(0,"arch-only","APT::Get::Arch-Only",0);
  196. addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
  197. addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
  198. addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
  199. addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
  200. addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
  201. return found_something;
  202. }
  203. /*}}}*/
  204. static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  205. {
  206. if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
  207. "showmanual", "showhold", "showholds", "install",
  208. "markauto", "unmarkauto"))
  209. ;
  210. else
  211. return false;
  212. addArg('v',"verbose","APT::MarkAuto::Verbose",0);
  213. addArg('s',"simulate","APT::Mark::Simulate",0);
  214. addArg('s',"just-print","APT::Mark::Simulate",0);
  215. addArg('s',"recon","APT::Mark::Simulate",0);
  216. addArg('s',"dry-run","APT::Mark::Simulate",0);
  217. addArg('s',"no-act","APT::Mark::Simulate",0);
  218. addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
  219. return true;
  220. }
  221. /*}}}*/
  222. static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
  223. {
  224. if (CmdMatches("list"))
  225. {
  226. addArg(0,"installed","APT::Cmd::Installed",0);
  227. addArg(0,"upgradable","APT::Cmd::Upgradable",0);
  228. addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
  229. addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
  230. addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
  231. }
  232. else if (CmdMatches("show"))
  233. {
  234. addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
  235. }
  236. else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
  237. {
  238. // we have no (supported) command-name overlaps so far, so we call
  239. // specifics in order until we find one which adds arguments
  240. }
  241. else
  242. return false;
  243. return true;
  244. }
  245. /*}}}*/
  246. std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd)/*{{{*/
  247. {
  248. std::vector<CommandLine::Args> Args;
  249. Args.reserve(50);
  250. if (Program == NULL || Cmd == NULL)
  251. ; // FIXME: Invalid command supplied
  252. else if (strcmp(Cmd, "help") == 0)
  253. ; // no options for help so no need to implement it in each
  254. else if (strcmp(Program, "apt-get") == 0)
  255. addArgumentsAPTGet(Args, Cmd);
  256. else if (strcmp(Program, "apt-cache") == 0)
  257. addArgumentsAPTCache(Args, Cmd);
  258. else if (strcmp(Program, "apt-cdrom") == 0)
  259. addArgumentsAPTCDROM(Args, Cmd);
  260. else if (strcmp(Program, "apt-config") == 0)
  261. addArgumentsAPTConfig(Args, Cmd);
  262. else if (strcmp(Program, "apt-mark") == 0)
  263. addArgumentsAPTMark(Args, Cmd);
  264. else if (strcmp(Program, "apt") == 0)
  265. addArgumentsAPT(Args, Cmd);
  266. // options without a command
  267. addArg('h', "help", "help", 0);
  268. addArg('v', "version", "version", 0);
  269. // general options
  270. addArg('q', "quiet", "quiet", CommandLine::IntLevel);
  271. addArg('q', "silent", "quiet", CommandLine::IntLevel);
  272. addArg('c', "config-file", 0, CommandLine::ConfigFile);
  273. addArg('o', "option", 0, CommandLine::ArbItem);
  274. addArg(0, NULL, NULL, 0);
  275. return Args;
  276. }
  277. /*}}}*/
  278. #undef CmdMatches
  279. #undef addArg
  280. void ParseCommandLine(CommandLine &CmdL, CommandLine::Dispatch * const Cmds, CommandLine::Args * const Args,/*{{{*/
  281. Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &CmdL))
  282. {
  283. CmdL = CommandLine(Args,_config);
  284. if ((Cnf != NULL && pkgInitConfig(**Cnf) == false) ||
  285. CmdL.Parse(argc,argv) == false ||
  286. (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
  287. {
  288. if (_config->FindB("version") == true)
  289. ShowHelp(CmdL);
  290. _error->DumpErrors();
  291. exit(100);
  292. }
  293. // See if the help should be shown
  294. if (_config->FindB("help") == true || _config->FindB("version") == true)
  295. {
  296. ShowHelp(CmdL);
  297. exit(0);
  298. }
  299. if (Cmds != NULL && CmdL.FileSize() == 0)
  300. {
  301. ShowHelp(CmdL);
  302. exit(1);
  303. }
  304. }
  305. /*}}}*/