baselist.cc 12 KB

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