private-output.cc 25 KB

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