baselist.cc 11 KB

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