xref: /freebsd/contrib/dialog/textbox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: textbox.c,v 1.127 2021/01/17 22:19:19 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  textbox.c -- implements the text box
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2000-2020,2021	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  *	Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn 
304c8945a0SNathan Whitehorn #define PAGE_LENGTH	(height - 4)
314c8945a0SNathan Whitehorn #define PAGE_WIDTH	(width - 2)
324c8945a0SNathan Whitehorn 
334c8945a0SNathan Whitehorn typedef struct {
344c8945a0SNathan Whitehorn     DIALOG_CALLBACK obj;
354c8945a0SNathan Whitehorn     WINDOW *text;
364c8945a0SNathan Whitehorn     const char **buttons;
374c8945a0SNathan Whitehorn     int hscroll;
384c8945a0SNathan Whitehorn     char line[MAX_LEN + 1];
394c8945a0SNathan Whitehorn     int fd;
404c8945a0SNathan Whitehorn     long file_size;
414c8945a0SNathan Whitehorn     long fd_bytes_read;
424c8945a0SNathan Whitehorn     long bytes_read;
434c8945a0SNathan Whitehorn     long buffer_len;
444c8945a0SNathan Whitehorn     bool begin_reached;
454c8945a0SNathan Whitehorn     bool buffer_first;
464c8945a0SNathan Whitehorn     bool end_reached;
474c8945a0SNathan Whitehorn     long page_length;		/* lines on the page which is shown */
484c8945a0SNathan Whitehorn     long in_buf;		/* ending index into buf[] for page */
494c8945a0SNathan Whitehorn     char *buf;
504c8945a0SNathan Whitehorn } MY_OBJ;
514c8945a0SNathan Whitehorn 
524c8945a0SNathan Whitehorn static long
lseek_obj(MY_OBJ * obj,long offset,int mode)534c8945a0SNathan Whitehorn lseek_obj(MY_OBJ * obj, long offset, int mode)
544c8945a0SNathan Whitehorn {
554c8945a0SNathan Whitehorn     long fpos;
564c8945a0SNathan Whitehorn     if ((fpos = (long) lseek(obj->fd, (off_t) offset, mode)) == -1) {
574c8945a0SNathan Whitehorn 	switch (mode) {
582a3e3873SBaptiste Daroussin 	default:
594c8945a0SNathan Whitehorn 	case SEEK_CUR:
604c8945a0SNathan Whitehorn 	    dlg_exiterr("Cannot get file position");
614c8945a0SNathan Whitehorn 	    break;
624c8945a0SNathan Whitehorn 	case SEEK_END:
634c8945a0SNathan Whitehorn 	    dlg_exiterr("Cannot seek to end of file");
644c8945a0SNathan Whitehorn 	    break;
654c8945a0SNathan Whitehorn 	case SEEK_SET:
664c8945a0SNathan Whitehorn 	    dlg_exiterr("Cannot set file position to %ld", offset);
674c8945a0SNathan Whitehorn 	    break;
684c8945a0SNathan Whitehorn 	}
694c8945a0SNathan Whitehorn     }
704c8945a0SNathan Whitehorn     return fpos;
714c8945a0SNathan Whitehorn }
724c8945a0SNathan Whitehorn 
734c8945a0SNathan Whitehorn static long
ftell_obj(MY_OBJ * obj)744c8945a0SNathan Whitehorn ftell_obj(MY_OBJ * obj)
754c8945a0SNathan Whitehorn {
767a1c0d96SNathan Whitehorn     return lseek_obj(obj, 0L, SEEK_CUR);
774c8945a0SNathan Whitehorn }
784c8945a0SNathan Whitehorn 
792a3e3873SBaptiste Daroussin static void
lseek_set(MY_OBJ * obj,long offset)802a3e3873SBaptiste Daroussin lseek_set(MY_OBJ * obj, long offset)
812a3e3873SBaptiste Daroussin {
822a3e3873SBaptiste Daroussin     long actual = lseek_obj(obj, offset, SEEK_SET);
832a3e3873SBaptiste Daroussin 
842a3e3873SBaptiste Daroussin     if (actual != offset) {
852a3e3873SBaptiste Daroussin 	dlg_exiterr("Cannot set file position to %ld (actual %ld)\n",
862a3e3873SBaptiste Daroussin 		    offset, actual);
872a3e3873SBaptiste Daroussin     }
882a3e3873SBaptiste Daroussin }
892a3e3873SBaptiste Daroussin 
902a3e3873SBaptiste Daroussin static void
lseek_end(MY_OBJ * obj,long offset)912a3e3873SBaptiste Daroussin lseek_end(MY_OBJ * obj, long offset)
922a3e3873SBaptiste Daroussin {
932a3e3873SBaptiste Daroussin     long actual = lseek_obj(obj, offset, SEEK_END);
942a3e3873SBaptiste Daroussin 
95f4f33ea0SBaptiste Daroussin     if (offset == 0L && actual > offset) {
962a3e3873SBaptiste Daroussin 	obj->file_size = actual;
972a3e3873SBaptiste Daroussin     }
982a3e3873SBaptiste Daroussin }
992a3e3873SBaptiste Daroussin 
1002a3e3873SBaptiste Daroussin static void
lseek_cur(MY_OBJ * obj,long offset)1012a3e3873SBaptiste Daroussin lseek_cur(MY_OBJ * obj, long offset)
1022a3e3873SBaptiste Daroussin {
1032a3e3873SBaptiste Daroussin     long actual = lseek_obj(obj, offset, SEEK_CUR);
1042a3e3873SBaptiste Daroussin 
1052a3e3873SBaptiste Daroussin     if (actual != offset) {
106f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# Lseek returned %ld, expected %ld\n", actual, offset));
1072a3e3873SBaptiste Daroussin     }
1082a3e3873SBaptiste Daroussin }
1092a3e3873SBaptiste Daroussin 
1104c8945a0SNathan Whitehorn static char *
xalloc(size_t size)1114c8945a0SNathan Whitehorn xalloc(size_t size)
1124c8945a0SNathan Whitehorn {
1134c8945a0SNathan Whitehorn     char *result = dlg_malloc(char, size);
1144c8945a0SNathan Whitehorn     assert_ptr(result, "xalloc");
1154c8945a0SNathan Whitehorn     return result;
1164c8945a0SNathan Whitehorn }
1174c8945a0SNathan Whitehorn 
1184c8945a0SNathan Whitehorn /*
1194c8945a0SNathan Whitehorn  * read_high() substitutes read() for tab->spaces conversion
1204c8945a0SNathan Whitehorn  *
1214c8945a0SNathan Whitehorn  * buffer_len, fd_bytes_read, bytes_read are modified
1224c8945a0SNathan Whitehorn  * buf is allocated
1234c8945a0SNathan Whitehorn  *
1244c8945a0SNathan Whitehorn  * fd_bytes_read is the effective number of bytes read from file
1254c8945a0SNathan Whitehorn  * bytes_read is the length of buf, that can be different if tab_correct
1264c8945a0SNathan Whitehorn  */
1274c8945a0SNathan Whitehorn static void
read_high(MY_OBJ * obj,size_t size_read)1284c8945a0SNathan Whitehorn read_high(MY_OBJ * obj, size_t size_read)
1294c8945a0SNathan Whitehorn {
130*a96ef450SBaptiste Daroussin     char *buftab;
1314c8945a0SNathan Whitehorn 
1324c8945a0SNathan Whitehorn     /* Allocate space for read buffer */
1334c8945a0SNathan Whitehorn     buftab = xalloc(size_read + 1);
1344c8945a0SNathan Whitehorn 
1354c8945a0SNathan Whitehorn     if ((obj->fd_bytes_read = read(obj->fd, buftab, size_read)) != -1) {
136*a96ef450SBaptiste Daroussin 	int j;
137*a96ef450SBaptiste Daroussin 	long begin_line;
1384c8945a0SNathan Whitehorn 
1394c8945a0SNathan Whitehorn 	buftab[obj->fd_bytes_read] = '\0';	/* mark end of valid data */
1404c8945a0SNathan Whitehorn 
1414c8945a0SNathan Whitehorn 	if (dialog_vars.tab_correct) {
1424c8945a0SNathan Whitehorn 
1434c8945a0SNathan Whitehorn 	    /* calculate bytes_read by buftab and fd_bytes_read */
1444c8945a0SNathan Whitehorn 	    obj->bytes_read = begin_line = 0;
1454c8945a0SNathan Whitehorn 	    for (j = 0; j < obj->fd_bytes_read; j++)
1464c8945a0SNathan Whitehorn 		if (buftab[j] == TAB)
1474c8945a0SNathan Whitehorn 		    obj->bytes_read += dialog_state.tab_len
1484c8945a0SNathan Whitehorn 			- ((obj->bytes_read - begin_line)
1494c8945a0SNathan Whitehorn 			   % dialog_state.tab_len);
1504c8945a0SNathan Whitehorn 		else if (buftab[j] == '\n') {
1514c8945a0SNathan Whitehorn 		    obj->bytes_read++;
1524c8945a0SNathan Whitehorn 		    begin_line = obj->bytes_read;
1534c8945a0SNathan Whitehorn 		} else
1544c8945a0SNathan Whitehorn 		    obj->bytes_read++;
1554c8945a0SNathan Whitehorn 
1564c8945a0SNathan Whitehorn 	    if (obj->bytes_read > obj->buffer_len) {
1574c8945a0SNathan Whitehorn 		if (obj->buffer_first)
1584c8945a0SNathan Whitehorn 		    obj->buffer_first = FALSE;	/* disp = 0 */
1594c8945a0SNathan Whitehorn 		else {
1604c8945a0SNathan Whitehorn 		    free(obj->buf);
1614c8945a0SNathan Whitehorn 		}
1624c8945a0SNathan Whitehorn 
1634c8945a0SNathan Whitehorn 		obj->buffer_len = obj->bytes_read;
1644c8945a0SNathan Whitehorn 
1654c8945a0SNathan Whitehorn 		/* Allocate space for read buffer */
1664c8945a0SNathan Whitehorn 		obj->buf = xalloc((size_t) obj->buffer_len + 1);
1674c8945a0SNathan Whitehorn 	    }
1684c8945a0SNathan Whitehorn 
1694c8945a0SNathan Whitehorn 	} else {
1704c8945a0SNathan Whitehorn 	    if (obj->buffer_first) {
1714c8945a0SNathan Whitehorn 		obj->buffer_first = FALSE;
1724c8945a0SNathan Whitehorn 
1734c8945a0SNathan Whitehorn 		/* Allocate space for read buffer */
1744c8945a0SNathan Whitehorn 		obj->buf = xalloc(size_read + 1);
1754c8945a0SNathan Whitehorn 	    }
1764c8945a0SNathan Whitehorn 
1774c8945a0SNathan Whitehorn 	    obj->bytes_read = obj->fd_bytes_read;
1784c8945a0SNathan Whitehorn 	}
1794c8945a0SNathan Whitehorn 
1804c8945a0SNathan Whitehorn 	j = 0;
1814c8945a0SNathan Whitehorn 	begin_line = 0;
182*a96ef450SBaptiste Daroussin 	if (obj->buf != NULL) {
183*a96ef450SBaptiste Daroussin 	    int i = 0;
184*a96ef450SBaptiste Daroussin 
185*a96ef450SBaptiste Daroussin 	    while (j < obj->fd_bytes_read) {
186*a96ef450SBaptiste Daroussin 		char ch;
187*a96ef450SBaptiste Daroussin 
188*a96ef450SBaptiste Daroussin 		if (((ch = buftab[j++]) == TAB)
189*a96ef450SBaptiste Daroussin 		    && (dialog_vars.tab_correct != 0)) {
190*a96ef450SBaptiste Daroussin 		    int n;
191*a96ef450SBaptiste Daroussin 		    int tmpint = (dialog_state.tab_len
1924c8945a0SNathan Whitehorn 				  - ((int) ((long) i - begin_line) % dialog_state.tab_len));
1934c8945a0SNathan Whitehorn 		    for (n = 0; n < tmpint; n++)
1944c8945a0SNathan Whitehorn 			obj->buf[i++] = ' ';
1954c8945a0SNathan Whitehorn 		} else {
1964c8945a0SNathan Whitehorn 		    if (ch == '\n')
1974c8945a0SNathan Whitehorn 			begin_line = i + 1;
1984c8945a0SNathan Whitehorn 		    obj->buf[i++] = ch;
1994c8945a0SNathan Whitehorn 		}
200*a96ef450SBaptiste Daroussin 	    }
2014c8945a0SNathan Whitehorn 
2024c8945a0SNathan Whitehorn 	    obj->buf[i] = '\0';	/* mark end of valid data */
203*a96ef450SBaptiste Daroussin 	}
2044c8945a0SNathan Whitehorn 
2054c8945a0SNathan Whitehorn     }
2064c8945a0SNathan Whitehorn     if (obj->bytes_read == -1)
2074c8945a0SNathan Whitehorn 	dlg_exiterr("Error reading file");
2084c8945a0SNathan Whitehorn     free(buftab);
2094c8945a0SNathan Whitehorn }
2104c8945a0SNathan Whitehorn 
2114c8945a0SNathan Whitehorn static long
find_first(MY_OBJ * obj,char * buffer,long length)2124c8945a0SNathan Whitehorn find_first(MY_OBJ * obj, char *buffer, long length)
2134c8945a0SNathan Whitehorn {
2144c8945a0SNathan Whitehorn     long result = 0;
2154c8945a0SNathan Whitehorn 
216*a96ef450SBaptiste Daroussin     if (buffer != NULL) {
217*a96ef450SBaptiste Daroussin 	long recount = obj->page_length;
2184c8945a0SNathan Whitehorn 	while (length > 0) {
2194c8945a0SNathan Whitehorn 	    if (buffer[length] == '\n') {
2204c8945a0SNathan Whitehorn 		if (--recount < 0) {
2214c8945a0SNathan Whitehorn 		    result = length;
2224c8945a0SNathan Whitehorn 		    break;
2234c8945a0SNathan Whitehorn 		}
2244c8945a0SNathan Whitehorn 	    }
2254c8945a0SNathan Whitehorn 	    --length;
2264c8945a0SNathan Whitehorn 	}
227*a96ef450SBaptiste Daroussin     }
2284c8945a0SNathan Whitehorn     return result;
2294c8945a0SNathan Whitehorn }
2304c8945a0SNathan Whitehorn 
2314c8945a0SNathan Whitehorn static long
tabize(MY_OBJ * obj,long val,long * first_pos)2324c8945a0SNathan Whitehorn tabize(MY_OBJ * obj, long val, long *first_pos)
2334c8945a0SNathan Whitehorn {
2344c8945a0SNathan Whitehorn     long fpos;
2354c8945a0SNathan Whitehorn     long i, count, begin_line;
2364c8945a0SNathan Whitehorn     char *buftab;
2374c8945a0SNathan Whitehorn 
2384c8945a0SNathan Whitehorn     if (!dialog_vars.tab_correct)
2394c8945a0SNathan Whitehorn 	return val;
2404c8945a0SNathan Whitehorn 
2414c8945a0SNathan Whitehorn     fpos = ftell_obj(obj);
2424c8945a0SNathan Whitehorn 
2432a3e3873SBaptiste Daroussin     lseek_set(obj, fpos - obj->fd_bytes_read);
2444c8945a0SNathan Whitehorn 
2454c8945a0SNathan Whitehorn     /* Allocate space for read buffer */
2464c8945a0SNathan Whitehorn     buftab = xalloc((size_t) val + 1);
2474c8945a0SNathan Whitehorn 
2484c8945a0SNathan Whitehorn     if ((read(obj->fd, buftab, (size_t) val)) == -1)
2494c8945a0SNathan Whitehorn 	dlg_exiterr("Error reading file in tabize().");
2504c8945a0SNathan Whitehorn 
2514c8945a0SNathan Whitehorn     begin_line = count = 0;
2524c8945a0SNathan Whitehorn     if (first_pos != 0)
2534c8945a0SNathan Whitehorn 	*first_pos = 0;
2544c8945a0SNathan Whitehorn 
2554c8945a0SNathan Whitehorn     for (i = 0; i < val; i++) {
2564c8945a0SNathan Whitehorn 	if ((first_pos != 0) && (count >= val)) {
2574c8945a0SNathan Whitehorn 	    *first_pos = find_first(obj, buftab, i);
2584c8945a0SNathan Whitehorn 	    break;
2594c8945a0SNathan Whitehorn 	}
2604c8945a0SNathan Whitehorn 	if (buftab[i] == TAB)
2614c8945a0SNathan Whitehorn 	    count += dialog_state.tab_len
2624c8945a0SNathan Whitehorn 		- ((count - begin_line) % dialog_state.tab_len);
2634c8945a0SNathan Whitehorn 	else if (buftab[i] == '\n') {
2644c8945a0SNathan Whitehorn 	    count++;
2654c8945a0SNathan Whitehorn 	    begin_line = count;
2664c8945a0SNathan Whitehorn 	} else
2674c8945a0SNathan Whitehorn 	    count++;
2684c8945a0SNathan Whitehorn     }
2694c8945a0SNathan Whitehorn 
2702a3e3873SBaptiste Daroussin     lseek_set(obj, fpos);
2714c8945a0SNathan Whitehorn     free(buftab);
2724c8945a0SNathan Whitehorn     return count;
2734c8945a0SNathan Whitehorn }
2744c8945a0SNathan Whitehorn /*
2754c8945a0SNathan Whitehorn  * Return current line of text.
2764c8945a0SNathan Whitehorn  * 'page' should point to start of current line before calling, and will be
2774c8945a0SNathan Whitehorn  * updated to point to start of next line.
2784c8945a0SNathan Whitehorn  */
2794c8945a0SNathan Whitehorn static char *
get_line(MY_OBJ * obj)2804c8945a0SNathan Whitehorn get_line(MY_OBJ * obj)
2814c8945a0SNathan Whitehorn {
2824c8945a0SNathan Whitehorn     int i = 0;
2834c8945a0SNathan Whitehorn 
2844c8945a0SNathan Whitehorn     obj->end_reached = FALSE;
285*a96ef450SBaptiste Daroussin     if (obj->buf != NULL) {
2864c8945a0SNathan Whitehorn 	while (obj->buf[obj->in_buf] != '\n') {
2874c8945a0SNathan Whitehorn 	    if (obj->buf[obj->in_buf] == '\0') {	/* Either end of file or end of buffer reached */
288*a96ef450SBaptiste Daroussin 		long fpos = ftell_obj(obj);
2894c8945a0SNathan Whitehorn 
2904c8945a0SNathan Whitehorn 		if (fpos < obj->file_size) {	/* Not end of file yet */
2914c8945a0SNathan Whitehorn 		    /* We've reached end of buffer, but not end of file yet, so
2924c8945a0SNathan Whitehorn 		     * read next part of file into buffer
2934c8945a0SNathan Whitehorn 		     */
2944c8945a0SNathan Whitehorn 		    read_high(obj, BUF_SIZE);
2954c8945a0SNathan Whitehorn 		    obj->in_buf = 0;
2964c8945a0SNathan Whitehorn 		} else {
2974c8945a0SNathan Whitehorn 		    if (!obj->end_reached)
2984c8945a0SNathan Whitehorn 			obj->end_reached = TRUE;
2994c8945a0SNathan Whitehorn 		    break;
3004c8945a0SNathan Whitehorn 		}
3014c8945a0SNathan Whitehorn 	    } else if (i < MAX_LEN)
3024c8945a0SNathan Whitehorn 		obj->line[i++] = obj->buf[obj->in_buf++];
3034c8945a0SNathan Whitehorn 	    else {
3044c8945a0SNathan Whitehorn 		if (i == MAX_LEN)	/* Truncate lines longer than MAX_LEN characters */
3054c8945a0SNathan Whitehorn 		    obj->line[i++] = '\0';
3064c8945a0SNathan Whitehorn 		obj->in_buf++;
3074c8945a0SNathan Whitehorn 	    }
3084c8945a0SNathan Whitehorn 	}
309*a96ef450SBaptiste Daroussin     }
3104c8945a0SNathan Whitehorn     if (i <= MAX_LEN)
3114c8945a0SNathan Whitehorn 	obj->line[i] = '\0';
3124c8945a0SNathan Whitehorn     if (!obj->end_reached)
3134c8945a0SNathan Whitehorn 	obj->in_buf++;		/* move past '\n' */
3144c8945a0SNathan Whitehorn 
3154c8945a0SNathan Whitehorn     return obj->line;
3164c8945a0SNathan Whitehorn }
3174c8945a0SNathan Whitehorn 
3184c8945a0SNathan Whitehorn static bool
match_string(MY_OBJ * obj,char * string)3194c8945a0SNathan Whitehorn match_string(MY_OBJ * obj, char *string)
3204c8945a0SNathan Whitehorn {
3214c8945a0SNathan Whitehorn     char *match = get_line(obj);
3224c8945a0SNathan Whitehorn     return strstr(match, string) != 0;
3234c8945a0SNathan Whitehorn }
3244c8945a0SNathan Whitehorn 
3254c8945a0SNathan Whitehorn /*
3264c8945a0SNathan Whitehorn  * Go back 'n' lines in text file. Called by dialog_textbox().
3274c8945a0SNathan Whitehorn  * 'in_buf' will be updated to point to the desired line in 'buf'.
3284c8945a0SNathan Whitehorn  */
3294c8945a0SNathan Whitehorn static void
back_lines(MY_OBJ * obj,long n)3304c8945a0SNathan Whitehorn back_lines(MY_OBJ * obj, long n)
3314c8945a0SNathan Whitehorn {
3324c8945a0SNathan Whitehorn     int i;
3334c8945a0SNathan Whitehorn     long fpos;
3344c8945a0SNathan Whitehorn     long val_to_tabize;
3354c8945a0SNathan Whitehorn 
3364c8945a0SNathan Whitehorn     obj->begin_reached = FALSE;
3374c8945a0SNathan Whitehorn     /* We have to distinguish between end_reached and !end_reached since at end
3384c8945a0SNathan Whitehorn        * of file, the line is not ended by a '\n'.  The code inside 'if'
3394c8945a0SNathan Whitehorn        * basically does a '--in_buf' to move one character backward so as to
3404c8945a0SNathan Whitehorn        * skip '\n' of the previous line */
3414c8945a0SNathan Whitehorn     if (!obj->end_reached) {
3424c8945a0SNathan Whitehorn 	/* Either beginning of buffer or beginning of file reached? */
3434c8945a0SNathan Whitehorn 
3444c8945a0SNathan Whitehorn 	if (obj->in_buf == 0) {
3454c8945a0SNathan Whitehorn 	    fpos = ftell_obj(obj);
3464c8945a0SNathan Whitehorn 
3474c8945a0SNathan Whitehorn 	    if (fpos > obj->fd_bytes_read) {	/* Not beginning of file yet */
3484c8945a0SNathan Whitehorn 		/* We've reached beginning of buffer, but not beginning of file
3494c8945a0SNathan Whitehorn 		 * yet, so read previous part of file into buffer.  Note that
3504c8945a0SNathan Whitehorn 		 * we only move backward for BUF_SIZE/2 bytes, but not BUF_SIZE
3514c8945a0SNathan Whitehorn 		 * bytes to avoid re-reading again in print_page() later
3524c8945a0SNathan Whitehorn 		 */
3534c8945a0SNathan Whitehorn 		/* Really possible to move backward BUF_SIZE/2 bytes? */
3544c8945a0SNathan Whitehorn 		if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
3554c8945a0SNathan Whitehorn 		    /* No, move less than */
3562a3e3873SBaptiste Daroussin 		    lseek_set(obj, 0L);
3574c8945a0SNathan Whitehorn 		    val_to_tabize = fpos - obj->fd_bytes_read;
3584c8945a0SNathan Whitehorn 		} else {	/* Move backward BUF_SIZE/2 bytes */
3592a3e3873SBaptiste Daroussin 		    lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
3604c8945a0SNathan Whitehorn 		    val_to_tabize = BUF_SIZE / 2;
3614c8945a0SNathan Whitehorn 		}
3624c8945a0SNathan Whitehorn 		read_high(obj, BUF_SIZE);
3634c8945a0SNathan Whitehorn 
3644c8945a0SNathan Whitehorn 		obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
3654c8945a0SNathan Whitehorn 
3664c8945a0SNathan Whitehorn 	    } else {		/* Beginning of file reached */
3674c8945a0SNathan Whitehorn 		obj->begin_reached = TRUE;
3684c8945a0SNathan Whitehorn 		return;
3694c8945a0SNathan Whitehorn 	    }
3704c8945a0SNathan Whitehorn 	}
3714c8945a0SNathan Whitehorn 	obj->in_buf--;
372*a96ef450SBaptiste Daroussin 	if (obj->buf == NULL
373*a96ef450SBaptiste Daroussin 	    || obj->in_buf < 0
374*a96ef450SBaptiste Daroussin 	    || obj->in_buf >= obj->bytes_read
375*a96ef450SBaptiste Daroussin 	    || obj->buf[obj->in_buf] != '\n')
3764c8945a0SNathan Whitehorn 	    /* Something's wrong... */
3774c8945a0SNathan Whitehorn 	    dlg_exiterr("Internal error in back_lines().");
3784c8945a0SNathan Whitehorn     }
3794c8945a0SNathan Whitehorn 
3804c8945a0SNathan Whitehorn     /* Go back 'n' lines */
3814c8945a0SNathan Whitehorn     for (i = 0; i < n; i++) {
3824c8945a0SNathan Whitehorn 	do {
3834c8945a0SNathan Whitehorn 	    if (obj->in_buf == 0) {
3844c8945a0SNathan Whitehorn 		fpos = ftell_obj(obj);
3854c8945a0SNathan Whitehorn 
3864c8945a0SNathan Whitehorn 		if (fpos > obj->fd_bytes_read) {
3874c8945a0SNathan Whitehorn 		    /* Really possible to move backward BUF_SIZE/2 bytes? */
3884c8945a0SNathan Whitehorn 		    if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
3894c8945a0SNathan Whitehorn 			/* No, move less than */
3902a3e3873SBaptiste Daroussin 			lseek_set(obj, 0L);
3914c8945a0SNathan Whitehorn 			val_to_tabize = fpos - obj->fd_bytes_read;
3924c8945a0SNathan Whitehorn 		    } else {	/* Move backward BUF_SIZE/2 bytes */
3932a3e3873SBaptiste Daroussin 			lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
3944c8945a0SNathan Whitehorn 			val_to_tabize = BUF_SIZE / 2;
3954c8945a0SNathan Whitehorn 		    }
3964c8945a0SNathan Whitehorn 		    read_high(obj, BUF_SIZE);
3974c8945a0SNathan Whitehorn 
3984c8945a0SNathan Whitehorn 		    obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
3994c8945a0SNathan Whitehorn 
4004c8945a0SNathan Whitehorn 		} else {	/* Beginning of file reached */
4014c8945a0SNathan Whitehorn 		    obj->begin_reached = TRUE;
4024c8945a0SNathan Whitehorn 		    return;
4034c8945a0SNathan Whitehorn 		}
4044c8945a0SNathan Whitehorn 	    }
4054c8945a0SNathan Whitehorn 	} while (obj->buf[--(obj->in_buf)] != '\n');
4064c8945a0SNathan Whitehorn     }
4074c8945a0SNathan Whitehorn     obj->in_buf++;
4084c8945a0SNathan Whitehorn }
4094c8945a0SNathan Whitehorn 
4104c8945a0SNathan Whitehorn /*
4114c8945a0SNathan Whitehorn  * Print a new line of text.
4124c8945a0SNathan Whitehorn  */
4134c8945a0SNathan Whitehorn static void
print_line(MY_OBJ * obj,int row,int width)4144c8945a0SNathan Whitehorn print_line(MY_OBJ * obj, int row, int width)
4154c8945a0SNathan Whitehorn {
4164c8945a0SNathan Whitehorn     if (wmove(obj->text, row, 0) != ERR) {
4174c8945a0SNathan Whitehorn 	int i, y, x;
4184c8945a0SNathan Whitehorn 	char *line = get_line(obj);
4194c8945a0SNathan Whitehorn 	const int *cols = dlg_index_columns(line);
4204c8945a0SNathan Whitehorn 	const int *indx = dlg_index_wchars(line);
4214c8945a0SNathan Whitehorn 	int limit = dlg_count_wchars(line);
4224c8945a0SNathan Whitehorn 	int first = 0;
4234c8945a0SNathan Whitehorn 	int last = limit;
4244c8945a0SNathan Whitehorn 
4254c8945a0SNathan Whitehorn 	if (width > getmaxx(obj->text))
4264c8945a0SNathan Whitehorn 	    width = getmaxx(obj->text);
4274c8945a0SNathan Whitehorn 	--width;		/* for the leading ' ' */
4284c8945a0SNathan Whitehorn 
4294c8945a0SNathan Whitehorn 	for (i = 0; i <= limit && cols[i] < obj->hscroll; ++i)
4304c8945a0SNathan Whitehorn 	    first = i;
4314c8945a0SNathan Whitehorn 
4324c8945a0SNathan Whitehorn 	for (i = first; (i <= limit) && ((cols[i] - cols[first]) < width); ++i)
4334c8945a0SNathan Whitehorn 	    last = i;
4344c8945a0SNathan Whitehorn 
4354c8945a0SNathan Whitehorn 	(void) waddch(obj->text, ' ');
4364c8945a0SNathan Whitehorn 	(void) waddnstr(obj->text, line + indx[first], indx[last] - indx[first]);
4374c8945a0SNathan Whitehorn 
4384c8945a0SNathan Whitehorn 	getyx(obj->text, y, x);
4394c8945a0SNathan Whitehorn 	if (y == row) {		/* Clear 'residue' of previous line */
4404c8945a0SNathan Whitehorn 	    for (i = 0; i <= width - x; i++) {
4414c8945a0SNathan Whitehorn 		(void) waddch(obj->text, ' ');
4424c8945a0SNathan Whitehorn 	    }
4434c8945a0SNathan Whitehorn 	}
4444c8945a0SNathan Whitehorn     }
4454c8945a0SNathan Whitehorn }
4464c8945a0SNathan Whitehorn 
4474c8945a0SNathan Whitehorn /*
4484c8945a0SNathan Whitehorn  * Print a new page of text.
4494c8945a0SNathan Whitehorn  */
4504c8945a0SNathan Whitehorn static void
print_page(MY_OBJ * obj,int height,int width)4514c8945a0SNathan Whitehorn print_page(MY_OBJ * obj, int height, int width)
4524c8945a0SNathan Whitehorn {
4534c8945a0SNathan Whitehorn     int i, passed_end = 0;
4544c8945a0SNathan Whitehorn 
4554c8945a0SNathan Whitehorn     obj->page_length = 0;
4564c8945a0SNathan Whitehorn     for (i = 0; i < height; i++) {
4574c8945a0SNathan Whitehorn 	print_line(obj, i, width);
4584c8945a0SNathan Whitehorn 	if (!passed_end)
4594c8945a0SNathan Whitehorn 	    obj->page_length++;
4604c8945a0SNathan Whitehorn 	if (obj->end_reached && !passed_end)
4614c8945a0SNathan Whitehorn 	    passed_end = 1;
4624c8945a0SNathan Whitehorn     }
4634c8945a0SNathan Whitehorn     (void) wnoutrefresh(obj->text);
4642a3e3873SBaptiste Daroussin     dlg_trace_win(obj->text);
4654c8945a0SNathan Whitehorn }
4664c8945a0SNathan Whitehorn 
4674c8945a0SNathan Whitehorn /*
4684c8945a0SNathan Whitehorn  * Print current position
4694c8945a0SNathan Whitehorn  */
4704c8945a0SNathan Whitehorn static void
print_position(MY_OBJ * obj,WINDOW * win,int height,int width)4714c8945a0SNathan Whitehorn print_position(MY_OBJ * obj, WINDOW *win, int height, int width)
4724c8945a0SNathan Whitehorn {
4734c8945a0SNathan Whitehorn     long fpos;
4744c8945a0SNathan Whitehorn     long size;
4754c8945a0SNathan Whitehorn     long first = -1;
4764c8945a0SNathan Whitehorn 
4774c8945a0SNathan Whitehorn     fpos = ftell_obj(obj);
4784c8945a0SNathan Whitehorn     if (dialog_vars.tab_correct)
4794c8945a0SNathan Whitehorn 	size = tabize(obj, obj->in_buf, &first);
4804c8945a0SNathan Whitehorn     else
4814c8945a0SNathan Whitehorn 	first = find_first(obj, obj->buf, size = obj->in_buf);
4824c8945a0SNathan Whitehorn 
4834c8945a0SNathan Whitehorn     dlg_draw_scrollbar(win,
4844c8945a0SNathan Whitehorn 		       first,
4854c8945a0SNathan Whitehorn 		       fpos - obj->fd_bytes_read + size,
4864c8945a0SNathan Whitehorn 		       fpos - obj->fd_bytes_read + size,
4874c8945a0SNathan Whitehorn 		       obj->file_size,
4884c8945a0SNathan Whitehorn 		       0, PAGE_WIDTH,
4894c8945a0SNathan Whitehorn 		       0, PAGE_LENGTH + 1,
4904c8945a0SNathan Whitehorn 		       border_attr,
4914c8945a0SNathan Whitehorn 		       border_attr);
4924c8945a0SNathan Whitehorn }
4934c8945a0SNathan Whitehorn 
4944c8945a0SNathan Whitehorn /*
4954c8945a0SNathan Whitehorn  * Display a dialog box and get the search term from user.
4964c8945a0SNathan Whitehorn  */
4974c8945a0SNathan Whitehorn static int
get_search_term(WINDOW * dialog,char * input,int height,int width)4984c8945a0SNathan Whitehorn get_search_term(WINDOW *dialog, char *input, int height, int width)
4994c8945a0SNathan Whitehorn {
5004c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
5014c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
5024c8945a0SNathan Whitehorn 	INPUTSTR_BINDINGS,
503682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
5044c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
5054c8945a0SNathan Whitehorn 	END_KEYS_BINDING
5064c8945a0SNathan Whitehorn     };
5074c8945a0SNathan Whitehorn     /* *INDENT-ON* */
5084c8945a0SNathan Whitehorn 
5094c8945a0SNathan Whitehorn     int old_x, old_y;
5104c8945a0SNathan Whitehorn     int box_x, box_y;
5114c8945a0SNathan Whitehorn     int box_height, box_width;
5124c8945a0SNathan Whitehorn     int offset = 0;
5134c8945a0SNathan Whitehorn     int key = 0;
5144c8945a0SNathan Whitehorn     int fkey = 0;
5154c8945a0SNathan Whitehorn     bool first = TRUE;
5164c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
5177a1c0d96SNathan Whitehorn     const char *caption = _("Search");
5184c8945a0SNathan Whitehorn     int len_caption = dlg_count_columns(caption);
5194c8945a0SNathan Whitehorn     const int *indx;
5204c8945a0SNathan Whitehorn     int limit;
5214c8945a0SNathan Whitehorn     WINDOW *widget;
5224c8945a0SNathan Whitehorn 
5234c8945a0SNathan Whitehorn     getbegyx(dialog, old_y, old_x);
5244c8945a0SNathan Whitehorn 
5254c8945a0SNathan Whitehorn     box_height = 1 + (2 * MARGIN);
5264c8945a0SNathan Whitehorn     box_width = len_caption + (2 * (MARGIN + 2));
5274c8945a0SNathan Whitehorn     box_width = MAX(box_width, 30);
5284c8945a0SNathan Whitehorn     box_width = MIN(box_width, getmaxx(dialog) - 2 * MARGIN);
5294c8945a0SNathan Whitehorn     len_caption = MIN(len_caption, box_width - (2 * (MARGIN + 1)));
5304c8945a0SNathan Whitehorn 
5314c8945a0SNathan Whitehorn     box_x = (width - box_width) / 2;
5324c8945a0SNathan Whitehorn     box_y = (height - box_height) / 2;
5334c8945a0SNathan Whitehorn     widget = dlg_new_modal_window(dialog,
5344c8945a0SNathan Whitehorn 				  box_height, box_width,
5354c8945a0SNathan Whitehorn 				  old_y + box_y, old_x + box_x);
5364c8945a0SNathan Whitehorn     keypad(widget, TRUE);
5374c8945a0SNathan Whitehorn     dlg_register_window(widget, "searchbox", binding);
5384c8945a0SNathan Whitehorn 
5392a3e3873SBaptiste Daroussin     dlg_draw_box2(widget, 0, 0, box_height, box_width,
5404c8945a0SNathan Whitehorn 		  searchbox_attr,
5412a3e3873SBaptiste Daroussin 		  searchbox_border_attr,
5422a3e3873SBaptiste Daroussin 		  searchbox_border2_attr);
543f4f33ea0SBaptiste Daroussin     dlg_attrset(widget, searchbox_title_attr);
5444c8945a0SNathan Whitehorn     (void) wmove(widget, 0, (box_width - len_caption) / 2);
5454c8945a0SNathan Whitehorn 
5464c8945a0SNathan Whitehorn     indx = dlg_index_wchars(caption);
5474c8945a0SNathan Whitehorn     limit = dlg_limit_columns(caption, len_caption, 0);
5484c8945a0SNathan Whitehorn     (void) waddnstr(widget, caption + indx[0], indx[limit] - indx[0]);
5494c8945a0SNathan Whitehorn 
5504c8945a0SNathan Whitehorn     box_width -= 2;
5514c8945a0SNathan Whitehorn     offset = dlg_count_columns(input);
5524c8945a0SNathan Whitehorn 
5534c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
5544c8945a0SNathan Whitehorn 	if (!first) {
5554c8945a0SNathan Whitehorn 	    key = dlg_getc(widget, &fkey);
5564c8945a0SNathan Whitehorn 	    if (fkey) {
5574c8945a0SNathan Whitehorn 		switch (fkey) {
5584c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
5594c8945a0SNathan Whitehorn 		case KEY_RESIZE:
5604c8945a0SNathan Whitehorn 		    result = DLG_EXIT_CANCEL;
5614c8945a0SNathan Whitehorn 		    continue;
5624c8945a0SNathan Whitehorn #endif
5634c8945a0SNathan Whitehorn 		case DLGK_ENTER:
5644c8945a0SNathan Whitehorn 		    result = DLG_EXIT_OK;
5654c8945a0SNathan Whitehorn 		    continue;
5664c8945a0SNathan Whitehorn 		}
5674c8945a0SNathan Whitehorn 	    } else if (key == ESC) {
5684c8945a0SNathan Whitehorn 		result = DLG_EXIT_ESC;
5694c8945a0SNathan Whitehorn 		continue;
5704c8945a0SNathan Whitehorn 	    } else if (key == ERR) {
5714c8945a0SNathan Whitehorn 		napms(50);
5724c8945a0SNathan Whitehorn 		continue;
5734c8945a0SNathan Whitehorn 	    }
5744c8945a0SNathan Whitehorn 	}
5754c8945a0SNathan Whitehorn 	if (dlg_edit_string(input, &offset, key, fkey, first)) {
5764c8945a0SNathan Whitehorn 	    dlg_show_string(widget, input, offset, searchbox_attr,
5774c8945a0SNathan Whitehorn 			    1, 1, box_width, FALSE, first);
5784c8945a0SNathan Whitehorn 	    first = FALSE;
5794c8945a0SNathan Whitehorn 	}
5804c8945a0SNathan Whitehorn     }
5814c8945a0SNathan Whitehorn     dlg_del_window(widget);
5824c8945a0SNathan Whitehorn     return result;
5834c8945a0SNathan Whitehorn }
5844c8945a0SNathan Whitehorn 
5854c8945a0SNathan Whitehorn static bool
perform_search(MY_OBJ * obj,int height,int width,int key,char * search_term)5864c8945a0SNathan Whitehorn perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
5874c8945a0SNathan Whitehorn {
5884c8945a0SNathan Whitehorn     int dir;
5894c8945a0SNathan Whitehorn     bool moved = FALSE;
5904c8945a0SNathan Whitehorn 
5914c8945a0SNathan Whitehorn     /* set search direction */
5924c8945a0SNathan Whitehorn     dir = (key == '/' || key == 'n') ? 1 : 0;
5934c8945a0SNathan Whitehorn     if (dir ? !obj->end_reached : !obj->begin_reached) {
594*a96ef450SBaptiste Daroussin 	long tempinx;
595*a96ef450SBaptiste Daroussin 	long fpos;
596*a96ef450SBaptiste Daroussin 	int result;
597*a96ef450SBaptiste Daroussin 	bool found;
598*a96ef450SBaptiste Daroussin 	bool temp, temp1;
599*a96ef450SBaptiste Daroussin 
6004c8945a0SNathan Whitehorn 	if (key == 'n' || key == 'N') {
6014c8945a0SNathan Whitehorn 	    if (search_term[0] == '\0') {	/* No search term yet */
6024c8945a0SNathan Whitehorn 		(void) beep();
6034c8945a0SNathan Whitehorn 		return FALSE;
6044c8945a0SNathan Whitehorn 	    }
6054c8945a0SNathan Whitehorn 	    /* Get search term from user */
6064c8945a0SNathan Whitehorn 	} else if ((result = get_search_term(obj->text, search_term,
6074c8945a0SNathan Whitehorn 					     PAGE_LENGTH,
6084c8945a0SNathan Whitehorn 					     PAGE_WIDTH)) != DLG_EXIT_OK
6094c8945a0SNathan Whitehorn 		   || search_term[0] == '\0') {
6104c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
6114c8945a0SNathan Whitehorn 	    if (result == DLG_EXIT_CANCEL) {
6124c8945a0SNathan Whitehorn 		ungetch(key);
6134c8945a0SNathan Whitehorn 		ungetch(KEY_RESIZE);
6144c8945a0SNathan Whitehorn 		/* FALLTHRU */
6154c8945a0SNathan Whitehorn 	    }
616*a96ef450SBaptiste Daroussin #else
617*a96ef450SBaptiste Daroussin 	    (void) result;
6184c8945a0SNathan Whitehorn #endif
6194c8945a0SNathan Whitehorn 	    /* ESC pressed, or no search term, reprint page to clear box */
620f4f33ea0SBaptiste Daroussin 	    dlg_attrset(obj->text, dialog_attr);
6214c8945a0SNathan Whitehorn 	    back_lines(obj, obj->page_length);
6224c8945a0SNathan Whitehorn 	    return TRUE;
6234c8945a0SNathan Whitehorn 	}
6244c8945a0SNathan Whitehorn 	/* Save variables for restoring in case search term can't be found */
6254c8945a0SNathan Whitehorn 	tempinx = obj->in_buf;
6264c8945a0SNathan Whitehorn 	temp = obj->begin_reached;
6274c8945a0SNathan Whitehorn 	temp1 = obj->end_reached;
6284c8945a0SNathan Whitehorn 	fpos = ftell_obj(obj) - obj->fd_bytes_read;
6294c8945a0SNathan Whitehorn 	/* update 'in_buf' to point to next (previous) line before
6304c8945a0SNathan Whitehorn 	   forward (backward) searching */
6314c8945a0SNathan Whitehorn 	back_lines(obj, (dir
6324c8945a0SNathan Whitehorn 			 ? obj->page_length - 1
6334c8945a0SNathan Whitehorn 			 : obj->page_length + 1));
6344c8945a0SNathan Whitehorn 	if (dir) {		/* Forward search */
6354c8945a0SNathan Whitehorn 	    while ((found = match_string(obj, search_term)) == FALSE) {
6364c8945a0SNathan Whitehorn 		if (obj->end_reached)
6374c8945a0SNathan Whitehorn 		    break;
6384c8945a0SNathan Whitehorn 	    }
6394c8945a0SNathan Whitehorn 	} else {		/* Backward search */
6404c8945a0SNathan Whitehorn 	    while ((found = match_string(obj, search_term)) == FALSE) {
6414c8945a0SNathan Whitehorn 		if (obj->begin_reached)
6424c8945a0SNathan Whitehorn 		    break;
6437a1c0d96SNathan Whitehorn 		back_lines(obj, 2L);
6444c8945a0SNathan Whitehorn 	    }
6454c8945a0SNathan Whitehorn 	}
6464c8945a0SNathan Whitehorn 	if (found == FALSE) {	/* not found */
6474c8945a0SNathan Whitehorn 	    (void) beep();
6484c8945a0SNathan Whitehorn 	    /* Restore program state to that before searching */
6492a3e3873SBaptiste Daroussin 	    lseek_set(obj, fpos);
6504c8945a0SNathan Whitehorn 
6514c8945a0SNathan Whitehorn 	    read_high(obj, BUF_SIZE);
6524c8945a0SNathan Whitehorn 
6534c8945a0SNathan Whitehorn 	    obj->in_buf = tempinx;
6544c8945a0SNathan Whitehorn 	    obj->begin_reached = temp;
6554c8945a0SNathan Whitehorn 	    obj->end_reached = temp1;
6564c8945a0SNathan Whitehorn 	    /* move 'in_buf' to point to start of current page to
6574c8945a0SNathan Whitehorn 	     * re-print current page.  Note that 'in_buf' always points
6584c8945a0SNathan Whitehorn 	     * to start of next page, so this is necessary
6594c8945a0SNathan Whitehorn 	     */
6604c8945a0SNathan Whitehorn 	    back_lines(obj, obj->page_length);
6614c8945a0SNathan Whitehorn 	} else {		/* Search term found */
6627a1c0d96SNathan Whitehorn 	    back_lines(obj, 1L);
6634c8945a0SNathan Whitehorn 	}
6644c8945a0SNathan Whitehorn 	/* Reprint page */
665f4f33ea0SBaptiste Daroussin 	dlg_attrset(obj->text, dialog_attr);
6664c8945a0SNathan Whitehorn 	moved = TRUE;
6674c8945a0SNathan Whitehorn     } else {			/* no need to find */
6684c8945a0SNathan Whitehorn 	(void) beep();
6694c8945a0SNathan Whitehorn     }
6704c8945a0SNathan Whitehorn     return moved;
6714c8945a0SNathan Whitehorn }
6724c8945a0SNathan Whitehorn 
6734c8945a0SNathan Whitehorn /*
6744c8945a0SNathan Whitehorn  * Display text from a file in a dialog box.
6754c8945a0SNathan Whitehorn  */
6764c8945a0SNathan Whitehorn int
dialog_textbox(const char * title,const char * filename,int height,int width)677f4f33ea0SBaptiste Daroussin dialog_textbox(const char *title, const char *filename, int height, int width)
6784c8945a0SNathan Whitehorn {
6794c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
6804c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
681682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
6824c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
6834c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'J' ),
6844c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'j' ),
6854c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  KEY_DOWN ),
6864c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
6874c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
6884c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
6894c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
6904c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
6914c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
6924c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_UP,    'K' ),
6934c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_UP,    'k' ),
6944c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_GRID_UP,    KEY_UP ),
6954c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_FIRST, 'g' ),
6964c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
6974c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  'G' ),
6984c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_END ),
6994c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_LL ),
700f4f33ea0SBaptiste Daroussin 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  CHR_SPACE ),
7014c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ),
7024c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'B' ),
7034c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'b' ),
7044c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE ),
7054c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_BEGIN,	'0' ),
7064c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_BEGIN,	KEY_BEG ),
7074c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
7084c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
7094c8945a0SNathan Whitehorn 	END_KEYS_BINDING
7104c8945a0SNathan Whitehorn     };
7114c8945a0SNathan Whitehorn     /* *INDENT-ON* */
7124c8945a0SNathan Whitehorn 
7134c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
7144c8945a0SNathan Whitehorn     int old_height = height;
7154c8945a0SNathan Whitehorn     int old_width = width;
7164c8945a0SNathan Whitehorn #endif
7174c8945a0SNathan Whitehorn     long fpos;
7184c8945a0SNathan Whitehorn     int x, y, cur_x, cur_y;
719*a96ef450SBaptiste Daroussin     int key, fkey;
7204c8945a0SNathan Whitehorn     int next = 0;
721*a96ef450SBaptiste Daroussin     int i, passed_end;
7224c8945a0SNathan Whitehorn     char search_term[MAX_LEN + 1];
7234c8945a0SNathan Whitehorn     MY_OBJ obj;
7244c8945a0SNathan Whitehorn     WINDOW *dialog;
7254c8945a0SNathan Whitehorn     bool moved;
7264c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
7272a3e3873SBaptiste Daroussin     int button = dlg_default_button();
7284c8945a0SNathan Whitehorn     int min_width = 12;
7294c8945a0SNathan Whitehorn 
730f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# textbox args:\n"));
731f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
732f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("filename", filename);
733f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
734f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
735f4f33ea0SBaptiste Daroussin 
7364c8945a0SNathan Whitehorn     search_term[0] = '\0';	/* no search term entered yet */
7374c8945a0SNathan Whitehorn 
7384c8945a0SNathan Whitehorn     memset(&obj, 0, sizeof(obj));
7394c8945a0SNathan Whitehorn 
7404c8945a0SNathan Whitehorn     obj.begin_reached = TRUE;
7414c8945a0SNathan Whitehorn     obj.buffer_first = TRUE;
7424c8945a0SNathan Whitehorn     obj.end_reached = FALSE;
7434c8945a0SNathan Whitehorn     obj.buttons = dlg_exit_label();
7444c8945a0SNathan Whitehorn 
7454c8945a0SNathan Whitehorn     /* Open input file for reading */
746f4f33ea0SBaptiste Daroussin     if ((obj.fd = open(filename, O_RDONLY)) == -1)
747f4f33ea0SBaptiste Daroussin 	dlg_exiterr("Can't open input file %s", filename);
7484c8945a0SNathan Whitehorn 
7494c8945a0SNathan Whitehorn     /* Get file size. Actually, 'file_size' is the real file size - 1,
7504c8945a0SNathan Whitehorn        since it's only the last byte offset from the beginning */
7512a3e3873SBaptiste Daroussin     lseek_end(&obj, 0L);
7524c8945a0SNathan Whitehorn 
7534c8945a0SNathan Whitehorn     /* Restore file pointer to beginning of file after getting file size */
7542a3e3873SBaptiste Daroussin     lseek_set(&obj, 0L);
7554c8945a0SNathan Whitehorn 
7564c8945a0SNathan Whitehorn     read_high(&obj, BUF_SIZE);
7574c8945a0SNathan Whitehorn 
7584c8945a0SNathan Whitehorn     dlg_button_layout(obj.buttons, &min_width);
7594c8945a0SNathan Whitehorn 
7604c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
7614c8945a0SNathan Whitehorn   retry:
7624c8945a0SNathan Whitehorn #endif
7634c8945a0SNathan Whitehorn     moved = TRUE;
7644c8945a0SNathan Whitehorn 
765f4f33ea0SBaptiste Daroussin     dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
7664c8945a0SNathan Whitehorn     dlg_print_size(height, width);
7674c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
7684c8945a0SNathan Whitehorn 
7694c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
7704c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
7714c8945a0SNathan Whitehorn 
7724c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
7734c8945a0SNathan Whitehorn     dlg_register_window(dialog, "textbox", binding);
7744c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "textbox", obj.buttons);
7754c8945a0SNathan Whitehorn 
7764c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
7774c8945a0SNathan Whitehorn 
7784c8945a0SNathan Whitehorn     /* Create window for text region, used for scrolling text */
7794c8945a0SNathan Whitehorn     obj.text = dlg_sub_window(dialog, PAGE_LENGTH, PAGE_WIDTH, y + 1, x + 1);
7804c8945a0SNathan Whitehorn 
7814c8945a0SNathan Whitehorn     /* register the new window, along with its borders */
7824c8945a0SNathan Whitehorn     dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH + 2, width, KEY_MAX, 1, 1, 1 /* lines */ );
7832a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
7842a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
7854c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
7864c8945a0SNathan Whitehorn 
7874c8945a0SNathan Whitehorn     dlg_draw_buttons(dialog, PAGE_LENGTH + 2, 0, obj.buttons, button, FALSE, width);
7884c8945a0SNathan Whitehorn     (void) wnoutrefresh(dialog);
7894c8945a0SNathan Whitehorn     getyx(dialog, cur_y, cur_x);	/* Save cursor position */
7904c8945a0SNathan Whitehorn 
7914c8945a0SNathan Whitehorn     dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);
7924c8945a0SNathan Whitehorn 
7934c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
794*a96ef450SBaptiste Daroussin 	int code;
7954c8945a0SNathan Whitehorn 
7964c8945a0SNathan Whitehorn 	/*
7974c8945a0SNathan Whitehorn 	 * Update the screen according to whether we shifted up/down by a line
7984c8945a0SNathan Whitehorn 	 * or not.
7994c8945a0SNathan Whitehorn 	 */
8004c8945a0SNathan Whitehorn 	if (moved) {
8014c8945a0SNathan Whitehorn 	    if (next < 0) {
8024c8945a0SNathan Whitehorn 		(void) scrollok(obj.text, TRUE);
8034c8945a0SNathan Whitehorn 		(void) scroll(obj.text);	/* Scroll text region up one line */
8044c8945a0SNathan Whitehorn 		(void) scrollok(obj.text, FALSE);
8054c8945a0SNathan Whitehorn 		print_line(&obj, PAGE_LENGTH - 1, PAGE_WIDTH);
8064c8945a0SNathan Whitehorn 		(void) wnoutrefresh(obj.text);
8074c8945a0SNathan Whitehorn 	    } else if (next > 0) {
8084c8945a0SNathan Whitehorn 		/*
8094c8945a0SNathan Whitehorn 		 * We don't call print_page() here but use scrolling to ensure
8104c8945a0SNathan Whitehorn 		 * faster screen update.  However, 'end_reached' and
8114c8945a0SNathan Whitehorn 		 * 'page_length' should still be updated, and 'in_buf' should
8124c8945a0SNathan Whitehorn 		 * point to start of next page.  This is done by calling
8134c8945a0SNathan Whitehorn 		 * get_line() in the following 'for' loop.
8144c8945a0SNathan Whitehorn 		 */
8154c8945a0SNathan Whitehorn 		(void) scrollok(obj.text, TRUE);
8164c8945a0SNathan Whitehorn 		(void) wscrl(obj.text, -1);	/* Scroll text region down one line */
8174c8945a0SNathan Whitehorn 		(void) scrollok(obj.text, FALSE);
8184c8945a0SNathan Whitehorn 		obj.page_length = 0;
8194c8945a0SNathan Whitehorn 		passed_end = 0;
8204c8945a0SNathan Whitehorn 		for (i = 0; i < PAGE_LENGTH; i++) {
8214c8945a0SNathan Whitehorn 		    if (!i) {
8224c8945a0SNathan Whitehorn 			print_line(&obj, 0, PAGE_WIDTH);	/* print first line of page */
8234c8945a0SNathan Whitehorn 			(void) wnoutrefresh(obj.text);
8244c8945a0SNathan Whitehorn 		    } else
8254c8945a0SNathan Whitehorn 			(void) get_line(&obj);	/* Called to update 'end_reached' and 'in_buf' */
8264c8945a0SNathan Whitehorn 		    if (!passed_end)
8274c8945a0SNathan Whitehorn 			obj.page_length++;
8284c8945a0SNathan Whitehorn 		    if (obj.end_reached && !passed_end)
8294c8945a0SNathan Whitehorn 			passed_end = 1;
8304c8945a0SNathan Whitehorn 		}
8314c8945a0SNathan Whitehorn 	    } else {
8324c8945a0SNathan Whitehorn 		print_page(&obj, PAGE_LENGTH, PAGE_WIDTH);
8334c8945a0SNathan Whitehorn 	    }
8344c8945a0SNathan Whitehorn 	    print_position(&obj, dialog, height, width);
8354c8945a0SNathan Whitehorn 	    (void) wmove(dialog, cur_y, cur_x);		/* Restore cursor position */
8364c8945a0SNathan Whitehorn 	    wrefresh(dialog);
8374c8945a0SNathan Whitehorn 	}
8384c8945a0SNathan Whitehorn 	moved = FALSE;		/* assume we'll not move */
8394c8945a0SNathan Whitehorn 	next = 0;		/* ...but not scroll by a line */
8404c8945a0SNathan Whitehorn 
8414c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch(dialog, &fkey);
842*a96ef450SBaptiste Daroussin 	if (dlg_result_key(key, fkey, &result)) {
843*a96ef450SBaptiste Daroussin 	    if (!dlg_ok_button_key(result, &button, &key, &fkey))
8444c8945a0SNathan Whitehorn 		break;
845*a96ef450SBaptiste Daroussin 	}
8464c8945a0SNathan Whitehorn 
8474c8945a0SNathan Whitehorn 	if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
8484c8945a0SNathan Whitehorn 	    result = dlg_ok_buttoncode(code);
8494c8945a0SNathan Whitehorn 	    break;
8504c8945a0SNathan Whitehorn 	}
8514c8945a0SNathan Whitehorn 
8524c8945a0SNathan Whitehorn 	if (fkey) {
8534c8945a0SNathan Whitehorn 	    switch (key) {
8544c8945a0SNathan Whitehorn 	    default:
8554c8945a0SNathan Whitehorn 		if (is_DLGK_MOUSE(key)) {
8564c8945a0SNathan Whitehorn 		    result = dlg_exit_buttoncode(key - M_EVENT);
8574c8945a0SNathan Whitehorn 		    if (result < 0)
8584c8945a0SNathan Whitehorn 			result = DLG_EXIT_OK;
8594c8945a0SNathan Whitehorn 		} else {
8604c8945a0SNathan Whitehorn 		    beep();
8614c8945a0SNathan Whitehorn 		}
8624c8945a0SNathan Whitehorn 		break;
8634c8945a0SNathan Whitehorn 	    case DLGK_FIELD_NEXT:
8644c8945a0SNathan Whitehorn 		button = dlg_next_button(obj.buttons, button);
8654c8945a0SNathan Whitehorn 		if (button < 0)
8664c8945a0SNathan Whitehorn 		    button = 0;
8674c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog,
8684c8945a0SNathan Whitehorn 				 height - 2, 0,
8694c8945a0SNathan Whitehorn 				 obj.buttons, button,
8704c8945a0SNathan Whitehorn 				 FALSE, width);
8714c8945a0SNathan Whitehorn 		break;
8724c8945a0SNathan Whitehorn 	    case DLGK_FIELD_PREV:
8734c8945a0SNathan Whitehorn 		button = dlg_prev_button(obj.buttons, button);
8744c8945a0SNathan Whitehorn 		if (button < 0)
8754c8945a0SNathan Whitehorn 		    button = 0;
8764c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog,
8774c8945a0SNathan Whitehorn 				 height - 2, 0,
8784c8945a0SNathan Whitehorn 				 obj.buttons, button,
8794c8945a0SNathan Whitehorn 				 FALSE, width);
8804c8945a0SNathan Whitehorn 		break;
8814c8945a0SNathan Whitehorn 	    case DLGK_ENTER:
882*a96ef450SBaptiste Daroussin 		result = dlg_enter_buttoncode(button);
883*a96ef450SBaptiste Daroussin 		break;
884*a96ef450SBaptiste Daroussin 	    case DLGK_LEAVE:
885*a96ef450SBaptiste Daroussin 		result = dlg_ok_buttoncode(button);
8864c8945a0SNathan Whitehorn 		break;
8874c8945a0SNathan Whitehorn 	    case DLGK_PAGE_FIRST:
8884c8945a0SNathan Whitehorn 		if (!obj.begin_reached) {
8894c8945a0SNathan Whitehorn 		    obj.begin_reached = 1;
8904c8945a0SNathan Whitehorn 		    /* First page not in buffer? */
8914c8945a0SNathan Whitehorn 		    fpos = ftell_obj(&obj);
8924c8945a0SNathan Whitehorn 
8934c8945a0SNathan Whitehorn 		    if (fpos > obj.fd_bytes_read) {
8944c8945a0SNathan Whitehorn 			/* Yes, we have to read it in */
8952a3e3873SBaptiste Daroussin 			lseek_set(&obj, 0L);
8964c8945a0SNathan Whitehorn 
8974c8945a0SNathan Whitehorn 			read_high(&obj, BUF_SIZE);
8984c8945a0SNathan Whitehorn 		    }
8994c8945a0SNathan Whitehorn 		    obj.in_buf = 0;
9004c8945a0SNathan Whitehorn 		    moved = TRUE;
9014c8945a0SNathan Whitehorn 		}
9024c8945a0SNathan Whitehorn 		break;
9034c8945a0SNathan Whitehorn 	    case DLGK_PAGE_LAST:
9044c8945a0SNathan Whitehorn 		obj.end_reached = TRUE;
9054c8945a0SNathan Whitehorn 		/* Last page not in buffer? */
9064c8945a0SNathan Whitehorn 		fpos = ftell_obj(&obj);
9074c8945a0SNathan Whitehorn 
9084c8945a0SNathan Whitehorn 		if (fpos < obj.file_size) {
9094c8945a0SNathan Whitehorn 		    /* Yes, we have to read it in */
9102a3e3873SBaptiste Daroussin 		    lseek_end(&obj, -BUF_SIZE);
9114c8945a0SNathan Whitehorn 
9124c8945a0SNathan Whitehorn 		    read_high(&obj, BUF_SIZE);
9134c8945a0SNathan Whitehorn 		}
9144c8945a0SNathan Whitehorn 		obj.in_buf = obj.bytes_read;
9157a1c0d96SNathan Whitehorn 		back_lines(&obj, (long) PAGE_LENGTH);
9164c8945a0SNathan Whitehorn 		moved = TRUE;
9174c8945a0SNathan Whitehorn 		break;
9184c8945a0SNathan Whitehorn 	    case DLGK_GRID_UP:	/* Previous line */
9194c8945a0SNathan Whitehorn 		if (!obj.begin_reached) {
9204c8945a0SNathan Whitehorn 		    back_lines(&obj, obj.page_length + 1);
9214c8945a0SNathan Whitehorn 		    next = 1;
9224c8945a0SNathan Whitehorn 		    moved = TRUE;
9234c8945a0SNathan Whitehorn 		}
9244c8945a0SNathan Whitehorn 		break;
9254c8945a0SNathan Whitehorn 	    case DLGK_PAGE_PREV:	/* Previous page */
9264c8945a0SNathan Whitehorn 	    case DLGK_MOUSE(KEY_PPAGE):
9274c8945a0SNathan Whitehorn 		if (!obj.begin_reached) {
9284c8945a0SNathan Whitehorn 		    back_lines(&obj, obj.page_length + PAGE_LENGTH);
9294c8945a0SNathan Whitehorn 		    moved = TRUE;
9304c8945a0SNathan Whitehorn 		}
9314c8945a0SNathan Whitehorn 		break;
9324c8945a0SNathan Whitehorn 	    case DLGK_GRID_DOWN:	/* Next line */
9334c8945a0SNathan Whitehorn 		if (!obj.end_reached) {
9344c8945a0SNathan Whitehorn 		    obj.begin_reached = 0;
9354c8945a0SNathan Whitehorn 		    next = -1;
9364c8945a0SNathan Whitehorn 		    moved = TRUE;
9374c8945a0SNathan Whitehorn 		}
9384c8945a0SNathan Whitehorn 		break;
9394c8945a0SNathan Whitehorn 	    case DLGK_PAGE_NEXT:	/* Next page */
9404c8945a0SNathan Whitehorn 	    case DLGK_MOUSE(KEY_NPAGE):
9414c8945a0SNathan Whitehorn 		if (!obj.end_reached) {
9424c8945a0SNathan Whitehorn 		    obj.begin_reached = 0;
9434c8945a0SNathan Whitehorn 		    moved = TRUE;
9444c8945a0SNathan Whitehorn 		}
9454c8945a0SNathan Whitehorn 		break;
9464c8945a0SNathan Whitehorn 	    case DLGK_BEGIN:	/* Beginning of line */
9474c8945a0SNathan Whitehorn 		if (obj.hscroll > 0) {
9484c8945a0SNathan Whitehorn 		    obj.hscroll = 0;
9494c8945a0SNathan Whitehorn 		    /* Reprint current page to scroll horizontally */
9504c8945a0SNathan Whitehorn 		    back_lines(&obj, obj.page_length);
9514c8945a0SNathan Whitehorn 		    moved = TRUE;
9524c8945a0SNathan Whitehorn 		}
9534c8945a0SNathan Whitehorn 		break;
9544c8945a0SNathan Whitehorn 	    case DLGK_GRID_LEFT:	/* Scroll left */
9554c8945a0SNathan Whitehorn 		if (obj.hscroll > 0) {
9564c8945a0SNathan Whitehorn 		    obj.hscroll--;
9574c8945a0SNathan Whitehorn 		    /* Reprint current page to scroll horizontally */
9584c8945a0SNathan Whitehorn 		    back_lines(&obj, obj.page_length);
9594c8945a0SNathan Whitehorn 		    moved = TRUE;
9604c8945a0SNathan Whitehorn 		}
9614c8945a0SNathan Whitehorn 		break;
9624c8945a0SNathan Whitehorn 	    case DLGK_GRID_RIGHT:	/* Scroll right */
9634c8945a0SNathan Whitehorn 		if (obj.hscroll < MAX_LEN) {
9644c8945a0SNathan Whitehorn 		    obj.hscroll++;
9654c8945a0SNathan Whitehorn 		    /* Reprint current page to scroll horizontally */
9664c8945a0SNathan Whitehorn 		    back_lines(&obj, obj.page_length);
9674c8945a0SNathan Whitehorn 		    moved = TRUE;
9684c8945a0SNathan Whitehorn 		}
9694c8945a0SNathan Whitehorn 		break;
9704c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
9714c8945a0SNathan Whitehorn 	    case KEY_RESIZE:
972f4f33ea0SBaptiste Daroussin 		dlg_will_resize(dialog);
9734c8945a0SNathan Whitehorn 		/* reset data */
9744c8945a0SNathan Whitehorn 		height = old_height;
9754c8945a0SNathan Whitehorn 		width = old_width;
9764c8945a0SNathan Whitehorn 		back_lines(&obj, obj.page_length);
9774c8945a0SNathan Whitehorn 		/* repaint */
978*a96ef450SBaptiste Daroussin 		_dlg_resize_cleanup(dialog);
9794c8945a0SNathan Whitehorn 		goto retry;
9804c8945a0SNathan Whitehorn #endif
9814c8945a0SNathan Whitehorn 	    }
9824c8945a0SNathan Whitehorn 	} else {
9834c8945a0SNathan Whitehorn 	    switch (key) {
9844c8945a0SNathan Whitehorn 	    case '/':		/* Forward search */
9854c8945a0SNathan Whitehorn 	    case 'n':		/* Repeat forward search */
9864c8945a0SNathan Whitehorn 	    case '?':		/* Backward search */
9874c8945a0SNathan Whitehorn 	    case 'N':		/* Repeat backward search */
9884c8945a0SNathan Whitehorn 		moved = perform_search(&obj, height, width, key, search_term);
9894c8945a0SNathan Whitehorn 		fkey = FALSE;
9904c8945a0SNathan Whitehorn 		break;
9914c8945a0SNathan Whitehorn 	    default:
9924c8945a0SNathan Whitehorn 		beep();
9934c8945a0SNathan Whitehorn 		break;
9944c8945a0SNathan Whitehorn 	    }
9954c8945a0SNathan Whitehorn 	}
9964c8945a0SNathan Whitehorn     }
997*a96ef450SBaptiste Daroussin     dlg_add_last_key(-1);
9984c8945a0SNathan Whitehorn 
9994c8945a0SNathan Whitehorn     dlg_del_window(dialog);
10004c8945a0SNathan Whitehorn     free(obj.buf);
10014c8945a0SNathan Whitehorn     (void) close(obj.fd);
10024c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
10034c8945a0SNathan Whitehorn     return result;
10044c8945a0SNathan Whitehorn }
1005