baselist.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * baselist.cc - list of somethings
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. extern "C" {
  23. #include <config.h>
  24. }
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/termios.h>
  32. extern "C" {
  33. #include <dpkg.h>
  34. #include <dpkg-db.h>
  35. }
  36. #include "dselect.h"
  37. #include "bindings.h"
  38. void mywerase(WINDOW *win) {
  39. int my,mx,y,x;
  40. getmaxyx(win,my,mx);
  41. for (y=0; y<my; y++) {
  42. wmove(win,y,0); for (x=0; x<mx; x++) waddch(win,' ');
  43. }
  44. wmove(win,0,0);
  45. }
  46. baselist *baselist::signallist= 0;
  47. void baselist::sigwinchhandler(int) {
  48. struct winsize size;
  49. if (debug) fprintf(debug,"baselist::sigwinchhandler(), signallist=%p\n",signallist);
  50. baselist *p= signallist;
  51. p->enddisplay();
  52. endwin(); initscr();
  53. if (ioctl(fileno(stdout), TIOCGWINSZ, &size) != 0) ohshite(_("ioctl(TIOCGWINSZ) failed"));
  54. resizeterm(size.ws_row, size.ws_col); wrefresh(curscr);
  55. p->startdisplay();
  56. if (doupdate() == ERR) ohshite(_("doupdate in SIGWINCH handler failed"));
  57. }
  58. static void cu_sigwinch(int, void **argv) {
  59. struct sigaction *osigactp= (struct sigaction*)argv[0];
  60. sigset_t *oblockedp= (sigset_t*)argv[1];
  61. if (sigaction(SIGWINCH,osigactp,0)) ohshite(_("failed to restore old SIGWINCH sigact"));
  62. delete osigactp;
  63. if (sigprocmask(SIG_SETMASK,oblockedp,0)) ohshite(_("failed to restore old signal mask"));
  64. delete oblockedp;
  65. }
  66. void baselist::setupsigwinch() {
  67. sigemptyset(&sigwinchset);
  68. sigaddset(&sigwinchset,SIGWINCH);
  69. osigactp= new(struct sigaction);
  70. oblockedp= new(sigset_t);
  71. if (sigprocmask(0,0,oblockedp)) ohshite(_("failed to get old signal mask"));
  72. if (sigaction(SIGWINCH,0,osigactp)) ohshite(_("failed to get old SIGWINCH sigact"));
  73. push_cleanup(cu_sigwinch,~0, 0,0, 2,(void*)osigactp,(void*)oblockedp);
  74. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite(_("failed to block SIGWINCH"));
  75. memset(&nsigact,0,sizeof(nsigact));
  76. nsigact.sa_handler= sigwinchhandler;
  77. sigemptyset(&nsigact.sa_mask);
  78. //nsigact.sa_flags= SA_INTERRUPT;
  79. if (sigaction(SIGWINCH,&nsigact,0)) ohshite(_("failed to set new SIGWINCH sigact"));
  80. }
  81. void baselist::setheights() {
  82. int y= ymax - (title_height + colheads_height + thisstate_height);
  83. assert(y>=1);
  84. if (showinfo==2 && y>=7) {
  85. list_height= 5;
  86. whatinfo_height= 1;
  87. info_height= y-6;
  88. } else if (showinfo==1 && y>=10) {
  89. list_height= y/2;
  90. info_height= (y-1)/2;
  91. whatinfo_height= 1;
  92. } else {
  93. list_height= y;
  94. info_height= 0;
  95. whatinfo_height= 0;
  96. }
  97. colheads_row= title_height;
  98. list_row= colheads_row + colheads_height;
  99. thisstate_row= list_row + list_height;
  100. info_row= thisstate_row + thisstate_height;
  101. whatinfo_row= ymax - 1;
  102. }
  103. void baselist::startdisplay() {
  104. if (debug) fprintf(debug,"baselist[%p]::startdisplay()\n",this);
  105. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  106. clear(); wnoutrefresh(stdscr);
  107. // find attributes
  108. if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
  109. int i;
  110. printf("allocing\n");
  111. for (i = 1; i < numscreenparts; i++) {
  112. if (init_pair(i, color[i].fore, color[i].back) != OK)
  113. ohshite(_("failed to allocate colour pair"));
  114. }
  115. /* TODO: should use an array of attr's, indexed by attr name. Oh well. */
  116. list_attr= COLOR_PAIR(list) | color[list].attr;
  117. listsel_attr= COLOR_PAIR(listsel) | color[listsel].attr;
  118. title_attr= COLOR_PAIR(title) | color[title].attr;
  119. thisstate_attr= COLOR_PAIR(thisstate) | color[thisstate].attr;
  120. selstate_attr= COLOR_PAIR(selstate) | color[selstate].attr;
  121. selstatesel_attr= COLOR_PAIR(selstatesel) | color[selstatesel].attr;
  122. colheads_attr= COLOR_PAIR(colheads) | color[colheads].attr;
  123. query_attr= COLOR_PAIR(query) | color[query].attr;
  124. info_attr= COLOR_PAIR(info) | color[info].attr;
  125. info_headattr= COLOR_PAIR(info_head) | color[info_head].attr;
  126. whatinfo_attr= COLOR_PAIR(whatinfo) | color[whatinfo].attr;
  127. helpscreen_attr= COLOR_PAIR(helpscreen) | color[helpscreen].attr;
  128. } else {
  129. /* User defined attributes for B&W mode are not currently supported. */
  130. title_attr= A_REVERSE;
  131. thisstate_attr= A_STANDOUT;
  132. list_attr= 0;
  133. listsel_attr= A_STANDOUT;
  134. selstate_attr= A_BOLD;
  135. selstatesel_attr= A_STANDOUT;
  136. colheads_attr= A_BOLD;
  137. query_attr= title_attr;
  138. info_attr= list_attr;
  139. info_headattr= A_BOLD;
  140. whatinfo_attr= thisstate_attr;
  141. helpscreen_attr= A_NORMAL;
  142. }
  143. // set up windows and pads, based on screen size
  144. getmaxyx(stdscr,ymax,xmax);
  145. title_height= ymax>=6;
  146. colheads_height= ymax>=5;
  147. thisstate_height= ymax>=3;
  148. setheights();
  149. setwidths();
  150. titlewin= newwin(1,xmax, 0,0);
  151. if (!titlewin) ohshite(_("failed to create title window"));
  152. wattrset(titlewin,title_attr);
  153. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  154. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  155. wattrset(whatinfowin,whatinfo_attr);
  156. listpad= newpad(nitems+1, total_width);
  157. if (!listpad) ohshite(_("failed to create baselist pad"));
  158. colheadspad= newpad(1, total_width);
  159. if (!colheadspad) ohshite(_("failed to create heading pad"));
  160. wattrset(colheadspad,colheads_attr);
  161. thisstatepad= newpad(1, total_width);
  162. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  163. wattrset(thisstatepad,thisstate_attr);
  164. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  165. if (!infopad) ohshite(_("failed to create info pad"));
  166. wattrset(infopad,info_attr);
  167. wbkgdset(infopad, ' ' | info_attr);
  168. querywin= newwin(1,xmax,ymax-1,0);
  169. if (!querywin) ohshite(_("failed to create query window"));
  170. wbkgdset(querywin, ' ' | query_attr);
  171. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  172. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  173. if (topofscreen < 0) topofscreen= 0;
  174. infotopofscreen= 0; leftofscreen= 0;
  175. redrawall();
  176. if (debug)
  177. fprintf(debug,
  178. _("baselist::startdisplay() done ...\n\n"
  179. " xmax=%d, ymax=%d;\n\n"
  180. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  181. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  182. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  183. " whatinfo_row=%d, list_row=%d;\n\n"),
  184. xmax, ymax,
  185. title_height, colheads_height, list_height,
  186. thisstate_height, info_height, whatinfo_height,
  187. colheads_row, thisstate_row, info_row,
  188. whatinfo_row, list_row);
  189. }
  190. void baselist::enddisplay() {
  191. delwin(titlewin);
  192. delwin(whatinfowin);
  193. delwin(listpad);
  194. delwin(colheadspad);
  195. delwin(thisstatepad);
  196. delwin(infopad);
  197. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  198. listpad= 0;
  199. }
  200. void baselist::redrawall() {
  201. redrawtitle();
  202. redrawcolheads();
  203. wattrset(listpad,list_attr); mywerase(listpad);
  204. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  205. refreshlist();
  206. redrawthisstate();
  207. redrawinfo();
  208. }
  209. void baselist::redraw1item(int index) {
  210. redraw1itemsel(index, index == cursorline);
  211. }
  212. baselist::baselist(keybindings *kb) {
  213. if (debug)
  214. fprintf(debug,"baselist[%p]::baselist()\n",this);
  215. bindings= kb;
  216. nitems= 0;
  217. xmax= -1;
  218. list_height=0; info_height=0;
  219. topofscreen= 0; leftofscreen= 0;
  220. listpad= 0; cursorline= -1;
  221. showinfo= 1;
  222. searchstring[0]= 0;
  223. }
  224. void baselist::itd_keys() {
  225. whatinfovb(_("Keybindings"));
  226. const int givek= xmax/3;
  227. bindings->describestart();
  228. const char **ta;
  229. while ((ta= bindings->describenext()) != 0) {
  230. const char **tap= ta+1;
  231. for (;;) {
  232. waddstr(infopad, gettext(*tap));
  233. tap++; if (!*tap) break;
  234. waddstr(infopad, ", ");
  235. }
  236. int y,x;
  237. getyx(infopad,y,x);
  238. if (x >= givek) y++;
  239. mvwaddstr(infopad, y,givek, ta[0]);
  240. waddch(infopad,'\n');
  241. }
  242. }
  243. void baselist::dosearch() {
  244. int offset, index;
  245. if (debug) fprintf(debug,"baselist[%p]::dosearch(); searchstring=`%s'\n",
  246. this,searchstring);
  247. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  248. offset<nitems;
  249. offset++, index++) {
  250. if (index >= nitems) index -= nitems;
  251. if (matchsearch(index)) {
  252. topofscreen= index-1;
  253. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  254. if (topofscreen < 0) topofscreen= 0;
  255. setcursor(index);
  256. return;
  257. }
  258. }
  259. beep();
  260. }
  261. void baselist::refreshinfo() {
  262. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  263. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  264. lesserint(total_width - leftofscreen - 1, xmax - 1));
  265. if (whatinfo_height) {
  266. mywerase(whatinfowin);
  267. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  268. if (infolines > info_height) {
  269. wprintw(whatinfowin,_(" -- %d%%, press "),
  270. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  271. if (infotopofscreen + info_height < infolines) {
  272. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  273. if (infotopofscreen) waddstr(whatinfowin, ", ");
  274. }
  275. if (infotopofscreen)
  276. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  277. waddch(whatinfowin,'.');
  278. }
  279. wnoutrefresh(whatinfowin);
  280. }
  281. }
  282. void baselist::wordwrapinfo(int offset, const char *m) {
  283. int usemax= xmax-5;
  284. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  285. int wrapping=0;
  286. for (;;) {
  287. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  288. const char *p= strchr(m,'\n');
  289. int l= p ? (int)(p-m) : strlen(m);
  290. while (l && isspace(m[l-1])) l--;
  291. if (!l || *m == '.' && l == 1) {
  292. if (wrapping) waddch(infopad,'\n');
  293. waddch(infopad,'\n');
  294. wrapping= 0;
  295. } else if (*m == ' ' || usemax < 10) {
  296. if (wrapping) waddch(infopad,'\n');
  297. waddnstr(infopad, m, l);
  298. waddch(infopad,'\n'); wrapping= 0;
  299. } else {
  300. int x,y;
  301. if (wrapping) {
  302. getyx(infopad, y,x);
  303. if (x+1 >= usemax) {
  304. waddch(infopad,'\n');
  305. } else {
  306. waddch(infopad,' ');
  307. }
  308. }
  309. for (;;) {
  310. getyx(infopad, y,x);
  311. int dosend= usemax-x;
  312. if (l <= dosend) {
  313. dosend=l;
  314. } else {
  315. int i=dosend;
  316. while (i > 0 && m[i] != ' ') i--;
  317. if (i > 0 || x > 0) dosend=i;
  318. }
  319. if (dosend) waddnstr(infopad, m, dosend);
  320. while (dosend < l && m[dosend] == ' ') dosend++;
  321. l-= dosend; m+= dosend;
  322. if (l <= 0) break;
  323. waddch(infopad,'\n');
  324. }
  325. wrapping= 1;
  326. }
  327. if (!p) break;
  328. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  329. waddstr(infopad,
  330. "[The package description is too long and has been truncated...]");
  331. break;
  332. }
  333. m= ++p;
  334. }
  335. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  336. }
  337. baselist::~baselist() { }
  338. /* vi: sw=2 ts=8
  339. */