pkgcmds.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * pkgcmds.cc - package list keyboard commands
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.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 <config.h>
  22. #include <compat.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <dpkg.h>
  27. #include <dpkg-db.h>
  28. #include "dselect.h"
  29. #include "pkglist.h"
  30. int packagelist::affectedmatches(struct pkginfo *pkg, struct pkginfo *comparewith) {
  31. switch (statsortorder) {
  32. case sso_avail:
  33. if (comparewith->clientdata->ssavail != pkg->clientdata->ssavail) return 0;
  34. break;
  35. case sso_state:
  36. if (comparewith->clientdata->ssstate != pkg->clientdata->ssstate) return 0;
  37. break;
  38. case sso_unsorted:
  39. break;
  40. default:
  41. internerr("unknown statsortorder in affectedmatches");
  42. }
  43. if (comparewith->priority != pkginfo::pri_unset &&
  44. (comparewith->priority != pkg->priority ||
  45. (comparewith->priority == pkginfo::pri_other &&
  46. strcasecmp(comparewith->otherpriority, pkg->otherpriority))))
  47. return 0;
  48. if (comparewith->section &&
  49. strcasecmp(comparewith->section,
  50. pkg->section ?
  51. pkg->section : ""))
  52. return 0;
  53. return 1;
  54. }
  55. void packagelist::affectedrange(int *startp, int *endp) {
  56. if (table[cursorline]->pkg->name) {
  57. *startp= cursorline;
  58. *endp= cursorline+1;
  59. return;
  60. }
  61. int index = cursorline;
  62. while (index < nitems && !table[index]->pkg->name)
  63. index++;
  64. if (index >= nitems) {
  65. *startp= *endp= cursorline;
  66. return;
  67. }
  68. *startp= index;
  69. while (index < nitems && affectedmatches(table[index]->pkg,table[cursorline]->pkg))
  70. index++;
  71. *endp= index;
  72. }
  73. void packagelist::movecursorafter(int ncursor) {
  74. if (ncursor >= nitems) ncursor= nitems-1;
  75. topofscreen += ncursor-cursorline;
  76. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  77. if (topofscreen < 0) topofscreen= 0;
  78. setcursor(ncursor);
  79. refreshlist(); redrawthisstate();
  80. }
  81. pkginfo::pkgwant packagelist::reallywant(pkginfo::pkgwant nwarg,
  82. struct perpackagestate *pkgstate) {
  83. if (nwarg != pkginfo::want_sentinel) return nwarg;
  84. pkginfo::pkgstatus status= pkgstate->pkg->status;
  85. if (status == pkginfo::stat_notinstalled) return pkginfo::want_purge;
  86. if (status == pkginfo::stat_configfiles) return pkginfo::want_deinstall;
  87. return pkginfo::want_install;
  88. }
  89. void packagelist::setwant(pkginfo::pkgwant nwarg) {
  90. int index, top, bot;
  91. pkginfo::pkgwant nw;
  92. if (!readwrite) { beep(); return; }
  93. if (recursive) {
  94. redrawitemsrange(cursorline,cursorline+1);
  95. table[cursorline]->selected= reallywant(nwarg,table[cursorline]);
  96. redraw1item(cursorline);
  97. top= cursorline;
  98. bot= cursorline+1;
  99. } else {
  100. packagelist *sub= new packagelist(bindings,0);
  101. affectedrange(&top,&bot);
  102. for (index= top; index < bot; index++) {
  103. if (!table[index]->pkg->name) continue;
  104. nw= reallywant(nwarg,table[index]);
  105. if (table[index]->selected == nw ||
  106. (table[index]->selected == pkginfo::want_purge &&
  107. nw == pkginfo::want_deinstall))
  108. continue;
  109. sub->add(table[index]->pkg,nw);
  110. }
  111. repeatedlydisplay(sub,dp_may,this);
  112. for (index=top; index < bot; index++)
  113. redraw1item(index);
  114. }
  115. movecursorafter(bot);
  116. }
  117. int manual_install = 0;
  118. void packagelist::kd_select() {
  119. manual_install = 1;
  120. setwant(pkginfo::want_install);
  121. manual_install = 0;
  122. }
  123. void packagelist::kd_hold() { setwant(pkginfo::want_hold); }
  124. void packagelist::kd_deselect() { setwant(pkginfo::want_deinstall); }
  125. void packagelist::kd_unhold() { setwant(pkginfo::want_sentinel); }
  126. void packagelist::kd_purge() { setwant(pkginfo::want_purge); }
  127. int would_like_to_install(pkginfo::pkgwant wantvalue, pkginfo *pkg) {
  128. /* Returns: 1 for yes, 0 for no, -1 for if they want to preserve an error condition. */
  129. if (debug) fprintf(debug,"would_like_to_install(%d, %s) status %d\n",
  130. wantvalue,pkg->name,pkg->status);
  131. if (wantvalue == pkginfo::want_install) return 1;
  132. if (wantvalue != pkginfo::want_hold) return 0;
  133. if (pkg->status == pkginfo::stat_installed) return 1;
  134. if (pkg->status == pkginfo::stat_notinstalled ||
  135. pkg->status == pkginfo::stat_configfiles) return 0;
  136. return -1;
  137. }
  138. const char *packagelist::itemname(int index) {
  139. return table[index]->pkg->name;
  140. }
  141. void packagelist::kd_swapstatorder() {
  142. if (sortorder == so_unsorted) return;
  143. switch (statsortorder) {
  144. case sso_avail: statsortorder= sso_state; break;
  145. case sso_state: statsortorder= sso_unsorted; break;
  146. case sso_unsorted: statsortorder= sso_avail; break;
  147. default: internerr("unknown statsort in kd_swapstatorder");
  148. }
  149. resortredisplay();
  150. }
  151. void packagelist::kd_swaporder() {
  152. switch (sortorder) {
  153. case so_priority: sortorder= so_section; break;
  154. case so_section: sortorder= so_alpha; break;
  155. case so_alpha: sortorder= so_priority; break;
  156. case so_unsorted: return;
  157. default: internerr("unknown sort in kd_swaporder");
  158. }
  159. resortredisplay();
  160. }
  161. void packagelist::resortredisplay() {
  162. const char *oldname= table[cursorline]->pkg->name;
  163. sortmakeheads();
  164. int newcursor;
  165. newcursor= 0;
  166. if (oldname) {
  167. int index;
  168. for (index=0; index<nitems; index++) {
  169. if (table[index]->pkg->name && !strcasecmp(oldname,table[index]->pkg->name)) {
  170. newcursor= index;
  171. break;
  172. }
  173. }
  174. }
  175. topofscreen= newcursor-1;
  176. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  177. if (topofscreen < 0) topofscreen= 0;
  178. setwidths();
  179. redrawtitle();
  180. redrawcolheads();
  181. ldrawnstart= ldrawnend= -1;
  182. cursorline= -1;
  183. setcursor(newcursor);
  184. refreshlist();
  185. }
  186. void packagelist::kd_versiondisplay() {
  187. switch (versiondisplayopt) {
  188. case vdo_both: versiondisplayopt= vdo_none; break;
  189. case vdo_none: versiondisplayopt= vdo_available; break;
  190. case vdo_available: versiondisplayopt= vdo_both; break;
  191. default: internerr("unknown versiondisplayopt in kd_versiondisplay");
  192. }
  193. setwidths();
  194. leftofscreen= 0;
  195. ldrawnstart= ldrawnend= -1;
  196. redrawtitle();
  197. redrawcolheads();
  198. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  199. refreshlist();
  200. }
  201. void packagelist::kd_verbose() {
  202. verbose= !verbose;
  203. setwidths();
  204. leftofscreen= 0;
  205. ldrawnstart= ldrawnend= -1;
  206. redrawtitle();
  207. redrawcolheads();
  208. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  209. refreshlist();
  210. }
  211. void packagelist::kd_quit_noop() { }
  212. void packagelist::kd_revert_abort() {
  213. int index;
  214. for (index=0; index<nitems; index++) {
  215. if (table[index]->pkg->name)
  216. table[index]->selected= table[index]->original;
  217. ldrawnstart= ldrawnend= -1;
  218. }
  219. refreshlist(); redrawthisstate();
  220. }
  221. void packagelist::kd_revertdirect() {
  222. int index;
  223. for (index=0; index<nitems; index++) {
  224. if (table[index]->pkg->name)
  225. table[index]->selected= table[index]->direct;
  226. ldrawnstart= ldrawnend= -1;
  227. }
  228. refreshlist(); redrawthisstate();
  229. }
  230. void packagelist::kd_revertsuggest() {
  231. int index;
  232. for (index=0; index<nitems; index++) {
  233. if (table[index]->pkg->name)
  234. table[index]->selected= table[index]->suggested;
  235. ldrawnstart= ldrawnend= -1;
  236. }
  237. refreshlist(); redrawthisstate();
  238. }
  239. void
  240. packagelist::kd_revertinstalled()
  241. {
  242. int i;
  243. for (i = 0; i < nitems; i++) {
  244. if (table[i]->pkg->name)
  245. table[i]->selected = reallywant(pkginfo::want_sentinel, table[i]);
  246. ldrawnstart = ldrawnend = -1;
  247. }
  248. refreshlist();
  249. redrawthisstate();
  250. }
  251. /* FIXME: configurable purge/deselect */
  252. void packagelist::kd_toggleinfo() {
  253. showinfo= (showinfo+2) % 3;
  254. setheights();
  255. if (cursorline >= topofscreen+list_height) topofscreen += list_height;
  256. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  257. if (topofscreen < 0) topofscreen= 0;
  258. infotopofscreen= 0;
  259. redraw1item(cursorline);
  260. refreshlist();
  261. redrawthisstate();
  262. redrawinfo();
  263. }
  264. void packagelist::kd_info() {
  265. if (!showinfo) {
  266. showinfo= 2; kd_toggleinfo();
  267. } else {
  268. currentinfo++;
  269. infotopofscreen=0;
  270. redrawinfo();
  271. }
  272. }