baselist.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. }
  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. debug(dbg_general,
  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, title_height, colheads_height, list_height,
  181. thisstate_height, info_height, whatinfo_height,
  182. colheads_row, thisstate_row, info_row, whatinfo_row, list_row);
  183. }
  184. void baselist::enddisplay() {
  185. delwin(titlewin);
  186. delwin(whatinfowin);
  187. delwin(listpad);
  188. delwin(colheadspad);
  189. delwin(thisstatepad);
  190. delwin(infopad);
  191. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  192. listpad= 0;
  193. }
  194. void baselist::redrawall() {
  195. redrawtitle();
  196. redrawcolheads();
  197. wattrset(listpad,list_attr); mywerase(listpad);
  198. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  199. refreshlist();
  200. redrawthisstate();
  201. redrawinfo();
  202. }
  203. void baselist::redraw1item(int index) {
  204. redraw1itemsel(index, index == cursorline);
  205. }
  206. baselist::baselist(keybindings *kb) {
  207. debug(dbg_general, "baselist[%p]::baselist()", this);
  208. bindings= kb;
  209. nitems= 0;
  210. xmax= -1;
  211. list_height=0; info_height=0;
  212. topofscreen= 0; leftofscreen= 0;
  213. listpad= 0; cursorline= -1;
  214. showinfo= 1;
  215. searchstring[0]= 0;
  216. }
  217. void baselist::itd_keys() {
  218. whatinfovb(_("Keybindings"));
  219. const int givek= xmax/3;
  220. bindings->describestart();
  221. const char **ta;
  222. while ((ta= bindings->describenext()) != 0) {
  223. const char **tap= ta+1;
  224. for (;;) {
  225. waddstr(infopad, gettext(*tap));
  226. tap++; if (!*tap) break;
  227. waddstr(infopad, ", ");
  228. }
  229. int y,x;
  230. getyx(infopad,y,x);
  231. if (x >= givek) y++;
  232. mvwaddstr(infopad, y,givek, ta[0]);
  233. waddch(infopad,'\n');
  234. }
  235. }
  236. void baselist::dosearch() {
  237. int offset, index;
  238. debug(dbg_general, "baselist[%p]::dosearch(); searchstring='%s'",
  239. this, searchstring);
  240. for (offset = 1, index = max(topofscreen, cursorline + 1);
  241. offset<nitems;
  242. offset++, index++) {
  243. if (index >= nitems) index -= nitems;
  244. if (matchsearch(index)) {
  245. topofscreen= index-1;
  246. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  247. if (topofscreen < 0) topofscreen= 0;
  248. setcursor(index);
  249. return;
  250. }
  251. }
  252. beep();
  253. }
  254. void baselist::refreshinfo() {
  255. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  256. min(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  257. min(total_width - leftofscreen - 1, xmax - 1));
  258. if (whatinfo_height) {
  259. mywerase(whatinfowin);
  260. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  261. if (infolines > info_height) {
  262. wprintw(whatinfowin,_(" -- %d%%, press "),
  263. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  264. if (infotopofscreen + info_height < infolines) {
  265. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  266. if (infotopofscreen) waddstr(whatinfowin, ", ");
  267. }
  268. if (infotopofscreen)
  269. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  270. waddch(whatinfowin,'.');
  271. }
  272. wnoutrefresh(whatinfowin);
  273. }
  274. }
  275. void baselist::wordwrapinfo(int offset, const char *m) {
  276. int usemax= xmax-5;
  277. debug(dbg_general, "baselist[%p]::wordwrapinfo(%d, '%s')", this, offset, m);
  278. int wrapping=0;
  279. for (;;) {
  280. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  281. const char *p= strchr(m,'\n');
  282. int l= p ? (int)(p-m) : strlen(m);
  283. while (l && isspace(m[l-1])) l--;
  284. if (!l || (*m == '.' && l == 1)) {
  285. if (wrapping) waddch(infopad,'\n');
  286. waddch(infopad,'\n');
  287. wrapping= 0;
  288. } else if (*m == ' ' || usemax < 10) {
  289. if (wrapping) waddch(infopad,'\n');
  290. waddnstr(infopad, m, l);
  291. waddch(infopad,'\n'); wrapping= 0;
  292. } else {
  293. int x, y DPKG_ATTR_UNUSED;
  294. if (wrapping) {
  295. getyx(infopad, y,x);
  296. if (x+1 >= usemax) {
  297. waddch(infopad,'\n');
  298. } else {
  299. waddch(infopad,' ');
  300. }
  301. }
  302. for (;;) {
  303. getyx(infopad, y,x);
  304. int dosend= usemax-x;
  305. if (l <= dosend) {
  306. dosend=l;
  307. } else {
  308. int i=dosend;
  309. while (i > 0 && m[i] != ' ') i--;
  310. if (i > 0 || x > 0) dosend=i;
  311. }
  312. if (dosend) waddnstr(infopad, m, dosend);
  313. while (dosend < l && m[dosend] == ' ') dosend++;
  314. l-= dosend; m+= dosend;
  315. if (l <= 0) break;
  316. waddch(infopad,'\n');
  317. }
  318. wrapping= 1;
  319. }
  320. if (!p) break;
  321. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  322. waddstr(infopad,
  323. "[The package description is too long and has been truncated...]");
  324. break;
  325. }
  326. m= ++p;
  327. }
  328. debug(dbg_general, "baselist[%p]::wordwrapinfo() done", this);
  329. }
  330. baselist::~baselist() { }
  331. /* vi: sw=2 ts=8
  332. */