pkglist.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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
  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 <config.h>
  23. #include <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <dpkg.h>
  31. #include <dpkg-db.h>
  32. #include "dselect.h"
  33. #include "bindings.h"
  34. int packagelist::compareentries(const struct perpackagestate *a,
  35. const struct perpackagestate *b) {
  36. switch (statsortorder) {
  37. case sso_avail:
  38. if (a->ssavail != b->ssavail) return a->ssavail - b->ssavail;
  39. break;
  40. case sso_state:
  41. if (a->ssstate != b->ssstate) return a->ssstate - b->ssstate;
  42. break;
  43. case sso_unsorted:
  44. break;
  45. default:
  46. internerr("unknown statsortorder in compareentries");
  47. }
  48. const char *asection= a->pkg->section;
  49. if (!asection && a->pkg->name) asection= "";
  50. const char *bsection= b->pkg->section;
  51. if (!bsection && b->pkg->name) bsection= "";
  52. int c_section=
  53. !asection || !bsection ?
  54. (!bsection) - (!asection) :
  55. !*asection || !*bsection ?
  56. (!*asection) - (!*bsection) :
  57. strcasecmp(asection,bsection);
  58. int c_priority=
  59. a->pkg->priority - b->pkg->priority;
  60. if (!c_priority && a->pkg->priority == pkginfo::pri_other)
  61. c_priority= strcasecmp(a->pkg->otherpriority, b->pkg->otherpriority);
  62. int c_name=
  63. a->pkg->name && b->pkg->name ?
  64. strcasecmp(a->pkg->name, b->pkg->name) :
  65. (!b->pkg->name) - (!a->pkg->name);
  66. switch (sortorder) {
  67. case so_section:
  68. return c_section ? c_section : c_priority ? c_priority : c_name;
  69. case so_priority:
  70. return c_priority ? c_priority : c_section ? c_section : c_name;
  71. case so_alpha:
  72. return c_name;
  73. case so_unsorted:
  74. default:
  75. internerr("unsorted or unknown in compareentries");
  76. }
  77. /* never reached, make gcc happy */
  78. return 1;
  79. }
  80. void packagelist::discardheadings() {
  81. int a,b;
  82. for (a=0, b=0; a<nitems; a++) {
  83. if (table[a]->pkg->name) {
  84. table[b++]= table[a];
  85. }
  86. }
  87. nitems= b;
  88. struct perpackagestate *head, *next;
  89. head= headings;
  90. while (head) {
  91. next= head->uprec;
  92. delete head->pkg;
  93. delete head;
  94. head= next;
  95. }
  96. headings= 0;
  97. }
  98. void packagelist::addheading(enum ssavailval ssavail,
  99. enum ssstateval ssstate,
  100. pkginfo::pkgpriority priority,
  101. const char *otherpriority,
  102. const char *section) {
  103. assert(nitems <= nallocated);
  104. if (nitems == nallocated) {
  105. nallocated += nallocated+50;
  106. struct perpackagestate **newtable= new struct perpackagestate*[nallocated];
  107. memcpy(newtable,table,nallocated*sizeof(struct perpackagestate*));
  108. delete[] table;
  109. table= newtable;
  110. }
  111. if (debug) fprintf(debug,"packagelist[%p]::addheading(%d,%d,%d,%s,%s)\n",
  112. this,ssavail,ssstate,priority,
  113. otherpriority ? otherpriority : "<null>",
  114. section ? section : "<null>");
  115. struct pkginfo *newhead= new pkginfo;
  116. newhead->name= 0;
  117. newhead->priority= priority;
  118. newhead->otherpriority= otherpriority;
  119. newhead->section= section;
  120. struct perpackagestate *newstate= new perpackagestate;
  121. newstate->pkg= newhead;
  122. newstate->uprec= headings;
  123. headings= newstate;
  124. newstate->ssavail= ssavail;
  125. newstate->ssstate= ssstate;
  126. newhead->clientdata= newstate;
  127. table[nitems++]= newstate;
  128. }
  129. static packagelist *sortpackagelist;
  130. int qsort_compareentries(const void *a, const void *b) {
  131. return sortpackagelist->compareentries(*(const struct perpackagestate**)a,
  132. *(const struct perpackagestate**)b);
  133. }
  134. void packagelist::sortinplace() {
  135. sortpackagelist= this;
  136. if (debug) fprintf(debug,"packagelist[%p]::sortinplace()\n",this);
  137. qsort(table,nitems,sizeof(struct pkginfoperfile*),qsort_compareentries);
  138. }
  139. void packagelist::ensurestatsortinfo() {
  140. const struct versionrevision *veri;
  141. const struct versionrevision *vera;
  142. struct pkginfo *pkg;
  143. int index;
  144. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() "
  145. "sortorder=%d nitems=%d\n",this,statsortorder,nitems);
  146. switch (statsortorder) {
  147. case sso_unsorted:
  148. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() unsorted\n",this);
  149. return;
  150. case sso_avail:
  151. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcssadone=%d\n",
  152. this,calcssadone);
  153. if (calcssadone) return;
  154. for (index=0; index < nitems; index++) {
  155. if (debug)
  156. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  157. this,index,table[index]->pkg->name);
  158. pkg= table[index]->pkg;
  159. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  160. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  161. switch (pkg->status) {
  162. case pkginfo::stat_unpacked:
  163. case pkginfo::stat_halfconfigured:
  164. case pkginfo::stat_halfinstalled:
  165. case pkginfo::stat_triggersawaited:
  166. case pkginfo::stat_triggerspending:
  167. table[index]->ssavail= ssa_broken;
  168. break;
  169. case pkginfo::stat_notinstalled:
  170. case pkginfo::stat_configfiles:
  171. if (!informativeversion(&pkg->available.version)) {
  172. table[index]->ssavail= ssa_notinst_gone;
  173. } else if (table[index]->original == pkginfo::want_unknown) {
  174. table[index]->ssavail= ssa_notinst_unseen;
  175. } else {
  176. table[index]->ssavail= ssa_notinst_seen;
  177. }
  178. break;
  179. case pkginfo::stat_installed:
  180. veri= &table[index]->pkg->installed.version;
  181. vera= &table[index]->pkg->available.version;
  182. if (!informativeversion(vera)) {
  183. table[index]->ssavail= ssa_installed_gone;
  184. } else if (versioncompare(vera,veri) > 0) {
  185. table[index]->ssavail= ssa_installed_newer;
  186. } else {
  187. table[index]->ssavail= ssa_installed_sameold;
  188. }
  189. break;
  190. default:
  191. internerr("unknown stat in ensurestatsortinfo sso_avail");
  192. }
  193. if (debug)
  194. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssavail=%d\n",
  195. this,index,table[index]->ssavail);
  196. }
  197. calcssadone= 1;
  198. break;
  199. case sso_state:
  200. if (debug) fprintf(debug,"packagelist[%p]::ensurestatsortinfos() calcsssdone=%d\n",
  201. this,calcsssdone);
  202. if (calcsssdone) return;
  203. for (index=0; index < nitems; index++) {
  204. if (debug)
  205. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d pkg=%s\n",
  206. this,index,table[index]->pkg->name);
  207. switch (table[index]->pkg->status) {
  208. case pkginfo::stat_unpacked:
  209. case pkginfo::stat_halfconfigured:
  210. case pkginfo::stat_halfinstalled:
  211. case pkginfo::stat_triggersawaited:
  212. case pkginfo::stat_triggerspending:
  213. table[index]->ssstate= sss_broken;
  214. break;
  215. case pkginfo::stat_notinstalled:
  216. table[index]->ssstate= sss_notinstalled;
  217. break;
  218. case pkginfo::stat_configfiles:
  219. table[index]->ssstate= sss_configfiles;
  220. break;
  221. case pkginfo::stat_installed:
  222. table[index]->ssstate= sss_installed;
  223. break;
  224. default:
  225. internerr("unknown stat in ensurestatsortinfo sso_state");
  226. }
  227. if (debug)
  228. fprintf(debug,"packagelist[%p]::ensurestatsortinfos() i=%d ssstate=%d\n",
  229. this,index,table[index]->ssstate);
  230. }
  231. calcsssdone= 1;
  232. break;
  233. default:
  234. internerr("unknown statsortorder in ensurestatsortinfo");
  235. }
  236. }
  237. void packagelist::sortmakeheads() {
  238. discardheadings();
  239. ensurestatsortinfo();
  240. sortinplace();
  241. assert(nitems);
  242. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads() "
  243. "sortorder=%d statsortorder=%d\n",this,sortorder,statsortorder);
  244. int nrealitems= nitems;
  245. addheading(ssa_none,sss_none,pkginfo::pri_unset,0,0);
  246. assert(sortorder != so_unsorted);
  247. if (sortorder == so_alpha && statsortorder == sso_unsorted) { sortinplace(); return; }
  248. // Important: do not save pointers into table in this function, because
  249. // addheading may need to reallocate table to make it larger !
  250. struct pkginfo *lastpkg;
  251. struct pkginfo *thispkg;
  252. lastpkg= 0;
  253. int a;
  254. for (a=0; a<nrealitems; a++) {
  255. thispkg= table[a]->pkg;
  256. assert(thispkg->name);
  257. int ssdiff= 0;
  258. ssavailval ssavail= ssa_none;
  259. ssstateval ssstate= sss_none;
  260. switch (statsortorder) {
  261. case sso_avail:
  262. ssavail= thispkg->clientdata->ssavail;
  263. ssdiff= (!lastpkg || ssavail != lastpkg->clientdata->ssavail);
  264. break;
  265. case sso_state:
  266. ssstate= thispkg->clientdata->ssstate;
  267. ssdiff= (!lastpkg || ssstate != lastpkg->clientdata->ssstate);
  268. break;
  269. case sso_unsorted:
  270. break;
  271. default:
  272. internerr("unknown statsortorder in sortmakeheads");
  273. }
  274. int prioritydiff= (!lastpkg ||
  275. thispkg->priority != lastpkg->priority ||
  276. (thispkg->priority == pkginfo::pri_other &&
  277. strcasecmp(thispkg->otherpriority,lastpkg->otherpriority)));
  278. int sectiondiff= (!lastpkg ||
  279. strcasecmp(thispkg->section ? thispkg->section : "",
  280. lastpkg->section ? lastpkg->section : ""));
  281. if (debug) fprintf(debug,"packagelist[%p]::sortmakeheads()"
  282. " pkg=%s state=%d avail=%d %s priority=%d"
  283. " otherpriority=%s %s section=%s %s\n",
  284. this, thispkg->name,
  285. thispkg->clientdata->ssavail,
  286. thispkg->clientdata->ssstate,
  287. ssdiff ? "*diff" : "same",
  288. thispkg->priority,
  289. thispkg->priority != pkginfo::pri_other ? "<none>"
  290. : thispkg->otherpriority ? thispkg->otherpriority
  291. : "<null>",
  292. prioritydiff ? "*diff*" : "same",
  293. thispkg->section ? thispkg->section : "<null>",
  294. sectiondiff ? "*diff*" : "same");
  295. if (ssdiff)
  296. addheading(ssavail,ssstate,
  297. pkginfo::pri_unset,0, 0);
  298. if (sortorder == so_section && sectiondiff)
  299. addheading(ssavail,ssstate,
  300. pkginfo::pri_unset,0, thispkg->section ? thispkg->section : "");
  301. if (sortorder == so_priority && prioritydiff)
  302. addheading(ssavail,ssstate,
  303. thispkg->priority,thispkg->otherpriority, 0);
  304. if (sortorder != so_alpha && (prioritydiff || sectiondiff))
  305. addheading(ssavail,ssstate,
  306. thispkg->priority,thispkg->otherpriority,
  307. thispkg->section ? thispkg->section : "");
  308. lastpkg= thispkg;
  309. }
  310. if (listpad) {
  311. werase(listpad);
  312. }
  313. sortinplace();
  314. }
  315. void packagelist::initialsetup() {
  316. if (debug)
  317. fprintf(debug,"packagelist[%p]::initialsetup()\n",this);
  318. int allpackages= countpackages();
  319. datatable= new struct perpackagestate[allpackages];
  320. nallocated= allpackages+150; // will realloc if necessary, so 150 not critical
  321. table= new struct perpackagestate*[nallocated];
  322. depsdone= 0;
  323. unavdone= 0;
  324. currentinfo= 0;
  325. headings= 0;
  326. verbose= 0;
  327. calcssadone= calcsssdone= 0;
  328. searchdescr= 0;
  329. }
  330. void packagelist::finalsetup() {
  331. setcursor(0);
  332. if (debug)
  333. fprintf(debug,"packagelist[%p]::finalsetup done; recursive=%d nitems=%d\n",
  334. this, recursive, nitems);
  335. }
  336. packagelist::packagelist(keybindings *kb) : baselist(kb) {
  337. // nonrecursive
  338. initialsetup();
  339. struct pkgiterator *iter;
  340. struct pkginfo *pkg;
  341. for (iter=iterpkgstart(), nitems=0;
  342. (pkg=iterpkgnext(iter));
  343. ) {
  344. struct perpackagestate *state= &datatable[nitems];
  345. state->pkg= pkg;
  346. if (pkg->status == pkginfo::stat_notinstalled &&
  347. !pkg->files &&
  348. pkg->want != pkginfo::want_install) {
  349. pkg->clientdata= 0; continue;
  350. }
  351. if (!pkg->available.valid) blankpackageperfile(&pkg->available);
  352. state->direct= state->original= pkg->want;
  353. if (readwrite && pkg->want == pkginfo::want_unknown) {
  354. state->suggested=
  355. pkg->status == pkginfo::stat_installed ||
  356. pkg->priority <= pkginfo::pri_standard /* FIXME: configurable */
  357. ? pkginfo::want_install : pkginfo::want_purge;
  358. state->spriority= sp_inherit;
  359. } else {
  360. state->suggested= pkg->want;
  361. state->spriority= sp_fixed;
  362. }
  363. state->dpriority= dp_must;
  364. state->selected= state->suggested;
  365. state->uprec= 0;
  366. state->relations.init();
  367. pkg->clientdata= state;
  368. table[nitems]= state;
  369. nitems++;
  370. }
  371. if (!nitems)
  372. 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. if (searchstring[0])
  415. regfree(&searchfsm);
  416. discardheadings();
  417. int index;
  418. for (index=0; index<nitems; index++) table[index]->free(recursive);
  419. delete[] table;
  420. delete[] datatable;
  421. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() tables freed\n",this);
  422. doneent *search, *next;
  423. for (search=depsdone; search; search=next) {
  424. next= search->next;
  425. delete search;
  426. }
  427. if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this);
  428. }
  429. int packagelist::checksearch(char* rx) {
  430. int r,opt = REG_NOSUB;
  431. if (!rx || !*rx) return 0;
  432. searchdescr=0;
  433. if (searchstring[0]) {
  434. regfree(&searchfsm);
  435. searchstring[0]=0;
  436. }
  437. /* look for search options */
  438. for (r=strlen(rx)-1; r>=0; r--)
  439. if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\')))
  440. break;
  441. if (r>=0) {
  442. rx[r++]='\0';
  443. if (strcspn(rx+r, "di")!=0) {
  444. displayerror(_("invalid search option given"));
  445. return 0;
  446. }
  447. while (rx[r]) {
  448. if (rx[r]=='i')
  449. opt|=REG_ICASE;
  450. else if (rx[r]=='d')
  451. searchdescr=1;
  452. r++;
  453. }
  454. }
  455. if ((r=regcomp(&searchfsm, rx, opt))!=0) {
  456. displayerror(_("error in regular expression"));
  457. return 0;
  458. }
  459. return 1;
  460. }
  461. int packagelist::matchsearch(int index) {
  462. const char* thisname;
  463. thisname=itemname(index);
  464. if (!thisname) return 0; /* Skip things without a name (seperators) */
  465. if (regexec(&searchfsm, thisname, 0, NULL, 0)==0)
  466. return 1;
  467. if (searchdescr) {
  468. const char* descr = table[index]->pkg->available.description;
  469. if (!descr || !*descr) return 0;
  470. if (regexec(&searchfsm, descr, 0, NULL, 0)==0)
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. pkginfo **packagelist::display() {
  476. // returns list of packages as null-terminated array, which becomes owned
  477. // by the caller, if a recursive check is desired.
  478. // returns 0 if no recursive check is desired.
  479. int response, index;
  480. const keybindings::interpretation *interp;
  481. pkginfo **retl;
  482. if (debug) fprintf(debug,"packagelist[%p]::display()\n",this);
  483. setupsigwinch();
  484. startdisplay();
  485. if (!expertmode)
  486. displayhelp(helpmenulist(),'i');
  487. if (debug) fprintf(debug,"packagelist[%p]::display() entering loop\n",this);
  488. for (;;) {
  489. if (whatinfo_height) wcursyncup(whatinfowin);
  490. if (doupdate() == ERR)
  491. ohshite(_("doupdate failed"));
  492. signallist= this;
  493. if (sigprocmask(SIG_UNBLOCK, &sigwinchset, 0))
  494. ohshite(_("failed to unblock SIGWINCH"));
  495. do
  496. response= getch();
  497. while (response == ERR && errno == EINTR);
  498. if (sigprocmask(SIG_BLOCK, &sigwinchset, 0))
  499. ohshite(_("failed to re-block SIGWINCH"));
  500. if (response == ERR)
  501. 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. */