pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * pkglist.cc - package list administration
  4. *
  5. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with this; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. extern "C" {
  23. #include <config.h>
  24. }
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. extern "C" {
  30. #include <dpkg.h>
  31. #include <dpkg-db.h>
  32. }
  33. #include "dselect.h"
  34. #include "bindings.h"
  35. int packagelist::compareentries(const struct perpackagestate *a,
  36. const struct perpackagestate *b) {
  37. switch (statsortorder) {
  38. case sso_avail:
  39. if (a->ssavail != b->ssavail) return a->ssavail - b->ssavail;
  40. break;
  41. case sso_state:
  42. if (a->ssstate != b->ssstate) return a->ssstate - b->ssstate;
  43. break;
  44. case sso_unsorted:
  45. break;
  46. default:
  47. internerr("unknown statsortorder in compareentries");
  48. }
  49. const char *asection= a->pkg->section;
  50. if (!asection && a->pkg->name) asection= "";
  51. const char *bsection= b->pkg->section;
  52. if (!bsection && b->pkg->name) 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->name && b->pkg->name ?
  65. strcasecmp(a->pkg->name, b->pkg->name) :
  66. (!b->pkg->name) - (!a->pkg->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->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;
  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. if (debug) fprintf(debug,"packagelist[%p]::addheading(%d,%d,%d,%s,%s)\n",
  113. this,ssavail,ssstate,priority,
  114. otherpriority ? otherpriority : _("<null>"),
  115. section ? section : _("<null>"));
  116. struct pkginfo *newhead= new pkginfo;
  117. newhead->name= 0;
  118. newhead->priority= priority;
  119. newhead->otherpriority= otherpriority;
  120. newhead->section= section;
  121. struct perpackagestate *newstate= new perpackagestate;
  122. newstate->pkg= newhead;
  123. newstate->uprec= headings;
  124. headings= newstate;
  125. newstate->ssavail= ssavail;
  126. newstate->ssstate= ssstate;
  127. newhead->clientdata= newstate;
  128. table[nitems++]= newstate;
  129. }
  130. static packagelist *sortpackagelist;
  131. int qsort_compareentries(const void *a, const void *b) {
  132. return sortpackagelist->compareentries(*(const struct perpackagestate**)a,
  133. *(const struct perpackagestate**)b);
  134. }
  135. void packagelist::sortinplace() {
  136. sortpackagelist= this;
  137. if (debug) fprintf(debug,"packagelist[%p]::sortinplace()\n",this);
  138. qsort(table,nitems,sizeof(struct pkginfoperfile*),qsort_compareentries);
  139. }
  140. void packagelist::ensurestatsortinfo() {
  141. const struct versionrevision *veri;
  142. const struct versionrevision *vera;
  143. struct pkginfo *pkg;
  144. int index;
  145. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() "
  146. "sortorder=%d nitems=%d\n",this,statsortorder,nitems);
  147. switch (statsortorder) {
  148. case sso_unsorted:
  149. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() unsorted\n",this);
  150. return;
  151. case sso_avail:
  152. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcssadone=%d\n",
  153. this,calcssadone);
  154. if (calcssadone) return;
  155. for (index=0; index < nitems; index++) {
  156. if (debug)
  157. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  158. this,index,table[index]->pkg->name);
  159. pkg= table[index]->pkg;
  160. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  161. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  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. } else if (table[index]->original == pkginfo::want_unknown) {
  175. table[index]->ssavail= ssa_notinst_unseen;
  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. int maxx, maxy;
  313. getmaxyx(listpad,maxx,maxy);
  314. if (nitems > maxy) {
  315. delwin(listpad);
  316. listpad= newpad(nitems+1, total_width);
  317. if (!listpad) ohshite("failed to create larger baselist pad");
  318. } else if (nitems < maxy) {
  319. werase(listpad);
  320. }
  321. }
  322. sortinplace();
  323. }
  324. void packagelist::initialsetup() {
  325. if (debug)
  326. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  327. int allpackages= countpackages();
  328. datatable= new struct perpackagestate[allpackages];
  329. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  330. table= new struct perpackagestate*[nallocated];
  331. depsdone= 0;
  332. unavdone= 0;
  333. currentinfo= 0;
  334. headings= 0;
  335. verbose= 0;
  336. calcssadone= calcsssdone= 0;
  337. searchdescr= 0;
  338. }
  339. void packagelist::finalsetup() {
  340. setcursor(0);
  341. if (debug)
  342. fprintf(debug,"packagelist[%p]::finalsetup done; recursive=%d nitems=%d\n",
  343. this, recursive, nitems);
  344. }
  345. packagelist::packagelist(keybindings *kb) : baselist(kb) {
  346. // nonrecursive
  347. initialsetup();
  348. struct pkgiterator *iter;
  349. struct pkginfo *pkg;
  350. for (iter=iterpkgstart(), nitems=0;
  351. (pkg=iterpkgnext(iter));
  352. ) {
  353. struct perpackagestate *state= &datatable[nitems];
  354. state->pkg= pkg;
  355. if (pkg->status == pkginfo::stat_notinstalled &&
  356. !pkg->files &&
  357. pkg->want != pkginfo::want_install) {
  358. pkg->clientdata= 0; continue;
  359. }
  360. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  361. state->direct= state->original= pkg->want;
  362. if (readwrite && pkg->want == pkginfo::want_unknown) {
  363. state->suggested=
  364. pkg->status == pkginfo::stat_installed ||
  365. pkg->priority <= pkginfo::pri_standard /* fixme: configurable */
  366. ? pkginfo::want_install : pkginfo::want_purge;
  367. state->spriority= sp_inherit;
  368. } else {
  369. state->suggested= pkg->want;
  370. state->spriority= sp_fixed;
  371. }
  372. state->dpriority= dp_must;
  373. state->selected= state->suggested;
  374. state->uprec= 0;
  375. state->relations.init();
  376. pkg->clientdata= state;
  377. table[nitems]= state;
  378. nitems++;
  379. }
  380. if (!nitems) ohshit("There are no packages.");
  381. recursive= 0;
  382. sortorder= so_priority;
  383. statsortorder= sso_avail;
  384. versiondisplayopt= vdo_both;
  385. sortmakeheads();
  386. finalsetup();
  387. }
  388. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  389. // takes over responsibility for pkgltab (recursive)
  390. initialsetup();
  391. recursive= 1;
  392. nitems= 0;
  393. if (pkgltab) {
  394. add(pkgltab);
  395. delete[] pkgltab;
  396. }
  397. sortorder= so_unsorted;
  398. statsortorder= sso_unsorted;
  399. versiondisplayopt= vdo_none;
  400. finalsetup();
  401. }
  402. void perpackagestate::free(int recursive) {
  403. if (pkg->name) {
  404. if (readwrite) {
  405. if (uprec) {
  406. assert(recursive);
  407. uprec->selected= selected;
  408. pkg->clientdata= uprec;
  409. } else {
  410. assert(!recursive);
  411. if (pkg->want != selected) {
  412. pkg->want= selected;
  413. }
  414. pkg->clientdata= 0;
  415. }
  416. }
  417. relations.free();
  418. }
  419. }
  420. packagelist::~packagelist() {
  421. if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this);
  422. if (searchstring[0])
  423. regfree(&searchfsm);
  424. discardheadings();
  425. int index;
  426. for (index=0; index<nitems; index++) table[index]->free(recursive);
  427. delete[] table;
  428. delete[] datatable;
  429. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  430. doneent *search, *next;
  431. for (search=depsdone; search; search=next) {
  432. next= search->next;
  433. delete search;
  434. }
  435. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  436. }
  437. int packagelist::checksearch(char* rx) {
  438. int r,opt = REG_NOSUB;
  439. if (!rx || !*rx) return 0;
  440. searchdescr=0;
  441. if (searchstring[0]) {
  442. regfree(&searchfsm);
  443. searchstring[0]=0;
  444. }
  445. /* look for search options */
  446. for (r=strlen(rx)-1; r>=0; r--)
  447. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  448. break;
  449. if (r>=0) {
  450. rx[r++]='\0';
  451. if (strcspn(rx+r, "di")!=0) {
  452. displayerror(_("invalid search option given"));
  453. return 0;
  454. }
  455. while (rx[r]) {
  456. if (rx[r]=='i')
  457. opt|=REG_ICASE;
  458. else if (rx[r]=='d')
  459. searchdescr=1;
  460. r++;
  461. }
  462. }
  463. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  464. displayerror(_("error in regular expression"));
  465. return 0;
  466. }
  467. return 1;
  468. }
  469. int packagelist::matchsearch(int index) {
  470. const char* thisname;
  471. thisname=itemname(index);
  472. if (!thisname) return 0; /* Skip things without a name (seperators) */
  473. if (regexec(&searchfsm, thisname, 0, NULL, 0)==0)
  474. return 1;
  475. if (searchdescr) {
  476. const char* descr = table[index]->pkg->available.description;
  477. if (!descr || !*descr) return 0;
  478. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  479. return 1;
  480. }
  481. return 0;
  482. }
  483. pkginfo **packagelist::display() {
  484. // returns list of packages as null-terminated array, which becomes owned
  485. // by the caller, if a recursive check is desired.
  486. // returns 0 if no recursive check is desired.
  487. int response, index;
  488. const keybindings::interpretation *interp;
  489. pkginfo **retl;
  490. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  491. setupsigwinch();
  492. startdisplay();
  493. if (!expertmode)
  494. displayhelp(helpmenulist(),'i');
  495. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  496. for (;;) {
  497. if (whatinfo_height) wcursyncup(whatinfowin);
  498. if (doupdate() == ERR) ohshite("doupdate failed");
  499. signallist= this;
  500. if (sigprocmask(SIG_UNBLOCK,&sigwinchset,0)) ohshite("failed to unblock SIGWINCH");
  501. do
  502. response= getch();
  503. while (response == ERR && errno == EINTR);
  504. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite("failed to re-block SIGWINCH");
  505. if (response == ERR) ohshite("getch failed");
  506. interp= (*bindings)(response);
  507. if (debug)
  508. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  509. this,response, interp ? interp->action : "[none]");
  510. if (!interp) { beep(); continue; }
  511. (this->*(interp->pfn))();
  512. if (interp->qa != qa_noquit) break;
  513. }
  514. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  515. enddisplay();
  516. if (interp->qa == qa_quitnochecksave || !readwrite) {
  517. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  518. return 0;
  519. }
  520. if (recursive) {
  521. retl= new pkginfo*[nitems+1];
  522. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  523. retl[nitems]= 0;
  524. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  525. return retl;
  526. } else {
  527. packagelist *sub= new packagelist(bindings,0);
  528. for (index=0; index < nitems; index++)
  529. if (table[index]->pkg->name)
  530. sub->add(table[index]->pkg);
  531. repeatedlydisplay(sub,dp_must);
  532. if (debug)
  533. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  534. return 0;
  535. }
  536. }
  537. /* vi: sw=2 ts=8
  538. */