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
23 #include <config.h>
24 #include <compat.h>
25
26 #include <sys/ioctl.h>
27
28 #include <errno.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <stdio.h>
33
34 #include <dpkg/i18n.h>
35 #include <dpkg/c-ctype.h>
36 #include <dpkg/dpkg.h>
37 #include <dpkg/dpkg-db.h>
38
39 #include "dselect.h"
40 #include "bindings.h"
41
mywerase(WINDOW * win)42 void mywerase(WINDOW *win) {
43 int my,mx,y,x;
44 getmaxyx(win,my,mx);
45 for (y=0; y<my; y++) {
46 wmove(win,y,0); for (x=0; x<mx; x++) waddch(win,' ');
47 }
48 wmove(win,0,0);
49 }
50
51 baselist *baselist::signallist = nullptr;
sigwinchhandler(int)52 void baselist::sigwinchhandler(int) {
53 int save_errno = errno;
54 struct winsize size;
55 debug(dbg_general, "baselist::sigwinchhandler(), signallist=%p", signallist);
56 baselist *p= signallist;
57 p->enddisplay();
58 endwin(); initscr();
59 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) != 0) ohshite(_("ioctl(TIOCGWINSZ) failed"));
60 resizeterm(size.ws_row, size.ws_col); wrefresh(curscr);
61 p->startdisplay();
62 if (doupdate() == ERR) ohshite(_("doupdate in SIGWINCH handler failed"));
63 errno = save_errno;
64 }
65
cu_sigwinch(int,void ** argv)66 static void cu_sigwinch(int, void **argv) {
67 struct sigaction *osigactp= (struct sigaction*)argv[0];
68 sigset_t *oblockedp= (sigset_t*)argv[1];
69
70 if (sigaction(SIGWINCH, osigactp, nullptr))
71 ohshite(_("failed to restore old SIGWINCH sigact"));
72 delete osigactp;
73 if (sigprocmask(SIG_SETMASK, oblockedp, nullptr))
74 ohshite(_("failed to restore old signal mask"));
75 delete oblockedp;
76 }
77
78 void
sigwinch_mask(int how)79 baselist::sigwinch_mask(int how)
80 {
81 sigset_t sigwinchset;
82 sigemptyset(&sigwinchset);
83 sigaddset(&sigwinchset,SIGWINCH);
84
85 int rc = sigprocmask(how, &sigwinchset, nullptr);
86 if (rc < 0) {
87 if (how == SIG_UNBLOCK)
88 ohshite(_("failed to unblock SIGWINCH"));
89 else
90 ohshite(_("failed to block SIGWINCH"));
91 }
92 }
93
94 void
setupsigwinch()95 baselist::setupsigwinch()
96 {
97 struct sigaction *osigactp = new(struct sigaction);
98 sigset_t *oblockedp = new(sigset_t);
99 if (sigprocmask(0, nullptr, oblockedp))
100 ohshite(_("failed to get old signal mask"));
101 if (sigaction(SIGWINCH, nullptr, osigactp))
102 ohshite(_("failed to get old SIGWINCH sigact"));
103
104 push_cleanup(cu_sigwinch, ~0, 2, osigactp, oblockedp);
105
106 sigwinch_mask(SIG_BLOCK);
107
108 struct sigaction nsigact;
109 memset(&nsigact,0,sizeof(nsigact));
110 nsigact.sa_handler= sigwinchhandler;
111 sigemptyset(&nsigact.sa_mask);
112 //nsigact.sa_flags= SA_INTERRUPT;
113 if (sigaction(SIGWINCH, &nsigact, nullptr))
114 ohshite(_("failed to set new SIGWINCH sigact"));
115 }
116
117 void
add_column(column & col,const char * title,int width)118 baselist::add_column(column &col, const char *title, int width)
119 {
120 col.title = title;
121 col.x = col_cur_x;
122 col.width = width;
123
124 col_cur_x += col.width + gap_width;
125 }
126
127 void
end_column(column & col,const char * title)128 baselist::end_column(column &col, const char *title)
129 {
130 col.title = title;
131 col.x = col_cur_x;
132 col.width = total_width - col.x;
133
134 col_cur_x += col.width + gap_width;
135 }
136
137 void
draw_column_head(column & col)138 baselist::draw_column_head(column &col)
139 {
140 mvwaddnstr(colheadspad, 0, col.x, col.title, col.width);
141 }
142
143 void
draw_column_sep(column & col,int y)144 baselist::draw_column_sep(column &col, int y)
145 {
146 mvwaddch(listpad, y, col.x - 1, ' ');
147 }
148
149 void
draw_column_item(column & col,int y,const char * item)150 baselist::draw_column_item(column &col, int y, const char *item)
151 {
152 mvwprintw(listpad, y, col.x, "%-*.*s", col.width, col.width, item);
153 }
154
setheights()155 void baselist::setheights() {
156 int y= ymax - (title_height + colheads_height + thisstate_height);
157
158 if (y < 1)
159 internerr("widget y=%d < 1", y);
160
161 if (showinfo==2 && y>=7) {
162 list_height= 5;
163 whatinfo_height= 1;
164 info_height= y-6;
165 } else if (showinfo==1 && y>=10) {
166 list_height= y/2;
167 info_height= (y-1)/2;
168 whatinfo_height= 1;
169 } else {
170 list_height= y;
171 info_height= 0;
172 whatinfo_height= 0;
173 }
174 colheads_row= title_height;
175 list_row= colheads_row + colheads_height;
176 thisstate_row= list_row + list_height;
177 info_row= thisstate_row + thisstate_height;
178 whatinfo_row= ymax - 1;
179 }
180
startdisplay()181 void baselist::startdisplay() {
182 debug(dbg_general, "baselist[%p]::startdisplay()", this);
183 cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
184 clear(); wnoutrefresh(stdscr);
185
186 // find attributes
187 if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
188 int i;
189 printf("allocing\n");
190 for (i = 1; i < numscreenparts; i++) {
191 if (init_pair(i, color[i].fore, color[i].back) != OK)
192 ohshite(_("failed to allocate colour pair"));
193 part_attr[i] = COLOR_PAIR(i) | color[i].attr;
194 }
195 } else {
196 /* User defined attributes for B&W mode are not currently supported. */
197 part_attr[title] = A_REVERSE;
198 part_attr[thisstate] = A_STANDOUT;
199 part_attr[list] = 0;
200 part_attr[listsel] = A_STANDOUT;
201 part_attr[selstate] = A_BOLD;
202 part_attr[selstatesel] = A_STANDOUT;
203 part_attr[colheads]= A_BOLD;
204 part_attr[query] = part_attr[title];
205 part_attr[info_body] = part_attr[list];
206 part_attr[info_head] = A_BOLD;
207 part_attr[whatinfo] = part_attr[thisstate];
208 part_attr[helpscreen] = A_NORMAL;
209 }
210
211 // set up windows and pads, based on screen size
212 getmaxyx(stdscr,ymax,xmax);
213 title_height= ymax>=6;
214 colheads_height= ymax>=5;
215 thisstate_height= ymax>=3;
216
217 setheights();
218 setwidths();
219
220 titlewin= newwin(1,xmax, 0,0);
221 if (!titlewin) ohshite(_("failed to create title window"));
222 wattrset(titlewin, part_attr[title]);
223
224 whatinfowin= newwin(1,xmax, whatinfo_row,0);
225 if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
226 wattrset(whatinfowin, part_attr[whatinfo]);
227
228 listpad = newpad(ymax, total_width);
229 if (!listpad) ohshite(_("failed to create baselist pad"));
230
231 colheadspad= newpad(1, total_width);
232 if (!colheadspad) ohshite(_("failed to create heading pad"));
233 wattrset(colheadspad, part_attr[colheads]);
234
235 thisstatepad= newpad(1, total_width);
236 if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
237 wattrset(thisstatepad, part_attr[thisstate]);
238
239 infopad= newpad(MAX_DISPLAY_INFO, total_width);
240 if (!infopad) ohshite(_("failed to create info pad"));
241 wattrset(infopad, part_attr[info_body]);
242 wbkgdset(infopad, ' ' | part_attr[info_body]);
243
244 querywin= newwin(1,xmax,ymax-1,0);
245 if (!querywin) ohshite(_("failed to create query window"));
246 wbkgdset(querywin, ' ' | part_attr[query]);
247
248 if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
249 if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
250 if (topofscreen < 0) topofscreen= 0;
251
252 infotopofscreen= 0; leftofscreen= 0;
253
254 redrawall();
255
256 debug(dbg_general,
257 "baselist::startdisplay() done ...\n\n"
258 " xmax=%d, ymax=%d;\n\n"
259 " title_height=%d, colheads_height=%d, list_height=%d;\n"
260 " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
261 " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
262 " whatinfo_row=%d, list_row=%d;\n\n",
263 xmax, ymax, title_height, colheads_height, list_height,
264 thisstate_height, info_height, whatinfo_height,
265 colheads_row, thisstate_row, info_row, whatinfo_row, list_row);
266 }
267
enddisplay()268 void baselist::enddisplay() {
269 delwin(titlewin);
270 delwin(whatinfowin);
271 delwin(listpad);
272 delwin(colheadspad);
273 delwin(thisstatepad);
274 delwin(infopad);
275 wmove(stdscr,ymax,0); wclrtoeol(stdscr);
276 listpad = nullptr;
277 col_cur_x = 0;
278 }
279
redrawall()280 void baselist::redrawall() {
281 redrawtitle();
282 redrawcolheads();
283 wattrset(listpad, part_attr[list]);
284 mywerase(listpad);
285 ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
286 refreshlist();
287 redrawthisstate();
288 redrawinfo();
289 }
290
redraw1item(int index)291 void baselist::redraw1item(int index) {
292 redraw1itemsel(index, index == cursorline);
293 }
294
baselist(keybindings * kb)295 baselist::baselist(keybindings *kb) {
296 debug(dbg_general, "baselist[%p]::baselist()", this);
297
298 bindings= kb;
299 nitems= 0;
300
301 col_cur_x = 0;
302 gap_width = 1;
303 total_width = max(TOTAL_LIST_WIDTH, COLS);
304
305 xmax= -1;
306 ymax = -1;
307
308 list_height = 0;
309 info_height = 0;
310 title_height = 0;
311 whatinfo_height = 0;
312 colheads_height = 0;
313 thisstate_height = 0;
314
315 list_row = 0;
316 info_row = 0;
317 whatinfo_row = 0;
318 colheads_row = 0;
319 thisstate_row = 0;
320
321 topofscreen = 0;
322 leftofscreen = 0;
323 infotopofscreen = 0;
324 infolines = 0;
325
326 listpad = nullptr;
327 infopad = nullptr;
328 colheadspad = nullptr;
329 thisstatepad = nullptr;
330 titlewin = nullptr;
331 querywin = nullptr;
332 whatinfowin = nullptr;
333
334 cursorline = -1;
335 ldrawnstart = 0;
336 ldrawnend = 0;
337 showinfo= 1;
338
339 searchstring[0]= 0;
340 }
341
itd_keys()342 void baselist::itd_keys() {
343 whatinfovb(_("Keybindings"));
344
345 const int givek= xmax/3;
346 bindings->describestart();
347 const char **ta;
348 while ((ta = bindings->describenext()) != nullptr) {
349 const char **tap= ta+1;
350 for (;;) {
351 waddstr(infopad, gettext(*tap));
352 tap++; if (!*tap) break;
353 waddstr(infopad, ", ");
354 }
355 int y,x;
356 getyx(infopad,y,x);
357 if (x >= givek) y++;
358 mvwaddstr(infopad, y,givek, ta[0]);
359 waddch(infopad,'\n');
360 delete [] ta;
361 }
362 }
363
dosearch()364 void baselist::dosearch() {
365 int offset, index;
366 debug(dbg_general, "baselist[%p]::dosearch(); searchstring='%s'",
367 this, searchstring);
368 for (offset = 1, index = max(topofscreen, cursorline + 1);
369 offset<nitems;
370 offset++, index++) {
371 if (index >= nitems) index -= nitems;
372 if (matchsearch(index)) {
373 topofscreen= index-1;
374 if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
375 if (topofscreen < 0) topofscreen= 0;
376 setcursor(index);
377 return;
378 }
379 }
380 beep();
381 }
382
refreshinfo()383 void baselist::refreshinfo() {
384 pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
385 min(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
386 min(total_width - leftofscreen - 1, xmax - 1));
387
388 if (whatinfo_height) {
389 mywerase(whatinfowin);
390 mvwaddstr(whatinfowin,0,0, whatinfovb.string());
391 if (infolines > info_height) {
392 wprintw(whatinfowin,_(" -- %d%%, press "),
393 (int)((infotopofscreen + info_height) * 100.0 / infolines));
394 if (infotopofscreen + info_height < infolines) {
395 wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
396 if (infotopofscreen) waddstr(whatinfowin, ", ");
397 }
398 if (infotopofscreen)
399 wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
400 waddch(whatinfowin,'.');
401 }
402 wnoutrefresh(whatinfowin);
403 }
404 }
405
wordwrapinfo(int offset,const char * m)406 void baselist::wordwrapinfo(int offset, const char *m) {
407 int usemax= xmax-5;
408 debug(dbg_general, "baselist[%p]::wordwrapinfo(%d, '%s')", this, offset, m);
409 bool wrapping = false;
410
411 for (;;) {
412 int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
413 const char *p = strchrnul(m, '\n');
414 int l = (int)(p - m);
415 while (l && c_isspace(m[l - 1]))
416 l--;
417 if (!l || (*m == '.' && l == 1)) {
418 if (wrapping) waddch(infopad,'\n');
419 waddch(infopad, '\n');
420 wrapping = false;
421 } else if (*m == ' ' || usemax < 10) {
422 if (wrapping) waddch(infopad,'\n');
423 waddnstr(infopad, m, l);
424 waddch(infopad, '\n');
425 wrapping = false;
426 } else {
427 int x, y DPKG_ATTR_UNUSED;
428
429 if (wrapping) {
430 getyx(infopad, y,x);
431 if (x+1 >= usemax) {
432 waddch(infopad,'\n');
433 } else {
434 waddch(infopad,' ');
435 }
436 }
437 for (;;) {
438 getyx(infopad, y,x);
439 int dosend= usemax-x;
440 if (l <= dosend) {
441 dosend=l;
442 } else {
443 int i=dosend;
444 while (i > 0 && m[i] != ' ') i--;
445 if (i > 0 || x > 0) dosend=i;
446 }
447 if (dosend) waddnstr(infopad, m, dosend);
448 while (dosend < l && m[dosend] == ' ') dosend++;
449 l-= dosend; m+= dosend;
450 if (l <= 0) break;
451 waddch(infopad,'\n');
452 }
453 wrapping = true;
454 }
455 if (*p == '\0')
456 break;
457 if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
458 waddstr(infopad,
459 "[The package description is too long and has been truncated...]");
460 break;
461 }
462 m= ++p;
463 }
464 debug(dbg_general, "baselist[%p]::wordwrapinfo() done", this);
465 }
466
~baselist()467 baselist::~baselist() { }
468