]> git.saurik.com Git - apt.git/blame - apt-private/private-cmndline.cc
generalize secure->insecure downgrade protection
[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>
8561c2fe 10#include <apt-pkg/strutl.h>
b9179170 11
453b82a3 12#include <apt-private/private-cmndline.h>
570ec96d 13#include <apt-private/private-main.h>
b9179170
MV
14
15#include <stdarg.h>
16#include <string.h>
ad7e0941 17#include <stdlib.h>
b9179170 18
8561c2fe
DK
19#include <vector>
20#include <iomanip>
21
b9179170
MV
22#include <apti18n.h>
23 /*}}}*/
24
a02db58f 25APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
b9179170 26{
007d8b48
DK
27 if (Cmd == nullptr)
28 return false;
b9179170
MV
29 va_list args;
30 bool found = false;
31 va_start(args, Cmd);
32 char const * Match = NULL;
33 while ((Match = va_arg(args, char const *)) != NULL)
34 {
35 if (strcmp(Cmd, Match) != 0)
36 continue;
37 found = true;
38 break;
39 }
40 va_end(args);
41 return found;
42}
43 /*}}}*/
44#define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z))
45#define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL)
c3ccac92 46static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
47{
48 if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
49 {
50 addArg('i', "important", "APT::Cache::Important", 0);
51 addArg(0, "installed", "APT::Cache::Installed", 0);
52 addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
53 addArg(0, "depends", "APT::Cache::ShowDepends", 0);
54 addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
55 addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
56 addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
57 addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
58 addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
59 addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
60 addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
8c7af4d4 61 addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
b9179170
MV
62 }
63 else if (CmdMatches("search"))
64 {
65 addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
66 addArg('f', "full", "APT::Cache::ShowFull", 0);
67 }
68 else if (CmdMatches("show"))
69 {
70 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
71 }
72 else if (CmdMatches("pkgnames"))
73 {
74 addArg(0, "all-names", "APT::Cache::AllNames", 0);
75 }
58c2833f
MV
76 else if (CmdMatches("unmet"))
77 {
78 addArg('i', "important", "APT::Cache::Important", 0);
79 }
a8275acf
MV
80 else if (CmdMatches("showsrc"))
81 {
82 addArg(0,"only-source","APT::Cache::Only-Source",0);
83 }
84 else if (CmdMatches("gencaches", "showpkg", "stats", "dump",
58c2833f 85 "dumpavail", "showauto", "policy", "madison"))
b9179170
MV
86 ;
87 else
88 return false;
89
859093da
DK
90 bool const found_something = Args.empty() == false;
91
b9179170
MV
92 // FIXME: move to the correct command(s)
93 addArg('g', "generate", "APT::Cache::Generate", 0);
94 addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
95 addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
96
97 addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
98 addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
859093da
DK
99
100 return found_something;
b9179170
MV
101}
102 /*}}}*/
c3ccac92 103static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
104{
105 if (CmdMatches("add", "ident") == false)
106 return false;
107
108 // FIXME: move to the correct command(s)
109 addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
110 addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
111 addArg('r', "rename", "APT::CDROM::Rename", 0);
112 addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
113 addArg('f', "fast", "APT::CDROM::Fast", 0);
114 addArg('n', "just-print", "APT::CDROM::NoAct", 0);
115 addArg('n', "recon", "APT::CDROM::NoAct", 0);
116 addArg('n', "no-act", "APT::CDROM::NoAct", 0);
117 addArg('a', "thorough", "APT::CDROM::Thorough", 0);
118 return true;
119}
120 /*}}}*/
c3ccac92 121static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
122{
123 if (CmdMatches("dump"))
124 {
125 addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
126 addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
127 }
128 else if (CmdMatches("shell"))
129 ;
130 else
131 return false;
132
385d9f2f
DK
133 return true;
134}
135 /*}}}*/
007d8b48 136static bool addArgumentsAPTDumpSolver(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
385d9f2f 137{
007d8b48 138 addArg(0,"user","APT::Solver::RunAsUser",CommandLine::HasArg);
e7e10e47
DK
139 return true;
140}
141 /*}}}*/
142static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
143{
144 addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
145 return true;
146}
147 /*}}}*/
148static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
149{
150 addArg(0,"md5","APT::FTPArchive::MD5",0);
151 addArg(0,"sha1","APT::FTPArchive::SHA1",0);
152 addArg(0,"sha256","APT::FTPArchive::SHA256",0);
153 addArg(0,"sha512","APT::FTPArchive::SHA512",0);
154 addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
155 addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
156 addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
157 addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
158 addArg(0,"contents","APT::FTPArchive::Contents",0);
159 addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
160 return true;
161}
162 /*}}}*/
163static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
011188e3
DK
164{
165 return true;
166}
167 /*}}}*/
df46a87a 168static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
e7e10e47 169{
df46a87a
JAK
170 if (CmdMatches("cat-file"))
171 {
172 addArg('C', "compress", "Apt-Helper::Cat-File::Compress",CommandLine::HasArg);
173 }
b9179170
MV
174 return true;
175}
176 /*}}}*/
c3ccac92 177static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
178{
179 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
ebe24b7a 180 "dselect-upgrade", "autoremove", "full-upgrade"))
b9179170 181 {
4fb66d87 182 addArg(0, "show-progress", "DpkgPM::Progress", 0);
b9179170
MV
183 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
184 addArg(0, "purge", "APT::Get::Purge", 0);
185 addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
186 addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
187 addArg(0, "reinstall", "APT::Get::ReInstall", 0);
188 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
189 if (CmdMatches("upgrade"))
190 {
2004d647 191 addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
c678a044 192 CommandLine::Boolean);
b9179170
MV
193 }
194 }
195 else if (CmdMatches("update"))
196 {
197 addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
198 }
199 else if (CmdMatches("source"))
200 {
201 addArg('b', "compile", "APT::Get::Compile", 0);
202 addArg('b', "build", "APT::Get::Compile", 0);
ce7f128c 203 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
b9179170
MV
204 addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
205 addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
206 addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
207 addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
208 }
209 else if (CmdMatches("build-dep"))
210 {
211 addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
ce7f128c 212 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
e4e5d47b 213 addArg(0, "purge", "APT::Get::Purge", 0);
b9179170 214 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
c872ac50
MV
215 // this has no effect *but* sbuild is using it (see LP: #1255806)
216 // once sbuild is fixed, this option can be removed
217 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
b9179170 218 }
c2a4a8dd 219 else if (CmdMatches("indextargets"))
8881b11e 220 {
c2a4a8dd
DK
221 addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
222 addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
8881b11e 223 }
f66738d7 224 else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
b9179170
MV
225 CmdMatches("markauto", "unmarkauto")) // deprecated commands
226 ;
227 else if (CmdMatches("moo"))
228 addArg(0, "color", "APT::Moo::Color", 0);
229
230 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
f66738d7 231 "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
f83b5627 232 "build-dep", "full-upgrade", "source"))
b9179170
MV
233 {
234 addArg('s', "simulate", "APT::Get::Simulate", 0);
235 addArg('s', "just-print", "APT::Get::Simulate", 0);
236 addArg('s', "recon", "APT::Get::Simulate", 0);
237 addArg('s', "dry-run", "APT::Get::Simulate", 0);
238 addArg('s', "no-act", "APT::Get::Simulate", 0);
239 }
240
859093da
DK
241 bool const found_something = Args.empty() == false;
242
b9179170
MV
243 // FIXME: move to the correct command(s)
244 addArg('d',"download-only","APT::Get::Download-Only",0);
245 addArg('y',"yes","APT::Get::Assume-Yes",0);
246 addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
247 addArg(0,"assume-no","APT::Get::Assume-No",0);
248 addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
249 addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
250 addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
251 addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
252 addArg(0,"download","APT::Get::Download",0);
253 addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
254 addArg(0,"ignore-hold","APT::Ignore-Hold",0);
255 addArg(0,"upgrade","APT::Get::upgrade",0);
256 addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
b381a482
JAK
257 addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
258 addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
259 addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
b9179170
MV
260 addArg(0,"force-yes","APT::Get::force-yes",0);
261 addArg(0,"print-uris","APT::Get::Print-URIs",0);
262 addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
263 addArg(0,"remove","APT::Get::Remove",0);
264 addArg(0,"only-source","APT::Get::Only-Source",0);
265 addArg(0,"arch-only","APT::Get::Arch-Only",0);
266 addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
c99fe2e1 267 addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
b9179170
MV
268 addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
269 addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
270 addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
271
859093da 272 return found_something;
b9179170
MV
273}
274 /*}}}*/
c3ccac92 275static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
276{
277 if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
64e3414e 278 "showmanual", "showhold", "showholds",
b9179170 279 "markauto", "unmarkauto"))
64e3414e
DK
280 {
281 addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
282 }
283 else if (CmdMatches("install", "remove", "deinstall", "purge",
284 "showinstall", "showinstalls", "showremove", "showremoves",
285 "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
b9179170
MV
286 ;
287 else
288 return false;
289
64e3414e
DK
290 if (CmdMatches("markauto", "unmarkauto"))
291 {
292 addArg('v',"verbose","APT::MarkAuto::Verbose",0);
293 }
294
295 if (strncmp(Cmd, "show", strlen("show")) != 0)
296 {
297 addArg('s',"simulate","APT::Mark::Simulate",0);
298 addArg('s',"just-print","APT::Mark::Simulate",0);
299 addArg('s',"recon","APT::Mark::Simulate",0);
300 addArg('s',"dry-run","APT::Mark::Simulate",0);
301 addArg('s',"no-act","APT::Mark::Simulate",0);
302 }
b9179170 303
e7e10e47
DK
304 return true;
305}
306 /*}}}*/
307static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
308{
309 addArg('s',"source","APT::SortPkgs::Source",0);
b9179170
MV
310 return true;
311}
312 /*}}}*/
c3ccac92 313static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
314{
315 if (CmdMatches("list"))
316 {
d2c712a1 317 addArg('i',"installed","APT::Cmd::Installed",0);
df7c9fd2 318 addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
d2c712a1 319 addArg('u',"upgradable","APT::Cmd::Upgradable",0);
3bdf7da5 320 addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
e1dc051a 321 addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
c1a61d1c 322 addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
b9179170 323 }
06293aa7
MV
324 else if (CmdMatches("show"))
325 {
326 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
327 }
b9179170
MV
328 else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
329 {
330 // we have no (supported) command-name overlaps so far, so we call
331 // specifics in order until we find one which adds arguments
332 }
333 else
334 return false;
335
336 return true;
337}
338 /*}}}*/
011188e3 339std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
b9179170
MV
340{
341 std::vector<CommandLine::Args> Args;
342 Args.reserve(50);
007d8b48 343 if (Cmd != nullptr && strcmp(Cmd, "help") == 0)
b9179170 344 ; // no options for help so no need to implement it in each
011188e3
DK
345 else
346 switch (Program)
347 {
348 case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
349 case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
350 case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
351 case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
352 case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
385d9f2f 353 case APT_CMD::APT_DUMP_SOLVER: addArgumentsAPTDumpSolver(Args, Cmd); break;
011188e3
DK
354 case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
355 case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
356 case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
357 case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
358 case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
359 case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
360 }
b9179170
MV
361
362 // options without a command
363 addArg('h', "help", "help", 0);
364 addArg('v', "version", "version", 0);
365 // general options
366 addArg('q', "quiet", "quiet", CommandLine::IntLevel);
367 addArg('q', "silent", "quiet", CommandLine::IntLevel);
368 addArg('c', "config-file", 0, CommandLine::ConfigFile);
369 addArg('o', "option", 0, CommandLine::ArbItem);
370 addArg(0, NULL, NULL, 0);
371
372 return Args;
373}
374 /*}}}*/
375#undef CmdMatches
376#undef addArg
8561c2fe 377static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
41d39345 378{
8561c2fe
DK
379 if (Cmds.empty() || Cmds[0].Match == nullptr)
380 return;
381 std::cout << std::endl << _("Most used commands:") << std::endl;
382 for (auto const &c: Cmds)
383 {
384 if (c.Help == nullptr)
385 continue;
386 std::cout << " " << c.Match << " - " << c.Help << std::endl;
387 }
41d39345
DK
388}
389 /*}}}*/
90986d4d
DK
390static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
391 bool (*ShowHelp)(CommandLine &))
41d39345 392{
8561c2fe
DK
393 std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
394 if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
395 return true;
396 if (ShowHelp(CmdL) == false)
397 return false;
398 if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
399 return true;
400 ShowHelpListCommands(Cmds);
401 std::cout << std::endl;
402 char const * cmd = nullptr;
403 switch (Binary)
41d39345 404 {
8561c2fe
DK
405 case APT_CMD::APT: cmd = "apt(8)"; break;
406 case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
407 case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
408 case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
385d9f2f 409 case APT_CMD::APT_DUMP_SOLVER: cmd = nullptr; break;
8561c2fe
DK
410 case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
411 case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
412 case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
413 case APT_CMD::APT_HELPER: cmd = nullptr; break;
414 case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
415 case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
416 case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
41d39345 417 }
8561c2fe
DK
418 if (cmd != nullptr)
419 ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
385d9f2f
DK
420 if (Binary != APT_CMD::APT_DUMP_SOLVER && Binary != APT_CMD::APT_INTERNAL_SOLVER)
421 std::cout << std::endl <<
422 _("Configuration options and syntax is detailed in apt.conf(5).\n"
423 "Information about how to configure sources can be found in sources.list(5).\n"
424 "Package and version choices can be expressed via apt_preferences(5).\n"
425 "Security details are available in apt-secure(8).\n");
8561c2fe
DK
426 if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
427 std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
385d9f2f 428 else if (Binary == APT_CMD::APT_HELPER || Binary == APT_CMD::APT_DUMP_SOLVER)
8561c2fe
DK
429 std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
430 return true;
41d39345
DK
431}
432 /*}}}*/
501cd23e
DK
433static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
434{
435 std::string const binary = flNotDir(Binary);
436 if (binary == "apt" || binary == "apt-config")
437 {
631800b1 438 _config->CndSet("Binary::apt::APT::Color", true);
501cd23e
DK
439 _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
440 _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
441 _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
631800b1 442 _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
c094c868
DK
443 _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
444 _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
631800b1
DK
445 _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
446 _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
447 _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
995a4bf6 448 _config->CndSet("Binary::apt::Acquire::AllowInsecureRepositories", false);
ee02b5b3 449 _config->CndSet("Binary::apt::APT::Keep-Downloaded-Packages", false);
501cd23e
DK
450 }
451
452 _config->Set("Binary", binary);
453 std::string const conf = "Binary::" + binary;
454 _config->MoveSubTree(conf.c_str(), NULL);
455}
456 /*}}}*/
6079b276 457std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
90986d4d
DK
458 Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
459 bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
ad7e0941 460{
570ec96d 461 InitLocale(Binary);
c3ded84c
DK
462 if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
463 {
464 _error->DumpErrors();
465 exit(100);
466 }
467
468 if (likely(argc != 0 && argv[0] != NULL))
501cd23e 469 BinarySpecificConfiguration(argv[0]);
c3ded84c 470
6079b276
DK
471 std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
472 std::vector<CommandLine::Dispatch> Cmds;
41d39345
DK
473 if (CmdsWithHelp.empty() == false)
474 {
475 CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
476 Cmds.push_back(std::move(help));
477 }
6079b276
DK
478 for (auto const& cmd : CmdsWithHelp)
479 Cmds.push_back({cmd.Match, cmd.Handler});
480
011188e3
DK
481 // Args running out of scope invalidates the pointer stored in CmdL,
482 // but we don't use the pointer after this function, so we ignore
483 // this problem for now and figure something out if we have to.
484 std::vector<CommandLine::Args> Args;
485 if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
486 Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds.data(), argc, argv));
487 else
488 Args = getCommandArgs(Binary, nullptr);
489 CmdL = CommandLine(Args.data(), _config);
490
c3ded84c 491 if (CmdL.Parse(argc,argv) == false ||
ad7e0941
DK
492 (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
493 {
494 if (_config->FindB("version") == true)
90986d4d 495 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
496
497 _error->DumpErrors();
498 exit(100);
499 }
500
501 // See if the help should be shown
cbbee23e
DK
502 if (_config->FindB("help") == true || _config->FindB("version") == true ||
503 (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
ad7e0941 504 {
90986d4d 505 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
506 exit(0);
507 }
011188e3 508 if (Cmds.empty() == false && CmdL.FileSize() == 0)
ad7e0941 509 {
90986d4d 510 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
511 exit(1);
512 }
011188e3 513 return Cmds;
ad7e0941
DK
514}
515 /*}}}*/
6079b276 516unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
e7e10e47
DK
517{
518 // Match the operation
011188e3 519 bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
e7e10e47
DK
520
521 // Print any errors or warnings found during parsing
522 bool const Errors = _error->PendingError();
523 if (_config->FindI("quiet",0) > 0)
524 _error->DumpErrors();
525 else
526 _error->DumpErrors(GlobalError::DEBUG);
527 if (returned == false)
528 return 100;
529 return Errors == true ? 100 : 0;
530}
531 /*}}}*/