pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  159. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  160. switch (pkg->status) {
  161. case pkginfo::stat_unpacked:
  162. case pkginfo::stat_halfconfigured:
  163. case pkginfo::stat_halfinstalled:
  164. case pkginfo::stat_triggersawaited:
  165. case pkginfo::stat_triggerspending:
  166. table[index]->ssavail= ssa_broken;
  167. break;
  168. case pkginfo::stat_notinstalled:
  169. case pkginfo::stat_configfiles:
  170. if (!informativeversion(&pkg->available.version)) {
  171. table[index]->ssavail= ssa_notinst_gone;
  172. // FIXME: Disable for now as a workaround, until dselect knows how to properly
  173. // store seen packages.
  174. #if 0
  175. } else if (table[index]->original == pkginfo::want_unknown) {
  176. table[index]->ssavail= ssa_notinst_unseen;
  177. #endif
  178. } else {
  179. table[index]->ssavail= ssa_notinst_seen;
  180. }
  181. break;
  182. case pkginfo::stat_installed:
  183. veri= &table[index]->pkg->installed.version;
  184. vera= &table[index]->pkg->available.version;
  185. if (!informativeversion(vera)) {
  186. table[index]->ssavail= ssa_installed_gone;
  187. } else if (versioncompare(vera,veri) > 0) {
  188. table[index]->ssavail= ssa_installed_newer;
  189. } else {
  190. table[index]->ssavail= ssa_installed_sameold;
  191. }
  192. break;
  193. default:
  194. internerr("unknown stat in ensurestatsortinfo sso_avail");
  195. }
  196. if (debug)
  197. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  198. this,index,table[index]->ssavail);
  199. }
  200. calcssadone= 1;
  201. break;
  202. case sso_state:
  203. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  204. this,calcsssdone);
  205. if (calcsssdone) return;
  206. for (index=0; index < nitems; index++) {
  207. if (debug)
  208. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  209. this,index,table[index]->pkg->name);
  210. switch (table[index]->pkg->status) {
  211. case pkginfo::stat_unpacked:
  212. case pkginfo::stat_halfconfigured:
  213. case pkginfo::stat_halfinstalled:
  214. case pkginfo::stat_triggersawaited:
  215. case pkginfo::stat_triggerspending:
  216. table[index]->ssstate= sss_broken;
  217. break;
  218. case pkginfo::stat_notinstalled:
  219. table[index]->ssstate= sss_notinstalled;
  220. break;
  221. case pkginfo::stat_configfiles:
  222. table[index]->ssstate= sss_configfiles;
  223. break;
  224. case pkginfo::stat_installed:
  225. table[index]->ssstate= sss_installed;
  226. break;
  227. default:
  228. internerr("unknown stat in ensurestatsortinfo sso_state");
  229. }
  230. if (debug)
  231. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  232. this,index,table[index]->ssstate);
  233. }
  234. calcsssdone= 1;
  235. break;
  236. default:
  237. internerr("unknown statsortorder in ensurestatsortinfo");
  238. }
  239. }
  240. void packagelist::sortmakeheads() {
  241. discardheadings();
  242. ensurestatsortinfo();
  243. sortinplace();
  244. assert(nitems);
  245. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  246. "sortorder=%d statsortorder=%d\n",this,sortorder,statsortorder);
  247. int nrealitems= nitems;
  248. addheading(ssa_none,sss_none,pkginfo::pri_unset,0,0);
  249. assert(sortorder != so_unsorted);
  250. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  251. // Important: do not save pointers into table in this function, because
  252. // addheading may need to reallocate table to make it larger !
  253. struct pkginfo *lastpkg;
  254. struct pkginfo *thispkg;
  255. lastpkg= 0;
  256. int a;
  257. for (a=0; a<nrealitems; a++) {
  258. thispkg= table[a]->pkg;
  259. assert(thispkg->name);
  260. int ssdiff= 0;
  261. ssavailval ssavail= ssa_none;
  262. ssstateval ssstate= sss_none;
  263. switch (statsortorder) {
  264. case sso_avail:
  265. ssavail= thispkg->clientdata->ssavail;
  266. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  267. break;
  268. case sso_state:
  269. ssstate= thispkg->clientdata->ssstate;
  270. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  271. break;
  272. case sso_unsorted:
  273. break;
  274. default:
  275. internerr("unknown statsortorder in sortmakeheads");
  276. }
  277. int prioritydiff= (!lastpkg ||
  278. thispkg->priority != lastpkg->priority ||
  279. (thispkg->priority == pkginfo::pri_other &&
  280. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  281. int sectiondiff= (!lastpkg ||
  282. strcasecmp(thispkg->section ? thispkg->section : "",
  283. lastpkg->section ? lastpkg->section : ""));
  284. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  285. " pkg=%s state=%d avail=%d %s priority=%d"
  286. " otherpriority=%s %s section=%s %s\n",
  287. this, thispkg->name,
  288. thispkg->clientdata->ssavail,
  289. thispkg->clientdata->ssstate,
  290. ssdiff ? "*diff" : "same",
  291. thispkg->priority,
  292. thispkg->priority != pkginfo::pri_other ? "<none>"
  293. : thispkg->otherpriority ? thispkg->otherpriority
  294. : "<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. if (debug)
  320. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  321. int allpackages= countpackages();
  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. if (debug)
  336. fprintf(debug,"packagelist[%p]::finalsetup done; recursive=%d nitems=%d\n",
  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. for (iter=iterpkgstart(), nitems=0;
  345. (pkg=iterpkgnext(iter));
  346. ) {
  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. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  355. state->direct= state->original= pkg->want;
  356. if (readwrite && pkg->want == 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= pkg->want;
  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. 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->name) {
  399. if (readwrite) {
  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= selected;
  408. }
  409. pkg->clientdata= 0;
  410. }
  411. }
  412. relations.free();
  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. int packagelist::checksearch(char* rx) {
  433. int r,opt = REG_NOSUB;
  434. if (!rx || !*rx) return 0;
  435. searchdescr=0;
  436. if (searchstring[0]) {
  437. regfree(&searchfsm);
  438. searchstring[0]=0;
  439. }
  440. /* look for search options */
  441. for (r=strlen(rx)-1; r>=0; r--)
  442. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  443. break;
  444. if (r>=0) {
  445. rx[r++]='\0';
  446. if (strcspn(rx+r, "di")!=0) {
  447. displayerror(_("invalid search option given"));
  448. return 0;
  449. }
  450. while (rx[r]) {
  451. if (rx[r]=='i')
  452. opt|=REG_ICASE;
  453. else if (rx[r]=='d')
  454. searchdescr=1;
  455. r++;
  456. }
  457. }
  458. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  459. displayerror(_("error in regular expression"));
  460. return 0;
  461. }
  462. return 1;
  463. }
  464. int packagelist::matchsearch(int index) {
  465. const char* thisname;
  466. thisname=itemname(index);
  467. if (!thisname) return 0; /* Skip things without a name (seperators) */
  468. if (regexec(&searchfsm, thisname, 0, NULL, 0)==0)
  469. return 1;
  470. if (searchdescr) {
  471. const char* descr = table[index]->pkg->available.description;
  472. if (!descr || !*descr) return 0;
  473. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  474. return 1;
  475. }
  476. return 0;
  477. }
  478. pkginfo **packagelist::display() {
  479. // returns list of packages as null-terminated array, which becomes owned
  480. // by the caller, if a recursive check is desired.
  481. // returns 0 if no recursive check is desired.
  482. int response, index;
  483. const keybindings::interpretation *interp;
  484. pkginfo **retl;
  485. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  486. setupsigwinch();
  487. startdisplay();
  488. if (!expertmode)
  489. displayhelp(helpmenulist(),'i');
  490. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  491. for (;;) {
  492. if (whatinfo_height) wcursyncup(whatinfowin);
  493. if (doupdate() == ERR)
  494. ohshite(_("doupdate failed"));
  495. signallist= this;
  496. if (sigprocmask(SIG_UNBLOCK, &sigwinchset, 0))
  497. ohshite(_("failed to unblock SIGWINCH"));
  498. do
  499. response= getch();
  500. while (response == ERR && errno == EINTR);
  501. if (sigprocmask(SIG_BLOCK, &sigwinchset, 0))
  502. ohshite(_("failed to re-block SIGWINCH"));
  503. if (response == ERR)
  504. ohshite(_("getch failed"));
  505. interp= (*bindings)(response);
  506. if (debug)
  507. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  508. this,response, interp ? interp->action : "[none]");
  509. if (!interp) { beep(); continue; }
  510. (this->*(interp->pfn))();
  511. if (interp->qa != qa_noquit) break;
  512. }
  513. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  514. enddisplay();
  515. if (interp->qa == qa_quitnochecksave || !readwrite) {
  516. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  517. return 0;
  518. }
  519. if (recursive) {
  520. retl= new pkginfo*[nitems+1];
  521. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  522. retl[nitems]= 0;
  523. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  524. return retl;
  525. } else {
  526. packagelist *sub= new packagelist(bindings,0);
  527. for (index=0; index < nitems; index++)
  528. if (table[index]->pkg->name)
  529. sub->add(table[index]->pkg);
  530. repeatedlydisplay(sub,dp_must);
  531. if (debug)
  532. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  533. return 0;
  534. }
  535. }
  536. /* vi: sw=2 ts=8
  537. */