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