pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. 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. } else if (table[index]->original == pkginfo::want_unknown) {
  173. table[index]->ssavail= ssa_notinst_unseen;
  174. } else {
  175. table[index]->ssavail= ssa_notinst_seen;
  176. }
  177. break;
  178. case pkginfo::stat_installed:
  179. veri= &table[index]->pkg->installed.version;
  180. vera= &table[index]->pkg->available.version;
  181. if (!informativeversion(vera)) {
  182. table[index]->ssavail= ssa_installed_gone;
  183. } else if (versioncompare(vera,veri) > 0) {
  184. table[index]->ssavail= ssa_installed_newer;
  185. } else {
  186. table[index]->ssavail= ssa_installed_sameold;
  187. }
  188. break;
  189. default:
  190. internerr("unknown stat in ensurestatsortinfo sso_avail");
  191. }
  192. if (debug)
  193. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  194. this,index,table[index]->ssavail);
  195. }
  196. calcssadone= 1;
  197. break;
  198. case sso_state:
  199. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  200. this,calcsssdone);
  201. if (calcsssdone) return;
  202. for (index=0; index < nitems; index++) {
  203. if (debug)
  204. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  205. this,index,table[index]->pkg->name);
  206. switch (table[index]->pkg->status) {
  207. case pkginfo::stat_unpacked:
  208. case pkginfo::stat_halfconfigured:
  209. case pkginfo::stat_halfinstalled:
  210. table[index]->ssstate= sss_broken;
  211. break;
  212. case pkginfo::stat_notinstalled:
  213. table[index]->ssstate= sss_notinstalled;
  214. break;
  215. case pkginfo::stat_configfiles:
  216. table[index]->ssstate= sss_configfiles;
  217. break;
  218. case pkginfo::stat_installed:
  219. table[index]->ssstate= sss_installed;
  220. break;
  221. default:
  222. internerr("unknown stat in ensurestatsortinfo sso_state");
  223. }
  224. if (debug)
  225. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  226. this,index,table[index]->ssstate);
  227. }
  228. calcsssdone= 1;
  229. break;
  230. default:
  231. internerr("unknown statsortorder in ensurestatsortinfo");
  232. }
  233. }
  234. void packagelist::sortmakeheads() {
  235. discardheadings();
  236. ensurestatsortinfo();
  237. sortinplace();
  238. assert(nitems);
  239. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  240. "sortorder=%d statsortorder=%d\n",this,sortorder,statsortorder);
  241. int nrealitems= nitems;
  242. addheading(ssa_none,sss_none,pkginfo::pri_unset,0,0);
  243. assert(sortorder != so_unsorted);
  244. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  245. // Important: do not save pointers into table in this function, because
  246. // addheading may need to reallocate table to make it larger !
  247. struct pkginfo *lastpkg;
  248. struct pkginfo *thispkg;
  249. lastpkg= 0;
  250. int a;
  251. for (a=0; a<nrealitems; a++) {
  252. thispkg= table[a]->pkg;
  253. assert(thispkg->name);
  254. int ssdiff= 0;
  255. ssavailval ssavail= ssa_none;
  256. ssstateval ssstate= sss_none;
  257. switch (statsortorder) {
  258. case sso_avail:
  259. ssavail= thispkg->clientdata->ssavail;
  260. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  261. break;
  262. case sso_state:
  263. ssstate= thispkg->clientdata->ssstate;
  264. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  265. break;
  266. case sso_unsorted:
  267. break;
  268. default:
  269. internerr("unknown statsortorder in sortmakeheads");
  270. }
  271. int prioritydiff= (!lastpkg ||
  272. thispkg->priority != lastpkg->priority ||
  273. (thispkg->priority == pkginfo::pri_other &&
  274. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  275. int sectiondiff= (!lastpkg ||
  276. strcasecmp(thispkg->section ? thispkg->section : "",
  277. lastpkg->section ? lastpkg->section : ""));
  278. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  279. " pkg=%s state=%d avail=%d %s priority=%d"
  280. " otherpriority=%s %s section=%s %s\n",
  281. this, thispkg->name,
  282. thispkg->clientdata->ssavail,
  283. thispkg->clientdata->ssstate,
  284. ssdiff ? "*diff" : "same",
  285. thispkg->priority,
  286. thispkg->priority != pkginfo::pri_other ? "<none>"
  287. : thispkg->otherpriority ? thispkg->otherpriority
  288. : "<null>",
  289. prioritydiff ? "*diff*" : "same",
  290. thispkg->section ? thispkg->section : "<null>",
  291. sectiondiff ? "*diff*" : "same");
  292. if (ssdiff)
  293. addheading(ssavail,ssstate,
  294. pkginfo::pri_unset,0, 0);
  295. if (sortorder == so_section && sectiondiff)
  296. addheading(ssavail,ssstate,
  297. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  298. if (sortorder == so_priority && prioritydiff)
  299. addheading(ssavail,ssstate,
  300. thispkg->priority,thispkg->otherpriority, 0);
  301. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  302. addheading(ssavail,ssstate,
  303. thispkg->priority,thispkg->otherpriority,
  304. thispkg->section ? thispkg->section : "");
  305. lastpkg= thispkg;
  306. }
  307. if (listpad) {
  308. int maxx, maxy;
  309. getmaxyx(listpad,maxx,maxy);
  310. if (nitems > maxy) {
  311. delwin(listpad);
  312. listpad= newpad(nitems+1, total_width);
  313. if (!listpad) ohshite("failed to create larger baselist pad");
  314. } else if (nitems < maxy) {
  315. werase(listpad);
  316. }
  317. }
  318. sortinplace();
  319. }
  320. void packagelist::initialsetup() {
  321. if (debug)
  322. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  323. int allpackages= countpackages();
  324. datatable= new struct perpackagestate[allpackages];
  325. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  326. table= new struct perpackagestate*[nallocated];
  327. depsdone= 0;
  328. unavdone= 0;
  329. currentinfo= 0;
  330. headings= 0;
  331. verbose= 0;
  332. calcssadone= calcsssdone= 0;
  333. searchdescr= 0;
  334. }
  335. void packagelist::finalsetup() {
  336. setcursor(0);
  337. if (debug)
  338. fprintf(debug,"packagelist[%p]::finalsetup done; recursive=%d nitems=%d\n",
  339. this, recursive, nitems);
  340. }
  341. packagelist::packagelist(keybindings *kb) : baselist(kb) {
  342. // nonrecursive
  343. initialsetup();
  344. struct pkgiterator *iter;
  345. struct pkginfo *pkg;
  346. for (iter=iterpkgstart(), nitems=0;
  347. (pkg=iterpkgnext(iter));
  348. ) {
  349. struct perpackagestate *state= &datatable[nitems];
  350. state->pkg= pkg;
  351. if (pkg->status == pkginfo::stat_notinstalled &&
  352. !pkg->files &&
  353. pkg->want != pkginfo::want_install) {
  354. pkg->clientdata= 0; continue;
  355. }
  356. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  357. state->direct= state->original= pkg->want;
  358. if (readwrite && pkg->want == pkginfo::want_unknown) {
  359. state->suggested=
  360. pkg->status == pkginfo::stat_installed ||
  361. pkg->priority <= pkginfo::pri_standard /* fixme: configurable */
  362. ? pkginfo::want_install : pkginfo::want_purge;
  363. state->spriority= sp_inherit;
  364. } else {
  365. state->suggested= pkg->want;
  366. state->spriority= sp_fixed;
  367. }
  368. state->dpriority= dp_must;
  369. state->selected= state->suggested;
  370. state->uprec= 0;
  371. state->relations.init();
  372. pkg->clientdata= state;
  373. table[nitems]= state;
  374. nitems++;
  375. }
  376. if (!nitems) ohshit("There are no packages.");
  377. recursive= 0;
  378. sortorder= so_priority;
  379. statsortorder= sso_avail;
  380. versiondisplayopt= vdo_both;
  381. sortmakeheads();
  382. finalsetup();
  383. }
  384. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  385. // takes over responsibility for pkgltab (recursive)
  386. initialsetup();
  387. recursive= 1;
  388. nitems= 0;
  389. if (pkgltab) {
  390. add(pkgltab);
  391. delete[] pkgltab;
  392. }
  393. sortorder= so_unsorted;
  394. statsortorder= sso_unsorted;
  395. versiondisplayopt= vdo_none;
  396. finalsetup();
  397. }
  398. void perpackagestate::free(int recursive) {
  399. if (pkg->name) {
  400. if (readwrite) {
  401. if (uprec) {
  402. assert(recursive);
  403. uprec->selected= selected;
  404. pkg->clientdata= uprec;
  405. } else {
  406. assert(!recursive);
  407. if (pkg->want != selected) {
  408. pkg->want= selected;
  409. }
  410. pkg->clientdata= 0;
  411. }
  412. }
  413. relations.free();
  414. }
  415. }
  416. packagelist::~packagelist() {
  417. if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this);
  418. if (searchstring[0])
  419. regfree(&searchfsm);
  420. discardheadings();
  421. int index;
  422. for (index=0; index<nitems; index++) table[index]->free(recursive);
  423. delete[] table;
  424. delete[] datatable;
  425. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  426. doneent *search, *next;
  427. for (search=depsdone; search; search=next) {
  428. next= search->next;
  429. delete search;
  430. }
  431. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  432. }
  433. int packagelist::checksearch(char* rx) {
  434. int r,opt = REG_NOSUB;
  435. if (!rx || !*rx) return 0;
  436. searchdescr=0;
  437. if (searchstring[0]) {
  438. regfree(&searchfsm);
  439. searchstring[0]=0;
  440. }
  441. /* look for search options */
  442. for (r=strlen(rx)-1; r>=0; r--)
  443. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  444. break;
  445. if (r>=0) {
  446. rx[r++]='\0';
  447. if (strcspn(rx+r, "di")!=0) {
  448. displayerror(_("invalid search option given"));
  449. return 0;
  450. }
  451. while (rx[r]) {
  452. if (rx[r]=='i')
  453. opt|=REG_ICASE;
  454. else if (rx[r]=='d')
  455. searchdescr=1;
  456. r++;
  457. }
  458. }
  459. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  460. displayerror(_("error in regular expression"));
  461. return 0;
  462. }
  463. return 1;
  464. }
  465. int packagelist::matchsearch(int index) {
  466. const char* thisname;
  467. thisname=itemname(index);
  468. if (!thisname) return 0; /* Skip things without a name (seperators) */
  469. if (regexec(&searchfsm, thisname, 0, NULL, 0)==0)
  470. return 1;
  471. if (searchdescr) {
  472. const char* descr = table[index]->pkg->available.description;
  473. if (!descr || !*descr) return 0;
  474. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  475. return 1;
  476. }
  477. return 0;
  478. }
  479. pkginfo **packagelist::display() {
  480. // returns list of packages as null-terminated array, which becomes owned
  481. // by the caller, if a recursive check is desired.
  482. // returns 0 if no recursive check is desired.
  483. int response, index;
  484. const keybindings::interpretation *interp;
  485. pkginfo **retl;
  486. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  487. setupsigwinch();
  488. startdisplay();
  489. if (!expertmode)
  490. displayhelp(helpmenulist(),'i');
  491. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  492. for (;;) {
  493. if (whatinfo_height) wcursyncup(whatinfowin);
  494. if (doupdate() == ERR) ohshite("doupdate failed");
  495. signallist= this;
  496. if (sigprocmask(SIG_UNBLOCK,&sigwinchset,0)) ohshite("failed to unblock SIGWINCH");
  497. do
  498. response= getch();
  499. while (response == ERR && errno == EINTR);
  500. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite("failed to re-block SIGWINCH");
  501. if (response == ERR) ohshite("getch failed");
  502. interp= (*bindings)(response);
  503. if (debug)
  504. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  505. this,response, interp ? interp->action : "[none]");
  506. if (!interp) { beep(); continue; }
  507. (this->*(interp->pfn))();
  508. if (interp->qa != qa_noquit) break;
  509. }
  510. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  511. enddisplay();
  512. if (interp->qa == qa_quitnochecksave || !readwrite) {
  513. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  514. return 0;
  515. }
  516. if (recursive) {
  517. retl= new pkginfo*[nitems+1];
  518. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  519. retl[nitems]= 0;
  520. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  521. return retl;
  522. } else {
  523. packagelist *sub= new packagelist(bindings,0);
  524. for (index=0; index < nitems; index++)
  525. if (table[index]->pkg->name)
  526. sub->add(table[index]->pkg);
  527. repeatedlydisplay(sub,dp_must);
  528. if (debug)
  529. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  530. return 0;
  531. }
  532. }
  533. /* vi: sw=2 ts=8
  534. */