baselist.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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. debug(dbg_general, "baselist::sigwinchhandler(), signallist=%p", 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. debug(dbg_general, "baselist[%p]::startdisplay()", 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. part_attr[i] = COLOR_PAIR(i) | color[i].attr;
  112. }
  113. } else {
  114. /* User defined attributes for B&W mode are not currently supported. */
  115. part_attr[title] = A_REVERSE;
  116. part_attr[thisstate] = A_STANDOUT;
  117. part_attr[list] = 0;
  118. part_attr[listsel] = A_STANDOUT;
  119. part_attr[selstate] = A_BOLD;
  120. part_attr[selstatesel] = A_STANDOUT;
  121. part_attr[colheads]= A_BOLD;
  122. part_attr[query] = part_attr[title];
  123. part_attr[info] = part_attr[list];
  124. part_attr[info_head] = A_BOLD;
  125. part_attr[whatinfo] = part_attr[thisstate];
  126. part_attr[helpscreen] = A_NORMAL;
  127. }
  128. // set up windows and pads, based on screen size
  129. getmaxyx(stdscr,ymax,xmax);
  130. title_height= ymax>=6;
  131. colheads_height= ymax>=5;
  132. thisstate_height= ymax>=3;
  133. setheights();
  134. setwidths();
  135. titlewin= newwin(1,xmax, 0,0);
  136. if (!titlewin) ohshite(_("failed to create title window"));
  137. wattrset(titlewin, part_attr[title]);
  138. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  139. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  140. wattrset(whatinfowin, part_attr[whatinfo]);
  141. listpad = newpad(ymax, total_width);
  142. if (!listpad) ohshite(_("failed to create baselist pad"));
  143. colheadspad= newpad(1, total_width);
  144. if (!colheadspad) ohshite(_("failed to create heading pad"));
  145. wattrset(colheadspad, part_attr[colheads]);
  146. thisstatepad= newpad(1, total_width);
  147. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  148. wattrset(thisstatepad, part_attr[thisstate]);
  149. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  150. if (!infopad) ohshite(_("failed to create info pad"));
  151. wattrset(infopad, part_attr[info]);
  152. wbkgdset(infopad, ' ' | part_attr[info]);
  153. querywin= newwin(1,xmax,ymax-1,0);
  154. if (!querywin) ohshite(_("failed to create query window"));
  155. wbkgdset(querywin, ' ' | part_attr[query]);
  156. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  157. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  158. if (topofscreen < 0) topofscreen= 0;
  159. infotopofscreen= 0; leftofscreen= 0;
  160. redrawall();
  161. debug(dbg_general,
  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, title_height, colheads_height, list_height,
  169. thisstate_height, info_height, whatinfo_height,
  170. colheads_row, thisstate_row, info_row, whatinfo_row, list_row);
  171. }
  172. void baselist::enddisplay() {
  173. delwin(titlewin);
  174. delwin(whatinfowin);
  175. delwin(listpad);
  176. delwin(colheadspad);
  177. delwin(thisstatepad);
  178. delwin(infopad);
  179. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  180. listpad= 0;
  181. }
  182. void baselist::redrawall() {
  183. redrawtitle();
  184. redrawcolheads();
  185. wattrset(listpad, part_attr[list]);
  186. mywerase(listpad);
  187. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  188. refreshlist();
  189. redrawthisstate();
  190. redrawinfo();
  191. }
  192. void baselist::redraw1item(int index) {
  193. redraw1itemsel(index, index == cursorline);
  194. }
  195. baselist::baselist(keybindings *kb) {
  196. debug(dbg_general, "baselist[%p]::baselist()", this);
  197. bindings= kb;
  198. nitems= 0;
  199. gap_width = 1;
  200. total_width = max(TOTAL_LIST_WIDTH, COLS);
  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;
  229. debug(dbg_general, "baselist[%p]::dosearch(); searchstring='%s'",
  230. this, searchstring);
  231. for (offset = 1, index = max(topofscreen, cursorline + 1);
  232. offset<nitems;
  233. offset++, index++) {
  234. if (index >= nitems) index -= nitems;
  235. if (matchsearch(index)) {
  236. topofscreen= index-1;
  237. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  238. if (topofscreen < 0) topofscreen= 0;
  239. setcursor(index);
  240. return;
  241. }
  242. }
  243. beep();
  244. }
  245. void baselist::refreshinfo() {
  246. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  247. min(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  248. min(total_width - leftofscreen - 1, xmax - 1));
  249. if (whatinfo_height) {
  250. mywerase(whatinfowin);
  251. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  252. if (infolines > info_height) {
  253. wprintw(whatinfowin,_(" -- %d%%, press "),
  254. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  255. if (infotopofscreen + info_height < infolines) {
  256. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  257. if (infotopofscreen) waddstr(whatinfowin, ", ");
  258. }
  259. if (infotopofscreen)
  260. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  261. waddch(whatinfowin,'.');
  262. }
  263. wnoutrefresh(whatinfowin);
  264. }
  265. }
  266. void baselist::wordwrapinfo(int offset, const char *m) {
  267. int usemax= xmax-5;
  268. debug(dbg_general, "baselist[%p]::wordwrapinfo(%d, '%s')", this, offset, m);
  269. int wrapping=0;
  270. for (;;) {
  271. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  272. const char *p= strchr(m,'\n');
  273. int l= p ? (int)(p-m) : strlen(m);
  274. while (l && isspace(m[l-1])) l--;
  275. if (!l || (*m == '.' && l == 1)) {
  276. if (wrapping) waddch(infopad,'\n');
  277. waddch(infopad,'\n');
  278. wrapping= 0;
  279. } else if (*m == ' ' || usemax < 10) {
  280. if (wrapping) waddch(infopad,'\n');
  281. waddnstr(infopad, m, l);
  282. waddch(infopad,'\n'); wrapping= 0;
  283. } else {
  284. int x, y DPKG_ATTR_UNUSED;
  285. if (wrapping) {
  286. getyx(infopad, y,x);
  287. if (x+1 >= usemax) {
  288. waddch(infopad,'\n');
  289. } else {
  290. waddch(infopad,' ');
  291. }
  292. }
  293. for (;;) {
  294. getyx(infopad, y,x);
  295. int dosend= usemax-x;
  296. if (l <= dosend) {
  297. dosend=l;
  298. } else {
  299. int i=dosend;
  300. while (i > 0 && m[i] != ' ') i--;
  301. if (i > 0 || x > 0) dosend=i;
  302. }
  303. if (dosend) waddnstr(infopad, m, dosend);
  304. while (dosend < l && m[dosend] == ' ') dosend++;
  305. l-= dosend; m+= dosend;
  306. if (l <= 0) break;
  307. waddch(infopad,'\n');
  308. }
  309. wrapping= 1;
  310. }
  311. if (!p) break;
  312. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  313. waddstr(infopad,
  314. "[The package description is too long and has been truncated...]");
  315. break;
  316. }
  317. m= ++p;
  318. }
  319. debug(dbg_general, "baselist[%p]::wordwrapinfo() done", this);
  320. }
  321. baselist::~baselist() { }
  322. /* vi: sw=2 ts=8
  323. */