xref: /freebsd/contrib/dialog/tailbox.c (revision 6ae1554a5d9b318f8ad53ccc39fa5a961403da73)
1 /*
2  *  $Id: tailbox.c,v 1.68 2012/11/18 15:48:52 tom Exp $
3  *
4  *  tailbox.c -- implements the tail box
5  *
6  *  Copyright 2000-2011,2012	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  *
23  *  An earlier version of this program lists as authors
24  *	Pasquale De Marco (demarco_p@abramo.it)
25  */
26 
27 #include <dialog.h>
28 #include <dlg_keys.h>
29 #include <sys/stat.h>
30 
31 typedef struct {
32     DIALOG_CALLBACK obj;
33     WINDOW *text;
34     const char **buttons;
35     int hscroll;
36     int old_hscroll;
37     char line[MAX_LEN + 2];
38     off_t last_pos;
39 } MY_OBJ;
40 
41 /*
42  * Return current line of text.
43  */
44 static char *
45 get_line(MY_OBJ * obj)
46 {
47     FILE *fp = obj->obj.input;
48     int col = -(obj->hscroll);
49     int j, tmpint, ch;
50 
51     do {
52 	if (((ch = getc(fp)) == EOF) && !feof(fp))
53 	    dlg_exiterr("Error moving file pointer in get_line().");
54 	else if (!feof(fp) && (ch != '\n')) {
55 	    if ((ch == TAB) && (dialog_vars.tab_correct)) {
56 		tmpint = dialog_state.tab_len
57 		    - ((col + obj->hscroll) % dialog_state.tab_len);
58 		for (j = 0; j < tmpint; j++) {
59 		    if (col >= 0 && col < MAX_LEN)
60 			obj->line[col] = ' ';
61 		    ++col;
62 		}
63 	    } else {
64 		if (col >= 0)
65 		    obj->line[col] = (char) ch;
66 		++col;
67 	    }
68 	    if (col >= MAX_LEN)
69 		break;
70 	}
71     } while (!feof(fp) && (ch != '\n'));
72 
73     if (col < 0)
74 	col = 0;
75     obj->line[col] = '\0';
76 
77     return obj->line;
78 }
79 
80 /*
81  * Print a new line of text.
82  */
83 static void
84 print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
85 {
86     int i, y, x;
87     char *line = get_line(obj);
88 
89     (void) wmove(win, row, 0);	/* move cursor to correct line */
90     (void) waddch(win, ' ');
91 #ifdef NCURSES_VERSION
92     (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
93 #else
94     line[MIN((int) strlen(line), width - 2)] = '\0';
95     waddstr(win, line);
96 #endif
97 
98     getyx(win, y, x);
99     (void) y;
100     /* Clear 'residue' of previous line */
101     for (i = 0; i < width - x; i++)
102 	(void) waddch(win, ' ');
103 }
104 
105 /*
106  * Go back 'target' lines in text file.  BUFSIZ has to be in 'size_t' range.
107  */
108 static void
109 last_lines(MY_OBJ * obj, int target)
110 {
111     FILE *fp = obj->obj.input;
112     size_t inx;
113     int count = 0;
114     char buf[BUFSIZ + 1];
115     size_t size_to_read;
116     size_t size_as_read;
117     long offset = 0;
118     long fpos = 0;
119 
120     if (fseek(fp, 0L, SEEK_END) == -1
121 	|| (fpos = ftell(fp)) < 0)
122 	dlg_exiterr("Error moving file pointer in last_lines().");
123 
124     if (fpos != 0) {
125 	++target;
126 	for (;;) {
127 	    if (fpos >= BUFSIZ) {
128 		size_to_read = BUFSIZ;
129 	    } else {
130 		size_to_read = (size_t) fpos;
131 	    }
132 	    fpos = fpos - (long) size_to_read;
133 	    if (fseek(fp, fpos, SEEK_SET) == -1)
134 		dlg_exiterr("Error moving file pointer in last_lines().");
135 	    size_as_read = fread(buf, sizeof(char), size_to_read, fp);
136 	    if (ferror(fp))
137 		dlg_exiterr("Error reading file in last_lines().");
138 
139 	    if (size_as_read == 0) {
140 		fpos = 0;
141 		offset = 0;
142 		break;
143 	    }
144 
145 	    offset += (long) size_as_read;
146 	    for (inx = size_as_read - 1; inx != 0; --inx) {
147 		if (buf[inx] == '\n') {
148 		    if (++count > target)
149 			break;
150 		    offset = (long) (inx + 1);
151 		}
152 	    }
153 
154 	    if (count > target) {
155 		break;
156 	    } else if (fpos == 0) {
157 		offset = 0;
158 		break;
159 	    }
160 	}
161 
162 	if (fseek(fp, fpos + offset, SEEK_SET) == -1)
163 	    dlg_exiterr("Error moving file pointer in last_lines().");
164     }
165 }
166 
167 /*
168  * Print a new page of text.
169  */
170 static void
171 print_page(MY_OBJ * obj, int height, int width)
172 {
173     int i;
174 
175     for (i = 0; i < height; i++) {
176 	print_line(obj, obj->text, i, width);
177     }
178     (void) wnoutrefresh(obj->text);
179 }
180 
181 static void
182 print_last_page(MY_OBJ * obj)
183 {
184     int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
185     int wide = getmaxx(obj->text);
186 
187     last_lines(obj, high);
188     print_page(obj, high, wide);
189 }
190 
191 static void
192 repaint_text(MY_OBJ * obj)
193 {
194     FILE *fp = obj->obj.input;
195     int cur_y, cur_x;
196 
197     getyx(obj->obj.win, cur_y, cur_x);
198     obj->old_hscroll = obj->hscroll;
199 
200     print_last_page(obj);
201     obj->last_pos = ftell(fp);
202 
203     (void) wmove(obj->obj.win, cur_y, cur_x);	/* Restore cursor position */
204     wrefresh(obj->obj.win);
205 }
206 
207 static bool
208 handle_input(DIALOG_CALLBACK * cb)
209 {
210     MY_OBJ *obj = (MY_OBJ *) cb;
211     FILE *fp = obj->obj.input;
212     struct stat sb;
213 
214     if (fstat(fileno(fp), &sb) == 0
215 	&& sb.st_size != obj->last_pos) {
216 	repaint_text(obj);
217     }
218 
219     return TRUE;
220 }
221 
222 static bool
223 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
224 {
225     MY_OBJ *obj = (MY_OBJ *) cb;
226     bool done = FALSE;
227 
228     if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
229 	ch = DLGK_ENTER;
230 	fkey = TRUE;
231     }
232 
233     if (fkey) {
234 	switch (ch) {
235 	case DLGK_ENTER:
236 	    *result = DLG_EXIT_OK;
237 	    done = TRUE;
238 	    break;
239 	case DLGK_BEGIN:	/* Beginning of line */
240 	    obj->hscroll = 0;
241 	    break;
242 	case DLGK_GRID_LEFT:	/* Scroll left */
243 	    if (obj->hscroll > 0) {
244 		obj->hscroll -= 1;
245 	    }
246 	    break;
247 	case DLGK_GRID_RIGHT:	/* Scroll right */
248 	    if (obj->hscroll < MAX_LEN)
249 		obj->hscroll += 1;
250 	    break;
251 	default:
252 	    beep();
253 	    break;
254 	}
255 	if ((obj->hscroll != obj->old_hscroll))
256 	    repaint_text(obj);
257     } else {
258 	switch (ch) {
259 	case ERR:
260 	    clearerr(cb->input);
261 	    ch = getc(cb->input);
262 	    (void) ungetc(ch, cb->input);
263 	    if (ch != EOF) {
264 		handle_input(cb);
265 	    }
266 	    break;
267 	case ESC:
268 	    done = TRUE;
269 	    *result = DLG_EXIT_ESC;
270 	    break;
271 	default:
272 	    beep();
273 	    break;
274 	}
275     }
276 
277     return !done;
278 }
279 
280 /*
281  * Display text from a file in a dialog box, like in a "tail -f".
282  */
283 int
284 dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task)
285 {
286     /* *INDENT-OFF* */
287     static DLG_KEYS_BINDING binding[] = {
288 	HELPKEY_BINDINGS,
289 	ENTERKEY_BINDINGS,
290 	DLG_KEYS_DATA( DLGK_BEGIN,      '0' ),
291 	DLG_KEYS_DATA( DLGK_BEGIN,      KEY_BEG ),
292 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
293 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
294 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
295 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
296 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
297 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
298 	END_KEYS_BINDING
299     };
300     /* *INDENT-ON* */
301 
302 #ifdef KEY_RESIZE
303     int old_height = height;
304     int old_width = width;
305 #endif
306     int fkey;
307     int x, y, result, thigh;
308     WINDOW *dialog, *text;
309     const char **buttons = 0;
310     MY_OBJ *obj;
311     FILE *fd;
312     int min_width = 12;
313 
314     /* Open input file for reading */
315     if ((fd = fopen(file, "rb")) == NULL)
316 	dlg_exiterr("Can't open input file in dialog_tailbox().");
317 
318 #ifdef KEY_RESIZE
319   retry:
320 #endif
321     dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
322     dlg_print_size(height, width);
323     dlg_ctl_size(height, width);
324 
325     x = dlg_box_x_ordinate(width);
326     y = dlg_box_y_ordinate(height);
327     thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
328 
329     dialog = dlg_new_window(height, width, y, x);
330 
331     dlg_mouse_setbase(x, y);
332 
333     /* Create window for text region, used for scrolling text */
334     text = dlg_sub_window(dialog,
335 			  thigh,
336 			  width - (2 * MARGIN),
337 			  y + MARGIN,
338 			  x + MARGIN);
339 
340     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
341     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
342     dlg_draw_title(dialog, title);
343     dlg_draw_helpline(dialog, FALSE);
344 
345     if (!bg_task) {
346 	buttons = dlg_exit_label();
347 	dlg_button_layout(buttons, &min_width);
348 	dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
349 			 FALSE, width);
350     }
351 
352     (void) wmove(dialog, thigh, (MARGIN + 1));
353     (void) wnoutrefresh(dialog);
354 
355     obj = dlg_calloc(MY_OBJ, 1);
356     assert_ptr(obj, "dialog_tailbox");
357 
358     obj->obj.input = fd;
359     obj->obj.win = dialog;
360     obj->obj.handle_getc = handle_my_getc;
361     obj->obj.handle_input = bg_task ? handle_input : 0;
362     obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
363     obj->obj.bg_task = bg_task;
364     obj->text = text;
365     obj->buttons = buttons;
366     dlg_add_callback(&(obj->obj));
367 
368     dlg_register_window(dialog, "tailbox", binding);
369     dlg_register_buttons(dialog, "tailbox", buttons);
370 
371     /* Print last page of text */
372     dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
373     repaint_text(obj);
374 
375     dlg_trace_win(dialog);
376     if (bg_task) {
377 	result = DLG_EXIT_OK;
378     } else {
379 	int ch;
380 	do {
381 	    ch = dlg_getc(dialog, &fkey);
382 #ifdef KEY_RESIZE
383 	    if (fkey && ch == KEY_RESIZE) {
384 		/* reset data */
385 		height = old_height;
386 		width = old_width;
387 		/* repaint */
388 		dlg_clear();
389 		dlg_del_window(dialog);
390 		refresh();
391 		dlg_mouse_free_regions();
392 		dlg_button_layout(buttons, &min_width);
393 		goto retry;
394 	    }
395 #endif
396 	}
397 	while (handle_my_getc(&(obj->obj), ch, fkey, &result));
398     }
399     dlg_mouse_free_regions();
400     return result;
401 }
402