pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. #include <stdio.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. extern "C" {
  27. #include <config.h>
  28. #include <dpkg.h>
  29. #include <dpkg-db.h>
  30. }
  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. 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. } else if (table[index]->original == pkginfo::want_unknown) {
  171. table[index]->ssavail= ssa_notinst_unseen;
  172. } else {
  173. table[index]->ssavail= ssa_notinst_seen;
  174. }
  175. break;
  176. case pkginfo::stat_installed:
  177. veri= &table[index]->pkg->installed.version;
  178. vera= &table[index]->pkg->available.version;
  179. if (!informativeversion(vera)) {
  180. table[index]->ssavail= ssa_installed_gone;
  181. } else if (versioncompare(vera,veri) > 0) {
  182. table[index]->ssavail= ssa_installed_newer;
  183. } else {
  184. table[index]->ssavail= ssa_installed_sameold;
  185. }
  186. break;
  187. default:
  188. internerr("unknown stat in ensurestatsortinfo sso_avail");
  189. }
  190. if (debug)
  191. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  192. this,index,table[index]->ssavail);
  193. }
  194. calcssadone= 1;
  195. break;
  196. case sso_state:
  197. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  198. this,calcsssdone);
  199. if (calcsssdone) return;
  200. for (index=0; index < nitems; index++) {
  201. if (debug)
  202. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  203. this,index,table[index]->pkg->name);
  204. switch (table[index]->pkg->status) {
  205. case pkginfo::stat_unpacked:
  206. case pkginfo::stat_halfconfigured:
  207. case pkginfo::stat_halfinstalled:
  208. table[index]->ssstate= sss_broken;
  209. break;
  210. case pkginfo::stat_notinstalled:
  211. table[index]->ssstate= sss_notinstalled;
  212. break;
  213. case pkginfo::stat_configfiles:
  214. table[index]->ssstate= sss_configfiles;
  215. break;
  216. case pkginfo::stat_installed:
  217. table[index]->ssstate= sss_installed;
  218. break;
  219. default:
  220. internerr("unknown stat in ensurestatsortinfo sso_state");
  221. }
  222. if (debug)
  223. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  224. this,index,table[index]->ssstate);
  225. }
  226. calcsssdone= 1;
  227. break;
  228. default:
  229. internerr("unknown statsortorder in ensurestatsortinfo");
  230. }
  231. }
  232. void packagelist::sortmakeheads() {
  233. discardheadings();
  234. ensurestatsortinfo();
  235. sortinplace();
  236. assert(nitems);
  237. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  238. "sortorder=%d statsortorder=%d\n",this,sortorder,statsortorder);
  239. int nrealitems= nitems;
  240. addheading(ssa_none,sss_none,pkginfo::pri_unset,0,0);
  241. assert(sortorder != so_unsorted);
  242. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  243. // Important: do not save pointers into table in this function, because
  244. // addheading may need to reallocate table to make it larger !
  245. struct pkginfo *lastpkg;
  246. struct pkginfo *thispkg;
  247. lastpkg= 0;
  248. int a;
  249. for (a=0; a<nrealitems; a++) {
  250. thispkg= table[a]->pkg;
  251. assert(thispkg->name);
  252. int ssdiff= 0;
  253. ssavailval ssavail= ssa_none;
  254. ssstateval ssstate= sss_none;
  255. switch (statsortorder) {
  256. case sso_avail:
  257. ssavail= thispkg->clientdata->ssavail;
  258. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  259. break;
  260. case sso_state:
  261. ssstate= thispkg->clientdata->ssstate;
  262. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  263. break;
  264. case sso_unsorted:
  265. break;
  266. default:
  267. internerr("unknown statsortorder in sortmakeheads");
  268. }
  269. int prioritydiff= (!lastpkg ||
  270. thispkg->priority != lastpkg->priority ||
  271. (thispkg->priority == pkginfo::pri_other &&
  272. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  273. int sectiondiff= (!lastpkg ||
  274. strcasecmp(thispkg->section ? thispkg->section : "",
  275. lastpkg->section ? lastpkg->section : ""));
  276. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  277. " pkg=%s state=%d avail=%d %s priority=%d"
  278. " otherpriority=%s %s section=%s %s\n",
  279. this, thispkg->name,
  280. thispkg->clientdata->ssavail,
  281. thispkg->clientdata->ssstate,
  282. ssdiff ? "*diff" : "same",
  283. thispkg->priority,
  284. thispkg->priority != pkginfo::pri_other ? "<none>"
  285. : thispkg->otherpriority ? thispkg->otherpriority
  286. : "<null>",
  287. prioritydiff ? "*diff*" : "same",
  288. thispkg->section ? thispkg->section : "<null>",
  289. sectiondiff ? "*diff*" : "same");
  290. if (ssdiff)
  291. addheading(ssavail,ssstate,
  292. pkginfo::pri_unset,0, 0);
  293. if (sortorder == so_section && sectiondiff)
  294. addheading(ssavail,ssstate,
  295. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  296. if (sortorder == so_priority && prioritydiff)
  297. addheading(ssavail,ssstate,
  298. thispkg->priority,thispkg->otherpriority, 0);
  299. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  300. addheading(ssavail,ssstate,
  301. thispkg->priority,thispkg->otherpriority,
  302. thispkg->section ? thispkg->section : "");
  303. lastpkg= thispkg;
  304. }
  305. if (listpad) {
  306. int maxx, maxy;
  307. getmaxyx(listpad,maxx,maxy);
  308. if (nitems > maxy) {
  309. delwin(listpad);
  310. listpad= newpad(nitems+1, total_width);
  311. if (!listpad) ohshite("failed to create larger baselist pad");
  312. } else if (nitems < maxy) {
  313. werase(listpad);
  314. }
  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) 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= selected;
  407. }
  408. pkg->clientdata= 0;
  409. }
  410. }
  411. relations.free();
  412. }
  413. }
  414. packagelist::~packagelist() {
  415. if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this);
  416. if (searchstring[0])
  417. regfree(&searchfsm);
  418. discardheadings();
  419. int index;
  420. for (index=0; index<nitems; index++) table[index]->free(recursive);
  421. delete[] table;
  422. delete[] datatable;
  423. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  424. doneent *search, *next;
  425. for (search=depsdone; search; search=next) {
  426. next= search->next;
  427. delete search;
  428. }
  429. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  430. }
  431. int packagelist::checksearch(char* rx) {
  432. int r,opt = REG_NOSUB;
  433. if (!rx || !*rx) return 0;
  434. searchdescr=0;
  435. if (searchstring[0]) {
  436. regfree(&searchfsm);
  437. searchstring[0]=0;
  438. }
  439. /* look for search options */
  440. for (r=strlen(rx)-1; r>=0; r--)
  441. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  442. break;
  443. if (r>=0) {
  444. rx[r++]='\0';
  445. if (strcspn(rx+r, "di")!=0) {
  446. displayerror(_("invalid search option given"));
  447. return 0;
  448. }
  449. while (rx[r]) {
  450. if (rx[r]=='i')
  451. opt|=REG_ICASE;
  452. else if (rx[r]=='d')
  453. searchdescr=1;
  454. r++;
  455. }
  456. }
  457. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  458. displayerror(_("error in regular expression"));
  459. return 0;
  460. }
  461. return 1;
  462. }
  463. int packagelist::matchsearch(int index) {
  464. const char* thisname;
  465. thisname=itemname(index);
  466. if (!thisname) return 0; /* Skip things without a name (seperators) */
  467. if (regexec(&searchfsm, thisname, 0, NULL, 0)==0)
  468. return 1;
  469. if (searchdescr) {
  470. const char* descr = table[index]->pkg->available.description;
  471. if (!descr || !*descr) return 0;
  472. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  473. return 1;
  474. }
  475. return 0;
  476. }
  477. pkginfo **packagelist::display() {
  478. // returns list of packages as null-terminated array, which becomes owned
  479. // by the caller, if a recursive check is desired.
  480. // returns 0 if no recursive check is desired.
  481. int response, index;
  482. const keybindings::interpretation *interp;
  483. pkginfo **retl;
  484. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  485. setupsigwinch();
  486. startdisplay();
  487. if (!expertmode)
  488. displayhelp(helpmenulist(),'i');
  489. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  490. for (;;) {
  491. if (whatinfo_height) wcursyncup(whatinfowin);
  492. if (doupdate() == ERR) ohshite("doupdate failed");
  493. signallist= this;
  494. if (sigprocmask(SIG_UNBLOCK,&sigwinchset,0)) ohshite("failed to unblock SIGWINCH");
  495. do
  496. response= getch();
  497. while (response == ERR && errno == EINTR);
  498. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite("failed to re-block SIGWINCH");
  499. if (response == ERR) ohshite("getch failed");
  500. interp= (*bindings)(response);
  501. if (debug)
  502. fprintf(debug,"packagelist[%p]::display() response=%d interp=%s\n",
  503. this,response, interp ? interp->action : "[none]");
  504. if (!interp) { beep(); continue; }
  505. (this->*(interp->pfn))();
  506. if (interp->qa != qa_noquit) break;
  507. }
  508. pop_cleanup(ehflag_normaltidy); // unset the SIGWINCH handler
  509. enddisplay();
  510. if (interp->qa == qa_quitnochecksave || !readwrite) {
  511. if (debug) fprintf(debug,"packagelist[%p]::display() done - quitNOcheck\n",this);
  512. return 0;
  513. }
  514. if (recursive) {
  515. retl= new pkginfo*[nitems+1];
  516. for (index=0; index<nitems; index++) retl[index]= table[index]->pkg;
  517. retl[nitems]= 0;
  518. if (debug) fprintf(debug,"packagelist[%p]::display() done, retl=%p\n",this,retl);
  519. return retl;
  520. } else {
  521. packagelist *sub= new packagelist(bindings,0);
  522. for (index=0; index < nitems; index++)
  523. if (table[index]->pkg->name)
  524. sub->add(table[index]->pkg);
  525. repeatedlydisplay(sub,dp_must);
  526. if (debug)
  527. fprintf(debug,"packagelist[%p]::display() done, not recursive no retl\n",this);
  528. return 0;
  529. }
  530. }
  531. /* vi: sw=2 ts=8
  532. */