pkglist.cc 18 KB

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