private-output.cc 23 KB

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