xref: /freebsd/contrib/dialog/tailbox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: tailbox.c,v 1.79 2020/11/21 21:31:49 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  tailbox.c -- implements the tail box
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2000-2019,2020	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  An earlier version of this program lists as authors
244c8945a0SNathan Whitehorn  *	Pasquale De Marco (demarco_p@abramo.it)
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
297a1c0d96SNathan Whitehorn #include <sys/stat.h>
304c8945a0SNathan Whitehorn 
314c8945a0SNathan Whitehorn typedef struct {
324c8945a0SNathan Whitehorn     DIALOG_CALLBACK obj;
334c8945a0SNathan Whitehorn     WINDOW *text;
344c8945a0SNathan Whitehorn     const char **buttons;
354c8945a0SNathan Whitehorn     int hscroll;
364c8945a0SNathan Whitehorn     int old_hscroll;
372a3e3873SBaptiste Daroussin     char line[MAX_LEN + 2];
387a1c0d96SNathan Whitehorn     off_t last_pos;
394c8945a0SNathan Whitehorn } MY_OBJ;
404c8945a0SNathan Whitehorn 
414c8945a0SNathan Whitehorn /*
424c8945a0SNathan Whitehorn  * Return current line of text.
434c8945a0SNathan Whitehorn  */
444c8945a0SNathan Whitehorn static char *
get_line(MY_OBJ * obj)454c8945a0SNathan Whitehorn get_line(MY_OBJ * obj)
464c8945a0SNathan Whitehorn {
474c8945a0SNathan Whitehorn     FILE *fp = obj->obj.input;
484c8945a0SNathan Whitehorn     int col = -(obj->hscroll);
494c8945a0SNathan Whitehorn     int j, tmpint, ch;
504c8945a0SNathan Whitehorn 
514c8945a0SNathan Whitehorn     do {
524c8945a0SNathan Whitehorn 	if (((ch = getc(fp)) == EOF) && !feof(fp))
534c8945a0SNathan Whitehorn 	    dlg_exiterr("Error moving file pointer in get_line().");
544c8945a0SNathan Whitehorn 	else if (!feof(fp) && (ch != '\n')) {
554c8945a0SNathan Whitehorn 	    if ((ch == TAB) && (dialog_vars.tab_correct)) {
564c8945a0SNathan Whitehorn 		tmpint = dialog_state.tab_len
574c8945a0SNathan Whitehorn 		    - ((col + obj->hscroll) % dialog_state.tab_len);
584c8945a0SNathan Whitehorn 		for (j = 0; j < tmpint; j++) {
594c8945a0SNathan Whitehorn 		    if (col >= 0 && col < MAX_LEN)
604c8945a0SNathan Whitehorn 			obj->line[col] = ' ';
614c8945a0SNathan Whitehorn 		    ++col;
624c8945a0SNathan Whitehorn 		}
634c8945a0SNathan Whitehorn 	    } else {
644c8945a0SNathan Whitehorn 		if (col >= 0)
654c8945a0SNathan Whitehorn 		    obj->line[col] = (char) ch;
664c8945a0SNathan Whitehorn 		++col;
674c8945a0SNathan Whitehorn 	    }
684c8945a0SNathan Whitehorn 	    if (col >= MAX_LEN)
694c8945a0SNathan Whitehorn 		break;
704c8945a0SNathan Whitehorn 	}
714c8945a0SNathan Whitehorn     } while (!feof(fp) && (ch != '\n'));
724c8945a0SNathan Whitehorn 
734c8945a0SNathan Whitehorn     if (col < 0)
744c8945a0SNathan Whitehorn 	col = 0;
754c8945a0SNathan Whitehorn     obj->line[col] = '\0';
764c8945a0SNathan Whitehorn 
774c8945a0SNathan Whitehorn     return obj->line;
784c8945a0SNathan Whitehorn }
794c8945a0SNathan Whitehorn 
804c8945a0SNathan Whitehorn /*
814c8945a0SNathan Whitehorn  * Print a new line of text.
824c8945a0SNathan Whitehorn  */
834c8945a0SNathan Whitehorn static void
print_line(MY_OBJ * obj,WINDOW * win,int row,int width)844c8945a0SNathan Whitehorn print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
854c8945a0SNathan Whitehorn {
864c8945a0SNathan Whitehorn     int i, y, x;
874c8945a0SNathan Whitehorn     char *line = get_line(obj);
884c8945a0SNathan Whitehorn 
894c8945a0SNathan Whitehorn     (void) wmove(win, row, 0);	/* move cursor to correct line */
904c8945a0SNathan Whitehorn     (void) waddch(win, ' ');
914c8945a0SNathan Whitehorn     (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
924c8945a0SNathan Whitehorn 
934c8945a0SNathan Whitehorn     getyx(win, y, x);
942a3e3873SBaptiste Daroussin     (void) y;
954c8945a0SNathan Whitehorn     /* Clear 'residue' of previous line */
964c8945a0SNathan Whitehorn     for (i = 0; i < width - x; i++)
974c8945a0SNathan Whitehorn 	(void) waddch(win, ' ');
984c8945a0SNathan Whitehorn }
994c8945a0SNathan Whitehorn 
1004c8945a0SNathan Whitehorn /*
1014c8945a0SNathan Whitehorn  * Go back 'target' lines in text file.  BUFSIZ has to be in 'size_t' range.
1024c8945a0SNathan Whitehorn  */
1034c8945a0SNathan Whitehorn static void
last_lines(MY_OBJ * obj,int target)1044c8945a0SNathan Whitehorn last_lines(MY_OBJ * obj, int target)
1054c8945a0SNathan Whitehorn {
1064c8945a0SNathan Whitehorn     FILE *fp = obj->obj.input;
1074c8945a0SNathan Whitehorn     long fpos = 0;
1084c8945a0SNathan Whitehorn 
1097a1c0d96SNathan Whitehorn     if (fseek(fp, 0L, SEEK_END) == -1
1104c8945a0SNathan Whitehorn 	|| (fpos = ftell(fp)) < 0)
1114c8945a0SNathan Whitehorn 	dlg_exiterr("Error moving file pointer in last_lines().");
1124c8945a0SNathan Whitehorn 
1134c8945a0SNathan Whitehorn     if (fpos != 0) {
114*a96ef450SBaptiste Daroussin 	long offset = 0;
115*a96ef450SBaptiste Daroussin 
1164c8945a0SNathan Whitehorn 	++target;
1174c8945a0SNathan Whitehorn 	for (;;) {
118*a96ef450SBaptiste Daroussin 	    size_t inx;
119*a96ef450SBaptiste Daroussin 	    size_t size_to_read;
120*a96ef450SBaptiste Daroussin 	    size_t size_as_read;
121*a96ef450SBaptiste Daroussin 	    int count = 0;
122*a96ef450SBaptiste Daroussin 	    char buf[BUFSIZ + 1];
123*a96ef450SBaptiste Daroussin 
1244c8945a0SNathan Whitehorn 	    if (fpos >= BUFSIZ) {
1254c8945a0SNathan Whitehorn 		size_to_read = BUFSIZ;
1264c8945a0SNathan Whitehorn 	    } else {
1277a1c0d96SNathan Whitehorn 		size_to_read = (size_t) fpos;
1284c8945a0SNathan Whitehorn 	    }
1297a1c0d96SNathan Whitehorn 	    fpos = fpos - (long) size_to_read;
1304c8945a0SNathan Whitehorn 	    if (fseek(fp, fpos, SEEK_SET) == -1)
1314c8945a0SNathan Whitehorn 		dlg_exiterr("Error moving file pointer in last_lines().");
1327a1c0d96SNathan Whitehorn 	    size_as_read = fread(buf, sizeof(char), size_to_read, fp);
1334c8945a0SNathan Whitehorn 	    if (ferror(fp))
1344c8945a0SNathan Whitehorn 		dlg_exiterr("Error reading file in last_lines().");
1354c8945a0SNathan Whitehorn 
1367a1c0d96SNathan Whitehorn 	    if (size_as_read == 0) {
1377a1c0d96SNathan Whitehorn 		fpos = 0;
1387a1c0d96SNathan Whitehorn 		offset = 0;
1397a1c0d96SNathan Whitehorn 		break;
1407a1c0d96SNathan Whitehorn 	    }
1417a1c0d96SNathan Whitehorn 
1427a1c0d96SNathan Whitehorn 	    offset += (long) size_as_read;
1437a1c0d96SNathan Whitehorn 	    for (inx = size_as_read - 1; inx != 0; --inx) {
1444c8945a0SNathan Whitehorn 		if (buf[inx] == '\n') {
1454c8945a0SNathan Whitehorn 		    if (++count > target)
1464c8945a0SNathan Whitehorn 			break;
1477a1c0d96SNathan Whitehorn 		    offset = (long) (inx + 1);
1484c8945a0SNathan Whitehorn 		}
1494c8945a0SNathan Whitehorn 	    }
1504c8945a0SNathan Whitehorn 
1514c8945a0SNathan Whitehorn 	    if (count > target) {
1524c8945a0SNathan Whitehorn 		break;
1534c8945a0SNathan Whitehorn 	    } else if (fpos == 0) {
1544c8945a0SNathan Whitehorn 		offset = 0;
1554c8945a0SNathan Whitehorn 		break;
1564c8945a0SNathan Whitehorn 	    }
1574c8945a0SNathan Whitehorn 	}
1584c8945a0SNathan Whitehorn 
1594c8945a0SNathan Whitehorn 	if (fseek(fp, fpos + offset, SEEK_SET) == -1)
1604c8945a0SNathan Whitehorn 	    dlg_exiterr("Error moving file pointer in last_lines().");
1614c8945a0SNathan Whitehorn     }
1624c8945a0SNathan Whitehorn }
1634c8945a0SNathan Whitehorn 
1644c8945a0SNathan Whitehorn /*
1654c8945a0SNathan Whitehorn  * Print a new page of text.
1664c8945a0SNathan Whitehorn  */
1674c8945a0SNathan Whitehorn static void
print_page(MY_OBJ * obj,int height,int width)1684c8945a0SNathan Whitehorn print_page(MY_OBJ * obj, int height, int width)
1694c8945a0SNathan Whitehorn {
1704c8945a0SNathan Whitehorn     int i;
1714c8945a0SNathan Whitehorn 
1724c8945a0SNathan Whitehorn     for (i = 0; i < height; i++) {
1734c8945a0SNathan Whitehorn 	print_line(obj, obj->text, i, width);
1744c8945a0SNathan Whitehorn     }
1754c8945a0SNathan Whitehorn     (void) wnoutrefresh(obj->text);
1764c8945a0SNathan Whitehorn }
1774c8945a0SNathan Whitehorn 
1784c8945a0SNathan Whitehorn static void
print_last_page(MY_OBJ * obj)1794c8945a0SNathan Whitehorn print_last_page(MY_OBJ * obj)
1804c8945a0SNathan Whitehorn {
1814c8945a0SNathan Whitehorn     int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
1824c8945a0SNathan Whitehorn     int wide = getmaxx(obj->text);
1834c8945a0SNathan Whitehorn 
1844c8945a0SNathan Whitehorn     last_lines(obj, high);
1854c8945a0SNathan Whitehorn     print_page(obj, high, wide);
1864c8945a0SNathan Whitehorn }
1874c8945a0SNathan Whitehorn 
1884c8945a0SNathan Whitehorn static void
repaint_text(MY_OBJ * obj)1894c8945a0SNathan Whitehorn repaint_text(MY_OBJ * obj)
1904c8945a0SNathan Whitehorn {
1917a1c0d96SNathan Whitehorn     FILE *fp = obj->obj.input;
1924c8945a0SNathan Whitehorn     int cur_y, cur_x;
1934c8945a0SNathan Whitehorn 
1944c8945a0SNathan Whitehorn     getyx(obj->obj.win, cur_y, cur_x);
1954c8945a0SNathan Whitehorn     obj->old_hscroll = obj->hscroll;
1967a1c0d96SNathan Whitehorn 
1974c8945a0SNathan Whitehorn     print_last_page(obj);
1987a1c0d96SNathan Whitehorn     obj->last_pos = ftell(fp);
1997a1c0d96SNathan Whitehorn 
2004c8945a0SNathan Whitehorn     (void) wmove(obj->obj.win, cur_y, cur_x);	/* Restore cursor position */
2014c8945a0SNathan Whitehorn     wrefresh(obj->obj.win);
2024c8945a0SNathan Whitehorn }
2034c8945a0SNathan Whitehorn 
2044c8945a0SNathan Whitehorn static bool
handle_input(DIALOG_CALLBACK * cb)2057a1c0d96SNathan Whitehorn handle_input(DIALOG_CALLBACK * cb)
2067a1c0d96SNathan Whitehorn {
2077a1c0d96SNathan Whitehorn     MY_OBJ *obj = (MY_OBJ *) cb;
2087a1c0d96SNathan Whitehorn     FILE *fp = obj->obj.input;
2097a1c0d96SNathan Whitehorn     struct stat sb;
2107a1c0d96SNathan Whitehorn 
2117a1c0d96SNathan Whitehorn     if (fstat(fileno(fp), &sb) == 0
2127a1c0d96SNathan Whitehorn 	&& sb.st_size != obj->last_pos) {
2137a1c0d96SNathan Whitehorn 	repaint_text(obj);
2147a1c0d96SNathan Whitehorn     }
2157a1c0d96SNathan Whitehorn 
2167a1c0d96SNathan Whitehorn     return TRUE;
2177a1c0d96SNathan Whitehorn }
2187a1c0d96SNathan Whitehorn 
2197a1c0d96SNathan Whitehorn static bool
valid_callback(DIALOG_CALLBACK * cb)220f4f33ea0SBaptiste Daroussin valid_callback(DIALOG_CALLBACK * cb)
221f4f33ea0SBaptiste Daroussin {
222f4f33ea0SBaptiste Daroussin     bool valid = FALSE;
223f4f33ea0SBaptiste Daroussin     DIALOG_CALLBACK *p;
224f4f33ea0SBaptiste Daroussin     for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
225f4f33ea0SBaptiste Daroussin 	if (p == cb) {
226f4f33ea0SBaptiste Daroussin 	    valid = TRUE;
227f4f33ea0SBaptiste Daroussin 	    break;
228f4f33ea0SBaptiste Daroussin 	}
229f4f33ea0SBaptiste Daroussin     }
230f4f33ea0SBaptiste Daroussin     return valid;
231f4f33ea0SBaptiste Daroussin }
232f4f33ea0SBaptiste Daroussin 
233f4f33ea0SBaptiste Daroussin static bool
handle_my_getc(DIALOG_CALLBACK * cb,int ch,int fkey,int * result)2344c8945a0SNathan Whitehorn handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
2354c8945a0SNathan Whitehorn {
2364c8945a0SNathan Whitehorn     MY_OBJ *obj = (MY_OBJ *) cb;
2374c8945a0SNathan Whitehorn     bool done = FALSE;
2384c8945a0SNathan Whitehorn 
239f4f33ea0SBaptiste Daroussin     if (!valid_callback(cb))
240f4f33ea0SBaptiste Daroussin 	return FALSE;
241f4f33ea0SBaptiste Daroussin 
2424c8945a0SNathan Whitehorn     if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
2434c8945a0SNathan Whitehorn 	ch = DLGK_ENTER;
2444c8945a0SNathan Whitehorn 	fkey = TRUE;
2454c8945a0SNathan Whitehorn     }
2464c8945a0SNathan Whitehorn 
2474c8945a0SNathan Whitehorn     if (fkey) {
2484c8945a0SNathan Whitehorn 	switch (ch) {
2494c8945a0SNathan Whitehorn 	case DLGK_ENTER:
2504c8945a0SNathan Whitehorn 	    *result = DLG_EXIT_OK;
2514c8945a0SNathan Whitehorn 	    done = TRUE;
2524c8945a0SNathan Whitehorn 	    break;
2534c8945a0SNathan Whitehorn 	case DLGK_BEGIN:	/* Beginning of line */
2544c8945a0SNathan Whitehorn 	    obj->hscroll = 0;
2554c8945a0SNathan Whitehorn 	    break;
2564c8945a0SNathan Whitehorn 	case DLGK_GRID_LEFT:	/* Scroll left */
2574c8945a0SNathan Whitehorn 	    if (obj->hscroll > 0) {
2584c8945a0SNathan Whitehorn 		obj->hscroll -= 1;
2594c8945a0SNathan Whitehorn 	    }
2604c8945a0SNathan Whitehorn 	    break;
2614c8945a0SNathan Whitehorn 	case DLGK_GRID_RIGHT:	/* Scroll right */
2624c8945a0SNathan Whitehorn 	    if (obj->hscroll < MAX_LEN)
2634c8945a0SNathan Whitehorn 		obj->hscroll += 1;
2644c8945a0SNathan Whitehorn 	    break;
2654c8945a0SNathan Whitehorn 	default:
266*a96ef450SBaptiste Daroussin 	    if (is_DLGK_MOUSE(ch)) {
267*a96ef450SBaptiste Daroussin 		*result = dlg_ok_buttoncode(ch - M_EVENT);
268*a96ef450SBaptiste Daroussin 		if (*result != DLG_EXIT_ERROR) {
269*a96ef450SBaptiste Daroussin 		    done = TRUE;
270*a96ef450SBaptiste Daroussin 		} else {
2714c8945a0SNathan Whitehorn 		    beep();
272*a96ef450SBaptiste Daroussin 		}
273*a96ef450SBaptiste Daroussin 	    } else {
274*a96ef450SBaptiste Daroussin 		beep();
275*a96ef450SBaptiste Daroussin 	    }
2764c8945a0SNathan Whitehorn 	    break;
2774c8945a0SNathan Whitehorn 	}
2784c8945a0SNathan Whitehorn 	if ((obj->hscroll != obj->old_hscroll))
2794c8945a0SNathan Whitehorn 	    repaint_text(obj);
2804c8945a0SNathan Whitehorn     } else {
2814c8945a0SNathan Whitehorn 	switch (ch) {
2824c8945a0SNathan Whitehorn 	case ERR:
2834c8945a0SNathan Whitehorn 	    clearerr(cb->input);
2844c8945a0SNathan Whitehorn 	    ch = getc(cb->input);
2854c8945a0SNathan Whitehorn 	    (void) ungetc(ch, cb->input);
2867a1c0d96SNathan Whitehorn 	    if (ch != EOF) {
2877a1c0d96SNathan Whitehorn 		handle_input(cb);
2884c8945a0SNathan Whitehorn 	    }
2894c8945a0SNathan Whitehorn 	    break;
2904c8945a0SNathan Whitehorn 	case ESC:
2914c8945a0SNathan Whitehorn 	    done = TRUE;
2924c8945a0SNathan Whitehorn 	    *result = DLG_EXIT_ESC;
2934c8945a0SNathan Whitehorn 	    break;
2944c8945a0SNathan Whitehorn 	default:
2954c8945a0SNathan Whitehorn 	    beep();
2964c8945a0SNathan Whitehorn 	    break;
2974c8945a0SNathan Whitehorn 	}
2984c8945a0SNathan Whitehorn     }
2994c8945a0SNathan Whitehorn 
3004c8945a0SNathan Whitehorn     return !done;
3014c8945a0SNathan Whitehorn }
3024c8945a0SNathan Whitehorn 
3034c8945a0SNathan Whitehorn /*
3044c8945a0SNathan Whitehorn  * Display text from a file in a dialog box, like in a "tail -f".
3054c8945a0SNathan Whitehorn  */
3064c8945a0SNathan Whitehorn int
dialog_tailbox(const char * title,const char * filename,int height,int width,int bg_task)307f4f33ea0SBaptiste Daroussin dialog_tailbox(const char *title,
308f4f33ea0SBaptiste Daroussin 	       const char *filename,
309f4f33ea0SBaptiste Daroussin 	       int height,
310f4f33ea0SBaptiste Daroussin 	       int width,
311f4f33ea0SBaptiste Daroussin 	       int bg_task)
3124c8945a0SNathan Whitehorn {
3134c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
3144c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
315682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
3164c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
3174c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_BEGIN,      '0' ),
3184c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_BEGIN,      KEY_BEG ),
3194c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
3204c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
3214c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
3224c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
3234c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
3244c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
3254c8945a0SNathan Whitehorn 	END_KEYS_BINDING
3264c8945a0SNathan Whitehorn     };
3274c8945a0SNathan Whitehorn     /* *INDENT-ON* */
3284c8945a0SNathan Whitehorn 
3294c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3304c8945a0SNathan Whitehorn     int old_height = height;
3314c8945a0SNathan Whitehorn     int old_width = width;
3324c8945a0SNathan Whitehorn #endif
3334c8945a0SNathan Whitehorn     int fkey;
334*a96ef450SBaptiste Daroussin     int x, y, result = DLG_EXIT_UNKNOWN, thigh;
3354c8945a0SNathan Whitehorn     WINDOW *dialog, *text;
3364c8945a0SNathan Whitehorn     const char **buttons = 0;
3374c8945a0SNathan Whitehorn     MY_OBJ *obj;
3384c8945a0SNathan Whitehorn     FILE *fd;
3394c8945a0SNathan Whitehorn     int min_width = 12;
3404c8945a0SNathan Whitehorn 
341f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# tailbox args:\n"));
342f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
343f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("filename", filename);
344f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
345f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
346f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("bg_task", bg_task);
347f4f33ea0SBaptiste Daroussin 
3484c8945a0SNathan Whitehorn     /* Open input file for reading */
349f4f33ea0SBaptiste Daroussin     if ((fd = fopen(filename, "rb")) == NULL)
3504c8945a0SNathan Whitehorn 	dlg_exiterr("Can't open input file in dialog_tailbox().");
3514c8945a0SNathan Whitehorn 
3524c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3534c8945a0SNathan Whitehorn   retry:
3544c8945a0SNathan Whitehorn #endif
355f4f33ea0SBaptiste Daroussin     dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
3564c8945a0SNathan Whitehorn     dlg_print_size(height, width);
3574c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
3584c8945a0SNathan Whitehorn 
3594c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
3604c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
3614c8945a0SNathan Whitehorn     thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
3624c8945a0SNathan Whitehorn 
3634c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
3644c8945a0SNathan Whitehorn 
3654c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
3664c8945a0SNathan Whitehorn 
3674c8945a0SNathan Whitehorn     /* Create window for text region, used for scrolling text */
3684c8945a0SNathan Whitehorn     text = dlg_sub_window(dialog,
3694c8945a0SNathan Whitehorn 			  thigh,
3704c8945a0SNathan Whitehorn 			  width - (2 * MARGIN),
3714c8945a0SNathan Whitehorn 			  y + MARGIN,
3724c8945a0SNathan Whitehorn 			  x + MARGIN);
3734c8945a0SNathan Whitehorn 
3742a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
3752a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
3764c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
377682c9e0fSNathan Whitehorn     dlg_draw_helpline(dialog, FALSE);
3784c8945a0SNathan Whitehorn 
3794c8945a0SNathan Whitehorn     if (!bg_task) {
3804c8945a0SNathan Whitehorn 	buttons = dlg_exit_label();
3814c8945a0SNathan Whitehorn 	dlg_button_layout(buttons, &min_width);
3824c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
3834c8945a0SNathan Whitehorn 			 FALSE, width);
3844c8945a0SNathan Whitehorn     }
3854c8945a0SNathan Whitehorn 
3864c8945a0SNathan Whitehorn     (void) wmove(dialog, thigh, (MARGIN + 1));
3874c8945a0SNathan Whitehorn     (void) wnoutrefresh(dialog);
3884c8945a0SNathan Whitehorn 
3894c8945a0SNathan Whitehorn     obj = dlg_calloc(MY_OBJ, 1);
3904c8945a0SNathan Whitehorn     assert_ptr(obj, "dialog_tailbox");
3914c8945a0SNathan Whitehorn 
3924c8945a0SNathan Whitehorn     obj->obj.input = fd;
3934c8945a0SNathan Whitehorn     obj->obj.win = dialog;
3944c8945a0SNathan Whitehorn     obj->obj.handle_getc = handle_my_getc;
3957a1c0d96SNathan Whitehorn     obj->obj.handle_input = bg_task ? handle_input : 0;
3964c8945a0SNathan Whitehorn     obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
397*a96ef450SBaptiste Daroussin     obj->obj.bg_task = (bool) bg_task;
3984c8945a0SNathan Whitehorn     obj->text = text;
3994c8945a0SNathan Whitehorn     obj->buttons = buttons;
4004c8945a0SNathan Whitehorn     dlg_add_callback(&(obj->obj));
4014c8945a0SNathan Whitehorn 
4024c8945a0SNathan Whitehorn     dlg_register_window(dialog, "tailbox", binding);
4034c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "tailbox", buttons);
4044c8945a0SNathan Whitehorn 
4054c8945a0SNathan Whitehorn     /* Print last page of text */
4064c8945a0SNathan Whitehorn     dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
4074c8945a0SNathan Whitehorn     repaint_text(obj);
4084c8945a0SNathan Whitehorn 
4092a3e3873SBaptiste Daroussin     dlg_trace_win(dialog);
4104c8945a0SNathan Whitehorn     if (bg_task) {
4114c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
4124c8945a0SNathan Whitehorn     } else {
4134c8945a0SNathan Whitehorn 	int ch;
4144c8945a0SNathan Whitehorn 	do {
415*a96ef450SBaptiste Daroussin 	    ch = dlg_mouse_wgetch(dialog, &fkey);
4164c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
4174c8945a0SNathan Whitehorn 	    if (fkey && ch == KEY_RESIZE) {
418f4f33ea0SBaptiste Daroussin 		dlg_will_resize(dialog);
4194c8945a0SNathan Whitehorn 		/* reset data */
4204c8945a0SNathan Whitehorn 		height = old_height;
4214c8945a0SNathan Whitehorn 		width = old_width;
4224c8945a0SNathan Whitehorn 		/* repaint */
423*a96ef450SBaptiste Daroussin 		_dlg_resize_cleanup(dialog);
4244c8945a0SNathan Whitehorn 		dlg_button_layout(buttons, &min_width);
4254c8945a0SNathan Whitehorn 		goto retry;
4264c8945a0SNathan Whitehorn 	    }
4274c8945a0SNathan Whitehorn #endif
4284c8945a0SNathan Whitehorn 	}
4294c8945a0SNathan Whitehorn 	while (handle_my_getc(&(obj->obj), ch, fkey, &result));
4304c8945a0SNathan Whitehorn     }
4314c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
4324c8945a0SNathan Whitehorn     return result;
4334c8945a0SNathan Whitehorn }
434