fix screen width detection for apt/apt-get lists
[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 /*{{{*/
a00a9b44
DK
10#include <config.h>
11
4128c846
DK
12#include <apt-pkg/error.h>
13#include <apt-pkg/cmndline.h>
14#include <apt-pkg/init.h>
15#include <apt-pkg/cachefile.h>
472ff00e 16#include <apt-pkg/cacheset.h>
4128c846
DK
17#include <apt-pkg/strutl.h>
18#include <apt-pkg/edsp.h>
19#include <apt-pkg/algorithms.h>
20#include <apt-pkg/fileutl.h>
472ff00e 21#include <apt-pkg/pkgsystem.h>
82e369c4 22#include <apt-pkg/upgrade.h>
453b82a3
DK
23#include <apt-pkg/configuration.h>
24#include <apt-pkg/depcache.h>
25#include <apt-pkg/pkgcache.h>
26#include <apt-pkg/cacheiterators.h>
4128c846 27
453b82a3
DK
28#include <string.h>
29#include <iostream>
30#include <list>
31#include <string>
4128c846
DK
32#include <unistd.h>
33#include <cstdio>
a00a9b44
DK
34
35#include <apti18n.h>
4128c846
DK
36 /*}}}*/
37
38// ShowHelp - Show a help screen /*{{{*/
39// ---------------------------------------------------------------------
40/* */
65512241 41static bool ShowHelp(CommandLine &) {
9179f697 42 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
4128c846
DK
43 COMMON_ARCH,__DATE__,__TIME__);
44
45 std::cout <<
3999d158 46 _("Usage: apt-internal-solver\n"
4128c846 47 "\n"
3999d158 48 "apt-internal-solver is an interface to use the current internal\n"
4128c846
DK
49 "like an external resolver for the APT family for debugging or alike\n"
50 "\n"
51 "Options:\n"
52 " -h This help text.\n"
53 " -q Loggable output - no progress indicator\n"
54 " -c=? Read this configuration file\n"
3999d158 55 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
4128c846
DK
56 return true;
57}
58 /*}}}*/
59int main(int argc,const char *argv[]) /*{{{*/
60{
61 CommandLine::Args Args[] = {
62 {'h',"help","help",0},
63 {'v',"version","version",0},
64 {'q',"quiet","quiet",CommandLine::IntLevel},
65 {'q',"silent","quiet",CommandLine::IntLevel},
904be352
DK
66 {'c',"config-file",0,CommandLine::ConfigFile},
67 {'o',"option",0,CommandLine::ArbItem},
4128c846
DK
68 {0,0,0,0}};
69
70 CommandLine CmdL(Args,_config);
71 if (pkgInitConfig(*_config) == false ||
72 CmdL.Parse(argc,argv) == false) {
73 _error->DumpErrors();
74 return 2;
75 }
76
77 // See if the help should be shown
78 if (_config->FindB("help") == true ||
79 _config->FindB("version") == true) {
80 ShowHelp(CmdL);
81 return 1;
82 }
83
904be352
DK
84 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
85 {
86 if (pkgInitSystem(*_config,_system) == false) {
87 std::cerr << "System could not be initialized!" << std::endl;
88 return 1;
89 }
90 pkgCacheFile CacheFile;
91 CacheFile.Open(NULL, false);
92 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
93 FILE* output = stdout;
94 if (pkgset.empty() == true)
95 EDSP::WriteScenario(CacheFile, output);
96 else
97 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
98 fclose(output);
99 _error->DumpErrors(std::cerr);
100 return 0;
101 }
102
4128c846
DK
103 // Deal with stdout not being a tty
104 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
105 _config->Set("quiet","1");
106
107 if (_config->FindI("quiet", 0) < 1)
108 _config->Set("Debug::EDSP::WriteSolution", true);
109
98278a81 110 _config->Set("APT::Solver", "internal");
4128c846
DK
111 _config->Set("edsp::scenario", "stdin");
112 int input = STDIN_FILENO;
113 FILE* output = stdout;
114 SetNonBlock(input, false);
115
e876223c
DK
116 EDSP::WriteProgress(0, "Start up solver…", output);
117
4128c846
DK
118 if (pkgInitSystem(*_config,_system) == false) {
119 std::cerr << "System could not be initialized!" << std::endl;
120 return 1;
121 }
122
e876223c
DK
123 EDSP::WriteProgress(1, "Read request…", output);
124
4128c846
DK
125 if (WaitFd(input, false, 5) == false)
126 std::cerr << "WAIT timed out in the resolver" << std::endl;
127
128 std::list<std::string> install, remove;
129 bool upgrade, distUpgrade, autoRemove;
130 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
131 std::cerr << "Parsing the request failed!" << std::endl;
132 return 2;
133 }
134
e876223c
DK
135 EDSP::WriteProgress(5, "Read scenario…", output);
136
4128c846
DK
137 pkgCacheFile CacheFile;
138 CacheFile.Open(NULL, false);
139
e876223c
DK
140 EDSP::WriteProgress(50, "Apply request on scenario…", output);
141
4128c846
DK
142 if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
143 std::cerr << "Failed to apply request to depcache!" << std::endl;
144 return 3;
145 }
d9933172
DK
146
147 pkgProblemResolver Fix(CacheFile);
148 for (std::list<std::string>::const_iterator i = remove.begin();
149 i != remove.end(); ++i) {
150 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
151 Fix.Clear(P);
152 Fix.Protect(P);
153 Fix.Remove(P);
154 }
155
156 for (std::list<std::string>::const_iterator i = install.begin();
157 i != install.end(); ++i) {
158 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
159 Fix.Clear(P);
160 Fix.Protect(P);
161 }
162
4128c846
DK
163 for (std::list<std::string>::const_iterator i = install.begin();
164 i != install.end(); ++i)
165 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
166
e876223c 167 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 168
80699703
DK
169 if (upgrade == true) {
170 if (pkgAllUpgrade(CacheFile) == false) {
1e3f4083 171 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output);
80699703
DK
172 return 0;
173 }
174 } else if (distUpgrade == true) {
175 if (pkgDistUpgrade(CacheFile) == false) {
1e3f4083 176 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output);
80699703
DK
177 return 0;
178 }
179 } else if (Fix.Resolve() == false) {
1e3f4083 180 EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output);
4128c846
DK
181 return 0;
182 }
183
e876223c
DK
184 EDSP::WriteProgress(95, "Write solution…", output);
185
4128c846
DK
186 if (EDSP::WriteSolution(CacheFile, output) == false) {
187 std::cerr << "Failed to output the solution!" << std::endl;
188 return 4;
189 }
190
e876223c
DK
191 EDSP::WriteProgress(100, "Done", output);
192
4128c846
DK
193 bool const Errors = _error->PendingError();
194 if (_config->FindI("quiet",0) > 0)
904be352 195 _error->DumpErrors(std::cerr);
4128c846 196 else
904be352 197 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
4128c846
DK
198 return Errors == true ? 100 : 0;
199}
200 /*}}}*/