baselist.cc 12 KB

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