private-cmndline.cc 10 KB

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