pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 <http://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= 0;
  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 = NULL;
  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= 1;
  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= 1;
  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,0,0);
  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= 0;
  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,0, 0);
  302. if (sortorder == so_section && sectiondiff)
  303. addheading(ssavail,ssstate,
  304. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  305. if (sortorder == so_priority && prioritydiff)
  306. addheading(ssavail,ssstate,
  307. thispkg->priority,thispkg->otherpriority, 0);
  308. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  309. addheading(ssavail,ssstate,
  310. thispkg->priority,thispkg->otherpriority,
  311. thispkg->section ? thispkg->section : "");
  312. lastpkg= thispkg;
  313. }
  314. if (listpad) {
  315. werase(listpad);
  316. }
  317. sortinplace();
  318. }
  319. void packagelist::initialsetup() {
  320. debug(dbg_general, "packagelist[%p]::initialsetup()", this);
  321. int allpackages = pkg_db_count_pkg();
  322. datatable= new struct perpackagestate[allpackages];
  323. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  324. table= new struct perpackagestate*[nallocated];
  325. depsdone= 0;
  326. unavdone= 0;
  327. currentinfo= 0;
  328. headings= 0;
  329. verbose= 0;
  330. calcssadone= calcsssdone= 0;
  331. searchdescr= 0;
  332. }
  333. void packagelist::finalsetup() {
  334. setcursor(0);
  335. debug(dbg_general, "packagelist[%p]::finalsetup done; recursive=%d nitems=%d",
  336. this, recursive, nitems);
  337. }
  338. packagelist::packagelist(keybindings *kb) : baselist(kb) {
  339. // nonrecursive
  340. initialsetup();
  341. struct pkgiterator *iter;
  342. struct pkginfo *pkg;
  343. nitems = 0;
  344. iter = pkg_db_iter_new();
  345. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  346. struct perpackagestate *state= &datatable[nitems];
  347. state->pkg= pkg;
  348. if (pkg->status == pkginfo::stat_notinstalled &&
  349. !pkg->files &&
  350. pkg->want != pkginfo::want_install) {
  351. pkg->clientdata= 0; continue;
  352. }
  353. // treat all unknown packages as already seen
  354. state->direct= state->original= (pkg->want == pkginfo::want_unknown ? pkginfo::want_purge : pkg->want);
  355. if (modstatdb_get_status() == msdbrw_write &&
  356. state->original == pkginfo::want_unknown) {
  357. state->suggested=
  358. pkg->status == pkginfo::stat_installed ||
  359. pkg->priority <= pkginfo::pri_standard /* FIXME: configurable */
  360. ? pkginfo::want_install : pkginfo::want_purge;
  361. state->spriority= sp_inherit;
  362. } else {
  363. state->suggested= state->original;
  364. state->spriority= sp_fixed;
  365. }
  366. state->dpriority= dp_must;
  367. state->selected= state->suggested;
  368. state->uprec= 0;
  369. state->relations.init();
  370. pkg->clientdata= state;
  371. table[nitems]= state;
  372. nitems++;
  373. }
  374. pkg_db_iter_free(iter);
  375. if (!nitems)
  376. ohshit(_("there are no packages"));
  377. recursive= 0;
  378. sortorder= so_priority;
  379. statsortorder= sso_avail;
  380. versiondisplayopt= vdo_both;
  381. sortmakeheads();
  382. finalsetup();
  383. }
  384. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  385. // takes over responsibility for pkgltab (recursive)
  386. initialsetup();
  387. recursive= 1;
  388. nitems= 0;
  389. if (pkgltab) {
  390. add(pkgltab);
  391. delete[] pkgltab;
  392. }
  393. sortorder= so_unsorted;
  394. statsortorder= sso_unsorted;
  395. versiondisplayopt= vdo_none;
  396. finalsetup();
  397. }
  398. void perpackagestate::free(int recursive) {
  399. if (pkg->set->name) {
  400. if (modstatdb_get_status() == msdbrw_write) {
  401. if (uprec) {
  402. assert(recursive);
  403. uprec->selected= selected;
  404. pkg->clientdata= uprec;
  405. } else {
  406. assert(!recursive);
  407. if (pkg->want != selected &&
  408. !(pkg->want == pkginfo::want_unknown && selected == pkginfo::want_purge)) {
  409. pkg->want= selected;
  410. }
  411. pkg->clientdata= 0;
  412. }
  413. }
  414. relations.destroy();
  415. }
  416. }
  417. packagelist::~packagelist() {
  418. debug(dbg_general, "packagelist[%p]::~packagelist()", this);
  419. if (searchstring[0])
  420. regfree(&searchfsm);
  421. discardheadings();
  422. int index;
  423. for (index=0; index<nitems; index++) table[index]->free(recursive);
  424. delete[] table;
  425. delete[] datatable;
  426. debug(dbg_general, "packagelist[%p]::~packagelist() tables freed", this);
  427. doneent *search, *next;
  428. for (search=depsdone; search; search=next) {
  429. next= search->next;
  430. delete search;
  431. }
  432. debug(dbg_general, "packagelist[%p]::~packagelist() done", this);
  433. }
  434. bool
  435. packagelist::checksearch(char *rx)
  436. {
  437. int r,opt = REG_NOSUB;
  438. if (str_is_unset(rx))
  439. return false;
  440. searchdescr=0;
  441. if (searchstring[0]) {
  442. regfree(&searchfsm);
  443. searchstring[0]=0;
  444. }
  445. /* look for search options */
  446. for (r=strlen(rx)-1; r>=0; r--)
  447. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  448. break;
  449. if (r>=0) {
  450. rx[r++]='\0';
  451. if (strcspn(rx+r, "di")!=0) {
  452. displayerror(_("invalid search option given"));
  453. return false;
  454. }
  455. while (rx[r]) {
  456. if (rx[r]=='i')
  457. opt|=REG_ICASE;
  458. else if (rx[r]=='d')
  459. searchdescr=1;
  460. r++;
  461. }
  462. }
  463. r = regcomp(&searchfsm, rx, opt);
  464. if (r != 0) {
  465. displayerror(_("error in regular expression"));
  466. return false;
  467. }
  468. return true;
  469. }
  470. bool
  471. packagelist::matchsearch(int index)
  472. {
  473. const char *name;
  474. name = itemname(index);
  475. if (!name)
  476. return false; /* Skip things without a name (seperators) */
  477. if (regexec(&searchfsm, name, 0, NULL, 0) == 0)
  478. return true;
  479. if (searchdescr) {
  480. const char *descr = table[index]->pkg->available.description;
  481. if (str_is_unset(descr))
  482. return false;
  483. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  484. return true;
  485. }
  486. return false;
  487. }
  488. pkginfo **packagelist::display() {
  489. // returns list of packages as null-terminated array, which becomes owned
  490. // by the caller, if a recursive check is desired.
  491. // returns 0 if no recursive check is desired.
  492. int response, index;
  493. const keybindings::interpretation *interp;
  494. pkginfo **retl;
  495. debug(dbg_general, "packagelist[%p]::display()", this);
  496. setupsigwinch();
  497. startdisplay();
  498. if (!expertmode)
  499. displayhelp(helpmenulist(),'i');
  500. debug(dbg_general, "packagelist[%p]::display() entering loop", this);
  501. for (;;) {
  502. if (whatinfo_height) wcursyncup(whatinfowin);
  503. if (doupdate() == ERR)
  504. ohshite(_("doupdate failed"));
  505. signallist= this;
  506. if (sigprocmask(SIG_UNBLOCK, &sigwinchset, 0))
  507. ohshite(_("failed to unblock SIGWINCH"));
  508. do
  509. response= getch();
  510. while (response == ERR && errno == EINTR);
  511. if (sigprocmask(SIG_BLOCK, &sigwinchset, 0))
  512. ohshite(_("failed to re-block SIGWINCH"));
  513. if (response == ERR)
  514. ohshite(_("getch failed"));
  515. interp= (*bindings)(response);
  516. debug(dbg_general, "packagelist[%p]::display() response=%d interp=%s",
  517. this, response, interp ? interp->action : "[none]");
  518. if (!interp) { beep(); continue; }
  519. (this->*(interp->pfn))();
  520. if (interp->qa != qa_noquit) break;
  521. }
  522. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  523. enddisplay();
  524. if (interp->qa == qa_quitnochecksave ||
  525. modstatdb_get_status() == msdbrw_readonly) {
  526. debug(dbg_general, "packagelist[%p]::display() done - quitNOcheck", this);
  527. return 0;
  528. }
  529. if (recursive) {
  530. retl= new pkginfo*[nitems+1];
  531. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  532. retl[nitems]= 0;
  533. debug(dbg_general, "packagelist[%p]::display() done, retl=%p", this, retl);
  534. return retl;
  535. } else {
  536. packagelist *sub= new packagelist(bindings,0);
  537. for (index=0; index < nitems; index++)
  538. if (table[index]->pkg->set->name)
  539. sub->add(table[index]->pkg);
  540. repeatedlydisplay(sub,dp_must);
  541. debug(dbg_general,
  542. "packagelist[%p]::display() done, not recursive no retl", this);
  543. return 0;
  544. }
  545. }