baselist.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/ioctl.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <termios.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 = nullptr;
  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, nullptr))
  63. ohshite(_("failed to restore old SIGWINCH sigact"));
  64. delete osigactp;
  65. if (sigprocmask(SIG_SETMASK, oblockedp, nullptr))
  66. ohshite(_("failed to restore old signal mask"));
  67. delete oblockedp;
  68. }
  69. void baselist::setupsigwinch() {
  70. sigemptyset(&sigwinchset);
  71. sigaddset(&sigwinchset,SIGWINCH);
  72. osigactp= new(struct sigaction);
  73. oblockedp= new(sigset_t);
  74. if (sigprocmask(0, nullptr, oblockedp))
  75. ohshite(_("failed to get old signal mask"));
  76. if (sigaction(SIGWINCH, nullptr, osigactp))
  77. ohshite(_("failed to get old SIGWINCH sigact"));
  78. push_cleanup(cu_sigwinch, ~0, nullptr, 0, 2, osigactp, oblockedp);
  79. if (sigprocmask(SIG_BLOCK, &sigwinchset, nullptr))
  80. ohshite(_("failed to block SIGWINCH"));
  81. memset(&nsigact,0,sizeof(nsigact));
  82. nsigact.sa_handler= sigwinchhandler;
  83. sigemptyset(&nsigact.sa_mask);
  84. //nsigact.sa_flags= SA_INTERRUPT;
  85. if (sigaction(SIGWINCH, &nsigact, nullptr))
  86. ohshite(_("failed to set new SIGWINCH sigact"));
  87. }
  88. void baselist::setheights() {
  89. int y= ymax - (title_height + colheads_height + thisstate_height);
  90. assert(y>=1);
  91. if (showinfo==2 && y>=7) {
  92. list_height= 5;
  93. whatinfo_height= 1;
  94. info_height= y-6;
  95. } else if (showinfo==1 && y>=10) {
  96. list_height= y/2;
  97. info_height= (y-1)/2;
  98. whatinfo_height= 1;
  99. } else {
  100. list_height= y;
  101. info_height= 0;
  102. whatinfo_height= 0;
  103. }
  104. colheads_row= title_height;
  105. list_row= colheads_row + colheads_height;
  106. thisstate_row= list_row + list_height;
  107. info_row= thisstate_row + thisstate_height;
  108. whatinfo_row= ymax - 1;
  109. }
  110. void baselist::startdisplay() {
  111. debug(dbg_general, "baselist[%p]::startdisplay()", this);
  112. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  113. clear(); wnoutrefresh(stdscr);
  114. // find attributes
  115. if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
  116. int i;
  117. printf("allocing\n");
  118. for (i = 1; i < numscreenparts; i++) {
  119. if (init_pair(i, color[i].fore, color[i].back) != OK)
  120. ohshite(_("failed to allocate colour pair"));
  121. part_attr[i] = COLOR_PAIR(i) | color[i].attr;
  122. }
  123. } else {
  124. /* User defined attributes for B&W mode are not currently supported. */
  125. part_attr[title] = A_REVERSE;
  126. part_attr[thisstate] = A_STANDOUT;
  127. part_attr[list] = 0;
  128. part_attr[listsel] = A_STANDOUT;
  129. part_attr[selstate] = A_BOLD;
  130. part_attr[selstatesel] = A_STANDOUT;
  131. part_attr[colheads]= A_BOLD;
  132. part_attr[query] = part_attr[title];
  133. part_attr[info] = part_attr[list];
  134. part_attr[info_head] = A_BOLD;
  135. part_attr[whatinfo] = part_attr[thisstate];
  136. part_attr[helpscreen] = A_NORMAL;
  137. }
  138. // set up windows and pads, based on screen size
  139. getmaxyx(stdscr,ymax,xmax);
  140. title_height= ymax>=6;
  141. colheads_height= ymax>=5;
  142. thisstate_height= ymax>=3;
  143. setheights();
  144. setwidths();
  145. titlewin= newwin(1,xmax, 0,0);
  146. if (!titlewin) ohshite(_("failed to create title window"));
  147. wattrset(titlewin, part_attr[title]);
  148. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  149. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  150. wattrset(whatinfowin, part_attr[whatinfo]);
  151. listpad = newpad(ymax, total_width);
  152. if (!listpad) ohshite(_("failed to create baselist pad"));
  153. colheadspad= newpad(1, total_width);
  154. if (!colheadspad) ohshite(_("failed to create heading pad"));
  155. wattrset(colheadspad, part_attr[colheads]);
  156. thisstatepad= newpad(1, total_width);
  157. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  158. wattrset(thisstatepad, part_attr[thisstate]);
  159. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  160. if (!infopad) ohshite(_("failed to create info pad"));
  161. wattrset(infopad, part_attr[info]);
  162. wbkgdset(infopad, ' ' | part_attr[info]);
  163. querywin= newwin(1,xmax,ymax-1,0);
  164. if (!querywin) ohshite(_("failed to create query window"));
  165. wbkgdset(querywin, ' ' | part_attr[query]);
  166. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  167. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  168. if (topofscreen < 0) topofscreen= 0;
  169. infotopofscreen= 0; leftofscreen= 0;
  170. redrawall();
  171. debug(dbg_general,
  172. "baselist::startdisplay() done ...\n\n"
  173. " xmax=%d, ymax=%d;\n\n"
  174. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  175. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  176. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  177. " whatinfo_row=%d, list_row=%d;\n\n",
  178. xmax, ymax, title_height, colheads_height, list_height,
  179. thisstate_height, info_height, whatinfo_height,
  180. colheads_row, thisstate_row, info_row, whatinfo_row, list_row);
  181. }
  182. void baselist::enddisplay() {
  183. delwin(titlewin);
  184. delwin(whatinfowin);
  185. delwin(listpad);
  186. delwin(colheadspad);
  187. delwin(thisstatepad);
  188. delwin(infopad);
  189. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  190. listpad = nullptr;
  191. }
  192. void baselist::redrawall() {
  193. redrawtitle();
  194. redrawcolheads();
  195. wattrset(listpad, part_attr[list]);
  196. mywerase(listpad);
  197. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  198. refreshlist();
  199. redrawthisstate();
  200. redrawinfo();
  201. }
  202. void baselist::redraw1item(int index) {
  203. redraw1itemsel(index, index == cursorline);
  204. }
  205. baselist::baselist(keybindings *kb) {
  206. debug(dbg_general, "baselist[%p]::baselist()", this);
  207. bindings= kb;
  208. nitems= 0;
  209. gap_width = 1;
  210. total_width = max(TOTAL_LIST_WIDTH, COLS);
  211. xmax= -1;
  212. list_height=0; info_height=0;
  213. topofscreen= 0; leftofscreen= 0;
  214. listpad = nullptr;
  215. cursorline = -1;
  216. showinfo= 1;
  217. searchstring[0]= 0;
  218. }
  219. void baselist::itd_keys() {
  220. whatinfovb(_("Keybindings"));
  221. const int givek= xmax/3;
  222. bindings->describestart();
  223. const char **ta;
  224. while ((ta = bindings->describenext()) != nullptr) {
  225. const char **tap= ta+1;
  226. for (;;) {
  227. waddstr(infopad, gettext(*tap));
  228. tap++; if (!*tap) break;
  229. waddstr(infopad, ", ");
  230. }
  231. int y,x;
  232. getyx(infopad,y,x);
  233. if (x >= givek) y++;
  234. mvwaddstr(infopad, y,givek, ta[0]);
  235. waddch(infopad,'\n');
  236. delete [] ta;
  237. }
  238. }
  239. void baselist::dosearch() {
  240. int offset, index;
  241. debug(dbg_general, "baselist[%p]::dosearch(); searchstring='%s'",
  242. this, searchstring);
  243. for (offset = 1, index = max(topofscreen, cursorline + 1);
  244. offset<nitems;
  245. offset++, index++) {
  246. if (index >= nitems) index -= nitems;
  247. if (matchsearch(index)) {
  248. topofscreen= index-1;
  249. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  250. if (topofscreen < 0) topofscreen= 0;
  251. setcursor(index);
  252. return;
  253. }
  254. }
  255. beep();
  256. }
  257. void baselist::refreshinfo() {
  258. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  259. min(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  260. min(total_width - leftofscreen - 1, xmax - 1));
  261. if (whatinfo_height) {
  262. mywerase(whatinfowin);
  263. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  264. if (infolines > info_height) {
  265. wprintw(whatinfowin,_(" -- %d%%, press "),
  266. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  267. if (infotopofscreen + info_height < infolines) {
  268. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  269. if (infotopofscreen) waddstr(whatinfowin, ", ");
  270. }
  271. if (infotopofscreen)
  272. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  273. waddch(whatinfowin,'.');
  274. }
  275. wnoutrefresh(whatinfowin);
  276. }
  277. }
  278. void baselist::wordwrapinfo(int offset, const char *m) {
  279. int usemax= xmax-5;
  280. debug(dbg_general, "baselist[%p]::wordwrapinfo(%d, '%s')", this, offset, m);
  281. bool wrapping = false;
  282. for (;;) {
  283. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  284. const char *p= strchr(m,'\n');
  285. int l= p ? (int)(p-m) : strlen(m);
  286. while (l && isspace(m[l-1])) l--;
  287. if (!l || (*m == '.' && l == 1)) {
  288. if (wrapping) waddch(infopad,'\n');
  289. waddch(infopad, '\n');
  290. wrapping = false;
  291. } else if (*m == ' ' || usemax < 10) {
  292. if (wrapping) waddch(infopad,'\n');
  293. waddnstr(infopad, m, l);
  294. waddch(infopad, '\n');
  295. wrapping = false;
  296. } else {
  297. int x, y DPKG_ATTR_UNUSED;
  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 = false;
  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. debug(dbg_general, "baselist[%p]::wordwrapinfo() done", this);
  333. }
  334. baselist::~baselist() { }