pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * pkglist.cc - package list administration
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. #include <dpkg/string.h>
  33. #include "dselect.h"
  34. #include "bindings.h"
  35. int packagelist::compareentries(const struct perpackagestate *a,
  36. const struct perpackagestate *b) {
  37. switch (statsortorder) {
  38. case sso_avail:
  39. if (a->ssavail != b->ssavail) return a->ssavail - b->ssavail;
  40. break;
  41. case sso_state:
  42. if (a->ssstate != b->ssstate) return a->ssstate - b->ssstate;
  43. break;
  44. case sso_unsorted:
  45. break;
  46. default:
  47. internerr("unknown statsortorder %d", statsortorder);
  48. }
  49. const char *asection= a->pkg->section;
  50. if (!asection && a->pkg->set->name)
  51. asection = "";
  52. const char *bsection= b->pkg->section;
  53. if (!bsection && b->pkg->set->name)
  54. bsection = "";
  55. int c_section=
  56. !asection || !bsection ?
  57. (!bsection) - (!asection) :
  58. !*asection || !*bsection ?
  59. (!*asection) - (!*bsection) :
  60. strcasecmp(asection,bsection);
  61. int c_priority=
  62. a->pkg->priority - b->pkg->priority;
  63. if (!c_priority && a->pkg->priority == PKG_PRIO_OTHER)
  64. c_priority= strcasecmp(a->pkg->otherpriority, b->pkg->otherpriority);
  65. int c_name=
  66. a->pkg->set->name && b->pkg->set->name ?
  67. strcasecmp(a->pkg->set->name, b->pkg->set->name) :
  68. (!b->pkg->set->name) - (!a->pkg->set->name);
  69. switch (sortorder) {
  70. case so_section:
  71. return c_section ? c_section : c_priority ? c_priority : c_name;
  72. case so_priority:
  73. return c_priority ? c_priority : c_section ? c_section : c_name;
  74. case so_alpha:
  75. return c_name;
  76. case so_unsorted:
  77. default:
  78. internerr("unsorted or unknown sort %d", sortorder);
  79. }
  80. /* never reached, make gcc happy */
  81. return 1;
  82. }
  83. void packagelist::discardheadings() {
  84. int a,b;
  85. for (a=0, b=0; a<nitems; a++) {
  86. if (table[a]->pkg->set->name) {
  87. table[b++]= table[a];
  88. }
  89. }
  90. nitems= b;
  91. struct perpackagestate *head, *next;
  92. head= headings;
  93. while (head) {
  94. next= head->uprec;
  95. delete head->pkg->set;
  96. delete head;
  97. head= next;
  98. }
  99. headings = nullptr;
  100. }
  101. void packagelist::addheading(enum ssavailval ssavail,
  102. enum ssstateval ssstate,
  103. pkgpriority priority,
  104. const char *otherpriority,
  105. const char *section) {
  106. assert(nitems <= nallocated);
  107. if (nitems == nallocated) {
  108. nallocated += nallocated+50;
  109. struct perpackagestate **newtable= new struct perpackagestate*[nallocated];
  110. memcpy(newtable, table, nallocated * sizeof(struct perpackagestate *));
  111. delete[] table;
  112. table= newtable;
  113. }
  114. debug(dbg_general, "packagelist[%p]::addheading(%d,%d,%d,%s,%s)",
  115. this, ssavail, ssstate, priority,
  116. otherpriority ? otherpriority : "<null>",
  117. section ? section : "<null>");
  118. struct pkgset *newset = new pkgset;
  119. newset->name = nullptr;
  120. struct pkginfo *newhead = &newset->pkg;
  121. newhead->set = newset;
  122. newhead->priority= priority;
  123. newhead->otherpriority= otherpriority;
  124. newhead->section= section;
  125. struct perpackagestate *newstate= new perpackagestate;
  126. newstate->pkg= newhead;
  127. newstate->uprec= headings;
  128. headings= newstate;
  129. newstate->ssavail= ssavail;
  130. newstate->ssstate= ssstate;
  131. newhead->clientdata= newstate;
  132. table[nitems++]= newstate;
  133. }
  134. static packagelist *sortpackagelist;
  135. int qsort_compareentries(const void *a, const void *b) {
  136. return sortpackagelist->compareentries(*(const struct perpackagestate **)a,
  137. *(const struct perpackagestate **)b);
  138. }
  139. void packagelist::sortinplace() {
  140. sortpackagelist= this;
  141. debug(dbg_general, "packagelist[%p]::sortinplace()", this);
  142. qsort(table, nitems, sizeof(struct pkgbin *), qsort_compareentries);
  143. }
  144. void packagelist::ensurestatsortinfo() {
  145. const struct dpkg_version *veri;
  146. const struct dpkg_version *vera;
  147. struct pkginfo *pkg;
  148. int index;
  149. debug(dbg_general,
  150. "packagelist[%p]::ensurestatsortinfos() sortorder=%d nitems=%d",
  151. this, statsortorder, nitems);
  152. switch (statsortorder) {
  153. case sso_unsorted:
  154. debug(dbg_general, "packagelist[%p]::ensurestatsortinfos() unsorted", this);
  155. return;
  156. case sso_avail:
  157. debug(dbg_general, "packagelist[%p]::ensurestatsortinfos() calcssadone=%d",
  158. this, calcssadone);
  159. if (calcssadone) return;
  160. for (index=0; index < nitems; index++) {
  161. debug(dbg_general, "packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s",
  162. this, index, pkg_name(table[index]->pkg, pnaw_always));
  163. pkg= table[index]->pkg;
  164. switch (pkg->status) {
  165. case PKG_STAT_UNPACKED:
  166. case PKG_STAT_HALFCONFIGURED:
  167. case PKG_STAT_HALFINSTALLED:
  168. case PKG_STAT_TRIGGERSAWAITED:
  169. case PKG_STAT_TRIGGERSPENDING:
  170. table[index]->ssavail= ssa_broken;
  171. break;
  172. case PKG_STAT_NOTINSTALLED:
  173. case PKG_STAT_CONFIGFILES:
  174. if (!dpkg_version_is_informative(&pkg->available.version)) {
  175. table[index]->ssavail= ssa_notinst_gone;
  176. // FIXME: Disable for now as a workaround, until dselect knows how to properly
  177. // store seen packages.
  178. #if 0
  179. } else if (table[index]->original == PKG_WANT_UNKNOWN) {
  180. table[index]->ssavail= ssa_notinst_unseen;
  181. #endif
  182. } else {
  183. table[index]->ssavail= ssa_notinst_seen;
  184. }
  185. break;
  186. case PKG_STAT_INSTALLED:
  187. veri= &table[index]->pkg->installed.version;
  188. vera= &table[index]->pkg->available.version;
  189. if (!dpkg_version_is_informative(vera)) {
  190. table[index]->ssavail= ssa_installed_gone;
  191. } else if (dpkg_version_compare(vera, veri) > 0) {
  192. table[index]->ssavail= ssa_installed_newer;
  193. } else {
  194. table[index]->ssavail= ssa_installed_sameold;
  195. }
  196. break;
  197. default:
  198. internerr("unknown status %d on sso_avail", pkg->status);
  199. }
  200. debug(dbg_general,
  201. "packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d",
  202. this, index, table[index]->ssavail);
  203. }
  204. calcssadone = true;
  205. break;
  206. case sso_state:
  207. debug(dbg_general, "packagelist[%p]::ensurestatsortinfos() calcsssdone=%d",
  208. this, calcsssdone);
  209. if (calcsssdone) return;
  210. for (index=0; index < nitems; index++) {
  211. debug(dbg_general, "packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s",
  212. this, index, pkg_name(table[index]->pkg, pnaw_always));
  213. switch (table[index]->pkg->status) {
  214. case PKG_STAT_UNPACKED:
  215. case PKG_STAT_HALFCONFIGURED:
  216. case PKG_STAT_HALFINSTALLED:
  217. case PKG_STAT_TRIGGERSAWAITED:
  218. case PKG_STAT_TRIGGERSPENDING:
  219. table[index]->ssstate= sss_broken;
  220. break;
  221. case PKG_STAT_NOTINSTALLED:
  222. table[index]->ssstate= sss_notinstalled;
  223. break;
  224. case PKG_STAT_CONFIGFILES:
  225. table[index]->ssstate= sss_configfiles;
  226. break;
  227. case PKG_STAT_INSTALLED:
  228. table[index]->ssstate= sss_installed;
  229. break;
  230. default:
  231. internerr("unknown status %d on sso_state", table[index]->pkg->status);
  232. }
  233. debug(dbg_general,
  234. "packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d",
  235. this, index, table[index]->ssstate);
  236. }
  237. calcsssdone = true;
  238. break;
  239. default:
  240. internerr("unknown statsortorder %d", statsortorder);
  241. }
  242. }
  243. void packagelist::sortmakeheads() {
  244. discardheadings();
  245. ensurestatsortinfo();
  246. sortinplace();
  247. assert(nitems);
  248. debug(dbg_general,
  249. "packagelist[%p]::sortmakeheads() sortorder=%d statsortorder=%d",
  250. this, sortorder, statsortorder);
  251. int nrealitems= nitems;
  252. addheading(ssa_none, sss_none, PKG_PRIO_UNSET, nullptr, nullptr);
  253. assert(sortorder != so_unsorted);
  254. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  255. // Important: do not save pointers into table in this function, because
  256. // addheading may need to reallocate table to make it larger !
  257. struct pkginfo *lastpkg;
  258. struct pkginfo *thispkg;
  259. lastpkg = nullptr;
  260. int a;
  261. for (a=0; a<nrealitems; a++) {
  262. thispkg= table[a]->pkg;
  263. assert(thispkg->set->name);
  264. int ssdiff= 0;
  265. ssavailval ssavail= ssa_none;
  266. ssstateval ssstate= sss_none;
  267. switch (statsortorder) {
  268. case sso_avail:
  269. ssavail= thispkg->clientdata->ssavail;
  270. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  271. break;
  272. case sso_state:
  273. ssstate= thispkg->clientdata->ssstate;
  274. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  275. break;
  276. case sso_unsorted:
  277. break;
  278. default:
  279. internerr("unknown statsortorder %d", statsortorder);
  280. }
  281. int prioritydiff= (!lastpkg ||
  282. thispkg->priority != lastpkg->priority ||
  283. (thispkg->priority == PKG_PRIO_OTHER &&
  284. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  285. int sectiondiff= (!lastpkg ||
  286. strcasecmp(thispkg->section ? thispkg->section : "",
  287. lastpkg->section ? lastpkg->section : ""));
  288. debug(dbg_general,
  289. "packagelist[%p]::sortmakeheads() pkg=%s state=%d avail=%d %s "
  290. "priority=%d otherpriority=%s %s section=%s %s",
  291. this, pkg_name(thispkg, pnaw_always),
  292. thispkg->clientdata->ssavail, thispkg->clientdata->ssstate,
  293. ssdiff ? "*diff" : "same",
  294. thispkg->priority,
  295. thispkg->priority != PKG_PRIO_OTHER ? "<none>" :
  296. thispkg->otherpriority ? thispkg->otherpriority : "<null>",
  297. prioritydiff ? "*diff*" : "same",
  298. thispkg->section ? thispkg->section : "<null>",
  299. sectiondiff ? "*diff*" : "same");
  300. if (ssdiff)
  301. addheading(ssavail,ssstate,
  302. PKG_PRIO_UNSET, nullptr, nullptr);
  303. if (sortorder == so_section && sectiondiff)
  304. addheading(ssavail,ssstate,
  305. PKG_PRIO_UNSET, nullptr,
  306. thispkg->section ? thispkg->section : "");
  307. if (sortorder == so_priority && prioritydiff)
  308. addheading(ssavail,ssstate,
  309. thispkg->priority, thispkg->otherpriority, nullptr);
  310. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  311. addheading(ssavail,ssstate,
  312. thispkg->priority,thispkg->otherpriority,
  313. thispkg->section ? thispkg->section : "");
  314. lastpkg= thispkg;
  315. }
  316. if (listpad) {
  317. werase(listpad);
  318. }
  319. sortinplace();
  320. }
  321. void packagelist::initialsetup() {
  322. debug(dbg_general, "packagelist[%p]::initialsetup()", this);
  323. int allpackages = pkg_db_count_pkg();
  324. datatable= new struct perpackagestate[allpackages];
  325. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  326. table= new struct perpackagestate*[nallocated];
  327. depsdone = nullptr;
  328. unavdone = nullptr;
  329. currentinfo = nullptr;
  330. headings = nullptr;
  331. verbose = false;
  332. calcssadone = calcsssdone = false;
  333. searchdescr = false;
  334. }
  335. void packagelist::finalsetup() {
  336. setcursor(0);
  337. debug(dbg_general, "packagelist[%p]::finalsetup done; recursive=%d nitems=%d",
  338. this, recursive, nitems);
  339. }
  340. packagelist::packagelist(keybindings *kb) : baselist(kb) {
  341. // nonrecursive
  342. initialsetup();
  343. struct pkgiterator *iter;
  344. struct pkginfo *pkg;
  345. nitems = 0;
  346. iter = pkg_db_iter_new();
  347. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  348. struct perpackagestate *state= &datatable[nitems];
  349. state->pkg= pkg;
  350. if (pkg->status == PKG_STAT_NOTINSTALLED &&
  351. !pkg->files &&
  352. pkg->want != PKG_WANT_INSTALL) {
  353. pkg->clientdata = nullptr;
  354. continue;
  355. }
  356. // treat all unknown packages as already seen
  357. state->direct = state->original = (pkg->want == PKG_WANT_UNKNOWN ? PKG_WANT_PURGE : pkg->want);
  358. if (modstatdb_get_status() == msdbrw_write &&
  359. state->original == PKG_WANT_UNKNOWN) {
  360. state->suggested=
  361. pkg->status == PKG_STAT_INSTALLED ||
  362. pkg->priority <= PKG_PRIO_STANDARD /* FIXME: configurable */
  363. ? PKG_WANT_INSTALL : PKG_WANT_PURGE;
  364. state->spriority= sp_inherit;
  365. } else {
  366. state->suggested= state->original;
  367. state->spriority= sp_fixed;
  368. }
  369. state->dpriority= dp_must;
  370. state->selected= state->suggested;
  371. state->uprec = nullptr;
  372. state->relations.init();
  373. pkg->clientdata= state;
  374. table[nitems]= state;
  375. nitems++;
  376. }
  377. pkg_db_iter_free(iter);
  378. if (!nitems)
  379. ohshit(_("there are no packages"));
  380. recursive = false;
  381. sortorder= so_priority;
  382. statsortorder= sso_avail;
  383. archdisplayopt = ado_both;
  384. versiondisplayopt= vdo_both;
  385. sortmakeheads();
  386. finalsetup();
  387. }
  388. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  389. // takes over responsibility for pkgltab (recursive)
  390. initialsetup();
  391. recursive = true;
  392. nitems= 0;
  393. if (pkgltab) {
  394. add(pkgltab);
  395. delete[] pkgltab;
  396. }
  397. sortorder= so_unsorted;
  398. statsortorder= sso_unsorted;
  399. archdisplayopt = ado_none;
  400. versiondisplayopt= vdo_none;
  401. finalsetup();
  402. }
  403. void
  404. perpackagestate::free(bool recursive)
  405. {
  406. if (pkg->set->name) {
  407. if (modstatdb_get_status() == msdbrw_write) {
  408. if (uprec) {
  409. assert(recursive);
  410. uprec->selected= selected;
  411. pkg->clientdata= uprec;
  412. } else {
  413. assert(!recursive);
  414. if (pkg->want != selected &&
  415. !(pkg->want == PKG_WANT_UNKNOWN && selected == PKG_WANT_PURGE)) {
  416. pkg->want= selected;
  417. }
  418. pkg->clientdata = nullptr;
  419. }
  420. }
  421. relations.destroy();
  422. }
  423. }
  424. packagelist::~packagelist() {
  425. debug(dbg_general, "packagelist[%p]::~packagelist()", this);
  426. if (searchstring[0])
  427. regfree(&searchfsm);
  428. discardheadings();
  429. int index;
  430. for (index=0; index<nitems; index++) table[index]->free(recursive);
  431. delete[] table;
  432. delete[] datatable;
  433. debug(dbg_general, "packagelist[%p]::~packagelist() tables freed", this);
  434. doneent *search, *next;
  435. for (search=depsdone; search; search=next) {
  436. next= search->next;
  437. delete search;
  438. }
  439. debug(dbg_general, "packagelist[%p]::~packagelist() done", this);
  440. }
  441. bool
  442. packagelist::checksearch(char *rx)
  443. {
  444. int rc, opt = REG_NOSUB;
  445. int pos;
  446. if (str_is_unset(rx))
  447. return false;
  448. searchdescr = false;
  449. if (searchstring[0]) {
  450. regfree(&searchfsm);
  451. searchstring[0]=0;
  452. }
  453. /* look for search options */
  454. for (pos = strlen(rx) - 1; pos >= 0; pos--)
  455. if ((rx[pos] == '/') && ((pos == 0) || (rx[pos - 1] != '\\')))
  456. break;
  457. if (pos >= 0) {
  458. rx[pos++] = '\0';
  459. if (strcspn(rx + pos, "di") != 0) {
  460. displayerror(_("invalid search option given"));
  461. return false;
  462. }
  463. while (rx[pos]) {
  464. if (rx[pos] == 'i')
  465. opt|=REG_ICASE;
  466. else if (rx[pos] == 'd')
  467. searchdescr = true;
  468. pos++;
  469. }
  470. }
  471. rc = regcomp(&searchfsm, rx, opt);
  472. if (rc != 0) {
  473. displayerror(_("error in regular expression"));
  474. return false;
  475. }
  476. return true;
  477. }
  478. bool
  479. packagelist::matchsearch(int index)
  480. {
  481. const char *name;
  482. name = itemname(index);
  483. if (!name)
  484. return false; /* Skip things without a name (seperators) */
  485. if (regexec(&searchfsm, name, 0, nullptr, 0) == 0)
  486. return true;
  487. if (searchdescr) {
  488. const char *descr = table[index]->pkg->available.description;
  489. if (str_is_unset(descr))
  490. return false;
  491. if (regexec(&searchfsm, descr, 0, nullptr, 0) == 0)
  492. return true;
  493. }
  494. return false;
  495. }
  496. pkginfo **packagelist::display() {
  497. // returns list of packages as null-terminated array, which becomes owned
  498. // by the caller, if a recursive check is desired.
  499. // returns 0 if no recursive check is desired.
  500. int response, index;
  501. const keybindings::interpretation *interp;
  502. pkginfo **retl;
  503. debug(dbg_general, "packagelist[%p]::display()", this);
  504. setupsigwinch();
  505. startdisplay();
  506. if (!expertmode)
  507. displayhelp(helpmenulist(),'i');
  508. debug(dbg_general, "packagelist[%p]::display() entering loop", this);
  509. for (;;) {
  510. if (whatinfo_height) wcursyncup(whatinfowin);
  511. if (doupdate() == ERR)
  512. ohshite(_("doupdate failed"));
  513. signallist= this;
  514. sigwinch_mask(SIG_UNBLOCK);
  515. do
  516. response= getch();
  517. while (response == ERR && errno == EINTR);
  518. sigwinch_mask(SIG_BLOCK);
  519. if (response == ERR)
  520. ohshite(_("getch failed"));
  521. interp= (*bindings)(response);
  522. debug(dbg_general, "packagelist[%p]::display() response=%d interp=%s",
  523. this, response, interp ? interp->action : "[none]");
  524. if (!interp) { beep(); continue; }
  525. (this->*(interp->pfn))();
  526. if (interp->qa != qa_noquit) break;
  527. }
  528. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  529. enddisplay();
  530. if (interp->qa == qa_quitnochecksave ||
  531. modstatdb_get_status() == msdbrw_readonly) {
  532. debug(dbg_general, "packagelist[%p]::display() done - quitNOcheck", this);
  533. return nullptr;
  534. }
  535. if (recursive) {
  536. retl= new pkginfo*[nitems+1];
  537. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  538. retl[nitems] = nullptr;
  539. debug(dbg_general, "packagelist[%p]::display() done, retl=%p", this, retl);
  540. return retl;
  541. } else {
  542. packagelist *sub = new packagelist(bindings, nullptr);
  543. for (index=0; index < nitems; index++)
  544. if (table[index]->pkg->set->name)
  545. sub->add(table[index]->pkg);
  546. repeatedlydisplay(sub,dp_must);
  547. debug(dbg_general,
  548. "packagelist[%p]::display() done, not recursive no retl", this);
  549. return nullptr;
  550. }
  551. }