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