]> git.saurik.com Git - apt.git/blob - apt-private/private-cmndline.cc
a0acbbd79bd4d9406e92054b6d8265a5d836d587
[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/fileutl.h>
7 #include <apt-pkg/pkgsystem.h>
8 #include <apt-pkg/init.h>
9 #include <apt-pkg/error.h>
10
11 #include <apt-private/private-cmndline.h>
12
13 #include <vector>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include <apti18n.h>
19 /*}}}*/
20
21 APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
22 {
23 va_list args;
24 bool found = false;
25 va_start(args, Cmd);
26 char const * Match = NULL;
27 while ((Match = va_arg(args, char const *)) != NULL)
28 {
29 if (strcmp(Cmd, Match) != 0)
30 continue;
31 found = true;
32 break;
33 }
34 va_end(args);
35 return found;
36 }
37 /*}}}*/
38 #define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z))
39 #define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL)
40 static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
41 {
42 if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
43 {
44 addArg('i', "important", "APT::Cache::Important", 0);
45 addArg(0, "installed", "APT::Cache::Installed", 0);
46 addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
47 addArg(0, "depends", "APT::Cache::ShowDepends", 0);
48 addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
49 addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
50 addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
51 addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
52 addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
53 addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
54 addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
55 addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
56 }
57 else if (CmdMatches("search"))
58 {
59 addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
60 addArg('f', "full", "APT::Cache::ShowFull", 0);
61 }
62 else if (CmdMatches("show"))
63 {
64 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
65 }
66 else if (CmdMatches("pkgnames"))
67 {
68 addArg(0, "all-names", "APT::Cache::AllNames", 0);
69 }
70 else if (CmdMatches("unmet"))
71 {
72 addArg('i', "important", "APT::Cache::Important", 0);
73 }
74 else if (CmdMatches("showsrc"))
75 {
76 addArg(0,"only-source","APT::Cache::Only-Source",0);
77 }
78 else if (CmdMatches("gencaches", "showpkg", "stats", "dump",
79 "dumpavail", "showauto", "policy", "madison"))
80 ;
81 else
82 return false;
83
84 bool const found_something = Args.empty() == false;
85
86 // FIXME: move to the correct command(s)
87 addArg('g', "generate", "APT::Cache::Generate", 0);
88 addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
89 addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
90
91 addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
92 addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
93
94 return found_something;
95 }
96 /*}}}*/
97 static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
98 {
99 if (CmdMatches("add", "ident") == false)
100 return false;
101
102 // FIXME: move to the correct command(s)
103 addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
104 addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
105 addArg('r', "rename", "APT::CDROM::Rename", 0);
106 addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
107 addArg('f', "fast", "APT::CDROM::Fast", 0);
108 addArg('n', "just-print", "APT::CDROM::NoAct", 0);
109 addArg('n', "recon", "APT::CDROM::NoAct", 0);
110 addArg('n', "no-act", "APT::CDROM::NoAct", 0);
111 addArg('a', "thorough", "APT::CDROM::Thorough", 0);
112 return true;
113 }
114 /*}}}*/
115 static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
116 {
117 if (CmdMatches("dump"))
118 {
119 addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
120 addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
121 }
122 else if (CmdMatches("shell"))
123 ;
124 else
125 return false;
126
127 return true;
128 }
129 /*}}}*/
130 static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
131 {
132 addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
133 return true;
134 }
135 /*}}}*/
136 static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
137 {
138 addArg(0,"md5","APT::FTPArchive::MD5",0);
139 addArg(0,"sha1","APT::FTPArchive::SHA1",0);
140 addArg(0,"sha256","APT::FTPArchive::SHA256",0);
141 addArg(0,"sha512","APT::FTPArchive::SHA512",0);
142 addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
143 addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
144 addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
145 addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
146 addArg(0,"contents","APT::FTPArchive::Contents",0);
147 addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
148 return true;
149 }
150 /*}}}*/
151 static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
152 {
153 return true;
154 }
155 /*}}}*/
156 static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
157 {
158 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
159 "dselect-upgrade", "autoremove", "full-upgrade"))
160 {
161 addArg(0, "show-progress", "DpkgPM::Progress", 0);
162 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
163 addArg(0, "purge", "APT::Get::Purge", 0);
164 addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
165 addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
166 addArg(0, "reinstall", "APT::Get::ReInstall", 0);
167 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
168 if (CmdMatches("upgrade"))
169 {
170 addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
171 CommandLine::Boolean);
172 }
173 }
174 else if (CmdMatches("update"))
175 {
176 addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
177 }
178 else if (CmdMatches("source"))
179 {
180 addArg('b', "compile", "APT::Get::Compile", 0);
181 addArg('b', "build", "APT::Get::Compile", 0);
182 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
183 addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
184 addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
185 addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
186 addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
187 }
188 else if (CmdMatches("build-dep"))
189 {
190 addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
191 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
192 addArg(0, "purge", "APT::Get::Purge", 0);
193 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
194 // this has no effect *but* sbuild is using it (see LP: #1255806)
195 // once sbuild is fixed, this option can be removed
196 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
197 }
198 else if (CmdMatches("indextargets"))
199 {
200 addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
201 addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
202 }
203 else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
204 CmdMatches("markauto", "unmarkauto")) // deprecated commands
205 ;
206 else if (CmdMatches("moo"))
207 addArg(0, "color", "APT::Moo::Color", 0);
208
209 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
210 "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
211 "build-dep", "full-upgrade", "source"))
212 {
213 addArg('s', "simulate", "APT::Get::Simulate", 0);
214 addArg('s', "just-print", "APT::Get::Simulate", 0);
215 addArg('s', "recon", "APT::Get::Simulate", 0);
216 addArg('s', "dry-run", "APT::Get::Simulate", 0);
217 addArg('s', "no-act", "APT::Get::Simulate", 0);
218 }
219
220 bool const found_something = Args.empty() == false;
221
222 // FIXME: move to the correct command(s)
223 addArg('d',"download-only","APT::Get::Download-Only",0);
224 addArg('y',"yes","APT::Get::Assume-Yes",0);
225 addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
226 addArg(0,"assume-no","APT::Get::Assume-No",0);
227 addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
228 addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
229 addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
230 addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
231 addArg(0,"download","APT::Get::Download",0);
232 addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
233 addArg(0,"ignore-hold","APT::Ignore-Hold",0);
234 addArg(0,"upgrade","APT::Get::upgrade",0);
235 addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
236 addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
237 addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
238 addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
239 addArg(0,"force-yes","APT::Get::force-yes",0);
240 addArg(0,"print-uris","APT::Get::Print-URIs",0);
241 addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
242 addArg(0,"remove","APT::Get::Remove",0);
243 addArg(0,"only-source","APT::Get::Only-Source",0);
244 addArg(0,"arch-only","APT::Get::Arch-Only",0);
245 addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
246 addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
247 addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
248 addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
249 addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
250
251 return found_something;
252 }
253 /*}}}*/
254 static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
255 {
256 if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
257 "showmanual", "showhold", "showholds",
258 "markauto", "unmarkauto"))
259 {
260 addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
261 }
262 else if (CmdMatches("install", "remove", "deinstall", "purge",
263 "showinstall", "showinstalls", "showremove", "showremoves",
264 "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
265 ;
266 else
267 return false;
268
269 if (CmdMatches("markauto", "unmarkauto"))
270 {
271 addArg('v',"verbose","APT::MarkAuto::Verbose",0);
272 }
273
274 if (strncmp(Cmd, "show", strlen("show")) != 0)
275 {
276 addArg('s',"simulate","APT::Mark::Simulate",0);
277 addArg('s',"just-print","APT::Mark::Simulate",0);
278 addArg('s',"recon","APT::Mark::Simulate",0);
279 addArg('s',"dry-run","APT::Mark::Simulate",0);
280 addArg('s',"no-act","APT::Mark::Simulate",0);
281 }
282
283 return true;
284 }
285 /*}}}*/
286 static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
287 {
288 addArg('s',"source","APT::SortPkgs::Source",0);
289 return true;
290 }
291 /*}}}*/
292 static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
293 {
294 if (CmdMatches("list"))
295 {
296 addArg('i',"installed","APT::Cmd::Installed",0);
297 addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
298 addArg('u',"upgradable","APT::Cmd::Upgradable",0);
299 addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
300 addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
301 addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
302 }
303 else if (CmdMatches("show"))
304 {
305 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
306 }
307 else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
308 {
309 // we have no (supported) command-name overlaps so far, so we call
310 // specifics in order until we find one which adds arguments
311 }
312 else
313 return false;
314
315 return true;
316 }
317 /*}}}*/
318 std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd)/*{{{*/
319 {
320 std::vector<CommandLine::Args> Args;
321 Args.reserve(50);
322 if (Program == NULL || Cmd == NULL)
323 ; // FIXME: Invalid command supplied
324 else if (strcmp(Cmd, "help") == 0)
325 ; // no options for help so no need to implement it in each
326 else if (strcmp(Program, "apt-get") == 0)
327 addArgumentsAPTGet(Args, Cmd);
328 else if (strcmp(Program, "apt-cache") == 0)
329 addArgumentsAPTCache(Args, Cmd);
330 else if (strcmp(Program, "apt-cdrom") == 0)
331 addArgumentsAPTCDROM(Args, Cmd);
332 else if (strcmp(Program, "apt-config") == 0)
333 addArgumentsAPTConfig(Args, Cmd);
334 else if (strcmp(Program, "apt-extracttemplates") == 0)
335 addArgumentsAPTExtractTemplates(Args, Cmd);
336 else if (strcmp(Program, "apt-ftparchive") == 0)
337 addArgumentsAPTFTPArchive(Args, Cmd);
338 else if (strcmp(Program, "apt-internal-solver") == 0)
339 addArgumentsAPTInternalSolver(Args, Cmd);
340 else if (strcmp(Program, "apt-mark") == 0)
341 addArgumentsAPTMark(Args, Cmd);
342 else if (strcmp(Program, "apt-sortpkg") == 0)
343 addArgumentsAPTSortPkgs(Args, Cmd);
344 else if (strcmp(Program, "apt") == 0)
345 addArgumentsAPT(Args, Cmd);
346
347 // options without a command
348 addArg('h', "help", "help", 0);
349 addArg('v', "version", "version", 0);
350 // general options
351 addArg('q', "quiet", "quiet", CommandLine::IntLevel);
352 addArg('q', "silent", "quiet", CommandLine::IntLevel);
353 addArg('c', "config-file", 0, CommandLine::ConfigFile);
354 addArg('o', "option", 0, CommandLine::ArbItem);
355 addArg(0, NULL, NULL, 0);
356
357 return Args;
358 }
359 /*}}}*/
360 #undef CmdMatches
361 #undef addArg
362 static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
363 {
364 std::string const binary = flNotDir(Binary);
365 if (binary == "apt" || binary == "apt-config")
366 {
367 _config->CndSet("Binary::apt::APT::Color", true);
368 _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
369 _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
370 _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
371 _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
372 _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
373 _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
374 _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
375 _config->CndSet("Binary::apt::Acquire::AllowInsecureRepositories", false);
376 }
377
378 _config->Set("Binary", binary);
379 std::string const conf = "Binary::" + binary;
380 _config->MoveSubTree(conf.c_str(), NULL);
381 }
382 /*}}}*/
383 void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, char const * const Binary,/*{{{*/
384 Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &, CommandLine::DispatchWithHelp const *))
385 {
386 // Args running out of scope invalidates the pointer stored in CmdL,
387 // but we don't use the pointer after this function, so we ignore
388 // this problem for now and figure something out if we have to.
389 std::vector<CommandLine::Args> Args;
390 if (Cmds != nullptr && Cmds[0].Handler != nullptr)
391 Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds, argc, argv));
392 else
393 Args = getCommandArgs(Binary, Binary);
394 CmdL = CommandLine(Args.data(), _config);
395
396 if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
397 {
398 _error->DumpErrors();
399 exit(100);
400 }
401
402 if (likely(argc != 0 && argv[0] != NULL))
403 BinarySpecificConfiguration(argv[0]);
404
405 if (CmdL.Parse(argc,argv) == false ||
406 (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
407 {
408 if (_config->FindB("version") == true)
409 ShowHelp(CmdL, Cmds);
410
411 _error->DumpErrors();
412 exit(100);
413 }
414
415 // See if the help should be shown
416 if (_config->FindB("help") == true || _config->FindB("version") == true ||
417 (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
418 {
419 ShowHelp(CmdL, Cmds);
420 exit(0);
421 }
422 if (Cmds != nullptr && CmdL.FileSize() == 0)
423 {
424 ShowHelp(CmdL, Cmds);
425 exit(1);
426 }
427 }
428 /*}}}*/
429 unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds) /*{{{*/
430 {
431 // Match the operation
432 bool const returned = (Cmds != nullptr) ? CmdL.DispatchArg(Cmds) : true;
433
434 // Print any errors or warnings found during parsing
435 bool const Errors = _error->PendingError();
436 if (_config->FindI("quiet",0) > 0)
437 _error->DumpErrors();
438 else
439 _error->DumpErrors(GlobalError::DEBUG);
440 if (returned == false)
441 return 100;
442 return Errors == true ? 100 : 0;
443 }
444 /*}}}*/