Interpret Remove and Install lines in Responses correctly
[ntk/apt.git] / cmdline / apt-internal-solver.cc
CommitLineData
4128c846
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* #####################################################################
4
5 cover around the internal solver to be able to run it like an external
6
7 ##################################################################### */
8 /*}}}*/
9// Include Files /*{{{*/
10#include <apt-pkg/error.h>
11#include <apt-pkg/cmndline.h>
12#include <apt-pkg/init.h>
13#include <apt-pkg/cachefile.h>
14#include <apt-pkg/strutl.h>
15#include <apt-pkg/edsp.h>
16#include <apt-pkg/algorithms.h>
17#include <apt-pkg/fileutl.h>
18
19#include <config.h>
20#include <apti18n.h>
21
22#include <unistd.h>
23#include <cstdio>
24 /*}}}*/
25
26// ShowHelp - Show a help screen /*{{{*/
27// ---------------------------------------------------------------------
28/* */
29bool ShowHelp(CommandLine &CmdL) {
30 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
31 COMMON_ARCH,__DATE__,__TIME__);
32
33 std::cout <<
34 _("Usage: apt-internal-resolver\n"
35 "\n"
36 "apt-internal-resolver is an interface to use the current internal\n"
37 "like an external resolver for the APT family for debugging or alike\n"
38 "\n"
39 "Options:\n"
40 " -h This help text.\n"
41 " -q Loggable output - no progress indicator\n"
42 " -c=? Read this configuration file\n"
43 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
44 "apt.conf(5) manual pages for more information and options.\n"
45 " This APT has Super Cow Powers.\n");
46 return true;
47}
48 /*}}}*/
49int main(int argc,const char *argv[]) /*{{{*/
50{
51 CommandLine::Args Args[] = {
52 {'h',"help","help",0},
53 {'v',"version","version",0},
54 {'q',"quiet","quiet",CommandLine::IntLevel},
55 {'q',"silent","quiet",CommandLine::IntLevel},
56 {0,0,0,0}};
57
58 CommandLine CmdL(Args,_config);
59 if (pkgInitConfig(*_config) == false ||
60 CmdL.Parse(argc,argv) == false) {
61 _error->DumpErrors();
62 return 2;
63 }
64
65 // See if the help should be shown
66 if (_config->FindB("help") == true ||
67 _config->FindB("version") == true) {
68 ShowHelp(CmdL);
69 return 1;
70 }
71
72 // Deal with stdout not being a tty
73 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
74 _config->Set("quiet","1");
75
76 if (_config->FindI("quiet", 0) < 1)
77 _config->Set("Debug::EDSP::WriteSolution", true);
78
79 _config->Set("APT::Solver::Name", "internal");
80 _config->Set("edsp::scenario", "stdin");
81 int input = STDIN_FILENO;
82 FILE* output = stdout;
83 SetNonBlock(input, false);
84
85 if (pkgInitSystem(*_config,_system) == false) {
86 std::cerr << "System could not be initialized!" << std::endl;
87 return 1;
88 }
89
90 if (WaitFd(input, false, 5) == false)
91 std::cerr << "WAIT timed out in the resolver" << std::endl;
92
93 std::list<std::string> install, remove;
94 bool upgrade, distUpgrade, autoRemove;
95 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
96 std::cerr << "Parsing the request failed!" << std::endl;
97 return 2;
98 }
99
100 pkgCacheFile CacheFile;
101 CacheFile.Open(NULL, false);
102
103 if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
104 std::cerr << "Failed to apply request to depcache!" << std::endl;
105 return 3;
106 }
107 for (std::list<std::string>::const_iterator i = install.begin();
108 i != install.end(); ++i)
109 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
110
111 pkgProblemResolver Fix(CacheFile);
112 if (Fix.Resolve() == false) {
113 EDSP::WriteError("An error occured", output);
114 return 0;
115 }
116
117 if (EDSP::WriteSolution(CacheFile, output) == false) {
118 std::cerr << "Failed to output the solution!" << std::endl;
119 return 4;
120 }
121
122 bool const Errors = _error->PendingError();
123 if (_config->FindI("quiet",0) > 0)
124 _error->DumpErrors();
125 else
126 _error->DumpErrors(GlobalError::DEBUG);
127 return Errors == true ? 100 : 0;
128}
129 /*}}}*/