pkglist.cc 18 KB

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