private-output.cc 23 KB

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