baselist.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * baselist.cc - list of somethings
  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 <curses.h>
  24. #include <assert.h>
  25. #include <signal.h>
  26. #include <ctype.h>
  27. extern "C" {
  28. #include <config.h>
  29. #include <dpkg.h>
  30. #include <dpkg-db.h>
  31. }
  32. #include "dselect.h"
  33. #include "bindings.h"
  34. void mywerase(WINDOW *win) {
  35. int my,mx,y,x;
  36. getmaxyx(win,my,mx);
  37. for (y=0; y<my; y++) {
  38. wmove(win,y,0); for (x=0; x<mx; x++) waddch(win,' ');
  39. }
  40. wmove(win,0,0);
  41. }
  42. baselist *baselist::signallist= 0;
  43. void baselist::sigwinchhandler(int) {
  44. baselist *p= signallist;
  45. p->enddisplay();
  46. endwin(); initscr();
  47. p->startdisplay();
  48. if (doupdate() == ERR) ohshite(_("doupdate in SIGWINCH handler failed"));
  49. }
  50. static void cu_sigwinch(int, void **argv) {
  51. struct sigaction *osigactp= (struct sigaction*)argv[0];
  52. sigset_t *oblockedp= (sigset_t*)argv[1];
  53. if (sigaction(SIGWINCH,osigactp,0)) ohshite(_("failed to restore old SIGWINCH sigact"));
  54. delete osigactp;
  55. if (sigprocmask(SIG_SETMASK,oblockedp,0)) ohshite(_("failed to restore old signal mask"));
  56. delete oblockedp;
  57. }
  58. void baselist::setupsigwinch() {
  59. sigemptyset(&sigwinchset);
  60. sigaddset(&sigwinchset,SIGWINCH);
  61. osigactp= new(struct sigaction);
  62. oblockedp= new(sigset_t);
  63. if (sigprocmask(0,0,oblockedp)) ohshite(_("failed to get old signal mask"));
  64. if (sigaction(SIGWINCH,0,osigactp)) ohshite(_("failed to get old SIGWINCH sigact"));
  65. push_cleanup(cu_sigwinch,~0, 0,0, 2,(void*)osigactp,(void*)oblockedp);
  66. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite(_("failed to block SIGWINCH"));
  67. memset(&nsigact,0,sizeof(nsigact));
  68. nsigact.sa_handler= sigwinchhandler;
  69. sigemptyset(&nsigact.sa_mask);
  70. nsigact.sa_flags= SA_INTERRUPT;
  71. if (sigaction(SIGWINCH,&nsigact,0)) ohshite(_("failed to set new SIGWINCH sigact"));
  72. }
  73. void baselist::setheights() {
  74. int y= ymax - (title_height + colheads_height + thisstate_height);
  75. assert(y>=1);
  76. if (showinfo==2 && y>=7) {
  77. list_height= 5;
  78. whatinfo_height= 1;
  79. info_height= y-6;
  80. } else if (showinfo==1 && y>=10) {
  81. list_height= y/2;
  82. info_height= (y-1)/2;
  83. whatinfo_height= 1;
  84. } else {
  85. list_height= y;
  86. info_height= 0;
  87. whatinfo_height= 0;
  88. }
  89. colheads_row= title_height;
  90. list_row= colheads_row + colheads_height;
  91. thisstate_row= list_row + list_height;
  92. info_row= thisstate_row + thisstate_height;
  93. whatinfo_row= ymax - 1;
  94. }
  95. void baselist::startdisplay() {
  96. if (debug) fprintf(debug,"baselist[%p]::startdisplay()\n",this);
  97. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  98. clear(); wnoutrefresh(stdscr);
  99. // find attributes
  100. if (has_colors() && start_color()==OK && COLOR_PAIRS >= 3) {
  101. if (init_pair(1,COLOR_WHITE,COLOR_BLACK) != OK ||
  102. init_pair(2,COLOR_WHITE,COLOR_RED) != OK ||
  103. init_pair(3,COLOR_WHITE,COLOR_BLUE) != OK)
  104. ohshite(_("failed to allocate colour pairs"));
  105. list_attr= COLOR_PAIR(1);
  106. listsel_attr= list_attr|A_REVERSE;
  107. title_attr= COLOR_PAIR(2);
  108. thisstate_attr= COLOR_PAIR(3);
  109. selstate_attr= list_attr|A_BOLD;
  110. selstatesel_attr= listsel_attr|A_BOLD;
  111. colheads_attr= COLOR_PAIR(3);
  112. } else {
  113. title_attr= A_REVERSE;
  114. thisstate_attr= A_STANDOUT;
  115. list_attr= 0;
  116. listsel_attr= A_STANDOUT;
  117. selstate_attr= A_BOLD;
  118. selstatesel_attr= A_STANDOUT;
  119. colheads_attr= A_BOLD;
  120. }
  121. query_attr= title_attr;
  122. info_attr= list_attr;
  123. info_headattr= A_BOLD;
  124. whatinfo_attr= thisstate_attr;
  125. // set up windows and pads, based on screen size
  126. getmaxyx(stdscr,ymax,xmax);
  127. title_height= ymax>=6;
  128. colheads_height= ymax>=5;
  129. thisstate_height= ymax>=3;
  130. setheights();
  131. setwidths();
  132. titlewin= newwin(1,xmax, 0,0);
  133. if (!titlewin) ohshite(_("failed to create title window"));
  134. wattrset(titlewin,title_attr);
  135. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  136. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  137. wattrset(whatinfowin,whatinfo_attr);
  138. listpad= newpad(nitems+1, total_width);
  139. if (!listpad) ohshite(_("failed to create baselist pad"));
  140. colheadspad= newpad(1, total_width);
  141. if (!colheadspad) ohshite(_("failed to create heading pad"));
  142. wattrset(colheadspad,colheads_attr);
  143. thisstatepad= newpad(1, total_width);
  144. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  145. wattrset(thisstatepad,thisstate_attr);
  146. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  147. if (!infopad) ohshite(_("failed to create info pad"));
  148. wattrset(infopad,info_attr);
  149. querywin= newwin(1,xmax,ymax-1,0);
  150. if (!querywin) ohshite(_("failed to create query window"));
  151. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  152. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  153. if (topofscreen < 0) topofscreen= 0;
  154. infotopofscreen= 0; leftofscreen= 0;
  155. redrawall();
  156. if (debug)
  157. fprintf(debug,
  158. _("baselist::startdisplay() done ...\n\n"
  159. " xmax=%d, ymax=%d;\n\n"
  160. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  161. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  162. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  163. " whatinfo_row=%d, list_row=%d;\n\n"),
  164. xmax, ymax,
  165. title_height, colheads_height, list_height,
  166. thisstate_height, info_height, whatinfo_height,
  167. colheads_row, thisstate_row, info_row,
  168. whatinfo_row, list_row);
  169. }
  170. void baselist::enddisplay() {
  171. delwin(titlewin);
  172. delwin(whatinfowin);
  173. delwin(listpad);
  174. delwin(colheadspad);
  175. delwin(thisstatepad);
  176. delwin(infopad);
  177. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  178. listpad= 0;
  179. }
  180. void baselist::redrawall() {
  181. redrawtitle();
  182. redrawcolheads();
  183. wattrset(listpad,list_attr); mywerase(listpad);
  184. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  185. refreshlist();
  186. redrawthisstate();
  187. redrawinfo();
  188. }
  189. void baselist::redraw1item(int index) {
  190. redraw1itemsel(index, index == cursorline);
  191. }
  192. baselist::baselist(keybindings *kb) {
  193. if (debug)
  194. fprintf(debug,"baselist[%p]::baselist()\n",this);
  195. bindings= kb;
  196. nitems= 0;
  197. xmax= -1;
  198. list_height=0; info_height=0;
  199. topofscreen= 0; leftofscreen= 0;
  200. listpad= 0; cursorline= -1;
  201. showinfo= 1;
  202. searchstring[0]= 0;
  203. }
  204. void baselist::itd_keys() {
  205. whatinfovb(_("keybindings"));
  206. const int givek= xmax/3;
  207. bindings->describestart();
  208. const char **ta;
  209. while ((ta= bindings->describenext()) != 0) {
  210. const char **tap= ta+1;
  211. for (;;) {
  212. waddstr(infopad, gettext(*tap));
  213. tap++; if (!*tap) break;
  214. waddstr(infopad, ", ");
  215. }
  216. int y,x;
  217. getyx(infopad,y,x);
  218. if (x >= givek) y++;
  219. mvwaddstr(infopad, y,givek, ta[0]);
  220. waddch(infopad,'\n');
  221. }
  222. }
  223. void baselist::dosearch() {
  224. int offset, index, searchlen;
  225. searchlen= strlen(searchstring);
  226. if (debug) fprintf(debug,"packagelist[%p]::dosearch(); searchstring=`%s' len=%d\n",
  227. this,searchstring,searchlen);
  228. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  229. offset<nitems;
  230. offset++, index++) {
  231. if (index >= nitems) index -= nitems;
  232. int lendiff, i;
  233. const char *thisname;
  234. thisname= itemname(index);
  235. if (!thisname) continue;
  236. lendiff= strlen(thisname) - searchlen;
  237. for (i=0; i<=lendiff; i++)
  238. if (!strncasecmp(thisname + i, searchstring, searchlen)) {
  239. topofscreen= index-1;
  240. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  241. if (topofscreen < 0) topofscreen= 0;
  242. setcursor(index);
  243. return;
  244. }
  245. }
  246. beep();
  247. }
  248. void baselist::refreshinfo() {
  249. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  250. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  251. lesserint(total_width - leftofscreen - 1, xmax - 1));
  252. if (whatinfo_height) {
  253. mywerase(whatinfowin);
  254. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  255. if (infolines > info_height) {
  256. wprintw(whatinfowin,_(" -- %d%%, press "),
  257. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  258. if (infotopofscreen + info_height < infolines) {
  259. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  260. if (infotopofscreen) waddstr(whatinfowin, ", ");
  261. }
  262. if (infotopofscreen)
  263. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  264. waddch(whatinfowin,'.');
  265. }
  266. wnoutrefresh(whatinfowin);
  267. }
  268. }
  269. void baselist::wordwrapinfo(int offset, const char *m) {
  270. int usemax= xmax-5;
  271. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  272. int wrapping=0;
  273. for (;;) {
  274. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  275. const char *p= strchr(m,'\n');
  276. int l= p ? (int)(p-m) : strlen(m);
  277. while (l && isspace(m[l-1])) l--;
  278. if (!l || *m == '.' && l == 1) {
  279. if (wrapping) waddch(infopad,'\n');
  280. waddch(infopad,'\n');
  281. wrapping= 0;
  282. } else if (*m == ' ' || usemax < 10) {
  283. if (wrapping) waddch(infopad,'\n');
  284. waddnstr(infopad, m, l);
  285. waddch(infopad,'\n'); wrapping= 0;
  286. } else {
  287. int x,y;
  288. if (wrapping) {
  289. getyx(infopad, y,x);
  290. if (x+1 >= usemax) {
  291. waddch(infopad,'\n');
  292. } else {
  293. waddch(infopad,' ');
  294. }
  295. }
  296. for (;;) {
  297. getyx(infopad, y,x);
  298. int dosend= usemax-x;
  299. if (l <= dosend) {
  300. dosend=l;
  301. } else {
  302. int i=dosend;
  303. while (i > 0 && m[i] != ' ') i--;
  304. if (i > 0 || x > 0) dosend=i;
  305. }
  306. if (dosend) waddnstr(infopad, m, dosend);
  307. while (dosend < l && m[dosend] == ' ') dosend++;
  308. l-= dosend; m+= dosend;
  309. if (l <= 0) break;
  310. waddch(infopad,'\n');
  311. }
  312. wrapping= 1;
  313. }
  314. if (!p) break;
  315. m= ++p;
  316. }
  317. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  318. }
  319. baselist::~baselist() { }