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