baselist.cc 13 KB

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