private-output.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Include files /*{{{*/
  2. #include<config.h>
  3. #include <apt-pkg/configuration.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apt-pkg/error.h>
  6. #include <apt-pkg/cachefile.h>
  7. #include <apt-pkg/pkgrecords.h>
  8. #include <apt-pkg/policy.h>
  9. #include <apt-pkg/depcache.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-pkg/cacheiterators.h>
  12. #include <apt-private/private-output.h>
  13. #include <apt-private/private-cachefile.h>
  14. #include <regex.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <langinfo.h>
  21. #include <unistd.h>
  22. #include <signal.h>
  23. #include <sys/ioctl.h>
  24. #include <sstream>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. using namespace std;
  28. std::ostream c0out(0);
  29. std::ostream c1out(0);
  30. std::ostream c2out(0);
  31. std::ofstream devnull("/dev/null");
  32. unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
  33. // SigWinch - Window size change signal handler /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. static void SigWinch(int)
  37. {
  38. // Riped from GNU ls
  39. #ifdef TIOCGWINSZ
  40. struct winsize ws;
  41. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  42. ScreenWidth = ws.ws_col - 1;
  43. #endif
  44. }
  45. /*}}}*/
  46. bool InitOutput(std::basic_streambuf<char> * const out) /*{{{*/
  47. {
  48. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  49. _config->Set("quiet","1");
  50. c0out.rdbuf(out);
  51. c1out.rdbuf(out);
  52. c2out.rdbuf(out);
  53. if (_config->FindI("quiet",0) > 0)
  54. c0out.rdbuf(devnull.rdbuf());
  55. if (_config->FindI("quiet",0) > 1)
  56. c1out.rdbuf(devnull.rdbuf());
  57. // deal with window size changes
  58. signal(SIGWINCH,SigWinch);
  59. SigWinch(0);
  60. if(!isatty(1))
  61. {
  62. _config->Set("APT::Color", "false");
  63. _config->Set("APT::Color::Highlight", "");
  64. _config->Set("APT::Color::Neutral", "");
  65. } else {
  66. // Colors
  67. _config->CndSet("APT::Color::Highlight", "\x1B[32m");
  68. _config->CndSet("APT::Color::Neutral", "\x1B[0m");
  69. _config->CndSet("APT::Color::Red", "\x1B[31m");
  70. _config->CndSet("APT::Color::Green", "\x1B[32m");
  71. _config->CndSet("APT::Color::Yellow", "\x1B[33m");
  72. _config->CndSet("APT::Color::Blue", "\x1B[34m");
  73. _config->CndSet("APT::Color::Magenta", "\x1B[35m");
  74. _config->CndSet("APT::Color::Cyan", "\x1B[36m");
  75. _config->CndSet("APT::Color::White", "\x1B[37m");
  76. }
  77. return true;
  78. }
  79. /*}}}*/
  80. static std::string GetArchiveSuite(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator ver) /*{{{*/
  81. {
  82. std::string suite = "";
  83. if (ver && ver.FileList())
  84. {
  85. pkgCache::VerFileIterator VF = ver.FileList();
  86. for (; VF.end() == false ; ++VF)
  87. {
  88. if(VF.File() == NULL || VF.File().Archive() == NULL)
  89. suite = suite + "," + _("unknown");
  90. else
  91. suite = suite + "," + VF.File().Archive();
  92. //suite = VF.File().Archive();
  93. }
  94. suite = suite.erase(0, 1);
  95. }
  96. return suite;
  97. }
  98. /*}}}*/
  99. static std::string GetFlagsStr(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
  100. {
  101. pkgDepCache *DepCache = CacheFile.GetDepCache();
  102. pkgDepCache::StateCache &state = (*DepCache)[P];
  103. std::string flags_str;
  104. if (state.NowBroken())
  105. flags_str = "B";
  106. if (P.CurrentVer() && state.Upgradable() && state.CandidateVer != NULL)
  107. flags_str = "g";
  108. else if (P.CurrentVer() != NULL)
  109. flags_str = "i";
  110. else
  111. flags_str = "-";
  112. return flags_str;
  113. }
  114. /*}}}*/
  115. static std::string GetCandidateVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
  116. {
  117. pkgPolicy *policy = CacheFile.GetPolicy();
  118. pkgCache::VerIterator cand = policy->GetCandidateVer(P);
  119. return cand ? cand.VerStr() : "(none)";
  120. }
  121. /*}}}*/
  122. static std::string GetInstalledVersion(pkgCacheFile &/*CacheFile*/, pkgCache::PkgIterator P)/*{{{*/
  123. {
  124. pkgCache::VerIterator inst = P.CurrentVer();
  125. return inst ? inst.VerStr() : "(none)";
  126. }
  127. /*}}}*/
  128. static std::string GetVersion(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator V)/*{{{*/
  129. {
  130. pkgCache::PkgIterator P = V.ParentPkg();
  131. if (V == P.CurrentVer())
  132. {
  133. std::string inst_str = DeNull(V.VerStr());
  134. #if 0 // FIXME: do we want this or something like this?
  135. pkgDepCache *DepCache = CacheFile.GetDepCache();
  136. pkgDepCache::StateCache &state = (*DepCache)[P];
  137. if (state.Upgradable())
  138. return "**"+inst_str;
  139. #endif
  140. return inst_str;
  141. }
  142. if(V)
  143. return DeNull(V.VerStr());
  144. return "(none)";
  145. }
  146. /*}}}*/
  147. static std::string GetArchitecture(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
  148. {
  149. if (P->CurrentVer == 0)
  150. {
  151. pkgDepCache * const DepCache = CacheFile.GetDepCache();
  152. pkgDepCache::StateCache const &state = (*DepCache)[P];
  153. if (state.CandidateVer != NULL)
  154. {
  155. pkgCache::VerIterator const CandV(CacheFile, state.CandidateVer);
  156. return CandV.Arch();
  157. }
  158. else
  159. {
  160. pkgCache::VerIterator const V = P.VersionList();
  161. if (V.end() == false)
  162. return V.Arch();
  163. else
  164. return P.Arch();
  165. }
  166. }
  167. else
  168. return P.CurrentVer().Arch();
  169. }
  170. /*}}}*/
  171. static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
  172. {
  173. pkgPolicy *policy = CacheFile.GetPolicy();
  174. pkgCache::VerIterator ver;
  175. if (P.CurrentVer())
  176. ver = P.CurrentVer();
  177. else
  178. ver = policy->GetCandidateVer(P);
  179. std::string ShortDescription = "(none)";
  180. if(ver)
  181. {
  182. pkgCache::DescIterator const Desc = ver.TranslatedDescription();
  183. if (Desc.end() == false)
  184. {
  185. pkgRecords::Parser & parser = records.Lookup(Desc.FileList());
  186. ShortDescription = parser.ShortDesc();
  187. }
  188. }
  189. return ShortDescription;
  190. }
  191. /*}}}*/
  192. static std::string GetLongDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
  193. {
  194. pkgPolicy *policy = CacheFile.GetPolicy();
  195. pkgCache::VerIterator ver;
  196. if (P->CurrentVer != 0)
  197. ver = P.CurrentVer();
  198. else
  199. ver = policy->GetCandidateVer(P);
  200. std::string const EmptyDescription = "(none)";
  201. if(ver.end() == true)
  202. return EmptyDescription;
  203. pkgCache::DescIterator const Desc = ver.TranslatedDescription();
  204. if (Desc.end() == false)
  205. {
  206. pkgRecords::Parser & parser = records.Lookup(Desc.FileList());
  207. std::string const longdesc = parser.LongDesc();
  208. if (longdesc.empty() == false)
  209. return SubstVar(longdesc, "\n ", "\n ");
  210. }
  211. return EmptyDescription;
  212. }
  213. /*}}}*/
  214. void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/
  215. pkgCache::VerIterator const &V, std::ostream &out,
  216. std::string const &format)
  217. {
  218. pkgCache::PkgIterator const P = V.ParentPkg();
  219. pkgDepCache * const DepCache = CacheFile.GetDepCache();
  220. pkgDepCache::StateCache const &state = (*DepCache)[P];
  221. std::string output;
  222. if (_config->FindB("APT::Cmd::use-format", false))
  223. output = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}");
  224. else
  225. output = format;
  226. // FIXME: some of these names are really icky – and all is nowhere documented
  227. output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P));
  228. output = SubstVar(output, "${Package}", P.Name());
  229. std::string const ArchStr = GetArchitecture(CacheFile, P);
  230. output = SubstVar(output, "${Architecture}", ArchStr);
  231. std::string const InstalledVerStr = GetInstalledVersion(CacheFile, P);
  232. output = SubstVar(output, "${installed:Version}", InstalledVerStr);
  233. std::string const CandidateVerStr = GetCandidateVersion(CacheFile, P);
  234. output = SubstVar(output, "${candidate:Version}", CandidateVerStr);
  235. std::string const VersionStr = GetVersion(CacheFile, V);
  236. output = SubstVar(output, "${Version}", VersionStr);
  237. output = SubstVar(output, "${Origin}", GetArchiveSuite(CacheFile, V));
  238. std::string StatusStr = "";
  239. if (P->CurrentVer != 0)
  240. {
  241. if (P.CurrentVer() == V)
  242. {
  243. if (state.Upgradable() && state.CandidateVer != NULL)
  244. strprintf(StatusStr, _("[installed,upgradable to: %s]"),
  245. CandidateVerStr.c_str());
  246. else if (V.Downloadable() == false)
  247. StatusStr = _("[installed,local]");
  248. else if(V.Automatic() == true && state.Garbage == true)
  249. StatusStr = _("[installed,auto-removable]");
  250. else if ((state.Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
  251. StatusStr = _("[installed,automatic]");
  252. else
  253. StatusStr = _("[installed]");
  254. }
  255. else if (state.CandidateVer == V && state.Upgradable())
  256. strprintf(StatusStr, _("[upgradable from: %s]"),
  257. InstalledVerStr.c_str());
  258. }
  259. else if (V.ParentPkg()->CurrentState == pkgCache::State::ConfigFiles)
  260. StatusStr = _("[residual-config]");
  261. output = SubstVar(output, "${apt:Status}", StatusStr);
  262. output = SubstVar(output, "${color:highlight}", _config->Find("APT::Color::Highlight", ""));
  263. output = SubstVar(output, "${color:neutral}", _config->Find("APT::Color::Neutral", ""));
  264. output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P));
  265. if (output.find("${LongDescription}") != string::npos)
  266. output = SubstVar(output, "${LongDescription}", GetLongDescription(CacheFile, records, P));
  267. output = SubstVar(output, "${ }${ }", "${ }");
  268. output = SubstVar(output, "${ }\n", "\n");
  269. output = SubstVar(output, "${ }", " ");
  270. if (APT::String::Endswith(output, " ") == true)
  271. output.erase(output.length() - 1);
  272. out << output;
  273. }
  274. /*}}}*/
  275. // ShowBroken - Debugging aide /*{{{*/
  276. // ---------------------------------------------------------------------
  277. /* This prints out the names of all the packages that are broken along
  278. with the name of each each broken dependency and a quite version
  279. description.
  280. The output looks like:
  281. The following packages have unmet dependencies:
  282. exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
  283. Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
  284. Depends: libsasl7 but it is not going to be installed
  285. */
  286. static void ShowBrokenPackage(ostream &out, pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg, bool const Now)
  287. {
  288. if (Now == true)
  289. {
  290. if ((*Cache)[Pkg].NowBroken() == false)
  291. return;
  292. }
  293. else
  294. {
  295. if ((*Cache)[Pkg].InstBroken() == false)
  296. return;
  297. }
  298. // Print out each package and the failed dependencies
  299. out << " " << Pkg.FullName(true) << " :";
  300. unsigned const Indent = Pkg.FullName(true).size() + 3;
  301. bool First = true;
  302. pkgCache::VerIterator Ver;
  303. if (Now == true)
  304. Ver = Pkg.CurrentVer();
  305. else
  306. Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  307. if (Ver.end() == true)
  308. {
  309. out << endl;
  310. return;
  311. }
  312. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
  313. {
  314. // Compute a single dependency element (glob or)
  315. pkgCache::DepIterator Start;
  316. pkgCache::DepIterator End;
  317. D.GlobOr(Start,End); // advances D
  318. if ((*Cache)->IsImportantDep(End) == false)
  319. continue;
  320. if (Now == true)
  321. {
  322. if (((*Cache)[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
  323. continue;
  324. }
  325. else
  326. {
  327. if (((*Cache)[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  328. continue;
  329. }
  330. bool FirstOr = true;
  331. while (1)
  332. {
  333. if (First == false)
  334. for (unsigned J = 0; J != Indent; J++)
  335. out << ' ';
  336. First = false;
  337. if (FirstOr == false)
  338. {
  339. for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
  340. out << ' ';
  341. }
  342. else
  343. out << ' ' << End.DepType() << ": ";
  344. FirstOr = false;
  345. out << Start.TargetPkg().FullName(true);
  346. // Show a quick summary of the version requirements
  347. if (Start.TargetVer() != 0)
  348. out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
  349. /* Show a summary of the target package if possible. In the case
  350. of virtual packages we show nothing */
  351. pkgCache::PkgIterator Targ = Start.TargetPkg();
  352. if (Targ->ProvidesList == 0)
  353. {
  354. out << ' ';
  355. pkgCache::VerIterator Ver = (*Cache)[Targ].InstVerIter(*Cache);
  356. if (Now == true)
  357. Ver = Targ.CurrentVer();
  358. if (Ver.end() == false)
  359. {
  360. if (Now == true)
  361. ioprintf(out,_("but %s is installed"),Ver.VerStr());
  362. else
  363. ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
  364. }
  365. else
  366. {
  367. if ((*Cache)[Targ].CandidateVerIter(*Cache).end() == true)
  368. {
  369. if (Targ->ProvidesList == 0)
  370. out << _("but it is not installable");
  371. else
  372. out << _("but it is a virtual package");
  373. }
  374. else
  375. out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
  376. }
  377. }
  378. if (Start != End)
  379. out << _(" or");
  380. out << endl;
  381. if (Start == End)
  382. break;
  383. ++Start;
  384. }
  385. }
  386. }
  387. void ShowBroken(ostream &out, CacheFile &Cache, bool const Now)
  388. {
  389. if (Cache->BrokenCount() == 0)
  390. return;
  391. out << _("The following packages have unmet dependencies:") << endl;
  392. SortedPackageUniverse Universe(Cache);
  393. for (auto const &Pkg: Universe)
  394. ShowBrokenPackage(out, &Cache, Pkg, Now);
  395. }
  396. void ShowBroken(ostream &out, pkgCacheFile &Cache, bool const Now)
  397. {
  398. if (Cache->BrokenCount() == 0)
  399. return;
  400. out << _("The following packages have unmet dependencies:") << endl;
  401. APT::PackageUniverse Universe(Cache);
  402. for (auto const &Pkg: Universe)
  403. ShowBrokenPackage(out, &Cache, Pkg, Now);
  404. }
  405. /*}}}*/
  406. // ShowNew - Show packages to newly install /*{{{*/
  407. void ShowNew(ostream &out,CacheFile &Cache)
  408. {
  409. SortedPackageUniverse Universe(Cache);
  410. ShowList(out,_("The following NEW packages will be installed:"), Universe,
  411. [&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].NewInstall(); },
  412. &PrettyFullName,
  413. CandidateVersion(&Cache));
  414. }
  415. /*}}}*/
  416. // ShowDel - Show packages to delete /*{{{*/
  417. void ShowDel(ostream &out,CacheFile &Cache)
  418. {
  419. SortedPackageUniverse Universe(Cache);
  420. ShowList(out,_("The following packages will be REMOVED:"), Universe,
  421. [&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].Delete(); },
  422. [&Cache](pkgCache::PkgIterator const &Pkg)
  423. {
  424. std::string str = PrettyFullName(Pkg);
  425. if (((*Cache)[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
  426. str.append("*");
  427. return str;
  428. },
  429. CandidateVersion(&Cache));
  430. }
  431. /*}}}*/
  432. // ShowKept - Show kept packages /*{{{*/
  433. void ShowKept(ostream &out,CacheFile &Cache)
  434. {
  435. SortedPackageUniverse Universe(Cache);
  436. ShowList(out,_("The following packages have been kept back:"), Universe,
  437. [&Cache](pkgCache::PkgIterator const &Pkg)
  438. {
  439. return Cache[Pkg].Upgrade() == false &&
  440. Cache[Pkg].Upgradable() == true &&
  441. Pkg->CurrentVer != 0 &&
  442. Cache[Pkg].Delete() == false;
  443. },
  444. &PrettyFullName,
  445. CurrentToCandidateVersion(&Cache));
  446. }
  447. /*}}}*/
  448. // ShowUpgraded - Show upgraded packages /*{{{*/
  449. void ShowUpgraded(ostream &out,CacheFile &Cache)
  450. {
  451. SortedPackageUniverse Universe(Cache);
  452. ShowList(out,_("The following packages will be upgraded:"), Universe,
  453. [&Cache](pkgCache::PkgIterator const &Pkg)
  454. {
  455. return Cache[Pkg].Upgrade() == true && Cache[Pkg].NewInstall() == false;
  456. },
  457. &PrettyFullName,
  458. CurrentToCandidateVersion(&Cache));
  459. }
  460. /*}}}*/
  461. // ShowDowngraded - Show downgraded packages /*{{{*/
  462. // ---------------------------------------------------------------------
  463. /* */
  464. bool ShowDowngraded(ostream &out,CacheFile &Cache)
  465. {
  466. SortedPackageUniverse Universe(Cache);
  467. return ShowList(out,_("The following packages will be DOWNGRADED:"), Universe,
  468. [&Cache](pkgCache::PkgIterator const &Pkg)
  469. {
  470. return Cache[Pkg].Downgrade() == true && Cache[Pkg].NewInstall() == false;
  471. },
  472. &PrettyFullName,
  473. CurrentToCandidateVersion(&Cache));
  474. }
  475. /*}}}*/
  476. // ShowHold - Show held but changed packages /*{{{*/
  477. bool ShowHold(ostream &out,CacheFile &Cache)
  478. {
  479. SortedPackageUniverse Universe(Cache);
  480. return ShowList(out,_("The following held packages will be changed:"), Universe,
  481. [&Cache](pkgCache::PkgIterator const &Pkg)
  482. {
  483. return Pkg->SelectedState == pkgCache::State::Hold &&
  484. Cache[Pkg].InstallVer != (pkgCache::Version *)Pkg.CurrentVer();
  485. },
  486. &PrettyFullName,
  487. CurrentToCandidateVersion(&Cache));
  488. }
  489. /*}}}*/
  490. // ShowEssential - Show an essential package warning /*{{{*/
  491. // ---------------------------------------------------------------------
  492. /* This prints out a warning message that is not to be ignored. It shows
  493. all essential packages and their dependents that are to be removed.
  494. It is insanely risky to remove the dependents of an essential package! */
  495. struct APT_HIDDEN PrettyFullNameWithDue {
  496. std::map<unsigned long long, pkgCache::PkgIterator> due;
  497. PrettyFullNameWithDue() {}
  498. std::string operator() (pkgCache::PkgIterator const &Pkg)
  499. {
  500. std::string const A = PrettyFullName(Pkg);
  501. std::map<unsigned long long, pkgCache::PkgIterator>::const_iterator d = due.find(Pkg->ID);
  502. if (d == due.end())
  503. return A;
  504. std::string const B = PrettyFullName(d->second);
  505. std::ostringstream outstr;
  506. ioprintf(outstr, _("%s (due to %s)"), A.c_str(), B.c_str());
  507. return outstr.str();
  508. }
  509. };
  510. bool ShowEssential(ostream &out,CacheFile &Cache)
  511. {
  512. std::vector<bool> Added(Cache->Head().PackageCount, false);
  513. APT::PackageDeque pkglist;
  514. PrettyFullNameWithDue withdue;
  515. SortedPackageUniverse Universe(Cache);
  516. for (pkgCache::PkgIterator const &I: Universe)
  517. {
  518. if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  519. (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
  520. continue;
  521. // The essential package is being removed
  522. if (Cache[I].Delete() == false)
  523. continue;
  524. if (Added[I->ID] == false)
  525. {
  526. Added[I->ID] = true;
  527. pkglist.insert(I);
  528. }
  529. if (I->CurrentVer == 0)
  530. continue;
  531. // Print out any essential package depenendents that are to be removed
  532. for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; ++D)
  533. {
  534. // Skip everything but depends
  535. if (D->Type != pkgCache::Dep::PreDepends &&
  536. D->Type != pkgCache::Dep::Depends)
  537. continue;
  538. pkgCache::PkgIterator P = D.SmartTargetPkg();
  539. if (Cache[P].Delete() == true)
  540. {
  541. if (Added[P->ID] == true)
  542. continue;
  543. Added[P->ID] = true;
  544. pkglist.insert(P);
  545. withdue.due[P->ID] = I;
  546. }
  547. }
  548. }
  549. return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
  550. "This should NOT be done unless you know exactly what you are doing!"),
  551. pkglist, &AlwaysTrue, withdue, &EmptyString);
  552. }
  553. /*}}}*/
  554. // Stats - Show some statistics /*{{{*/
  555. // ---------------------------------------------------------------------
  556. /* */
  557. void Stats(ostream &out,pkgDepCache &Dep)
  558. {
  559. unsigned long Upgrade = 0;
  560. unsigned long Downgrade = 0;
  561. unsigned long Install = 0;
  562. unsigned long ReInstall = 0;
  563. for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; ++I)
  564. {
  565. if (Dep[I].NewInstall() == true)
  566. Install++;
  567. else
  568. {
  569. if (Dep[I].Upgrade() == true)
  570. Upgrade++;
  571. else
  572. if (Dep[I].Downgrade() == true)
  573. Downgrade++;
  574. }
  575. if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
  576. ReInstall++;
  577. }
  578. ioprintf(out,_("%lu upgraded, %lu newly installed, "),
  579. Upgrade,Install);
  580. if (ReInstall != 0)
  581. ioprintf(out,_("%lu reinstalled, "),ReInstall);
  582. if (Downgrade != 0)
  583. ioprintf(out,_("%lu downgraded, "),Downgrade);
  584. ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
  585. Dep.DelCount(),Dep.KeepCount());
  586. if (Dep.BadCount() != 0)
  587. ioprintf(out,_("%lu not fully installed or removed.\n"),
  588. Dep.BadCount());
  589. }
  590. /*}}}*/
  591. // YnPrompt - Yes No Prompt. /*{{{*/
  592. // ---------------------------------------------------------------------
  593. /* Returns true on a Yes.*/
  594. bool YnPrompt(char const * const Question, bool Default)
  595. {
  596. auto const AssumeYes = _config->FindB("APT::Get::Assume-Yes",false);
  597. auto const AssumeNo = _config->FindB("APT::Get::Assume-No",false);
  598. // if we ask interactively, show warnings/notices before the question
  599. if (AssumeYes == false && AssumeNo == false)
  600. {
  601. if (_config->FindI("quiet",0) > 0)
  602. _error->DumpErrors(c2out);
  603. else
  604. _error->DumpErrors(c2out, GlobalError::DEBUG);
  605. }
  606. c2out << Question << std::flush;
  607. /* nl_langinfo does not support LANGUAGE setting, so we unset it here
  608. to have the help-message (hopefully) match the expected characters */
  609. char * language = getenv("LANGUAGE");
  610. if (language != NULL)
  611. language = strdup(language);
  612. if (language != NULL)
  613. unsetenv("LANGUAGE");
  614. if (Default == true)
  615. // TRANSLATOR: Yes/No question help-text: defaulting to Y[es]
  616. // e.g. "Do you want to continue? [Y/n] "
  617. // The user has to answer with an input matching the
  618. // YESEXPR/NOEXPR defined in your l10n.
  619. c2out << " " << _("[Y/n]") << " " << std::flush;
  620. else
  621. // TRANSLATOR: Yes/No question help-text: defaulting to N[o]
  622. // e.g. "Should this file be removed? [y/N] "
  623. // The user has to answer with an input matching the
  624. // YESEXPR/NOEXPR defined in your l10n.
  625. c2out << " " << _("[y/N]") << " " << std::flush;
  626. if (language != NULL)
  627. {
  628. setenv("LANGUAGE", language, 0);
  629. free(language);
  630. }
  631. if (AssumeYes)
  632. {
  633. // TRANSLATOR: "Yes" answer printed for a yes/no question if --assume-yes is set
  634. c1out << _("Y") << std::endl;
  635. return true;
  636. }
  637. else if (AssumeNo)
  638. {
  639. // TRANSLATOR: "No" answer printed for a yes/no question if --assume-no is set
  640. c1out << _("N") << std::endl;
  641. return false;
  642. }
  643. char response[1024] = "";
  644. std::cin.getline(response, sizeof(response));
  645. if (!std::cin)
  646. return false;
  647. if (strlen(response) == 0)
  648. return Default;
  649. regex_t Pattern;
  650. int Res;
  651. Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
  652. REG_EXTENDED|REG_ICASE|REG_NOSUB);
  653. if (Res != 0) {
  654. char Error[300];
  655. regerror(Res,&Pattern,Error,sizeof(Error));
  656. return _error->Error(_("Regex compilation error - %s"),Error);
  657. }
  658. Res = regexec(&Pattern, response, 0, NULL, 0);
  659. if (Res == 0)
  660. return true;
  661. return false;
  662. }
  663. /*}}}*/
  664. // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
  665. // ---------------------------------------------------------------------
  666. /* Returns true on a Yes.*/
  667. bool AnalPrompt(std::string const &Question, const char *Text)
  668. {
  669. if (_config->FindI("quiet",0) > 0)
  670. _error->DumpErrors(c2out);
  671. else
  672. _error->DumpErrors(c2out, GlobalError::DEBUG);
  673. c2out << Question << std::flush;
  674. char Buf[1024];
  675. std::cin.getline(Buf,sizeof(Buf));
  676. if (strcmp(Buf,Text) == 0)
  677. return true;
  678. return false;
  679. }
  680. /*}}}*/
  681. std::string PrettyFullName(pkgCache::PkgIterator const &Pkg)
  682. {
  683. return Pkg.FullName(true);
  684. }
  685. std::string CandidateVersion(pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg)
  686. {
  687. return (*Cache)[Pkg].CandVersion;
  688. }
  689. std::function<std::string(pkgCache::PkgIterator const &)> CandidateVersion(pkgCacheFile * const Cache)
  690. {
  691. return std::bind(static_cast<std::string(*)(pkgCacheFile * const, pkgCache::PkgIterator const&)>(&CandidateVersion), Cache, std::placeholders::_1);
  692. }
  693. std::string CurrentToCandidateVersion(pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg)
  694. {
  695. return std::string((*Cache)[Pkg].CurVersion) + " => " + (*Cache)[Pkg].CandVersion;
  696. }
  697. std::function<std::string(pkgCache::PkgIterator const &)> CurrentToCandidateVersion(pkgCacheFile * const Cache)
  698. {
  699. return std::bind(static_cast<std::string(*)(pkgCacheFile * const, pkgCache::PkgIterator const&)>(&CurrentToCandidateVersion), Cache, std::placeholders::_1);
  700. }
  701. bool AlwaysTrue(pkgCache::PkgIterator const &)
  702. {
  703. return true;
  704. }
  705. std::string EmptyString(pkgCache::PkgIterator const &)
  706. {
  707. return std::string();
  708. }