private-output.cc 23 KB

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