baselist.cc 11 KB

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