apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

private-cmndline.cc
Go to the documentation of this file.
1 // Include Files /*{{{*/
2 #include <config.h>
3 
4 #include <apt-pkg/cmndline.h>
5 #include <apt-pkg/configuration.h>
6 #include <apt-pkg/error.h>
7 #include <apt-pkg/fileutl.h>
8 #include <apt-pkg/init.h>
9 #include <apt-pkg/pkgsystem.h>
10 #include <apt-pkg/strutl.h>
11 
14 
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <algorithm>
20 #include <iomanip>
21 #include <vector>
22 
23 #include <apti18n.h>
24  /*}}}*/
25 
26 APT_NONNULL(1, 2)
27 static bool CmdMatches_fn(char const *const Cmd, char const *const Match)
28 {
29  return strcmp(Cmd, Match) == 0;
30 }
31 template <typename... Tail>
32 APT_NONNULL(1, 2)
33 static bool CmdMatches_fn(char const *const Cmd, char const *const Match, Tail... MoreMatches)
34 {
35  return CmdMatches_fn(Cmd, Match) || CmdMatches_fn(Cmd, MoreMatches...);
36 }
37 #define addArg(w, x, y, z) Args.emplace_back(CommandLine::MakeArgs(w, x, y, z))
38 #define CmdMatches(...) (Cmd != nullptr && CmdMatches_fn(Cmd, __VA_ARGS__))
39 
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") | CmdMatches("info"))
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  addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
94 
95  return found_something;
96 }
97  /*}}}*/
98 static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
99 {
100  if (CmdMatches("add", "ident") == false)
101  return false;
102 
103  // FIXME: move to the correct command(s)
104  addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
105  addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
106  addArg('r', "rename", "APT::CDROM::Rename", 0);
107  addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
108  addArg('f', "fast", "APT::CDROM::Fast", 0);
109  addArg('n', "just-print", "APT::CDROM::NoAct", 0);
110  addArg('n', "recon", "APT::CDROM::NoAct", 0);
111  addArg('n', "no-act", "APT::CDROM::NoAct", 0);
112  addArg('a', "thorough", "APT::CDROM::Thorough", 0);
113  return true;
114 }
115  /*}}}*/
116 static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
117 {
118  if (CmdMatches("dump"))
119  {
120  addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
121  addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
122  }
123  else if (CmdMatches("shell"))
124  ;
125  else
126  return false;
127 
128  return true;
129 }
130  /*}}}*/
131 static bool addArgumentsAPTDumpSolver(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
132 {
133  addArg(0,"user","APT::Solver::RunAsUser",CommandLine::HasArg);
134  return true;
135 }
136  /*}}}*/
137 static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
138 {
139  addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
140  return true;
141 }
142  /*}}}*/
143 static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
144 {
145  addArg(0,"md5","APT::FTPArchive::MD5",0);
146  addArg(0,"sha1","APT::FTPArchive::SHA1",0);
147  addArg(0,"sha256","APT::FTPArchive::SHA256",0);
148  addArg(0,"sha512","APT::FTPArchive::SHA512",0);
149  addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
150  addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
151  addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
152  addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
153  addArg(0,"contents","APT::FTPArchive::Contents",0);
154  addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
155  return true;
156 }
157  /*}}}*/
158 static bool addArgumentsAPTInternalPlanner(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
159 {
160  return true;
161 }
162  /*}}}*/
163 static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
164 {
165  return true;
166 }
167  /*}}}*/
168 static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
169 {
170  if (CmdMatches("cat-file"))
171  {
172  addArg('C', "compress", "Apt-Helper::Cat-File::Compress",CommandLine::HasArg);
173  }
174  return true;
175 }
176  /*}}}*/
177 static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
178 {
179  if (CmdMatches("install", "reinstall", "remove", "purge", "upgrade", "dist-upgrade",
180  "dselect-upgrade", "autoremove", "autopurge", "full-upgrade"))
181  {
182  addArg(0, "show-progress", "DpkgPM::Progress", 0);
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, "autoremove", "APT::Get::AutomaticRemove", 0);
187  addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
188  addArg(0, "reinstall", "APT::Get::ReInstall", 0);
189  addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
190  addArg(0, "planner", "APT::Planner", CommandLine::HasArg);
191  if (CmdMatches("upgrade"))
192  {
193  addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
195  }
196  }
197  else if (CmdMatches("update"))
198  {
199  addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
200  addArg(0, "allow-insecure-repositories", "Acquire::AllowInsecureRepositories", 0);
201  addArg(0, "allow-weak-repositories", "Acquire::AllowWeakRepositories", 0);
202  addArg(0, "allow-releaseinfo-change", "Acquire::AllowReleaseInfoChange", 0);
203  addArg(0, "allow-releaseinfo-change-origin", "Acquire::AllowReleaseInfoChange::Origin", 0);
204  addArg(0, "allow-releaseinfo-change-label", "Acquire::AllowReleaseInfoChange::Label", 0);
205  addArg(0, "allow-releaseinfo-change-version", "Acquire::AllowReleaseInfoChange::Version", 0);
206  addArg(0, "allow-releaseinfo-change-codename", "Acquire::AllowReleaseInfoChange::Codename", 0);
207  addArg(0, "allow-releaseinfo-change-suite", "Acquire::AllowReleaseInfoChange::Suite", 0);
208  addArg(0, "allow-releaseinfo-change-defaultpin", "Acquire::AllowReleaseInfoChange::DefaultPin", 0);
209  addArg('e', "error-on", "APT::Update::Error-Mode", CommandLine::HasArg);
210  }
211  else if (CmdMatches("source"))
212  {
213  addArg('b', "compile", "APT::Get::Compile", 0);
214  addArg('b', "build", "APT::Get::Compile", 0);
215  addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
216  addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
217  addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
218  addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
219  addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
220  }
221  else if (CmdMatches("build-dep") || CmdMatches("satisfy"))
222  {
223  addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
224  addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
225  addArg(0, "purge", "APT::Get::Purge", 0);
226  addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
227  if (CmdMatches("build-dep"))
228  {
229  addArg(0,"arch-only","APT::Get::Arch-Only",0);
230  addArg(0,"indep-only","APT::Get::Indep-Only",0);
231  }
232  // this has no effect *but* sbuild is using it (see LP: #1255806)
233  // once sbuild is fixed, this option can be removed
234  addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
235  }
236  else if (CmdMatches("indextargets"))
237  {
238  addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
239  addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
240  }
241  else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
242  CmdMatches("markauto", "unmarkauto")) // deprecated commands
243  ;
244  else if (CmdMatches("moo"))
245  addArg(0, "color", "APT::Moo::Color", 0);
246 
247  if (CmdMatches("install", "reinstall", "remove", "purge", "upgrade", "dist-upgrade",
248  "dselect-upgrade", "autoremove", "auto-remove", "autopurge", "clean", "autoclean", "auto-clean", "check",
249  "build-dep", "satisfy", "full-upgrade", "source"))
250  {
251  addArg('s', "simulate", "APT::Get::Simulate", 0);
252  addArg('s', "just-print", "APT::Get::Simulate", 0);
253  addArg('s', "recon", "APT::Get::Simulate", 0);
254  addArg('s', "dry-run", "APT::Get::Simulate", 0);
255  addArg('s', "no-act", "APT::Get::Simulate", 0);
256  }
257 
258  bool const found_something = Args.empty() == false;
259 
260  // FIXME: move to the correct command(s)
261  addArg('d',"download-only","APT::Get::Download-Only",0);
262  addArg('y',"yes","APT::Get::Assume-Yes",0);
263  addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
264  addArg(0,"assume-no","APT::Get::Assume-No",0);
265  addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
266  addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
267  addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
268  addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
269  addArg(0,"download","APT::Get::Download",0);
270  addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
271  addArg(0,"ignore-hold","APT::Ignore-Hold",0);
272  addArg(0,"upgrade","APT::Get::upgrade",0);
273  addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
274  addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
275  addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
276  addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
277  addArg(0,"force-yes","APT::Get::force-yes",0);
278  addArg(0,"print-uris","APT::Get::Print-URIs",0);
279  addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
280  addArg(0,"mark-auto","APT::Get::Mark-Auto",0);
281  addArg(0,"remove","APT::Get::Remove",0);
282  addArg(0,"only-source","APT::Get::Only-Source",0);
283  addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
284  addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
285  addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
286  addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
287  addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
288 
289  return found_something;
290 }
291  /*}}}*/
292 static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
293 {
294  if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
295  "showmanual", "showhold", "showholds", "showheld",
296  "markauto", "unmarkauto", "minimize-manual"))
297  {
298  addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
299  }
300  else if (CmdMatches("install", "reinstall", "remove", "deinstall", "purge",
301  "showinstall", "showinstalls", "showremove", "showremoves",
302  "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
303  ;
304  else
305  return false;
306 
307  if (CmdMatches("markauto", "unmarkauto"))
308  {
309  addArg('v',"verbose","APT::MarkAuto::Verbose",0);
310  }
311 
312  if (CmdMatches("minimize-manual"))
313  {
314  addArg('y',"yes","APT::Get::Assume-Yes",0);
315  addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
316  addArg(0,"assume-no","APT::Get::Assume-No",0);
317  }
318 
319  if (CmdMatches("minimize-manual") || (Cmd != nullptr && strncmp(Cmd, "show", strlen("show")) != 0))
320  {
321  addArg('s',"simulate","APT::Mark::Simulate",0);
322  addArg('s',"just-print","APT::Mark::Simulate",0);
323  addArg('s',"recon","APT::Mark::Simulate",0);
324  addArg('s',"dry-run","APT::Mark::Simulate",0);
325  addArg('s',"no-act","APT::Mark::Simulate",0);
326  }
327  addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
328 
329  return true;
330 }
331  /*}}}*/
332 static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
333 {
334  addArg('s',"source","APT::SortPkgs::Source",0);
335  return true;
336 }
337  /*}}}*/
338 static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
339 {
340  if (CmdMatches("list"))
341  {
342  addArg('i',"installed","APT::Cmd::Installed",0);
343  addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
344  addArg('u',"upgradable","APT::Cmd::Upgradable",0);
345  addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
346  addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
347  addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
348  }
349  else if (CmdMatches("show") || CmdMatches("info"))
350  {
351  addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
352  }
353  else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
354  {
355  // we have no (supported) command-name overlaps so far, so we call
356  // specifics in order until we find one which adds arguments
357  }
358  else
359  return false;
360 
361  addArg(0, "with-source", "APT::Sources::With::", CommandLine::HasArg);
362 
363  return true;
364 }
365  /*}}}*/
366 static bool addArgumentsRred(std::vector<CommandLine::Args> &Args, char const * const /*Cmd*/)/*{{{*/
367 {
368  addArg('t', nullptr, "Rred::T", 0);
369  addArg('f', nullptr, "Rred::F", 0);
370  addArg('C', "compress", "Rred::Compress",CommandLine::HasArg);
371  return true;
372 }
373  /*}}}*/
374 std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
375 {
376  std::vector<CommandLine::Args> Args;
377  Args.reserve(50);
378  if (Cmd != nullptr && strcmp(Cmd, "help") == 0)
379  ; // no options for help so no need to implement it in each
380  else
381  switch (Program)
382  {
383  case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
384  case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
385  case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
386  case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
387  case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
388  case APT_CMD::APT_DUMP_SOLVER: addArgumentsAPTDumpSolver(Args, Cmd); break;
390  case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
391  case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
394  case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
395  case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
396  case APT_CMD::RRED: addArgumentsRred(Args, Cmd); break;
397  }
398 
399  // options without a command
400  addArg('h', "help", "help", 0);
401  addArg('v', "version", "version", 0);
402  // general options
403  addArg('q', "quiet", "quiet", CommandLine::IntLevel);
404  addArg('q', "silent", "quiet", CommandLine::IntLevel);
405  addArg('c', "config-file", 0, CommandLine::ConfigFile);
406  addArg('o', "option", 0, CommandLine::ArbItem);
407  addArg(0, NULL, NULL, 0);
408 
409  return Args;
410 }
411  /*}}}*/
412 #undef addArg
413 static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
414 {
415  if (Cmds.empty() || Cmds[0].Match == nullptr)
416  return;
417  std::cout << std::endl << _("Most used commands:") << std::endl;
418  for (auto const &c: Cmds)
419  {
420  if (c.Help == nullptr)
421  continue;
422  std::cout << " " << c.Match << " - " << c.Help << std::endl;
423  }
424 }
425  /*}}}*/
426 static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
427  bool (*ShowHelp)(CommandLine &))
428 {
429  std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
430  if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
431  return true;
432  if (ShowHelp(CmdL) == false)
433  return false;
434  if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
435  return true;
436  ShowHelpListCommands(Cmds);
437  std::cout << std::endl;
438  char const * cmd = nullptr;
439  switch (Binary)
440  {
441  case APT_CMD::APT: cmd = "apt(8)"; break;
442  case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
443  case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
444  case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
445  case APT_CMD::APT_DUMP_SOLVER: cmd = nullptr; break;
446  case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
447  case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
448  case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
449  case APT_CMD::APT_HELPER: cmd = nullptr; break;
450  case APT_CMD::APT_INTERNAL_PLANNER: cmd = nullptr; break;
451  case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
452  case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
453  case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
454  case APT_CMD::RRED: cmd = nullptr; break;
455  }
456  if (cmd != nullptr)
457  ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
458  if (Binary != APT_CMD::APT_DUMP_SOLVER && Binary != APT_CMD::APT_INTERNAL_SOLVER &&
459  Binary != APT_CMD::APT_INTERNAL_PLANNER && Binary != APT_CMD::RRED)
460  std::cout << std::endl <<
461  _("Configuration options and syntax is detailed in apt.conf(5).\n"
462  "Information about how to configure sources can be found in sources.list(5).\n"
463  "Package and version choices can be expressed via apt_preferences(5).\n"
464  "Security details are available in apt-secure(8).\n");
465  if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
466  std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
467  else if (Binary == APT_CMD::APT_HELPER || Binary == APT_CMD::APT_DUMP_SOLVER)
468  std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
469  return true;
470 }
471  /*}}}*/
472 static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
473 {
474  std::string const binary = flNotDir(Binary);
475  if (binary == "apt" || binary == "apt-config")
476  {
477  _config->CndSet("Binary::apt::APT::Color", true);
478  _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
479  _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
480  _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
481  _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
482  _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
483  _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
484  _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
485  _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
486  _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
487  _config->CndSet("Binary::apt::APT::Keep-Downloaded-Packages", false);
488  _config->CndSet("Binary::apt::APT::Get::Update::InteractiveReleaseInfoChanges", true);
489  _config->CndSet("Binary::apt::APT::Cmd::Pattern-Only", true);
490 
491  if (isatty(STDIN_FILENO))
492  _config->CndSet("Binary::apt::Dpkg::Lock::Timeout", -1);
493  else
494  _config->CndSet("Binary::apt::Dpkg::Lock::Timeout", 120);
495  }
496 
497  _config->Set("Binary", binary);
498 }
499  /*}}}*/
500 static void BinaryCommandSpecificConfiguration(char const * const Binary, char const * const Cmd)/*{{{*/
501 {
502  std::string const binary = flNotDir(Binary);
503  if ((binary == "apt" || binary == "apt-get") && CmdMatches("upgrade", "dist-upgrade", "full-upgrade"))
504  {
505  //FIXME: the option is documented to apply only for install/remove, so
506  // we force it false for configuration files where users can be confused if
507  // we support it anyhow, but allow it on the commandline to take effect
508  // even through it isn't documented as a user who doesn't want it wouldn't
509  // ask for it
510  _config->Set("APT::Get::AutomaticRemove", "");
511  }
512 }
513 #undef CmdMatches
514  /*}}}*/
515 std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
516  Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
517  bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
518 {
519  InitLocale(Binary);
520  if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
521  {
522  _error->DumpErrors();
523  exit(100);
524  }
525 
526  if (likely(argc != 0 && argv[0] != NULL))
528 
529  std::vector<CommandLine::Dispatch> Cmds;
530  std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
531  if (CmdsWithHelp.empty() == false)
532  {
533  CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
534  Cmds.push_back(std::move(help));
535  }
536  std::transform(CmdsWithHelp.begin(), CmdsWithHelp.end(), std::back_inserter(Cmds),
537  [](auto &&cmd) { return CommandLine::Dispatch{cmd.Match, cmd.Handler}; });
538 
539  char const * CmdCalled = nullptr;
540  if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
541  CmdCalled = CommandLine::GetCommand(Cmds.data(), argc, argv);
542  if (CmdCalled != nullptr)
543  BinaryCommandSpecificConfiguration(argv[0], CmdCalled);
544  std::string const conf = "Binary::" + _config->Find("Binary");
545  _config->MoveSubTree(conf.c_str(), nullptr);
546 
547  // Args running out of scope invalidates the pointer stored in CmdL,
548  // but we don't use the pointer after this function, so we ignore
549  // this problem for now and figure something out if we have to.
550  auto Args = getCommandArgs(Binary, CmdCalled);
551  CmdL = CommandLine(Args.data(), _config);
552 
553  if (CmdL.Parse(argc,argv) == false ||
554  (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
555  {
556  if (_config->FindB("version") == true)
557  ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
558 
559  _error->DumpErrors();
560  exit(100);
561  }
562 
563  if (_config->FindB("APT::Get::Force-Yes", false) == true)
564  {
565  _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
566  }
567 
568  // See if the help should be shown
569  if (_config->FindB("help") == true || _config->FindB("version") == true ||
570  (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
571  {
572  ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
573  exit(0);
574  }
575  if (Cmds.empty() == false && CmdL.FileSize() == 0)
576  {
577  ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
578  exit(1);
579  }
580  return Cmds;
581 }
582  /*}}}*/
583 unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
584 {
585  // Match the operation
586  bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
587 
588  // Print any errors or warnings found during parsing
589  bool const Errors = _error->PendingError();
590  if (_config->FindI("quiet",0) > 0)
591  _error->DumpErrors();
592  else
593  _error->DumpErrors(GlobalError::DEBUG);
594  if (returned == false)
595  return 100;
596  return Errors == true ? 100 : 0;
597 }
598  /*}}}*/
static bool ShowHelp(CommandLine &)
Definition: apt-cache.cc:1093
static std::vector< aptDispatchWithHelp > GetCommands()
Definition: apt-cache.cc:1108
const char ** FileList
Definition: cmndline.h:78
unsigned int FileSize() const APT_PURE
Definition: cmndline.cc:353
bool Parse(int argc, const char **argv)
Definition: cmndline.cc:90
@ ConfigFile
Definition: cmndline.h:74
bool DispatchArg(Dispatch const *const List, bool NoMatch=true)
Definition: cmndline.cc:362
static char const * GetCommand(Dispatch const *const Map, unsigned int const argc, char const *const *const argv) APT_PURE
Definition: cmndline.cc:49
void MoveSubTree(char const *const OldRoot, char const *const NewRoot)
int FindI(const char *Name, int const &Default=0) const
void Set(const std::string &Name, const std::string &Value)
Definition: configuration.h:92
std::string Find(const char *Name, const char *Default=0) const
bool FindB(const char *Name, bool const &Default=false) const
void CndSet(const char *Name, const std::string &Value)
@ DEBUG
for developers only in areas it is hard to print something directly
Definition: error.h:66
Configuration * _config
string flNotDir(string File)
Definition: fileutl.cc:664
bool pkgInitConfig(Configuration &Cnf)
Definition: init.cc:126
bool pkgInitSystem(Configuration &Cnf, pkgSystem *&Sys)
Definition: init.cc:257
#define APT_NONNULL(...)
Definition: macros.h:67
static bool addArgumentsAPTMark(std::vector< CommandLine::Args > &Args, char const *const Cmd)
static void BinaryCommandSpecificConfiguration(char const *const Binary, char const *const Cmd)
static void ShowHelpListCommands(std::vector< aptDispatchWithHelp > const &Cmds)
static bool addArgumentsAPTInternalPlanner(std::vector< CommandLine::Args > &, char const *const)
static bool addArgumentsAPTSortPkgs(std::vector< CommandLine::Args > &Args, char const *const)
static bool addArgumentsAPTDumpSolver(std::vector< CommandLine::Args > &Args, char const *const)
static bool addArgumentsRred(std::vector< CommandLine::Args > &Args, char const *const)
std::vector< CommandLine::Dispatch > ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary, Configuration *const *const Cnf, pkgSystem **const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &), std::vector< aptDispatchWithHelp >(*GetCommands)(void))
unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector< CommandLine::Dispatch > const &Cmds)
static bool addArgumentsAPTFTPArchive(std::vector< CommandLine::Args > &Args, char const *const)
static bool addArgumentsAPTCDROM(std::vector< CommandLine::Args > &Args, char const *const Cmd)
#define addArg(w, x, y, z)
static void BinarySpecificConfiguration(char const *const Binary)
static bool addArgumentsAPTGet(std::vector< CommandLine::Args > &Args, char const *const Cmd)
static bool addArgumentsAPT(std::vector< CommandLine::Args > &Args, char const *const Cmd)
static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector< aptDispatchWithHelp > const &Cmds, bool(*ShowHelp)(CommandLine &))
static bool addArgumentsAPTInternalSolver(std::vector< CommandLine::Args > &, char const *const)
static bool addArgumentsAPTExtractTemplates(std::vector< CommandLine::Args > &Args, char const *const)
static bool addArgumentsAPTConfig(std::vector< CommandLine::Args > &Args, char const *const Cmd)
static bool CmdMatches_fn(char const *const Cmd, char const *const Match)
std::vector< CommandLine::Args > getCommandArgs(APT_CMD const Program, char const *const Cmd)
static bool addArgumentsAPTCache(std::vector< CommandLine::Args > &Args, char const *const Cmd)
static bool addArgumentsAPTHelper(std::vector< CommandLine::Args > &Args, char const *const Cmd)
#define CmdMatches(...)
APT_CMD
@ APT_EXTRACTTEMPLATES
@ APT_DUMP_SOLVER
@ APT_INTERNAL_PLANNER
@ APT_FTPARCHIVE
@ APT_INTERNAL_SOLVER
void InitLocale(APT_CMD const binary)
Definition: private-main.cc:19
void ioprintf(ostream &out, const char *format,...)
Definition: strutl.cc:1433