pkglist.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * pkglist.cc - package list administration
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. extern "C" {
  26. #include <config.h>
  27. #include <dpkg.h>
  28. #include <dpkg-db.h>
  29. }
  30. #include "dselect.h"
  31. #include "bindings.h"
  32. int packagelist::compareentries(struct perpackagestate *a,
  33. struct perpackagestate *b) {
  34. switch (statsortorder) {
  35. case sso_avail:
  36. if (a->ssavail != b->ssavail) return a->ssavail - b->ssavail;
  37. break;
  38. case sso_state:
  39. if (a->ssstate != b->ssstate) return a->ssstate - b->ssstate;
  40. break;
  41. case sso_unsorted:
  42. break;
  43. default:
  44. internerr("unknown statsortorder in compareentries");
  45. }
  46. const char *asection= a->pkg->section;
  47. if (!asection && a->pkg->name) asection= "";
  48. const char *bsection= b->pkg->section;
  49. if (!bsection && b->pkg->name) bsection= "";
  50. int c_section=
  51. !asection || !bsection ?
  52. (!bsection) - (!asection) :
  53. !*asection || !*bsection ?
  54. (!*asection) - (!*bsection) :
  55. strcasecmp(asection,bsection);
  56. int c_priority=
  57. a->pkg->priority - b->pkg->priority;
  58. if (!c_priority && a->pkg->priority == pkginfo::pri_other)
  59. c_priority= strcasecmp(a->pkg->otherpriority, b->pkg->otherpriority);
  60. int c_name=
  61. a->pkg->name && b->pkg->name ?
  62. strcasecmp(a->pkg->name, b->pkg->name) :
  63. (!b->pkg->name) - (!a->pkg->name);
  64. switch (sortorder) {
  65. case so_section:
  66. return c_section ? c_section : c_priority ? c_priority : c_name;
  67. case so_priority:
  68. return c_priority ? c_priority : c_section ? c_section : c_name;
  69. case so_alpha:
  70. return c_name;
  71. case so_unsorted:
  72. default:
  73. internerr("unsorted or unknown in compareentries");
  74. }
  75. /* never reached, make gcc happy */
  76. return 1;
  77. }
  78. void packagelist::discardheadings() {
  79. int a,b;
  80. for (a=0, b=0; a<nitems; a++) {
  81. if (table[a]->pkg->name) {
  82. table[b++]= table[a];
  83. }
  84. }
  85. nitems= b;
  86. struct perpackagestate *head, *next;
  87. head= headings;
  88. while (head) {
  89. next= head->uprec;
  90. delete head->pkg;
  91. delete head;
  92. head= next;
  93. }
  94. headings= 0;
  95. }
  96. void packagelist::addheading(enum ssavailval ssavail,
  97. enum ssstateval ssstate,
  98. pkginfo::pkgpriority priority,
  99. const char *otherpriority,
  100. const char *section) {
  101. assert(nitems <= nallocated);
  102. if (nitems == nallocated) {
  103. nallocated += nallocated+50;
  104. struct perpackagestate **newtable= new struct perpackagestate*[nallocated];
  105. memcpy(newtable,table,nallocated*sizeof(struct perpackagestate*));
  106. delete[] table;
  107. table= newtable;
  108. }
  109. if (debug) fprintf(debug,"packagelist[%p]::addheading(%d,%d,%d,%s,%s)\n",
  110. this,ssavail,ssstate,priority,
  111. otherpriority ? otherpriority : _("<null>"),
  112. section ? section : _("<null>"));
  113. struct pkginfo *newhead= new pkginfo;
  114. newhead->name= 0;
  115. newhead->priority= priority;
  116. newhead->otherpriority= (char*)otherpriority;
  117. newhead->section= (char*)section;
  118. struct perpackagestate *newstate= new perpackagestate;
  119. newstate->pkg= newhead;
  120. newstate->uprec= headings;
  121. headings= newstate;
  122. newstate->ssavail= ssavail;
  123. newstate->ssstate= ssstate;
  124. newhead->clientdata= newstate;
  125. table[nitems++]= newstate;
  126. }
  127. static packagelist *sortpackagelist;
  128. int qsort_compareentries(const void *a, const void *b) {
  129. return sortpackagelist->compareentries(*(perpackagestate**)a,
  130. *(perpackagestate**)b);
  131. }
  132. void packagelist::sortinplace() {
  133. sortpackagelist= this;
  134. if (debug) fprintf(debug,"packagelist[%p]::sortinplace()\n",this);
  135. qsort(table,nitems,sizeof(struct pkginfoperfile*),qsort_compareentries);
  136. }
  137. void packagelist::ensurestatsortinfo() {
  138. const struct versionrevision *veri;
  139. const struct versionrevision *vera;
  140. struct pkginfo *pkg;
  141. int index;
  142. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() "
  143. "sortorder=%d nitems=%d\n",this,statsortorder,nitems);
  144. switch (statsortorder) {
  145. case sso_unsorted:
  146. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() unsorted\n",this);
  147. return;
  148. case sso_avail:
  149. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcssadone=%d\n",
  150. this,calcssadone);
  151. if (calcssadone) return;
  152. for (index=0; index < nitems; index++) {
  153. if (debug)
  154. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  155. this,index,table[index]->pkg->name);
  156. pkg= table[index]->pkg;
  157. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  158. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  159. switch (pkg->status) {
  160. case pkginfo::stat_unpacked:
  161. case pkginfo::stat_halfconfigured:
  162. case pkginfo::stat_halfinstalled:
  163. table[index]->ssavail= ssa_broken;
  164. break;
  165. case pkginfo::stat_notinstalled:
  166. case pkginfo::stat_configfiles:
  167. if (!informativeversion(&pkg->available.version)) {
  168. table[index]->ssavail= ssa_notinst_gone;
  169. } else if (table[index]->original == pkginfo::want_unknown) {
  170. table[index]->ssavail= ssa_notinst_unseen;
  171. } else {
  172. table[index]->ssavail= ssa_notinst_seen;
  173. }
  174. break;
  175. case pkginfo::stat_installed:
  176. veri= &table[index]->pkg->installed.version;
  177. vera= &table[index]->pkg->available.version;
  178. if (!informativeversion(vera)) {
  179. table[index]->ssavail= ssa_installed_gone;
  180. } else if (versioncompare(vera,veri) > 0) {
  181. table[index]->ssavail= ssa_installed_newer;
  182. } else {
  183. table[index]->ssavail= ssa_installed_sameold;
  184. }
  185. break;
  186. default:
  187. internerr("unknown stat in ensurestatsortinfo sso_avail");
  188. }
  189. if (debug)
  190. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  191. this,index,table[index]->ssavail);
  192. }
  193. calcssadone= 1;
  194. break;
  195. case sso_state:
  196. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  197. this,calcsssdone);
  198. if (calcsssdone) return;
  199. for (index=0; index < nitems; index++) {
  200. if (debug)
  201. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  202. this,index,table[index]->pkg->name);
  203. switch (table[index]->pkg->status) {
  204. case pkginfo::stat_unpacked:
  205. case pkginfo::stat_halfconfigured:
  206. case pkginfo::stat_halfinstalled:
  207. table[index]->ssstate= sss_broken;
  208. break;
  209. case pkginfo::stat_notinstalled:
  210. table[index]->ssstate= sss_notinstalled;
  211. break;
  212. case pkginfo::stat_configfiles:
  213. table[index]->ssstate= sss_configfiles;
  214. break;
  215. case pkginfo::stat_installed:
  216. table[index]->ssstate= sss_installed;
  217. break;
  218. default:
  219. internerr("unknown stat in ensurestatsortinfo sso_state");
  220. }
  221. if (debug)
  222. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  223. this,index,table[index]->ssstate);
  224. }
  225. calcsssdone= 1;
  226. break;
  227. default:
  228. internerr("unknown statsortorder in ensurestatsortinfo");
  229. }
  230. }
  231. void packagelist::sortmakeheads() {
  232. discardheadings();
  233. ensurestatsortinfo();
  234. sortinplace();
  235. assert(nitems);
  236. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  237. "sortorder=%d statsortorder=%d\n",this,sortorder,statsortorder);
  238. int nrealitems= nitems;
  239. addheading(ssa_none,sss_none,pkginfo::pri_unset,0,0);
  240. assert(sortorder != so_unsorted);
  241. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  242. // Important: do not save pointers into table in this function, because
  243. // addheading may need to reallocate table to make it larger !
  244. struct pkginfo *lastpkg;
  245. struct pkginfo *thispkg;
  246. lastpkg= 0;
  247. int a;
  248. for (a=0; a<nrealitems; a++) {
  249. thispkg= table[a]->pkg;
  250. assert(thispkg->name);
  251. int ssdiff= 0;
  252. ssavailval ssavail= ssa_none;
  253. ssstateval ssstate= sss_none;
  254. switch (statsortorder) {
  255. case sso_avail:
  256. ssavail= thispkg->clientdata->ssavail;
  257. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  258. break;
  259. case sso_state:
  260. ssstate= thispkg->clientdata->ssstate;
  261. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  262. break;
  263. case sso_unsorted:
  264. break;
  265. default:
  266. internerr("unknown statsortorder in sortmakeheads");
  267. }
  268. int prioritydiff= (!lastpkg ||
  269. thispkg->priority != lastpkg->priority ||
  270. (thispkg->priority == pkginfo::pri_other &&
  271. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  272. int sectiondiff= (!lastpkg ||
  273. strcasecmp(thispkg->section ? thispkg->section : "",
  274. lastpkg->section ? lastpkg->section : ""));
  275. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  276. " pkg=%s state=%d avail=%d %s priority=%d"
  277. " otherpriority=%s %s section=%s %s\n",
  278. this, thispkg->name,
  279. thispkg->clientdata->ssavail,
  280. thispkg->clientdata->ssstate,
  281. ssdiff ? "*diff" : "same",
  282. thispkg->priority,
  283. thispkg->priority != pkginfo::pri_other ? "<none>"
  284. : thispkg->otherpriority ? thispkg->otherpriority
  285. : "<null>",
  286. prioritydiff ? "*diff*" : "same",
  287. thispkg->section ? thispkg->section : "<null>",
  288. sectiondiff ? "*diff*" : "same");
  289. if (ssdiff)
  290. addheading(ssavail,ssstate,
  291. pkginfo::pri_unset,0, 0);
  292. if (sortorder == so_section && sectiondiff)
  293. addheading(ssavail,ssstate,
  294. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  295. if (sortorder == so_priority && prioritydiff)
  296. addheading(ssavail,ssstate,
  297. thispkg->priority,thispkg->otherpriority, 0);
  298. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  299. addheading(ssavail,ssstate,
  300. thispkg->priority,thispkg->otherpriority,
  301. thispkg->section ? thispkg->section : "");
  302. lastpkg= thispkg;
  303. }
  304. if (listpad) {
  305. int maxx, maxy;
  306. getmaxyx(listpad,maxx,maxy);
  307. if (nitems > maxy) {
  308. delwin(listpad);
  309. listpad= newpad(nitems+1, total_width);
  310. if (!listpad) ohshite("failed to create larger baselist pad");
  311. } else if (nitems < maxy) {
  312. werase(listpad);
  313. }
  314. }
  315. sortinplace();
  316. }
  317. void packagelist::initialsetup() {
  318. if (debug)
  319. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  320. int allpackages= countpackages();
  321. datatable= new struct perpackagestate[allpackages];
  322. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  323. table= new struct perpackagestate*[nallocated];
  324. depsdone= 0;
  325. unavdone= 0;
  326. currentinfo= 0;
  327. headings= 0;
  328. verbose= 0;
  329. calcssadone= calcsssdone= 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. for (iter=iterpkgstart(), nitems=0;
  343. (pkg=iterpkgnext(iter));
  344. ) {
  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. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  353. state->direct= state->original= pkg->want;
  354. if (readwrite && pkg->want == 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= pkg->want;
  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. if (!nitems) ohshit("There are no packages.");
  373. recursive= 0;
  374. sortorder= so_priority;
  375. statsortorder= sso_avail;
  376. versiondisplayopt= vdo_both;
  377. sortmakeheads();
  378. finalsetup();
  379. }
  380. packagelist::packagelist(keybindings *kb, pkginfo **pkgltab) : baselist(kb) {
  381. // takes over responsibility for pkgltab (recursive)
  382. initialsetup();
  383. recursive= 1;
  384. nitems= 0;
  385. if (pkgltab) {
  386. add(pkgltab);
  387. delete[] pkgltab;
  388. }
  389. sortorder= so_unsorted;
  390. statsortorder= sso_unsorted;
  391. versiondisplayopt= vdo_none;
  392. finalsetup();
  393. }
  394. void perpackagestate::free(int recursive) {
  395. if (pkg->name) {
  396. if (readwrite) {
  397. if (uprec) {
  398. assert(recursive);
  399. uprec->selected= selected;
  400. pkg->clientdata= uprec;
  401. } else {
  402. assert(!recursive);
  403. if (pkg->want != selected) {
  404. pkg->want= selected;
  405. }
  406. pkg->clientdata= 0;
  407. }
  408. }
  409. relations.free();
  410. }
  411. }
  412. packagelist::~packagelist() {
  413. if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this);
  414. discardheadings();
  415. int index;
  416. for (index=0; index<nitems; index++) table[index]->free(recursive);
  417. delete[] table;
  418. delete[] datatable;
  419. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  420. doneent *search, *next;
  421. for (search=depsdone; search; search=next) {
  422. next= search->next;
  423. delete search;
  424. }
  425. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  426. }
  427. pkginfo **packagelist::display() {
  428. // returns list of packages as null-terminated array, which becomes owned
  429. // by the caller, if a recursive check is desired.
  430. // returns 0 if no recursive check is desired.
  431. int response, index;
  432. const keybindings::interpretation *interp;
  433. pkginfo **retl;
  434. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  435. setupsigwinch();
  436. startdisplay();
  437. if (!expertmode)
  438. displayhelp(helpmenulist(),'i');
  439. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  440. for (;;) {
  441. if (whatinfo_height) wcursyncup(whatinfowin);
  442. if (doupdate() == ERR) ohshite("doupdate failed");
  443. signallist= this;
  444. if (sigprocmask(SIG_UNBLOCK,&sigwinchset,0)) ohshite("failed to unblock SIGWINCH");
  445. do
  446. response= getch();
  447. while (response == ERR && errno == EINTR);
  448. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite("failed to re-block SIGWINCH");
  449. if (response == ERR) ohshite("getch failed");
  450. interp= (*bindings)(response);
  451. if (debug)
  452. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  453. this,response, interp ? interp->action : "[none]");
  454. if (!interp) { beep(); continue; }
  455. (this->*(interp->pfn))();
  456. if (interp->qa != qa_noquit) break;
  457. }
  458. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  459. enddisplay();
  460. if (interp->qa == qa_quitnochecksave || !readwrite) {
  461. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  462. return 0;
  463. }
  464. if (recursive) {
  465. retl= new pkginfo*[nitems+1];
  466. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  467. retl[nitems]= 0;
  468. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  469. return retl;
  470. } else {
  471. packagelist *sub= new packagelist(bindings,0);
  472. for (index=0; index < nitems; index++)
  473. if (table[index]->pkg->name)
  474. sub->add(table[index]->pkg);
  475. repeatedlydisplay(sub,dp_must);
  476. if (debug)
  477. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  478. return 0;
  479. }
  480. }