private-output.cc 24 KB

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