]> git.saurik.com Git - apt.git/blame - apt-private/private-cmndline.cc
generate commands array after config is loaded
[apt.git] / apt-private / private-cmndline.cc
CommitLineData
b9179170
MV
1// Include Files /*{{{*/
2#include <config.h>
3
4#include <apt-pkg/cmndline.h>
ad7e0941 5#include <apt-pkg/configuration.h>
c3ded84c 6#include <apt-pkg/fileutl.h>
ad7e0941
DK
7#include <apt-pkg/pkgsystem.h>
8#include <apt-pkg/init.h>
9#include <apt-pkg/error.h>
b9179170 10
453b82a3 11#include <apt-private/private-cmndline.h>
b9179170 12
453b82a3 13#include <vector>
b9179170
MV
14#include <stdarg.h>
15#include <string.h>
ad7e0941 16#include <stdlib.h>
b9179170 17
b9179170
MV
18#include <apti18n.h>
19 /*}}}*/
20
a02db58f 21APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
b9179170
MV
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)
c3ccac92 40static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
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);
8c7af4d4 55 addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
b9179170
MV
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 }
58c2833f
MV
70 else if (CmdMatches("unmet"))
71 {
72 addArg('i', "important", "APT::Cache::Important", 0);
73 }
a8275acf
MV
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",
58c2833f 79 "dumpavail", "showauto", "policy", "madison"))
b9179170
MV
80 ;
81 else
82 return false;
83
859093da
DK
84 bool const found_something = Args.empty() == false;
85
b9179170
MV
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);
859093da
DK
93
94 return found_something;
b9179170
MV
95}
96 /*}}}*/
c3ccac92 97static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
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 /*}}}*/
c3ccac92 115static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
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
e7e10e47
DK
127 return true;
128}
129 /*}}}*/
130static 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 /*}}}*/
136static 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 /*}}}*/
151static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
011188e3
DK
152{
153 return true;
154}
155 /*}}}*/
156static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
e7e10e47 157{
b9179170
MV
158 return true;
159}
160 /*}}}*/
c3ccac92 161static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
162{
163 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
ebe24b7a 164 "dselect-upgrade", "autoremove", "full-upgrade"))
b9179170 165 {
4fb66d87 166 addArg(0, "show-progress", "DpkgPM::Progress", 0);
b9179170
MV
167 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
168 addArg(0, "purge", "APT::Get::Purge", 0);
169 addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
170 addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
171 addArg(0, "reinstall", "APT::Get::ReInstall", 0);
172 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
173 if (CmdMatches("upgrade"))
174 {
2004d647 175 addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
c678a044 176 CommandLine::Boolean);
b9179170
MV
177 }
178 }
179 else if (CmdMatches("update"))
180 {
181 addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
182 }
183 else if (CmdMatches("source"))
184 {
185 addArg('b', "compile", "APT::Get::Compile", 0);
186 addArg('b', "build", "APT::Get::Compile", 0);
ce7f128c 187 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
b9179170
MV
188 addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
189 addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
190 addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
191 addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
192 }
193 else if (CmdMatches("build-dep"))
194 {
195 addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
ce7f128c 196 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
e4e5d47b 197 addArg(0, "purge", "APT::Get::Purge", 0);
b9179170 198 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
c872ac50
MV
199 // this has no effect *but* sbuild is using it (see LP: #1255806)
200 // once sbuild is fixed, this option can be removed
201 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
b9179170 202 }
c2a4a8dd 203 else if (CmdMatches("indextargets"))
8881b11e 204 {
c2a4a8dd
DK
205 addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
206 addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
8881b11e 207 }
f66738d7 208 else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
b9179170
MV
209 CmdMatches("markauto", "unmarkauto")) // deprecated commands
210 ;
211 else if (CmdMatches("moo"))
212 addArg(0, "color", "APT::Moo::Color", 0);
213
214 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
f66738d7 215 "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
f83b5627 216 "build-dep", "full-upgrade", "source"))
b9179170
MV
217 {
218 addArg('s', "simulate", "APT::Get::Simulate", 0);
219 addArg('s', "just-print", "APT::Get::Simulate", 0);
220 addArg('s', "recon", "APT::Get::Simulate", 0);
221 addArg('s', "dry-run", "APT::Get::Simulate", 0);
222 addArg('s', "no-act", "APT::Get::Simulate", 0);
223 }
224
859093da
DK
225 bool const found_something = Args.empty() == false;
226
b9179170
MV
227 // FIXME: move to the correct command(s)
228 addArg('d',"download-only","APT::Get::Download-Only",0);
229 addArg('y',"yes","APT::Get::Assume-Yes",0);
230 addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
231 addArg(0,"assume-no","APT::Get::Assume-No",0);
232 addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
233 addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
234 addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
235 addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
236 addArg(0,"download","APT::Get::Download",0);
237 addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
238 addArg(0,"ignore-hold","APT::Ignore-Hold",0);
239 addArg(0,"upgrade","APT::Get::upgrade",0);
240 addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
b381a482
JAK
241 addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
242 addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
243 addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
b9179170
MV
244 addArg(0,"force-yes","APT::Get::force-yes",0);
245 addArg(0,"print-uris","APT::Get::Print-URIs",0);
246 addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
247 addArg(0,"remove","APT::Get::Remove",0);
248 addArg(0,"only-source","APT::Get::Only-Source",0);
249 addArg(0,"arch-only","APT::Get::Arch-Only",0);
250 addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
c99fe2e1 251 addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
b9179170
MV
252 addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
253 addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
254 addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
255
859093da 256 return found_something;
b9179170
MV
257}
258 /*}}}*/
c3ccac92 259static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
260{
261 if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
64e3414e 262 "showmanual", "showhold", "showholds",
b9179170 263 "markauto", "unmarkauto"))
64e3414e
DK
264 {
265 addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
266 }
267 else if (CmdMatches("install", "remove", "deinstall", "purge",
268 "showinstall", "showinstalls", "showremove", "showremoves",
269 "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
b9179170
MV
270 ;
271 else
272 return false;
273
64e3414e
DK
274 if (CmdMatches("markauto", "unmarkauto"))
275 {
276 addArg('v',"verbose","APT::MarkAuto::Verbose",0);
277 }
278
279 if (strncmp(Cmd, "show", strlen("show")) != 0)
280 {
281 addArg('s',"simulate","APT::Mark::Simulate",0);
282 addArg('s',"just-print","APT::Mark::Simulate",0);
283 addArg('s',"recon","APT::Mark::Simulate",0);
284 addArg('s',"dry-run","APT::Mark::Simulate",0);
285 addArg('s',"no-act","APT::Mark::Simulate",0);
286 }
b9179170 287
e7e10e47
DK
288 return true;
289}
290 /*}}}*/
291static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
292{
293 addArg('s',"source","APT::SortPkgs::Source",0);
b9179170
MV
294 return true;
295}
296 /*}}}*/
c3ccac92 297static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
298{
299 if (CmdMatches("list"))
300 {
d2c712a1 301 addArg('i',"installed","APT::Cmd::Installed",0);
df7c9fd2 302 addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
d2c712a1 303 addArg('u',"upgradable","APT::Cmd::Upgradable",0);
3bdf7da5 304 addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
e1dc051a 305 addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
c1a61d1c 306 addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
b9179170 307 }
06293aa7
MV
308 else if (CmdMatches("show"))
309 {
310 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
311 }
b9179170
MV
312 else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
313 {
314 // we have no (supported) command-name overlaps so far, so we call
315 // specifics in order until we find one which adds arguments
316 }
317 else
318 return false;
319
320 return true;
321}
322 /*}}}*/
011188e3 323std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
b9179170
MV
324{
325 std::vector<CommandLine::Args> Args;
326 Args.reserve(50);
011188e3
DK
327 if (Cmd == nullptr)
328 {
329 if (Program == APT_CMD::APT_EXTRACTTEMPLATES)
330 addArgumentsAPTExtractTemplates(Args, Cmd);
331 }
b9179170
MV
332 else if (strcmp(Cmd, "help") == 0)
333 ; // no options for help so no need to implement it in each
011188e3
DK
334 else
335 switch (Program)
336 {
337 case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
338 case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
339 case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
340 case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
341 case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
342 case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
343 case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
344 case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
345 case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
346 case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
347 case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
348 }
b9179170
MV
349
350 // options without a command
351 addArg('h', "help", "help", 0);
352 addArg('v', "version", "version", 0);
353 // general options
354 addArg('q', "quiet", "quiet", CommandLine::IntLevel);
355 addArg('q', "silent", "quiet", CommandLine::IntLevel);
356 addArg('c', "config-file", 0, CommandLine::ConfigFile);
357 addArg('o', "option", 0, CommandLine::ArbItem);
358 addArg(0, NULL, NULL, 0);
359
360 return Args;
361}
362 /*}}}*/
363#undef CmdMatches
364#undef addArg
501cd23e
DK
365static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
366{
367 std::string const binary = flNotDir(Binary);
368 if (binary == "apt" || binary == "apt-config")
369 {
631800b1 370 _config->CndSet("Binary::apt::APT::Color", true);
501cd23e
DK
371 _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
372 _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
373 _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
631800b1
DK
374 _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
375 _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
376 _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
377 _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
995a4bf6 378 _config->CndSet("Binary::apt::Acquire::AllowInsecureRepositories", false);
501cd23e
DK
379 }
380
381 _config->Set("Binary", binary);
382 std::string const conf = "Binary::" + binary;
383 _config->MoveSubTree(conf.c_str(), NULL);
384}
385 /*}}}*/
011188e3
DK
386std::vector<CommandLine::DispatchWithHelp> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
387 Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[])
ad7e0941 388{
c3ded84c
DK
389 if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
390 {
391 _error->DumpErrors();
392 exit(100);
393 }
394
395 if (likely(argc != 0 && argv[0] != NULL))
501cd23e 396 BinarySpecificConfiguration(argv[0]);
c3ded84c 397
011188e3
DK
398 std::vector<CommandLine::DispatchWithHelp> const Cmds = GetCommands();
399 // Args running out of scope invalidates the pointer stored in CmdL,
400 // but we don't use the pointer after this function, so we ignore
401 // this problem for now and figure something out if we have to.
402 std::vector<CommandLine::Args> Args;
403 if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
404 Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds.data(), argc, argv));
405 else
406 Args = getCommandArgs(Binary, nullptr);
407 CmdL = CommandLine(Args.data(), _config);
408
c3ded84c 409 if (CmdL.Parse(argc,argv) == false ||
ad7e0941
DK
410 (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
411 {
412 if (_config->FindB("version") == true)
011188e3 413 ShowHelp(CmdL, Cmds.data());
ad7e0941
DK
414
415 _error->DumpErrors();
416 exit(100);
417 }
418
419 // See if the help should be shown
cbbee23e
DK
420 if (_config->FindB("help") == true || _config->FindB("version") == true ||
421 (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
ad7e0941 422 {
011188e3 423 ShowHelp(CmdL, Cmds.data());
ad7e0941
DK
424 exit(0);
425 }
011188e3 426 if (Cmds.empty() == false && CmdL.FileSize() == 0)
ad7e0941 427 {
011188e3 428 ShowHelp(CmdL, Cmds.data());
ad7e0941
DK
429 exit(1);
430 }
011188e3 431 return Cmds;
ad7e0941
DK
432}
433 /*}}}*/
011188e3 434unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::DispatchWithHelp> const &Cmds) /*{{{*/
e7e10e47
DK
435{
436 // Match the operation
011188e3 437 bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
e7e10e47
DK
438
439 // Print any errors or warnings found during parsing
440 bool const Errors = _error->PendingError();
441 if (_config->FindI("quiet",0) > 0)
442 _error->DumpErrors();
443 else
444 _error->DumpErrors(GlobalError::DEBUG);
445 if (returned == false)
446 return 100;
447 return Errors == true ? 100 : 0;
448}
449 /*}}}*/