baselist.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/termios.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  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. helpscreen_attr= A_NORMAL;
  139. }
  140. // set up windows and pads, based on screen size
  141. getmaxyx(stdscr,ymax,xmax);
  142. title_height= ymax>=6;
  143. colheads_height= ymax>=5;
  144. thisstate_height= ymax>=3;
  145. setheights();
  146. setwidths();
  147. titlewin= newwin(1,xmax, 0,0);
  148. if (!titlewin) ohshite(_("failed to create title window"));
  149. wattrset(titlewin,title_attr);
  150. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  151. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  152. wattrset(whatinfowin,whatinfo_attr);
  153. listpad = newpad(ymax, total_width);
  154. if (!listpad) ohshite(_("failed to create baselist pad"));
  155. colheadspad= newpad(1, total_width);
  156. if (!colheadspad) ohshite(_("failed to create heading pad"));
  157. wattrset(colheadspad,colheads_attr);
  158. thisstatepad= newpad(1, total_width);
  159. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  160. wattrset(thisstatepad,thisstate_attr);
  161. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  162. if (!infopad) ohshite(_("failed to create info pad"));
  163. wattrset(infopad,info_attr);
  164. wbkgdset(infopad, ' ' | info_attr);
  165. querywin= newwin(1,xmax,ymax-1,0);
  166. if (!querywin) ohshite(_("failed to create query window"));
  167. wbkgdset(querywin, ' ' | query_attr);
  168. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  169. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  170. if (topofscreen < 0) topofscreen= 0;
  171. infotopofscreen= 0; leftofscreen= 0;
  172. redrawall();
  173. if (debug)
  174. fprintf(debug,
  175. "baselist::startdisplay() done ...\n\n"
  176. " xmax=%d, ymax=%d;\n\n"
  177. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  178. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  179. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  180. " whatinfo_row=%d, list_row=%d;\n\n",
  181. xmax, ymax,
  182. title_height, colheads_height, list_height,
  183. thisstate_height, info_height, whatinfo_height,
  184. colheads_row, thisstate_row, info_row,
  185. whatinfo_row, list_row);
  186. }
  187. void baselist::enddisplay() {
  188. delwin(titlewin);
  189. delwin(whatinfowin);
  190. delwin(listpad);
  191. delwin(colheadspad);
  192. delwin(thisstatepad);
  193. delwin(infopad);
  194. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  195. listpad= 0;
  196. }
  197. void baselist::redrawall() {
  198. redrawtitle();
  199. redrawcolheads();
  200. wattrset(listpad,list_attr); mywerase(listpad);
  201. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  202. refreshlist();
  203. redrawthisstate();
  204. redrawinfo();
  205. }
  206. void baselist::redraw1item(int index) {
  207. redraw1itemsel(index, index == cursorline);
  208. }
  209. baselist::baselist(keybindings *kb) {
  210. if (debug)
  211. fprintf(debug,"baselist[%p]::baselist()\n",this);
  212. bindings= kb;
  213. nitems= 0;
  214. xmax= -1;
  215. list_height=0; info_height=0;
  216. topofscreen= 0; leftofscreen= 0;
  217. listpad= 0; cursorline= -1;
  218. showinfo= 1;
  219. searchstring[0]= 0;
  220. }
  221. void baselist::itd_keys() {
  222. whatinfovb(_("Keybindings"));
  223. const int givek= xmax/3;
  224. bindings->describestart();
  225. const char **ta;
  226. while ((ta= bindings->describenext()) != 0) {
  227. const char **tap= ta+1;
  228. for (;;) {
  229. waddstr(infopad, gettext(*tap));
  230. tap++; if (!*tap) break;
  231. waddstr(infopad, ", ");
  232. }
  233. int y,x;
  234. getyx(infopad,y,x);
  235. if (x >= givek) y++;
  236. mvwaddstr(infopad, y,givek, ta[0]);
  237. waddch(infopad,'\n');
  238. }
  239. }
  240. void baselist::dosearch() {
  241. int offset, index;
  242. if (debug) fprintf(debug,"baselist[%p]::dosearch(); searchstring=`%s'\n",
  243. this,searchstring);
  244. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  245. offset<nitems;
  246. offset++, index++) {
  247. if (index >= nitems) index -= nitems;
  248. if (matchsearch(index)) {
  249. topofscreen= index-1;
  250. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  251. if (topofscreen < 0) topofscreen= 0;
  252. setcursor(index);
  253. return;
  254. }
  255. }
  256. beep();
  257. }
  258. void baselist::refreshinfo() {
  259. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  260. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  261. lesserint(total_width - leftofscreen - 1, xmax - 1));
  262. if (whatinfo_height) {
  263. mywerase(whatinfowin);
  264. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  265. if (infolines > info_height) {
  266. wprintw(whatinfowin,_(" -- %d%%, press "),
  267. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  268. if (infotopofscreen + info_height < infolines) {
  269. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  270. if (infotopofscreen) waddstr(whatinfowin, ", ");
  271. }
  272. if (infotopofscreen)
  273. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  274. waddch(whatinfowin,'.');
  275. }
  276. wnoutrefresh(whatinfowin);
  277. }
  278. }
  279. void baselist::wordwrapinfo(int offset, const char *m) {
  280. int usemax= xmax-5;
  281. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  282. int wrapping=0;
  283. for (;;) {
  284. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  285. const char *p= strchr(m,'\n');
  286. int l= p ? (int)(p-m) : strlen(m);
  287. while (l && isspace(m[l-1])) l--;
  288. if (!l || (*m == '.' && l == 1)) {
  289. if (wrapping) waddch(infopad,'\n');
  290. waddch(infopad,'\n');
  291. wrapping= 0;
  292. } else if (*m == ' ' || usemax < 10) {
  293. if (wrapping) waddch(infopad,'\n');
  294. waddnstr(infopad, m, l);
  295. waddch(infopad,'\n'); wrapping= 0;
  296. } else {
  297. int x,y;
  298. if (wrapping) {
  299. getyx(infopad, y,x);
  300. if (x+1 >= usemax) {
  301. waddch(infopad,'\n');
  302. } else {
  303. waddch(infopad,' ');
  304. }
  305. }
  306. for (;;) {
  307. getyx(infopad, y,x);
  308. int dosend= usemax-x;
  309. if (l <= dosend) {
  310. dosend=l;
  311. } else {
  312. int i=dosend;
  313. while (i > 0 && m[i] != ' ') i--;
  314. if (i > 0 || x > 0) dosend=i;
  315. }
  316. if (dosend) waddnstr(infopad, m, dosend);
  317. while (dosend < l && m[dosend] == ' ') dosend++;
  318. l-= dosend; m+= dosend;
  319. if (l <= 0) break;
  320. waddch(infopad,'\n');
  321. }
  322. wrapping= 1;
  323. }
  324. if (!p) break;
  325. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  326. waddstr(infopad,
  327. "[The package description is too long and has been truncated...]");
  328. break;
  329. }
  330. m= ++p;
  331. }
  332. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  333. }
  334. baselist::~baselist() { }
  335. /* vi: sw=2 ts=8
  336. */