private-output.cc 23 KB

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