baselist.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 <string.h>
  28. #include <termios.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #include <dpkg/i18n.h>
  32. #include <dpkg/c-ctype.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
  89. baselist::add_column(column &col, const char *title, int width)
  90. {
  91. col.title = title;
  92. col.x = col_cur_x;
  93. col.width = width;
  94. col_cur_x += col.width + gap_width;
  95. }
  96. void
  97. baselist::end_column(column &col, const char *title)
  98. {
  99. col.title = title;
  100. col.x = col_cur_x;
  101. col.width = total_width - col.x;
  102. col_cur_x += col.width + gap_width;
  103. }
  104. void
  105. baselist::draw_column_head(column &col)
  106. {
  107. mvwaddnstr(colheadspad, 0, col.x, col.title, col.width);
  108. }
  109. void
  110. baselist::draw_column_sep(column &col, int y)
  111. {
  112. mvwaddch(listpad, y, col.x - 1, ' ');
  113. }
  114. void
  115. baselist::draw_column_item(column &col, int y, const char *item)
  116. {
  117. mvwprintw(listpad, y, col.x, "%-*.*s", col.width, col.width, item);
  118. }
  119. void baselist::setheights() {
  120. int y= ymax - (title_height + colheads_height + thisstate_height);
  121. assert(y>=1);
  122. if (showinfo==2 && y>=7) {
  123. list_height= 5;
  124. whatinfo_height= 1;
  125. info_height= y-6;
  126. } else if (showinfo==1 && y>=10) {
  127. list_height= y/2;
  128. info_height= (y-1)/2;
  129. whatinfo_height= 1;
  130. } else {
  131. list_height= y;
  132. info_height= 0;
  133. whatinfo_height= 0;
  134. }
  135. colheads_row= title_height;
  136. list_row= colheads_row + colheads_height;
  137. thisstate_row= list_row + list_height;
  138. info_row= thisstate_row + thisstate_height;
  139. whatinfo_row= ymax - 1;
  140. }
  141. void baselist::startdisplay() {
  142. debug(dbg_general, "baselist[%p]::startdisplay()", this);
  143. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  144. clear(); wnoutrefresh(stdscr);
  145. // find attributes
  146. if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
  147. int i;
  148. printf("allocing\n");
  149. for (i = 1; i < numscreenparts; i++) {
  150. if (init_pair(i, color[i].fore, color[i].back) != OK)
  151. ohshite(_("failed to allocate colour pair"));
  152. part_attr[i] = COLOR_PAIR(i) | color[i].attr;
  153. }
  154. } else {
  155. /* User defined attributes for B&W mode are not currently supported. */
  156. part_attr[title] = A_REVERSE;
  157. part_attr[thisstate] = A_STANDOUT;
  158. part_attr[list] = 0;
  159. part_attr[listsel] = A_STANDOUT;
  160. part_attr[selstate] = A_BOLD;
  161. part_attr[selstatesel] = A_STANDOUT;
  162. part_attr[colheads]= A_BOLD;
  163. part_attr[query] = part_attr[title];
  164. part_attr[info] = part_attr[list];
  165. part_attr[info_head] = A_BOLD;
  166. part_attr[whatinfo] = part_attr[thisstate];
  167. part_attr[helpscreen] = A_NORMAL;
  168. }
  169. // set up windows and pads, based on screen size
  170. getmaxyx(stdscr,ymax,xmax);
  171. title_height= ymax>=6;
  172. colheads_height= ymax>=5;
  173. thisstate_height= ymax>=3;
  174. setheights();
  175. setwidths();
  176. titlewin= newwin(1,xmax, 0,0);
  177. if (!titlewin) ohshite(_("failed to create title window"));
  178. wattrset(titlewin, part_attr[title]);
  179. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  180. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  181. wattrset(whatinfowin, part_attr[whatinfo]);
  182. listpad = newpad(ymax, total_width);
  183. if (!listpad) ohshite(_("failed to create baselist pad"));
  184. colheadspad= newpad(1, total_width);
  185. if (!colheadspad) ohshite(_("failed to create heading pad"));
  186. wattrset(colheadspad, part_attr[colheads]);
  187. thisstatepad= newpad(1, total_width);
  188. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  189. wattrset(thisstatepad, part_attr[thisstate]);
  190. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  191. if (!infopad) ohshite(_("failed to create info pad"));
  192. wattrset(infopad, part_attr[info]);
  193. wbkgdset(infopad, ' ' | part_attr[info]);
  194. querywin= newwin(1,xmax,ymax-1,0);
  195. if (!querywin) ohshite(_("failed to create query window"));
  196. wbkgdset(querywin, ' ' | part_attr[query]);
  197. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  198. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  199. if (topofscreen < 0) topofscreen= 0;
  200. infotopofscreen= 0; leftofscreen= 0;
  201. redrawall();
  202. debug(dbg_general,
  203. "baselist::startdisplay() done ...\n\n"
  204. " xmax=%d, ymax=%d;\n\n"
  205. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  206. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  207. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  208. " whatinfo_row=%d, list_row=%d;\n\n",
  209. xmax, ymax, title_height, colheads_height, list_height,
  210. thisstate_height, info_height, whatinfo_height,
  211. colheads_row, thisstate_row, info_row, whatinfo_row, list_row);
  212. }
  213. void baselist::enddisplay() {
  214. delwin(titlewin);
  215. delwin(whatinfowin);
  216. delwin(listpad);
  217. delwin(colheadspad);
  218. delwin(thisstatepad);
  219. delwin(infopad);
  220. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  221. listpad = nullptr;
  222. col_cur_x = 0;
  223. }
  224. void baselist::redrawall() {
  225. redrawtitle();
  226. redrawcolheads();
  227. wattrset(listpad, part_attr[list]);
  228. mywerase(listpad);
  229. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  230. refreshlist();
  231. redrawthisstate();
  232. redrawinfo();
  233. }
  234. void baselist::redraw1item(int index) {
  235. redraw1itemsel(index, index == cursorline);
  236. }
  237. baselist::baselist(keybindings *kb) {
  238. debug(dbg_general, "baselist[%p]::baselist()", this);
  239. bindings= kb;
  240. nitems= 0;
  241. col_cur_x = 0;
  242. gap_width = 1;
  243. total_width = max(TOTAL_LIST_WIDTH, COLS);
  244. xmax= -1;
  245. list_height=0; info_height=0;
  246. topofscreen= 0; leftofscreen= 0;
  247. listpad = nullptr;
  248. cursorline = -1;
  249. showinfo= 1;
  250. searchstring[0]= 0;
  251. }
  252. void baselist::itd_keys() {
  253. whatinfovb(_("Keybindings"));
  254. const int givek= xmax/3;
  255. bindings->describestart();
  256. const char **ta;
  257. while ((ta = bindings->describenext()) != nullptr) {
  258. const char **tap= ta+1;
  259. for (;;) {
  260. waddstr(infopad, gettext(*tap));
  261. tap++; if (!*tap) break;
  262. waddstr(infopad, ", ");
  263. }
  264. int y,x;
  265. getyx(infopad,y,x);
  266. if (x >= givek) y++;
  267. mvwaddstr(infopad, y,givek, ta[0]);
  268. waddch(infopad,'\n');
  269. delete [] ta;
  270. }
  271. }
  272. void baselist::dosearch() {
  273. int offset, index;
  274. debug(dbg_general, "baselist[%p]::dosearch(); searchstring='%s'",
  275. this, searchstring);
  276. for (offset = 1, index = max(topofscreen, cursorline + 1);
  277. offset<nitems;
  278. offset++, index++) {
  279. if (index >= nitems) index -= nitems;
  280. if (matchsearch(index)) {
  281. topofscreen= index-1;
  282. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  283. if (topofscreen < 0) topofscreen= 0;
  284. setcursor(index);
  285. return;
  286. }
  287. }
  288. beep();
  289. }
  290. void baselist::refreshinfo() {
  291. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  292. min(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  293. min(total_width - leftofscreen - 1, xmax - 1));
  294. if (whatinfo_height) {
  295. mywerase(whatinfowin);
  296. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  297. if (infolines > info_height) {
  298. wprintw(whatinfowin,_(" -- %d%%, press "),
  299. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  300. if (infotopofscreen + info_height < infolines) {
  301. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  302. if (infotopofscreen) waddstr(whatinfowin, ", ");
  303. }
  304. if (infotopofscreen)
  305. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  306. waddch(whatinfowin,'.');
  307. }
  308. wnoutrefresh(whatinfowin);
  309. }
  310. }
  311. void baselist::wordwrapinfo(int offset, const char *m) {
  312. int usemax= xmax-5;
  313. debug(dbg_general, "baselist[%p]::wordwrapinfo(%d, '%s')", this, offset, m);
  314. bool wrapping = false;
  315. for (;;) {
  316. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  317. const char *p= strchr(m,'\n');
  318. int l= p ? (int)(p-m) : strlen(m);
  319. while (l && c_isspace(m[l - 1]))
  320. l--;
  321. if (!l || (*m == '.' && l == 1)) {
  322. if (wrapping) waddch(infopad,'\n');
  323. waddch(infopad, '\n');
  324. wrapping = false;
  325. } else if (*m == ' ' || usemax < 10) {
  326. if (wrapping) waddch(infopad,'\n');
  327. waddnstr(infopad, m, l);
  328. waddch(infopad, '\n');
  329. wrapping = false;
  330. } else {
  331. int x, y DPKG_ATTR_UNUSED;
  332. if (wrapping) {
  333. getyx(infopad, y,x);
  334. if (x+1 >= usemax) {
  335. waddch(infopad,'\n');
  336. } else {
  337. waddch(infopad,' ');
  338. }
  339. }
  340. for (;;) {
  341. getyx(infopad, y,x);
  342. int dosend= usemax-x;
  343. if (l <= dosend) {
  344. dosend=l;
  345. } else {
  346. int i=dosend;
  347. while (i > 0 && m[i] != ' ') i--;
  348. if (i > 0 || x > 0) dosend=i;
  349. }
  350. if (dosend) waddnstr(infopad, m, dosend);
  351. while (dosend < l && m[dosend] == ' ') dosend++;
  352. l-= dosend; m+= dosend;
  353. if (l <= 0) break;
  354. waddch(infopad,'\n');
  355. }
  356. wrapping = true;
  357. }
  358. if (!p) break;
  359. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  360. waddstr(infopad,
  361. "[The package description is too long and has been truncated...]");
  362. break;
  363. }
  364. m= ++p;
  365. }
  366. debug(dbg_general, "baselist[%p]::wordwrapinfo() done", this);
  367. }
  368. baselist::~baselist() { }