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