pkgcmds.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * pkgcmds.cc - package list keyboard commands
  4. *
  5. * Copyright (C) 1994,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 <ncurses.h>
  24. #include <assert.h>
  25. #include <signal.h>
  26. extern "C" {
  27. #include "config.h"
  28. #include "dpkg.h"
  29. #include "dpkg-db.h"
  30. }
  31. #include "dselect.h"
  32. #include "pkglist.h"
  33. static int matches(struct pkginfo *pkg, struct pkginfo *comparewith) {
  34. if (comparewith->priority != pkginfo::pri_unset &&
  35. (comparewith->priority != pkg->priority ||
  36. comparewith->priority == pkginfo::pri_other &&
  37. strcasecmp(comparewith->otherpriority,pkg->otherpriority)))
  38. return 0;
  39. if (comparewith->section &&
  40. strcasecmp(comparewith->section,
  41. pkg->section ?
  42. pkg->section : ""))
  43. return 0;
  44. return 1;
  45. }
  46. void packagelist::affectedrange(int *startp, int *endp) {
  47. if (table[cursorline]->pkg->name) {
  48. *startp= cursorline;
  49. *endp= cursorline+1;
  50. return;
  51. }
  52. int index;
  53. for (index= cursorline; index < nitems && !table[index]->pkg->name; index++);
  54. if (index >= nitems) {
  55. *startp= *endp= cursorline;
  56. return;
  57. }
  58. *startp= index;
  59. while (index < nitems && matches(table[index]->pkg,table[cursorline]->pkg)) index++;
  60. *endp= index;
  61. }
  62. void packagelist::movecursorafter(int ncursor) {
  63. if (ncursor >= nitems) ncursor= nitems-1;
  64. topofscreen += ncursor-cursorline;
  65. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  66. if (topofscreen < 0) topofscreen= 0;
  67. setcursor(ncursor);
  68. refreshlist(); redrawthisstate();
  69. }
  70. void packagelist::setwant(pkginfo::pkgwant nw) {
  71. int index, top, bot;
  72. if (!readwrite) { beep(); return; }
  73. if (recursive) {
  74. redrawitemsrange(cursorline,cursorline+1);
  75. table[cursorline]->selected= nw;
  76. redraw1item(cursorline);
  77. top= cursorline;
  78. bot= cursorline+1;
  79. } else {
  80. packagelist *sub= new packagelist(bindings,0);
  81. affectedrange(&top,&bot);
  82. for (index= top; index < bot; index++) {
  83. if (!table[index]->pkg->name ||
  84. table[index]->selected == nw ||
  85. table[index]->selected == pkginfo::want_purge && nw == pkginfo::want_deinstall)
  86. continue;
  87. sub->add(table[index]->pkg,nw);
  88. }
  89. repeatedlydisplay(sub,dp_may,this);
  90. for (index=top; index < bot; index++)
  91. redraw1item(index);
  92. }
  93. movecursorafter(bot);
  94. }
  95. void packagelist::kd_select() { setwant(pkginfo::want_install); }
  96. void packagelist::kd_deselect() { setwant(pkginfo::want_deinstall); }
  97. void packagelist::kd_purge() { setwant(pkginfo::want_purge); }
  98. void packagelist::sethold(int hold) {
  99. int top, bot, index;
  100. if (!readwrite) { beep(); return; }
  101. affectedrange(&top,&bot);
  102. for (index= top; index < bot; index++) {
  103. // Sorry about the contortions, but GCC produces a silly warning otherwise
  104. unsigned int neweflag= table[index]->pkg->eflag;
  105. if (hold) neweflag |= pkginfo::eflagf_hold;
  106. else neweflag &= ~pkginfo::eflagf_hold;
  107. table[index]->pkg->eflag= (enum pkginfo::pkgeflag)neweflag;
  108. redraw1item(index);
  109. }
  110. movecursorafter(bot);
  111. }
  112. void packagelist::kd_hold() { sethold(1); }
  113. void packagelist::kd_unhold() { sethold(0); }
  114. const char *packagelist::itemname(int index) {
  115. return table[index]->pkg->name;
  116. }
  117. void packagelist::kd_swaporder() {
  118. switch (sortorder) {
  119. case so_priority: sortorder= so_section; break;
  120. case so_section: sortorder= so_alpha; break;
  121. case so_alpha: sortorder= so_priority; break;
  122. case so_unsorted: return;
  123. default: internerr("unknown sort in kd_swaporder");
  124. }
  125. const char *oldname= table[cursorline]->pkg->name;
  126. sortmakeheads();
  127. int newcursor;
  128. newcursor= 0;
  129. if (oldname) {
  130. int index;
  131. for (index=0; index<nitems; index++) {
  132. if (table[index]->pkg->name && !strcasecmp(oldname,table[index]->pkg->name)) {
  133. newcursor= index;
  134. break;
  135. }
  136. }
  137. }
  138. topofscreen= newcursor-1;
  139. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  140. if (topofscreen < 0) topofscreen= 0;
  141. setwidths();
  142. redrawtitle();
  143. redrawcolheads();
  144. ldrawnstart= ldrawnend= -1;
  145. cursorline= -1;
  146. setcursor(newcursor);
  147. refreshlist();
  148. }
  149. void packagelist::kd_verbose() {
  150. verbose= !verbose;
  151. setwidths();
  152. leftofscreen= 0;
  153. ldrawnstart= ldrawnend= -1;
  154. redrawtitle();
  155. redrawcolheads();
  156. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  157. refreshlist();
  158. }
  159. void packagelist::kd_quit_noop() { }
  160. void packagelist::kd_revert_abort() {
  161. int index;
  162. for (index=0; index<nitems; index++) {
  163. if (table[index]->pkg->name)
  164. table[index]->selected= table[index]->original;
  165. ldrawnstart= ldrawnend= -1;
  166. }
  167. refreshlist(); redrawthisstate();
  168. }
  169. void packagelist::kd_revertdirect() {
  170. int index;
  171. for (index=0; index<nitems; index++) {
  172. if (table[index]->pkg->name)
  173. table[index]->selected= table[index]->direct;
  174. ldrawnstart= ldrawnend= -1;
  175. }
  176. refreshlist(); redrawthisstate();
  177. }
  178. void packagelist::kd_revertsuggest() {
  179. int index;
  180. for (index=0; index<nitems; index++) {
  181. if (table[index]->pkg->name)
  182. table[index]->selected= table[index]->suggested;
  183. ldrawnstart= ldrawnend= -1;
  184. }
  185. refreshlist(); redrawthisstate();
  186. }
  187. /* fixme: configurable purge/deselect */
  188. /* fixme: un-hold things */
  189. void packagelist::kd_info() {
  190. currentinfo++;
  191. infotopofscreen=0;
  192. redrawinfo();
  193. }