pkglist.cc 18 KB

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