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