private-output.cc 24 KB

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