pkglist.cc 18 KB

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