private-output.cc 23 KB

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