private-output.cc 23 KB

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