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->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. if (debug) fprintf(debug,"packagelist[%p]::addheading(%d,%d,%d,%s,%s)\n",
  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. if (debug) fprintf(debug,"packagelist[%p]::sortinplace()\n",this);
  136. qsort(table,nitems,sizeof(struct pkginfoperfile*),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. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() "
  144. "sortorder=%d nitems=%d\n",this,statsortorder,nitems);
  145. switch (statsortorder) {
  146. case sso_unsorted:
  147. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() unsorted\n",this);
  148. return;
  149. case sso_avail:
  150. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcssadone=%d\n",
  151. this,calcssadone);
  152. if (calcssadone) return;
  153. for (index=0; index < nitems; index++) {
  154. if (debug)
  155. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  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. if (debug)
  195. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  196. this,index,table[index]->ssavail);
  197. }
  198. calcssadone= 1;
  199. break;
  200. case sso_state:
  201. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  202. this,calcsssdone);
  203. if (calcsssdone) return;
  204. for (index=0; index < nitems; index++) {
  205. if (debug)
  206. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  207. this,index,table[index]->pkg->name);
  208. switch (table[index]->pkg->status) {
  209. case pkginfo::stat_unpacked:
  210. case pkginfo::stat_halfconfigured:
  211. case pkginfo::stat_halfinstalled:
  212. case pkginfo::stat_triggersawaited:
  213. case pkginfo::stat_triggerspending:
  214. table[index]->ssstate= sss_broken;
  215. break;
  216. case pkginfo::stat_notinstalled:
  217. table[index]->ssstate= sss_notinstalled;
  218. break;
  219. case pkginfo::stat_configfiles:
  220. table[index]->ssstate= sss_configfiles;
  221. break;
  222. case pkginfo::stat_installed:
  223. table[index]->ssstate= sss_installed;
  224. break;
  225. default:
  226. internerr("unknown stat in ensurestatsortinfo sso_state");
  227. }
  228. if (debug)
  229. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  230. this,index,table[index]->ssstate);
  231. }
  232. calcsssdone= 1;
  233. break;
  234. default:
  235. internerr("unknown statsortorder in ensurestatsortinfo");
  236. }
  237. }
  238. void packagelist::sortmakeheads() {
  239. discardheadings();
  240. ensurestatsortinfo();
  241. sortinplace();
  242. assert(nitems);
  243. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  244. "sortorder=%d statsortorder=%d\n",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. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  283. " pkg=%s state=%d avail=%d %s priority=%d"
  284. " otherpriority=%s %s section=%s %s\n",
  285. this, thispkg->name,
  286. thispkg->clientdata->ssavail,
  287. thispkg->clientdata->ssstate,
  288. ssdiff ? "*diff" : "same",
  289. thispkg->priority,
  290. thispkg->priority != pkginfo::pri_other ? "<none>"
  291. : thispkg->otherpriority ? thispkg->otherpriority
  292. : "<null>",
  293. prioritydiff ? "*diff*" : "same",
  294. thispkg->section ? thispkg->section : "<null>",
  295. sectiondiff ? "*diff*" : "same");
  296. if (ssdiff)
  297. addheading(ssavail,ssstate,
  298. pkginfo::pri_unset,0, 0);
  299. if (sortorder == so_section && sectiondiff)
  300. addheading(ssavail,ssstate,
  301. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  302. if (sortorder == so_priority && prioritydiff)
  303. addheading(ssavail,ssstate,
  304. thispkg->priority,thispkg->otherpriority, 0);
  305. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  306. addheading(ssavail,ssstate,
  307. thispkg->priority,thispkg->otherpriority,
  308. thispkg->section ? thispkg->section : "");
  309. lastpkg= thispkg;
  310. }
  311. if (listpad) {
  312. werase(listpad);
  313. }
  314. sortinplace();
  315. }
  316. void packagelist::initialsetup() {
  317. if (debug)
  318. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  319. int allpackages= countpackages();
  320. datatable= new struct perpackagestate[allpackages];
  321. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  322. table= new struct perpackagestate*[nallocated];
  323. depsdone= 0;
  324. unavdone= 0;
  325. currentinfo= 0;
  326. headings= 0;
  327. verbose= 0;
  328. calcssadone= calcsssdone= 0;
  329. searchdescr= 0;
  330. }
  331. void packagelist::finalsetup() {
  332. setcursor(0);
  333. if (debug)
  334. fprintf(debug,"packagelist[%p]::finalsetup done; recursive=%d nitems=%d\n",
  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 = iterpkgstart();
  344. while ((pkg = iterpkgnext(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 (readwrite && state->original == pkginfo::want_unknown) {
  355. state->suggested=
  356. pkg->status == pkginfo::stat_installed ||
  357. pkg->priority <= pkginfo::pri_standard /* FIXME: configurable */
  358. ? pkginfo::want_install : pkginfo::want_purge;
  359. state->spriority= sp_inherit;
  360. } else {
  361. state->suggested= state->original;
  362. state->spriority= sp_fixed;
  363. }
  364. state->dpriority= dp_must;
  365. state->selected= state->suggested;
  366. state->uprec= 0;
  367. state->relations.init();
  368. pkg->clientdata= state;
  369. table[nitems]= state;
  370. nitems++;
  371. }
  372. iterpkgend(iter);
  373. if (!nitems)
  374. ohshit(_("There are no packages."));
  375. recursive= 0;
  376. sortorder= so_priority;
  377. statsortorder= sso_avail;
  378. versiondisplayopt= vdo_both;
  379. sortmakeheads();
  380. finalsetup();
  381. }
  382. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  383. // takes over responsibility for pkgltab (recursive)
  384. initialsetup();
  385. recursive= 1;
  386. nitems= 0;
  387. if (pkgltab) {
  388. add(pkgltab);
  389. delete[] pkgltab;
  390. }
  391. sortorder= so_unsorted;
  392. statsortorder= sso_unsorted;
  393. versiondisplayopt= vdo_none;
  394. finalsetup();
  395. }
  396. void perpackagestate::free(int recursive) {
  397. if (pkg->name) {
  398. if (readwrite) {
  399. if (uprec) {
  400. assert(recursive);
  401. uprec->selected= selected;
  402. pkg->clientdata= uprec;
  403. } else {
  404. assert(!recursive);
  405. if (pkg->want != selected &&
  406. !(pkg->want == pkginfo::want_unknown && selected == pkginfo::want_purge)) {
  407. pkg->want= selected;
  408. }
  409. pkg->clientdata= 0;
  410. }
  411. }
  412. relations.destroy();
  413. }
  414. }
  415. packagelist::~packagelist() {
  416. if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this);
  417. if (searchstring[0])
  418. regfree(&searchfsm);
  419. discardheadings();
  420. int index;
  421. for (index=0; index<nitems; index++) table[index]->free(recursive);
  422. delete[] table;
  423. delete[] datatable;
  424. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  425. doneent *search, *next;
  426. for (search=depsdone; search; search=next) {
  427. next= search->next;
  428. delete search;
  429. }
  430. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  431. }
  432. bool
  433. packagelist::checksearch(char *rx)
  434. {
  435. int r,opt = REG_NOSUB;
  436. if (!rx || !*rx)
  437. return false;
  438. searchdescr=0;
  439. if (searchstring[0]) {
  440. regfree(&searchfsm);
  441. searchstring[0]=0;
  442. }
  443. /* look for search options */
  444. for (r=strlen(rx)-1; r>=0; r--)
  445. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  446. break;
  447. if (r>=0) {
  448. rx[r++]='\0';
  449. if (strcspn(rx+r, "di")!=0) {
  450. displayerror(_("invalid search option given"));
  451. return false;
  452. }
  453. while (rx[r]) {
  454. if (rx[r]=='i')
  455. opt|=REG_ICASE;
  456. else if (rx[r]=='d')
  457. searchdescr=1;
  458. r++;
  459. }
  460. }
  461. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  462. displayerror(_("error in regular expression"));
  463. return false;
  464. }
  465. return true;
  466. }
  467. bool
  468. packagelist::matchsearch(int index)
  469. {
  470. const char *name;
  471. name = itemname(index);
  472. if (!name)
  473. return false; /* Skip things without a name (seperators) */
  474. if (regexec(&searchfsm, name, 0, NULL, 0) == 0)
  475. return true;
  476. if (searchdescr) {
  477. const char* descr = table[index]->pkg->available.description;
  478. if (!descr || !*descr)
  479. return false;
  480. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  481. return true;
  482. }
  483. return false;
  484. }
  485. pkginfo **packagelist::display() {
  486. // returns list of packages as null-terminated array, which becomes owned
  487. // by the caller, if a recursive check is desired.
  488. // returns 0 if no recursive check is desired.
  489. int response, index;
  490. const keybindings::interpretation *interp;
  491. pkginfo **retl;
  492. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  493. setupsigwinch();
  494. startdisplay();
  495. if (!expertmode)
  496. displayhelp(helpmenulist(),'i');
  497. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  498. for (;;) {
  499. if (whatinfo_height) wcursyncup(whatinfowin);
  500. if (doupdate() == ERR)
  501. ohshite(_("doupdate failed"));
  502. signallist= this;
  503. if (sigprocmask(SIG_UNBLOCK, &sigwinchset, 0))
  504. ohshite(_("failed to unblock SIGWINCH"));
  505. do
  506. response= getch();
  507. while (response == ERR && errno == EINTR);
  508. if (sigprocmask(SIG_BLOCK, &sigwinchset, 0))
  509. ohshite(_("failed to re-block SIGWINCH"));
  510. if (response == ERR)
  511. ohshite(_("getch failed"));
  512. interp= (*bindings)(response);
  513. if (debug)
  514. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  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 || !readwrite) {
  523. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  524. return 0;
  525. }
  526. if (recursive) {
  527. retl= new pkginfo*[nitems+1];
  528. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  529. retl[nitems]= 0;
  530. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  531. return retl;
  532. } else {
  533. packagelist *sub= new packagelist(bindings,0);
  534. for (index=0; index < nitems; index++)
  535. if (table[index]->pkg->name)
  536. sub->add(table[index]->pkg);
  537. repeatedlydisplay(sub,dp_must);
  538. if (debug)
  539. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  540. return 0;
  541. }
  542. }
  543. /* vi: sw=2 ts=8
  544. */