xref: /freebsd/contrib/dialog/tailbox.c (revision 87c1627502a5dde91e5284118eec8682b60f27a2)
1 /*
2  *  $Id: tailbox.c,v 1.63 2011/06/27 08:19:43 tom Exp $
3  *
4  *  tailbox.c -- implements the tail box
5  *
6  *  Copyright 2000-2010,2011	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 + 1];
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     /* Clear 'residue' of previous line */
100     for (i = 0; i < width - x; i++)
101 	(void) waddch(win, ' ');
102 }
103 
104 /*
105  * Go back 'target' lines in text file.  BUFSIZ has to be in 'size_t' range.
106  */
107 static void
108 last_lines(MY_OBJ * obj, int target)
109 {
110     FILE *fp = obj->obj.input;
111     size_t inx;
112     int count = 0;
113     char buf[BUFSIZ + 1];
114     size_t size_to_read;
115     size_t size_as_read;
116     long offset = 0;
117     long fpos = 0;
118 
119     if (fseek(fp, 0L, SEEK_END) == -1
120 	|| (fpos = ftell(fp)) < 0)
121 	dlg_exiterr("Error moving file pointer in last_lines().");
122 
123     if (fpos != 0) {
124 	++target;
125 	for (;;) {
126 	    if (fpos >= BUFSIZ) {
127 		size_to_read = BUFSIZ;
128 	    } else {
129 		size_to_read = (size_t) fpos;
130 	    }
131 	    fpos = fpos - (long) size_to_read;
132 	    if (fseek(fp, fpos, SEEK_SET) == -1)
133 		dlg_exiterr("Error moving file pointer in last_lines().");
134 	    size_as_read = fread(buf, sizeof(char), size_to_read, fp);
135 	    if (ferror(fp))
136 		dlg_exiterr("Error reading file in last_lines().");
137 
138 	    if (size_as_read == 0) {
139 		fpos = 0;
140 		offset = 0;
141 		break;
142 	    }
143 
144 	    offset += (long) size_as_read;
145 	    for (inx = size_as_read - 1; inx != 0; --inx) {
146 		if (buf[inx] == '\n') {
147 		    if (++count > target)
148 			break;
149 		    offset = (long) (inx + 1);
150 		}
151 	    }
152 
153 	    if (count > target) {
154 		break;
155 	    } else if (fpos == 0) {
156 		offset = 0;
157 		break;
158 	    }
159 	}
160 
161 	if (fseek(fp, fpos + offset, SEEK_SET) == -1)
162 	    dlg_exiterr("Error moving file pointer in last_lines().");
163     }
164 }
165 
166 /*
167  * Print a new page of text.
168  */
169 static void
170 print_page(MY_OBJ * obj, int height, int width)
171 {
172     int i;
173 
174     for (i = 0; i < height; i++) {
175 	print_line(obj, obj->text, i, width);
176     }
177     (void) wnoutrefresh(obj->text);
178 }
179 
180 static void
181 print_last_page(MY_OBJ * obj)
182 {
183     int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
184     int wide = getmaxx(obj->text);
185 
186     last_lines(obj, high);
187     print_page(obj, high, wide);
188 }
189 
190 static void
191 repaint_text(MY_OBJ * obj)
192 {
193     FILE *fp = obj->obj.input;
194     int cur_y, cur_x;
195 
196     getyx(obj->obj.win, cur_y, cur_x);
197     obj->old_hscroll = obj->hscroll;
198 
199     print_last_page(obj);
200     obj->last_pos = ftell(fp);
201 
202     (void) wmove(obj->obj.win, cur_y, cur_x);	/* Restore cursor position */
203     wrefresh(obj->obj.win);
204 }
205 
206 static bool
207 handle_input(DIALOG_CALLBACK * cb)
208 {
209     MY_OBJ *obj = (MY_OBJ *) cb;
210     FILE *fp = obj->obj.input;
211     struct stat sb;
212 
213     if (fstat(fileno(fp), &sb) == 0
214 	&& sb.st_size != obj->last_pos) {
215 	repaint_text(obj);
216     }
217 
218     return TRUE;
219 }
220 
221 static bool
222 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
223 {
224     MY_OBJ *obj = (MY_OBJ *) cb;
225     bool done = FALSE;
226 
227     if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
228 	ch = DLGK_ENTER;
229 	fkey = TRUE;
230     }
231 
232     if (fkey) {
233 	switch (ch) {
234 	case DLGK_ENTER:
235 	    *result = DLG_EXIT_OK;
236 	    done = TRUE;
237 	    break;
238 	case DLGK_BEGIN:	/* Beginning of line */
239 	    obj->hscroll = 0;
240 	    break;
241 	case DLGK_GRID_LEFT:	/* Scroll left */
242 	    if (obj->hscroll > 0) {
243 		obj->hscroll -= 1;
244 	    }
245 	    break;
246 	case DLGK_GRID_RIGHT:	/* Scroll right */
247 	    if (obj->hscroll < MAX_LEN)
248 		obj->hscroll += 1;
249 	    break;
250 	default:
251 	    beep();
252 	    break;
253 	}
254 	if ((obj->hscroll != obj->old_hscroll))
255 	    repaint_text(obj);
256     } else {
257 	switch (ch) {
258 	case ERR:
259 	    clearerr(cb->input);
260 	    ch = getc(cb->input);
261 	    (void) ungetc(ch, cb->input);
262 	    if (ch != EOF) {
263 		handle_input(cb);
264 	    }
265 	    break;
266 	case ESC:
267 	    done = TRUE;
268 	    *result = DLG_EXIT_ESC;
269 	    break;
270 	default:
271 	    beep();
272 	    break;
273 	}
274     }
275 
276     return !done;
277 }
278 
279 /*
280  * Display text from a file in a dialog box, like in a "tail -f".
281  */
282 int
283 dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task)
284 {
285     /* *INDENT-OFF* */
286     static DLG_KEYS_BINDING binding[] = {
287 	HELPKEY_BINDINGS,
288 	ENTERKEY_BINDINGS,
289 	DLG_KEYS_DATA( DLGK_BEGIN,      '0' ),
290 	DLG_KEYS_DATA( DLGK_BEGIN,      KEY_BEG ),
291 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
292 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
293 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
294 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
295 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
296 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
297 	END_KEYS_BINDING
298     };
299     /* *INDENT-ON* */
300 
301 #ifdef KEY_RESIZE
302     int old_height = height;
303     int old_width = width;
304 #endif
305     int fkey;
306     int x, y, result, thigh;
307     WINDOW *dialog, *text;
308     const char **buttons = 0;
309     MY_OBJ *obj;
310     FILE *fd;
311     int min_width = 12;
312 
313     /* Open input file for reading */
314     if ((fd = fopen(file, "rb")) == NULL)
315 	dlg_exiterr("Can't open input file in dialog_tailbox().");
316 
317 #ifdef KEY_RESIZE
318   retry:
319 #endif
320     dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
321     dlg_print_size(height, width);
322     dlg_ctl_size(height, width);
323 
324     x = dlg_box_x_ordinate(width);
325     y = dlg_box_y_ordinate(height);
326     thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
327 
328     dialog = dlg_new_window(height, width, y, x);
329 
330     dlg_mouse_setbase(x, y);
331 
332     /* Create window for text region, used for scrolling text */
333     text = dlg_sub_window(dialog,
334 			  thigh,
335 			  width - (2 * MARGIN),
336 			  y + MARGIN,
337 			  x + MARGIN);
338 
339     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
340     dlg_draw_bottom_box(dialog);
341     dlg_draw_title(dialog, title);
342     dlg_draw_helpline(dialog, FALSE);
343 
344     if (!bg_task) {
345 	buttons = dlg_exit_label();
346 	dlg_button_layout(buttons, &min_width);
347 	dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
348 			 FALSE, width);
349     }
350 
351     (void) wmove(dialog, thigh, (MARGIN + 1));
352     (void) wnoutrefresh(dialog);
353 
354     obj = dlg_calloc(MY_OBJ, 1);
355     assert_ptr(obj, "dialog_tailbox");
356 
357     obj->obj.input = fd;
358     obj->obj.win = dialog;
359     obj->obj.handle_getc = handle_my_getc;
360     obj->obj.handle_input = bg_task ? handle_input : 0;
361     obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
362     obj->obj.bg_task = bg_task;
363     obj->text = text;
364     obj->buttons = buttons;
365     dlg_add_callback(&(obj->obj));
366 
367     dlg_register_window(dialog, "tailbox", binding);
368     dlg_register_buttons(dialog, "tailbox", buttons);
369 
370     /* Print last page of text */
371     dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
372     repaint_text(obj);
373 
374     if (bg_task) {
375 	result = DLG_EXIT_OK;
376     } else {
377 	int ch;
378 	do {
379 	    ch = dlg_getc(dialog, &fkey);
380 #ifdef KEY_RESIZE
381 	    if (fkey && ch == KEY_RESIZE) {
382 		/* reset data */
383 		height = old_height;
384 		width = old_width;
385 		/* repaint */
386 		dlg_clear();
387 		dlg_del_window(dialog);
388 		refresh();
389 		dlg_mouse_free_regions();
390 		dlg_button_layout(buttons, &min_width);
391 		goto retry;
392 	    }
393 #endif
394 	}
395 	while (handle_my_getc(&(obj->obj), ch, fkey, &result));
396     }
397     dlg_mouse_free_regions();
398     return result;
399 }
400