pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 %d in compareentries", statsortorder);
  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 sort %d in compareentries", sortorder);
  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, pkg_name(table[index]->pkg, pnaw_always));
  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 status %d in ensurestatsortinfo sso_avail",
  197. 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 in ensurestatsortinfo sso_state",
  231. 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= 1;
  238. break;
  239. default:
  240. internerr("unknown statsortorder %d in ensurestatsortinfo", 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,pkginfo::pri_unset,0,0);
  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= 0;
  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 in sortmakeheads", statsortorder);
  280. }
  281. int prioritydiff= (!lastpkg ||
  282. thispkg->priority != lastpkg->priority ||
  283. (thispkg->priority == pkginfo::pri_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 != pkginfo::pri_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. pkginfo::pri_unset,0, 0);
  303. if (sortorder == so_section && sectiondiff)
  304. addheading(ssavail,ssstate,
  305. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  306. if (sortorder == so_priority && prioritydiff)
  307. addheading(ssavail,ssstate,
  308. thispkg->priority,thispkg->otherpriority, 0);
  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= 0;
  327. unavdone= 0;
  328. currentinfo= 0;
  329. headings= 0;
  330. verbose= 0;
  331. calcssadone= calcsssdone= 0;
  332. searchdescr= 0;
  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= 0; continue;
  353. }
  354. // treat all unknown packages as already seen
  355. state->direct= state->original= (pkg->want == pkginfo::want_unknown ? pkginfo::want_purge : pkg->want);
  356. if (modstatdb_get_status() == msdbrw_write &&
  357. state->original == pkginfo::want_unknown) {
  358. state->suggested=
  359. pkg->status == pkginfo::stat_installed ||
  360. pkg->priority <= pkginfo::pri_standard /* FIXME: configurable */
  361. ? pkginfo::want_install : pkginfo::want_purge;
  362. state->spriority= sp_inherit;
  363. } else {
  364. state->suggested= state->original;
  365. state->spriority= sp_fixed;
  366. }
  367. state->dpriority= dp_must;
  368. state->selected= state->suggested;
  369. state->uprec= 0;
  370. state->relations.init();
  371. pkg->clientdata= state;
  372. table[nitems]= state;
  373. nitems++;
  374. }
  375. pkg_db_iter_free(iter);
  376. if (!nitems)
  377. ohshit(_("There are no packages."));
  378. recursive= 0;
  379. sortorder= so_priority;
  380. statsortorder= sso_avail;
  381. versiondisplayopt= vdo_both;
  382. sortmakeheads();
  383. finalsetup();
  384. }
  385. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  386. // takes over responsibility for pkgltab (recursive)
  387. initialsetup();
  388. recursive= 1;
  389. nitems= 0;
  390. if (pkgltab) {
  391. add(pkgltab);
  392. delete[] pkgltab;
  393. }
  394. sortorder= so_unsorted;
  395. statsortorder= sso_unsorted;
  396. versiondisplayopt= vdo_none;
  397. finalsetup();
  398. }
  399. void perpackagestate::free(int recursive) {
  400. if (pkg->set->name) {
  401. if (modstatdb_get_status() == msdbrw_write) {
  402. if (uprec) {
  403. assert(recursive);
  404. uprec->selected= selected;
  405. pkg->clientdata= uprec;
  406. } else {
  407. assert(!recursive);
  408. if (pkg->want != selected &&
  409. !(pkg->want == pkginfo::want_unknown && selected == pkginfo::want_purge)) {
  410. pkg->want= selected;
  411. }
  412. pkg->clientdata= 0;
  413. }
  414. }
  415. relations.destroy();
  416. }
  417. }
  418. packagelist::~packagelist() {
  419. debug(dbg_general, "packagelist[%p]::~packagelist()", this);
  420. if (searchstring[0])
  421. regfree(&searchfsm);
  422. discardheadings();
  423. int index;
  424. for (index=0; index<nitems; index++) table[index]->free(recursive);
  425. delete[] table;
  426. delete[] datatable;
  427. debug(dbg_general, "packagelist[%p]::~packagelist() tables freed", this);
  428. doneent *search, *next;
  429. for (search=depsdone; search; search=next) {
  430. next= search->next;
  431. delete search;
  432. }
  433. debug(dbg_general, "packagelist[%p]::~packagelist() done", this);
  434. }
  435. bool
  436. packagelist::checksearch(char *rx)
  437. {
  438. int r,opt = REG_NOSUB;
  439. if (!rx || !*rx)
  440. return false;
  441. searchdescr=0;
  442. if (searchstring[0]) {
  443. regfree(&searchfsm);
  444. searchstring[0]=0;
  445. }
  446. /* look for search options */
  447. for (r=strlen(rx)-1; r>=0; r--)
  448. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  449. break;
  450. if (r>=0) {
  451. rx[r++]='\0';
  452. if (strcspn(rx+r, "di")!=0) {
  453. displayerror(_("invalid search option given"));
  454. return false;
  455. }
  456. while (rx[r]) {
  457. if (rx[r]=='i')
  458. opt|=REG_ICASE;
  459. else if (rx[r]=='d')
  460. searchdescr=1;
  461. r++;
  462. }
  463. }
  464. if ((r=regcomp(&searchfsm, rx, opt))!=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 (!descr || !*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. }
  546. /* vi: sw=2 ts=8
  547. */