baselist.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <ncurses.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>=3) {
  77. list_height= 1;
  78. whatinfo_height= 1;
  79. info_height= y-2;
  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. } else {
  112. title_attr= A_REVERSE;
  113. thisstate_attr= A_STANDOUT;
  114. list_attr= 0;
  115. listsel_attr= A_STANDOUT;
  116. selstate_attr= A_BOLD;
  117. selstatesel_attr= A_STANDOUT;
  118. }
  119. query_attr= title_attr;
  120. info_attr= list_attr;
  121. colheads_attr= info_headattr= A_BOLD;
  122. whatinfo_attr= thisstate_attr;
  123. // set up windows and pads, based on screen size
  124. getmaxyx(stdscr,ymax,xmax);
  125. title_height= ymax>=6;
  126. colheads_height= ymax>=5;
  127. thisstate_height= ymax>=3;
  128. setheights();
  129. setwidths();
  130. titlewin= newwin(1,xmax, 0,0);
  131. if (!titlewin) ohshite("failed to create title window");
  132. wattrset(titlewin,title_attr);
  133. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  134. if (!whatinfowin) ohshite("failed to create whatinfo window");
  135. wattrset(whatinfowin,whatinfo_attr);
  136. listpad= newpad(nitems+1, total_width);
  137. if (!listpad) ohshite("failed to create baselist pad");
  138. colheadspad= newpad(1, total_width);
  139. if (!colheadspad) ohshite("failed to create heading pad");
  140. wattrset(colheadspad,colheads_attr);
  141. thisstatepad= newpad(1, total_width);
  142. if (!thisstatepad) ohshite("failed to create thisstate pad");
  143. wattrset(thisstatepad,thisstate_attr);
  144. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  145. if (!infopad) ohshite("failed to create info pad");
  146. wattrset(infopad,info_attr);
  147. querywin= newwin(1,xmax,ymax-1,0);
  148. if (!querywin) ohshite("failed to create query window");
  149. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  150. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  151. if (topofscreen < 0) topofscreen= 0;
  152. infotopofscreen= 0; leftofscreen= 0;
  153. redrawall();
  154. if (debug)
  155. fprintf(debug,
  156. "baselist::startdisplay() done ...\n\n"
  157. " xmax=%d, ymax=%d;\n\n"
  158. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  159. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  160. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  161. " whatinfo_row=%d, list_row=%d;\n\n",
  162. xmax, ymax,
  163. title_height, colheads_height, list_height,
  164. thisstate_height, info_height, whatinfo_height,
  165. colheads_row, thisstate_row, info_row,
  166. whatinfo_row, list_row);
  167. }
  168. void baselist::enddisplay() {
  169. delwin(titlewin);
  170. delwin(whatinfowin);
  171. delwin(listpad);
  172. delwin(colheadspad);
  173. delwin(thisstatepad);
  174. delwin(infopad);
  175. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  176. listpad= 0;
  177. }
  178. void baselist::redrawall() {
  179. redrawtitle();
  180. redrawcolheads();
  181. wattrset(listpad,list_attr); mywerase(listpad);
  182. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  183. refreshlist();
  184. redrawthisstate();
  185. redrawinfo();
  186. }
  187. void baselist::redraw1item(int index) {
  188. redraw1itemsel(index, showinfo == 2 ? 0 : index == cursorline);
  189. }
  190. baselist::baselist(keybindings *kb) {
  191. if (debug)
  192. fprintf(debug,"baselist[%p]::baselist()\n",this);
  193. bindings= kb;
  194. nitems= 0;
  195. xmax= -1;
  196. list_height=0; info_height=0;
  197. topofscreen= 0; leftofscreen= 0;
  198. listpad= 0; cursorline= -1;
  199. showinfo= 1;
  200. searchstring[0]= 0;
  201. }
  202. void baselist::itd_keys() {
  203. whatinfovb("keybindings");
  204. const int givek= xmax/3;
  205. bindings->describestart();
  206. const char **ta;
  207. while ((ta= bindings->describenext()) != 0) {
  208. const char **tap= ta+1;
  209. for (;;) {
  210. waddstr(infopad, *tap);
  211. tap++; if (!*tap) break;
  212. waddstr(infopad, ", ");
  213. }
  214. int y,x;
  215. getyx(infopad,y,x);
  216. if (x >= givek) y++;
  217. mvwaddstr(infopad, y,givek, ta[0]);
  218. waddch(infopad,'\n');
  219. }
  220. }
  221. void baselist::dosearch() {
  222. int offset, index, searchlen;
  223. searchlen= strlen(searchstring);
  224. if (debug) fprintf(debug,"packagelist[%p]::dosearch(); searchstring=`%s' len=%d\n",
  225. this,searchstring,searchlen);
  226. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  227. offset<nitems;
  228. offset++, index++) {
  229. if (index >= nitems) index -= nitems;
  230. int lendiff, i;
  231. const char *thisname;
  232. thisname= itemname(index);
  233. if (!thisname) continue;
  234. lendiff= strlen(thisname) - searchlen;
  235. for (i=0; i<=lendiff; i++)
  236. if (!strncasecmp(thisname + i, searchstring, searchlen)) {
  237. topofscreen= index-1;
  238. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  239. if (topofscreen < 0) topofscreen= 0;
  240. setcursor(index);
  241. return;
  242. }
  243. }
  244. beep();
  245. }
  246. void baselist::refreshinfo() {
  247. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  248. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  249. lesserint(total_width - leftofscreen - 1, xmax - 1));
  250. if (whatinfo_height) {
  251. mywerase(whatinfowin);
  252. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  253. if (infolines > info_height) {
  254. wprintw(whatinfowin," -- %d%%, press ",
  255. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  256. if (infotopofscreen + info_height < infolines) {
  257. wprintw(whatinfowin,"%s for more", bindings->find("iscrollon"));
  258. if (infotopofscreen) waddstr(whatinfowin, ", ");
  259. }
  260. if (infotopofscreen)
  261. wprintw(whatinfowin, "%s to go back",bindings->find("iscrollback"));
  262. waddch(whatinfowin,'.');
  263. }
  264. wnoutrefresh(whatinfowin);
  265. }
  266. }
  267. void baselist::wordwrapinfo(int offset, const char *m) {
  268. int usemax= xmax-5;
  269. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  270. int wrapping=0;
  271. for (;;) {
  272. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  273. const char *p= strchr(m,'\n');
  274. int l= p ? (int)(p-m) : strlen(m);
  275. while (l && isspace(m[l-1])) l--;
  276. if (!l || *m == '.' && l == 1) {
  277. if (wrapping) waddch(infopad,'\n');
  278. waddch(infopad,'\n');
  279. wrapping= 0;
  280. } else if (*m == ' ' || usemax < 10) {
  281. if (wrapping) waddch(infopad,'\n');
  282. waddnstr(infopad, m, l);
  283. waddch(infopad,'\n'); wrapping= 0;
  284. } else {
  285. int x,y;
  286. if (wrapping) {
  287. getyx(infopad, y,x);
  288. if (x+1 >= usemax) {
  289. waddch(infopad,'\n');
  290. } else {
  291. waddch(infopad,' ');
  292. }
  293. }
  294. for (;;) {
  295. getyx(infopad, y,x);
  296. int dosend= usemax-x;
  297. if (l <= dosend) {
  298. dosend=l;
  299. } else {
  300. int i=dosend;
  301. while (i > 0 && m[i] != ' ') i--;
  302. if (i > 0 || x > 0) dosend=i;
  303. }
  304. if (dosend) waddnstr(infopad, m, dosend);
  305. while (dosend < l && m[dosend] == ' ') dosend++;
  306. l-= dosend; m+= dosend;
  307. if (l <= 0) break;
  308. waddch(infopad,'\n');
  309. }
  310. wrapping= 1;
  311. }
  312. if (!p) break;
  313. m= ++p;
  314. }
  315. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  316. }
  317. baselist::~baselist() { }