]> git.saurik.com Git - apt.git/blame - apt-private/private-cmndline.cc
move 'search' implementations as well
[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
127 return true;
128}
129 /*}}}*/
c3ccac92 130static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
131{
132 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
ebe24b7a 133 "dselect-upgrade", "autoremove", "full-upgrade"))
b9179170 134 {
4fb66d87 135 addArg(0, "show-progress", "DpkgPM::Progress", 0);
b9179170
MV
136 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
137 addArg(0, "purge", "APT::Get::Purge", 0);
138 addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
139 addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
140 addArg(0, "reinstall", "APT::Get::ReInstall", 0);
141 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
142 if (CmdMatches("upgrade"))
143 {
2004d647 144 addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
c678a044 145 CommandLine::Boolean);
b9179170
MV
146 }
147 }
148 else if (CmdMatches("update"))
149 {
150 addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
151 }
152 else if (CmdMatches("source"))
153 {
154 addArg('b', "compile", "APT::Get::Compile", 0);
155 addArg('b', "build", "APT::Get::Compile", 0);
ce7f128c 156 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
b9179170
MV
157 addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
158 addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
159 addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
160 addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
161 }
162 else if (CmdMatches("build-dep"))
163 {
164 addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
ce7f128c 165 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
e4e5d47b 166 addArg(0, "purge", "APT::Get::Purge", 0);
b9179170 167 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
c872ac50
MV
168 // this has no effect *but* sbuild is using it (see LP: #1255806)
169 // once sbuild is fixed, this option can be removed
170 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
b9179170 171 }
c2a4a8dd 172 else if (CmdMatches("indextargets"))
8881b11e 173 {
c2a4a8dd
DK
174 addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
175 addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
8881b11e 176 }
f66738d7 177 else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
b9179170
MV
178 CmdMatches("markauto", "unmarkauto")) // deprecated commands
179 ;
180 else if (CmdMatches("moo"))
181 addArg(0, "color", "APT::Moo::Color", 0);
182
183 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
f66738d7 184 "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
f83b5627 185 "build-dep", "full-upgrade", "source"))
b9179170
MV
186 {
187 addArg('s', "simulate", "APT::Get::Simulate", 0);
188 addArg('s', "just-print", "APT::Get::Simulate", 0);
189 addArg('s', "recon", "APT::Get::Simulate", 0);
190 addArg('s', "dry-run", "APT::Get::Simulate", 0);
191 addArg('s', "no-act", "APT::Get::Simulate", 0);
192 }
193
859093da
DK
194 bool const found_something = Args.empty() == false;
195
b9179170
MV
196 // FIXME: move to the correct command(s)
197 addArg('d',"download-only","APT::Get::Download-Only",0);
198 addArg('y',"yes","APT::Get::Assume-Yes",0);
199 addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
200 addArg(0,"assume-no","APT::Get::Assume-No",0);
201 addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
202 addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
203 addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
204 addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
205 addArg(0,"download","APT::Get::Download",0);
206 addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
207 addArg(0,"ignore-hold","APT::Ignore-Hold",0);
208 addArg(0,"upgrade","APT::Get::upgrade",0);
209 addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
b381a482
JAK
210 addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
211 addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
212 addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
b9179170
MV
213 addArg(0,"force-yes","APT::Get::force-yes",0);
214 addArg(0,"print-uris","APT::Get::Print-URIs",0);
215 addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
216 addArg(0,"remove","APT::Get::Remove",0);
217 addArg(0,"only-source","APT::Get::Only-Source",0);
218 addArg(0,"arch-only","APT::Get::Arch-Only",0);
219 addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
c99fe2e1 220 addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
b9179170
MV
221 addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
222 addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
223 addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
224
859093da 225 return found_something;
b9179170
MV
226}
227 /*}}}*/
c3ccac92 228static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
229{
230 if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
64e3414e 231 "showmanual", "showhold", "showholds",
b9179170 232 "markauto", "unmarkauto"))
64e3414e
DK
233 {
234 addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
235 }
236 else if (CmdMatches("install", "remove", "deinstall", "purge",
237 "showinstall", "showinstalls", "showremove", "showremoves",
238 "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
b9179170
MV
239 ;
240 else
241 return false;
242
64e3414e
DK
243 if (CmdMatches("markauto", "unmarkauto"))
244 {
245 addArg('v',"verbose","APT::MarkAuto::Verbose",0);
246 }
247
248 if (strncmp(Cmd, "show", strlen("show")) != 0)
249 {
250 addArg('s',"simulate","APT::Mark::Simulate",0);
251 addArg('s',"just-print","APT::Mark::Simulate",0);
252 addArg('s',"recon","APT::Mark::Simulate",0);
253 addArg('s',"dry-run","APT::Mark::Simulate",0);
254 addArg('s',"no-act","APT::Mark::Simulate",0);
255 }
b9179170
MV
256
257 return true;
258}
259 /*}}}*/
c3ccac92 260static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
261{
262 if (CmdMatches("list"))
263 {
d2c712a1 264 addArg('i',"installed","APT::Cmd::Installed",0);
df7c9fd2 265 addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
d2c712a1 266 addArg('u',"upgradable","APT::Cmd::Upgradable",0);
3bdf7da5 267 addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
e1dc051a 268 addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
c1a61d1c 269 addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
b9179170 270 }
06293aa7
MV
271 else if (CmdMatches("show"))
272 {
273 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
274 }
b9179170
MV
275 else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
276 {
277 // we have no (supported) command-name overlaps so far, so we call
278 // specifics in order until we find one which adds arguments
279 }
280 else
281 return false;
282
283 return true;
284}
285 /*}}}*/
286std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd)/*{{{*/
287{
288 std::vector<CommandLine::Args> Args;
289 Args.reserve(50);
290 if (Program == NULL || Cmd == NULL)
291 ; // FIXME: Invalid command supplied
292 else if (strcmp(Cmd, "help") == 0)
293 ; // no options for help so no need to implement it in each
294 else if (strcmp(Program, "apt-get") == 0)
295 addArgumentsAPTGet(Args, Cmd);
296 else if (strcmp(Program, "apt-cache") == 0)
297 addArgumentsAPTCache(Args, Cmd);
298 else if (strcmp(Program, "apt-cdrom") == 0)
299 addArgumentsAPTCDROM(Args, Cmd);
300 else if (strcmp(Program, "apt-config") == 0)
301 addArgumentsAPTConfig(Args, Cmd);
302 else if (strcmp(Program, "apt-mark") == 0)
303 addArgumentsAPTMark(Args, Cmd);
304 else if (strcmp(Program, "apt") == 0)
305 addArgumentsAPT(Args, Cmd);
306
307 // options without a command
308 addArg('h', "help", "help", 0);
309 addArg('v', "version", "version", 0);
310 // general options
311 addArg('q', "quiet", "quiet", CommandLine::IntLevel);
312 addArg('q', "silent", "quiet", CommandLine::IntLevel);
313 addArg('c', "config-file", 0, CommandLine::ConfigFile);
314 addArg('o', "option", 0, CommandLine::ArbItem);
315 addArg(0, NULL, NULL, 0);
316
317 return Args;
318}
319 /*}}}*/
320#undef CmdMatches
321#undef addArg
501cd23e
DK
322static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
323{
324 std::string const binary = flNotDir(Binary);
325 if (binary == "apt" || binary == "apt-config")
326 {
631800b1 327 _config->CndSet("Binary::apt::APT::Color", true);
501cd23e
DK
328 _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
329 _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
330 _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
631800b1
DK
331 _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
332 _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
333 _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
334 _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
501cd23e
DK
335 }
336
337 _config->Set("Binary", binary);
338 std::string const conf = "Binary::" + binary;
339 _config->MoveSubTree(conf.c_str(), NULL);
340}
341 /*}}}*/
ad7e0941
DK
342void ParseCommandLine(CommandLine &CmdL, CommandLine::Dispatch * const Cmds, CommandLine::Args * const Args,/*{{{*/
343 Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &CmdL))
344{
345 CmdL = CommandLine(Args,_config);
c3ded84c
DK
346 if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
347 {
348 _error->DumpErrors();
349 exit(100);
350 }
351
352 if (likely(argc != 0 && argv[0] != NULL))
501cd23e 353 BinarySpecificConfiguration(argv[0]);
c3ded84c
DK
354
355 if (CmdL.Parse(argc,argv) == false ||
ad7e0941
DK
356 (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
357 {
358 if (_config->FindB("version") == true)
359 ShowHelp(CmdL);
360
361 _error->DumpErrors();
362 exit(100);
363 }
364
365 // See if the help should be shown
366 if (_config->FindB("help") == true || _config->FindB("version") == true)
367 {
368 ShowHelp(CmdL);
369 exit(0);
370 }
371 if (Cmds != NULL && CmdL.FileSize() == 0)
372 {
373 ShowHelp(CmdL);
374 exit(1);
375 }
376}
377 /*}}}*/