private-cmndline.cc 9.6 KB

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