xref: /freebsd/contrib/dialog/textbox.c (revision f078c492a9b57877c723586db26d789cda1b98ea)
1 /*
2  *  $Id: textbox.c,v 1.117 2018/06/19 22:57:01 tom Exp $
3  *
4  *  textbox.c -- implements the text box
5  *
6  *  Copyright 2000-2017,2018	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  *	Savio Lam (lam836@cs.cuhk.hk)
25  */
26 
27 #include <dialog.h>
28 #include <dlg_keys.h>
29 
30 #define PAGE_LENGTH	(height - 4)
31 #define PAGE_WIDTH	(width - 2)
32 
33 typedef struct {
34     DIALOG_CALLBACK obj;
35     WINDOW *text;
36     const char **buttons;
37     int hscroll;
38     char line[MAX_LEN + 1];
39     int fd;
40     long file_size;
41     long fd_bytes_read;
42     long bytes_read;
43     long buffer_len;
44     bool begin_reached;
45     bool buffer_first;
46     bool end_reached;
47     long page_length;		/* lines on the page which is shown */
48     long in_buf;		/* ending index into buf[] for page */
49     char *buf;
50 } MY_OBJ;
51 
52 static long
53 lseek_obj(MY_OBJ * obj, long offset, int mode)
54 {
55     long fpos;
56     if ((fpos = (long) lseek(obj->fd, (off_t) offset, mode)) == -1) {
57 	switch (mode) {
58 	default:
59 	case SEEK_CUR:
60 	    dlg_exiterr("Cannot get file position");
61 	    break;
62 	case SEEK_END:
63 	    dlg_exiterr("Cannot seek to end of file");
64 	    break;
65 	case SEEK_SET:
66 	    dlg_exiterr("Cannot set file position to %ld", offset);
67 	    break;
68 	}
69     }
70     return fpos;
71 }
72 
73 static long
74 ftell_obj(MY_OBJ * obj)
75 {
76     return lseek_obj(obj, 0L, SEEK_CUR);
77 }
78 
79 static void
80 lseek_set(MY_OBJ * obj, long offset)
81 {
82     long actual = lseek_obj(obj, offset, SEEK_SET);
83 
84     if (actual != offset) {
85 	dlg_exiterr("Cannot set file position to %ld (actual %ld)\n",
86 		    offset, actual);
87     }
88 }
89 
90 static void
91 lseek_end(MY_OBJ * obj, long offset)
92 {
93     long actual = lseek_obj(obj, offset, SEEK_END);
94 
95     if (offset == 0L && actual > offset) {
96 	obj->file_size = actual;
97     }
98 }
99 
100 static void
101 lseek_cur(MY_OBJ * obj, long offset)
102 {
103     long actual = lseek_obj(obj, offset, SEEK_CUR);
104 
105     if (actual != offset) {
106 	DLG_TRACE(("# Lseek returned %ld, expected %ld\n", actual, offset));
107     }
108 }
109 
110 static char *
111 xalloc(size_t size)
112 {
113     char *result = dlg_malloc(char, size);
114     assert_ptr(result, "xalloc");
115     return result;
116 }
117 
118 /*
119  * read_high() substitutes read() for tab->spaces conversion
120  *
121  * buffer_len, fd_bytes_read, bytes_read are modified
122  * buf is allocated
123  *
124  * fd_bytes_read is the effective number of bytes read from file
125  * bytes_read is the length of buf, that can be different if tab_correct
126  */
127 static void
128 read_high(MY_OBJ * obj, size_t size_read)
129 {
130     char *buftab, ch;
131     int i = 0, j, n, tmpint;
132     long begin_line;
133 
134     /* Allocate space for read buffer */
135     buftab = xalloc(size_read + 1);
136 
137     if ((obj->fd_bytes_read = read(obj->fd, buftab, size_read)) != -1) {
138 
139 	buftab[obj->fd_bytes_read] = '\0';	/* mark end of valid data */
140 
141 	if (dialog_vars.tab_correct) {
142 
143 	    /* calculate bytes_read by buftab and fd_bytes_read */
144 	    obj->bytes_read = begin_line = 0;
145 	    for (j = 0; j < obj->fd_bytes_read; j++)
146 		if (buftab[j] == TAB)
147 		    obj->bytes_read += dialog_state.tab_len
148 			- ((obj->bytes_read - begin_line)
149 			   % dialog_state.tab_len);
150 		else if (buftab[j] == '\n') {
151 		    obj->bytes_read++;
152 		    begin_line = obj->bytes_read;
153 		} else
154 		    obj->bytes_read++;
155 
156 	    if (obj->bytes_read > obj->buffer_len) {
157 		if (obj->buffer_first)
158 		    obj->buffer_first = FALSE;	/* disp = 0 */
159 		else {
160 		    free(obj->buf);
161 		}
162 
163 		obj->buffer_len = obj->bytes_read;
164 
165 		/* Allocate space for read buffer */
166 		obj->buf = xalloc((size_t) obj->buffer_len + 1);
167 	    }
168 
169 	} else {
170 	    if (obj->buffer_first) {
171 		obj->buffer_first = FALSE;
172 
173 		/* Allocate space for read buffer */
174 		obj->buf = xalloc(size_read + 1);
175 	    }
176 
177 	    obj->bytes_read = obj->fd_bytes_read;
178 	}
179 
180 	j = 0;
181 	begin_line = 0;
182 	while (j < obj->fd_bytes_read)
183 	    if (((ch = buftab[j++]) == TAB) && (dialog_vars.tab_correct != 0)) {
184 		tmpint = (dialog_state.tab_len
185 			  - ((int) ((long) i - begin_line) % dialog_state.tab_len));
186 		for (n = 0; n < tmpint; n++)
187 		    obj->buf[i++] = ' ';
188 	    } else {
189 		if (ch == '\n')
190 		    begin_line = i + 1;
191 		obj->buf[i++] = ch;
192 	    }
193 
194 	obj->buf[i] = '\0';	/* mark end of valid data */
195 
196     }
197     if (obj->bytes_read == -1)
198 	dlg_exiterr("Error reading file");
199     free(buftab);
200 }
201 
202 static long
203 find_first(MY_OBJ * obj, char *buffer, long length)
204 {
205     long recount = obj->page_length;
206     long result = 0;
207 
208     while (length > 0) {
209 	if (buffer[length] == '\n') {
210 	    if (--recount < 0) {
211 		result = length;
212 		break;
213 	    }
214 	}
215 	--length;
216     }
217     return result;
218 }
219 
220 static long
221 tabize(MY_OBJ * obj, long val, long *first_pos)
222 {
223     long fpos;
224     long i, count, begin_line;
225     char *buftab;
226 
227     if (!dialog_vars.tab_correct)
228 	return val;
229 
230     fpos = ftell_obj(obj);
231 
232     lseek_set(obj, fpos - obj->fd_bytes_read);
233 
234     /* Allocate space for read buffer */
235     buftab = xalloc((size_t) val + 1);
236 
237     if ((read(obj->fd, buftab, (size_t) val)) == -1)
238 	dlg_exiterr("Error reading file in tabize().");
239 
240     begin_line = count = 0;
241     if (first_pos != 0)
242 	*first_pos = 0;
243 
244     for (i = 0; i < val; i++) {
245 	if ((first_pos != 0) && (count >= val)) {
246 	    *first_pos = find_first(obj, buftab, i);
247 	    break;
248 	}
249 	if (buftab[i] == TAB)
250 	    count += dialog_state.tab_len
251 		- ((count - begin_line) % dialog_state.tab_len);
252 	else if (buftab[i] == '\n') {
253 	    count++;
254 	    begin_line = count;
255 	} else
256 	    count++;
257     }
258 
259     lseek_set(obj, fpos);
260     free(buftab);
261     return count;
262 }
263 /*
264  * Return current line of text.
265  * 'page' should point to start of current line before calling, and will be
266  * updated to point to start of next line.
267  */
268 static char *
269 get_line(MY_OBJ * obj)
270 {
271     int i = 0;
272     long fpos;
273 
274     obj->end_reached = FALSE;
275     while (obj->buf[obj->in_buf] != '\n') {
276 	if (obj->buf[obj->in_buf] == '\0') {	/* Either end of file or end of buffer reached */
277 	    fpos = ftell_obj(obj);
278 
279 	    if (fpos < obj->file_size) {	/* Not end of file yet */
280 		/* We've reached end of buffer, but not end of file yet, so
281 		 * read next part of file into buffer
282 		 */
283 		read_high(obj, BUF_SIZE);
284 		obj->in_buf = 0;
285 	    } else {
286 		if (!obj->end_reached)
287 		    obj->end_reached = TRUE;
288 		break;
289 	    }
290 	} else if (i < MAX_LEN)
291 	    obj->line[i++] = obj->buf[obj->in_buf++];
292 	else {
293 	    if (i == MAX_LEN)	/* Truncate lines longer than MAX_LEN characters */
294 		obj->line[i++] = '\0';
295 	    obj->in_buf++;
296 	}
297     }
298     if (i <= MAX_LEN)
299 	obj->line[i] = '\0';
300     if (!obj->end_reached)
301 	obj->in_buf++;		/* move past '\n' */
302 
303     return obj->line;
304 }
305 
306 static bool
307 match_string(MY_OBJ * obj, char *string)
308 {
309     char *match = get_line(obj);
310     return strstr(match, string) != 0;
311 }
312 
313 /*
314  * Go back 'n' lines in text file. Called by dialog_textbox().
315  * 'in_buf' will be updated to point to the desired line in 'buf'.
316  */
317 static void
318 back_lines(MY_OBJ * obj, long n)
319 {
320     int i;
321     long fpos;
322     long val_to_tabize;
323 
324     obj->begin_reached = FALSE;
325     /* We have to distinguish between end_reached and !end_reached since at end
326        * of file, the line is not ended by a '\n'.  The code inside 'if'
327        * basically does a '--in_buf' to move one character backward so as to
328        * skip '\n' of the previous line */
329     if (!obj->end_reached) {
330 	/* Either beginning of buffer or beginning of file reached? */
331 
332 	if (obj->in_buf == 0) {
333 	    fpos = ftell_obj(obj);
334 
335 	    if (fpos > obj->fd_bytes_read) {	/* Not beginning of file yet */
336 		/* We've reached beginning of buffer, but not beginning of file
337 		 * yet, so read previous part of file into buffer.  Note that
338 		 * we only move backward for BUF_SIZE/2 bytes, but not BUF_SIZE
339 		 * bytes to avoid re-reading again in print_page() later
340 		 */
341 		/* Really possible to move backward BUF_SIZE/2 bytes? */
342 		if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
343 		    /* No, move less than */
344 		    lseek_set(obj, 0L);
345 		    val_to_tabize = fpos - obj->fd_bytes_read;
346 		} else {	/* Move backward BUF_SIZE/2 bytes */
347 		    lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
348 		    val_to_tabize = BUF_SIZE / 2;
349 		}
350 		read_high(obj, BUF_SIZE);
351 
352 		obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
353 
354 	    } else {		/* Beginning of file reached */
355 		obj->begin_reached = TRUE;
356 		return;
357 	    }
358 	}
359 	obj->in_buf--;
360 	if (obj->buf[obj->in_buf] != '\n')
361 	    /* Something's wrong... */
362 	    dlg_exiterr("Internal error in back_lines().");
363     }
364 
365     /* Go back 'n' lines */
366     for (i = 0; i < n; i++) {
367 	do {
368 	    if (obj->in_buf == 0) {
369 		fpos = ftell_obj(obj);
370 
371 		if (fpos > obj->fd_bytes_read) {
372 		    /* Really possible to move backward BUF_SIZE/2 bytes? */
373 		    if (fpos < BUF_SIZE / 2 + obj->fd_bytes_read) {
374 			/* No, move less than */
375 			lseek_set(obj, 0L);
376 			val_to_tabize = fpos - obj->fd_bytes_read;
377 		    } else {	/* Move backward BUF_SIZE/2 bytes */
378 			lseek_cur(obj, -(BUF_SIZE / 2 + obj->fd_bytes_read));
379 			val_to_tabize = BUF_SIZE / 2;
380 		    }
381 		    read_high(obj, BUF_SIZE);
382 
383 		    obj->in_buf = tabize(obj, val_to_tabize, (long *) 0);
384 
385 		} else {	/* Beginning of file reached */
386 		    obj->begin_reached = TRUE;
387 		    return;
388 		}
389 	    }
390 	} while (obj->buf[--(obj->in_buf)] != '\n');
391     }
392     obj->in_buf++;
393 }
394 
395 /*
396  * Print a new line of text.
397  */
398 static void
399 print_line(MY_OBJ * obj, int row, int width)
400 {
401     if (wmove(obj->text, row, 0) != ERR) {
402 	int i, y, x;
403 	char *line = get_line(obj);
404 	const int *cols = dlg_index_columns(line);
405 	const int *indx = dlg_index_wchars(line);
406 	int limit = dlg_count_wchars(line);
407 	int first = 0;
408 	int last = limit;
409 
410 	if (width > getmaxx(obj->text))
411 	    width = getmaxx(obj->text);
412 	--width;		/* for the leading ' ' */
413 
414 	for (i = 0; i <= limit && cols[i] < obj->hscroll; ++i)
415 	    first = i;
416 
417 	for (i = first; (i <= limit) && ((cols[i] - cols[first]) < width); ++i)
418 	    last = i;
419 
420 	(void) waddch(obj->text, ' ');
421 	(void) waddnstr(obj->text, line + indx[first], indx[last] - indx[first]);
422 
423 	getyx(obj->text, y, x);
424 	if (y == row) {		/* Clear 'residue' of previous line */
425 	    for (i = 0; i <= width - x; i++) {
426 		(void) waddch(obj->text, ' ');
427 	    }
428 	}
429     }
430 }
431 
432 /*
433  * Print a new page of text.
434  */
435 static void
436 print_page(MY_OBJ * obj, int height, int width)
437 {
438     int i, passed_end = 0;
439 
440     obj->page_length = 0;
441     for (i = 0; i < height; i++) {
442 	print_line(obj, i, width);
443 	if (!passed_end)
444 	    obj->page_length++;
445 	if (obj->end_reached && !passed_end)
446 	    passed_end = 1;
447     }
448     (void) wnoutrefresh(obj->text);
449     dlg_trace_win(obj->text);
450 }
451 
452 /*
453  * Print current position
454  */
455 static void
456 print_position(MY_OBJ * obj, WINDOW *win, int height, int width)
457 {
458     long fpos;
459     long size;
460     long first = -1;
461 
462     fpos = ftell_obj(obj);
463     if (dialog_vars.tab_correct)
464 	size = tabize(obj, obj->in_buf, &first);
465     else
466 	first = find_first(obj, obj->buf, size = obj->in_buf);
467 
468     dlg_draw_scrollbar(win,
469 		       first,
470 		       fpos - obj->fd_bytes_read + size,
471 		       fpos - obj->fd_bytes_read + size,
472 		       obj->file_size,
473 		       0, PAGE_WIDTH,
474 		       0, PAGE_LENGTH + 1,
475 		       border_attr,
476 		       border_attr);
477 }
478 
479 /*
480  * Display a dialog box and get the search term from user.
481  */
482 static int
483 get_search_term(WINDOW *dialog, char *input, int height, int width)
484 {
485     /* *INDENT-OFF* */
486     static DLG_KEYS_BINDING binding[] = {
487 	INPUTSTR_BINDINGS,
488 	HELPKEY_BINDINGS,
489 	ENTERKEY_BINDINGS,
490 	END_KEYS_BINDING
491     };
492     /* *INDENT-ON* */
493 
494     int old_x, old_y;
495     int box_x, box_y;
496     int box_height, box_width;
497     int offset = 0;
498     int key = 0;
499     int fkey = 0;
500     bool first = TRUE;
501     int result = DLG_EXIT_UNKNOWN;
502     const char *caption = _("Search");
503     int len_caption = dlg_count_columns(caption);
504     const int *indx;
505     int limit;
506     WINDOW *widget;
507 
508     getbegyx(dialog, old_y, old_x);
509 
510     box_height = 1 + (2 * MARGIN);
511     box_width = len_caption + (2 * (MARGIN + 2));
512     box_width = MAX(box_width, 30);
513     box_width = MIN(box_width, getmaxx(dialog) - 2 * MARGIN);
514     len_caption = MIN(len_caption, box_width - (2 * (MARGIN + 1)));
515 
516     box_x = (width - box_width) / 2;
517     box_y = (height - box_height) / 2;
518     widget = dlg_new_modal_window(dialog,
519 				  box_height, box_width,
520 				  old_y + box_y, old_x + box_x);
521     keypad(widget, TRUE);
522     dlg_register_window(widget, "searchbox", binding);
523 
524     dlg_draw_box2(widget, 0, 0, box_height, box_width,
525 		  searchbox_attr,
526 		  searchbox_border_attr,
527 		  searchbox_border2_attr);
528     dlg_attrset(widget, searchbox_title_attr);
529     (void) wmove(widget, 0, (box_width - len_caption) / 2);
530 
531     indx = dlg_index_wchars(caption);
532     limit = dlg_limit_columns(caption, len_caption, 0);
533     (void) waddnstr(widget, caption + indx[0], indx[limit] - indx[0]);
534 
535     box_width -= 2;
536     offset = dlg_count_columns(input);
537 
538     while (result == DLG_EXIT_UNKNOWN) {
539 	if (!first) {
540 	    key = dlg_getc(widget, &fkey);
541 	    if (fkey) {
542 		switch (fkey) {
543 #ifdef KEY_RESIZE
544 		case KEY_RESIZE:
545 		    result = DLG_EXIT_CANCEL;
546 		    continue;
547 #endif
548 		case DLGK_ENTER:
549 		    result = DLG_EXIT_OK;
550 		    continue;
551 		}
552 	    } else if (key == ESC) {
553 		result = DLG_EXIT_ESC;
554 		continue;
555 	    } else if (key == ERR) {
556 		napms(50);
557 		continue;
558 	    }
559 	}
560 	if (dlg_edit_string(input, &offset, key, fkey, first)) {
561 	    dlg_show_string(widget, input, offset, searchbox_attr,
562 			    1, 1, box_width, FALSE, first);
563 	    first = FALSE;
564 	}
565     }
566     dlg_del_window(widget);
567     return result;
568 }
569 
570 static bool
571 perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
572 {
573     int dir;
574     long tempinx;
575     long fpos;
576     int result;
577     bool found;
578     bool temp, temp1;
579     bool moved = FALSE;
580 
581     /* set search direction */
582     dir = (key == '/' || key == 'n') ? 1 : 0;
583     if (dir ? !obj->end_reached : !obj->begin_reached) {
584 	if (key == 'n' || key == 'N') {
585 	    if (search_term[0] == '\0') {	/* No search term yet */
586 		(void) beep();
587 		return FALSE;
588 	    }
589 	    /* Get search term from user */
590 	} else if ((result = get_search_term(obj->text, search_term,
591 					     PAGE_LENGTH,
592 					     PAGE_WIDTH)) != DLG_EXIT_OK
593 		   || search_term[0] == '\0') {
594 #ifdef KEY_RESIZE
595 	    if (result == DLG_EXIT_CANCEL) {
596 		ungetch(key);
597 		ungetch(KEY_RESIZE);
598 		/* FALLTHRU */
599 	    }
600 #endif
601 	    /* ESC pressed, or no search term, reprint page to clear box */
602 	    dlg_attrset(obj->text, dialog_attr);
603 	    back_lines(obj, obj->page_length);
604 	    return TRUE;
605 	}
606 	/* Save variables for restoring in case search term can't be found */
607 	tempinx = obj->in_buf;
608 	temp = obj->begin_reached;
609 	temp1 = obj->end_reached;
610 	fpos = ftell_obj(obj) - obj->fd_bytes_read;
611 	/* update 'in_buf' to point to next (previous) line before
612 	   forward (backward) searching */
613 	back_lines(obj, (dir
614 			 ? obj->page_length - 1
615 			 : obj->page_length + 1));
616 	if (dir) {		/* Forward search */
617 	    while ((found = match_string(obj, search_term)) == FALSE) {
618 		if (obj->end_reached)
619 		    break;
620 	    }
621 	} else {		/* Backward search */
622 	    while ((found = match_string(obj, search_term)) == FALSE) {
623 		if (obj->begin_reached)
624 		    break;
625 		back_lines(obj, 2L);
626 	    }
627 	}
628 	if (found == FALSE) {	/* not found */
629 	    (void) beep();
630 	    /* Restore program state to that before searching */
631 	    lseek_set(obj, fpos);
632 
633 	    read_high(obj, BUF_SIZE);
634 
635 	    obj->in_buf = tempinx;
636 	    obj->begin_reached = temp;
637 	    obj->end_reached = temp1;
638 	    /* move 'in_buf' to point to start of current page to
639 	     * re-print current page.  Note that 'in_buf' always points
640 	     * to start of next page, so this is necessary
641 	     */
642 	    back_lines(obj, obj->page_length);
643 	} else {		/* Search term found */
644 	    back_lines(obj, 1L);
645 	}
646 	/* Reprint page */
647 	dlg_attrset(obj->text, dialog_attr);
648 	moved = TRUE;
649     } else {			/* no need to find */
650 	(void) beep();
651     }
652     return moved;
653 }
654 
655 /*
656  * Display text from a file in a dialog box.
657  */
658 int
659 dialog_textbox(const char *title, const char *filename, int height, int width)
660 {
661     /* *INDENT-OFF* */
662     static DLG_KEYS_BINDING binding[] = {
663 	HELPKEY_BINDINGS,
664 	ENTERKEY_BINDINGS,
665 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'J' ),
666 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'j' ),
667 	DLG_KEYS_DATA( DLGK_GRID_DOWN,  KEY_DOWN ),
668 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
669 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
670 	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
671 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
672 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
673 	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
674 	DLG_KEYS_DATA( DLGK_GRID_UP,    'K' ),
675 	DLG_KEYS_DATA( DLGK_GRID_UP,    'k' ),
676 	DLG_KEYS_DATA( DLGK_GRID_UP,    KEY_UP ),
677 	DLG_KEYS_DATA( DLGK_PAGE_FIRST, 'g' ),
678 	DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
679 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  'G' ),
680 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_END ),
681 	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_LL ),
682 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  CHR_SPACE ),
683 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ),
684 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'B' ),
685 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'b' ),
686 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE ),
687 	DLG_KEYS_DATA( DLGK_BEGIN,	'0' ),
688 	DLG_KEYS_DATA( DLGK_BEGIN,	KEY_BEG ),
689 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
690 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
691 	END_KEYS_BINDING
692     };
693     /* *INDENT-ON* */
694 
695 #ifdef KEY_RESIZE
696     int old_height = height;
697     int old_width = width;
698 #endif
699     long fpos;
700     int x, y, cur_x, cur_y;
701     int key = 0, fkey;
702     int next = 0;
703     int i, code, passed_end;
704     char search_term[MAX_LEN + 1];
705     MY_OBJ obj;
706     WINDOW *dialog;
707     bool moved;
708     int result = DLG_EXIT_UNKNOWN;
709     int button = dlg_default_button();
710     int min_width = 12;
711 
712     DLG_TRACE(("# textbox args:\n"));
713     DLG_TRACE2S("title", title);
714     DLG_TRACE2S("filename", filename);
715     DLG_TRACE2N("height", height);
716     DLG_TRACE2N("width", width);
717 
718     search_term[0] = '\0';	/* no search term entered yet */
719 
720     memset(&obj, 0, sizeof(obj));
721 
722     obj.begin_reached = TRUE;
723     obj.buffer_first = TRUE;
724     obj.end_reached = FALSE;
725     obj.buttons = dlg_exit_label();
726 
727     /* Open input file for reading */
728     if ((obj.fd = open(filename, O_RDONLY)) == -1)
729 	dlg_exiterr("Can't open input file %s", filename);
730 
731     /* Get file size. Actually, 'file_size' is the real file size - 1,
732        since it's only the last byte offset from the beginning */
733     lseek_end(&obj, 0L);
734 
735     /* Restore file pointer to beginning of file after getting file size */
736     lseek_set(&obj, 0L);
737 
738     read_high(&obj, BUF_SIZE);
739 
740     dlg_button_layout(obj.buttons, &min_width);
741 
742 #ifdef KEY_RESIZE
743   retry:
744 #endif
745     moved = TRUE;
746 
747     dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
748     dlg_print_size(height, width);
749     dlg_ctl_size(height, width);
750 
751     x = dlg_box_x_ordinate(width);
752     y = dlg_box_y_ordinate(height);
753 
754     dialog = dlg_new_window(height, width, y, x);
755     dlg_register_window(dialog, "textbox", binding);
756     dlg_register_buttons(dialog, "textbox", obj.buttons);
757 
758     dlg_mouse_setbase(x, y);
759 
760     /* Create window for text region, used for scrolling text */
761     obj.text = dlg_sub_window(dialog, PAGE_LENGTH, PAGE_WIDTH, y + 1, x + 1);
762 
763     /* register the new window, along with its borders */
764     dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH + 2, width, KEY_MAX, 1, 1, 1 /* lines */ );
765     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
766     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
767     dlg_draw_title(dialog, title);
768 
769     dlg_draw_buttons(dialog, PAGE_LENGTH + 2, 0, obj.buttons, button, FALSE, width);
770     (void) wnoutrefresh(dialog);
771     getyx(dialog, cur_y, cur_x);	/* Save cursor position */
772 
773     dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);
774 
775     while (result == DLG_EXIT_UNKNOWN) {
776 
777 	/*
778 	 * Update the screen according to whether we shifted up/down by a line
779 	 * or not.
780 	 */
781 	if (moved) {
782 	    if (next < 0) {
783 		(void) scrollok(obj.text, TRUE);
784 		(void) scroll(obj.text);	/* Scroll text region up one line */
785 		(void) scrollok(obj.text, FALSE);
786 		print_line(&obj, PAGE_LENGTH - 1, PAGE_WIDTH);
787 		(void) wnoutrefresh(obj.text);
788 	    } else if (next > 0) {
789 		/*
790 		 * We don't call print_page() here but use scrolling to ensure
791 		 * faster screen update.  However, 'end_reached' and
792 		 * 'page_length' should still be updated, and 'in_buf' should
793 		 * point to start of next page.  This is done by calling
794 		 * get_line() in the following 'for' loop.
795 		 */
796 		(void) scrollok(obj.text, TRUE);
797 		(void) wscrl(obj.text, -1);	/* Scroll text region down one line */
798 		(void) scrollok(obj.text, FALSE);
799 		obj.page_length = 0;
800 		passed_end = 0;
801 		for (i = 0; i < PAGE_LENGTH; i++) {
802 		    if (!i) {
803 			print_line(&obj, 0, PAGE_WIDTH);	/* print first line of page */
804 			(void) wnoutrefresh(obj.text);
805 		    } else
806 			(void) get_line(&obj);	/* Called to update 'end_reached' and 'in_buf' */
807 		    if (!passed_end)
808 			obj.page_length++;
809 		    if (obj.end_reached && !passed_end)
810 			passed_end = 1;
811 		}
812 	    } else {
813 		print_page(&obj, PAGE_LENGTH, PAGE_WIDTH);
814 	    }
815 	    print_position(&obj, dialog, height, width);
816 	    (void) wmove(dialog, cur_y, cur_x);		/* Restore cursor position */
817 	    wrefresh(dialog);
818 	}
819 	moved = FALSE;		/* assume we'll not move */
820 	next = 0;		/* ...but not scroll by a line */
821 
822 	key = dlg_mouse_wgetch(dialog, &fkey);
823 	if (dlg_result_key(key, fkey, &result))
824 	    break;
825 
826 	if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
827 	    result = dlg_ok_buttoncode(code);
828 	    break;
829 	}
830 
831 	if (fkey) {
832 	    switch (key) {
833 	    default:
834 		if (is_DLGK_MOUSE(key)) {
835 		    result = dlg_exit_buttoncode(key - M_EVENT);
836 		    if (result < 0)
837 			result = DLG_EXIT_OK;
838 		} else {
839 		    beep();
840 		}
841 		break;
842 	    case DLGK_FIELD_NEXT:
843 		button = dlg_next_button(obj.buttons, button);
844 		if (button < 0)
845 		    button = 0;
846 		dlg_draw_buttons(dialog,
847 				 height - 2, 0,
848 				 obj.buttons, button,
849 				 FALSE, width);
850 		break;
851 	    case DLGK_FIELD_PREV:
852 		button = dlg_prev_button(obj.buttons, button);
853 		if (button < 0)
854 		    button = 0;
855 		dlg_draw_buttons(dialog,
856 				 height - 2, 0,
857 				 obj.buttons, button,
858 				 FALSE, width);
859 		break;
860 	    case DLGK_ENTER:
861 		if (dialog_vars.nook)
862 		    result = DLG_EXIT_OK;
863 		else
864 		    result = dlg_exit_buttoncode(button);
865 		break;
866 	    case DLGK_PAGE_FIRST:
867 		if (!obj.begin_reached) {
868 		    obj.begin_reached = 1;
869 		    /* First page not in buffer? */
870 		    fpos = ftell_obj(&obj);
871 
872 		    if (fpos > obj.fd_bytes_read) {
873 			/* Yes, we have to read it in */
874 			lseek_set(&obj, 0L);
875 
876 			read_high(&obj, BUF_SIZE);
877 		    }
878 		    obj.in_buf = 0;
879 		    moved = TRUE;
880 		}
881 		break;
882 	    case DLGK_PAGE_LAST:
883 		obj.end_reached = TRUE;
884 		/* Last page not in buffer? */
885 		fpos = ftell_obj(&obj);
886 
887 		if (fpos < obj.file_size) {
888 		    /* Yes, we have to read it in */
889 		    lseek_end(&obj, -BUF_SIZE);
890 
891 		    read_high(&obj, BUF_SIZE);
892 		}
893 		obj.in_buf = obj.bytes_read;
894 		back_lines(&obj, (long) PAGE_LENGTH);
895 		moved = TRUE;
896 		break;
897 	    case DLGK_GRID_UP:	/* Previous line */
898 		if (!obj.begin_reached) {
899 		    back_lines(&obj, obj.page_length + 1);
900 		    next = 1;
901 		    moved = TRUE;
902 		}
903 		break;
904 	    case DLGK_PAGE_PREV:	/* Previous page */
905 	    case DLGK_MOUSE(KEY_PPAGE):
906 		if (!obj.begin_reached) {
907 		    back_lines(&obj, obj.page_length + PAGE_LENGTH);
908 		    moved = TRUE;
909 		}
910 		break;
911 	    case DLGK_GRID_DOWN:	/* Next line */
912 		if (!obj.end_reached) {
913 		    obj.begin_reached = 0;
914 		    next = -1;
915 		    moved = TRUE;
916 		}
917 		break;
918 	    case DLGK_PAGE_NEXT:	/* Next page */
919 	    case DLGK_MOUSE(KEY_NPAGE):
920 		if (!obj.end_reached) {
921 		    obj.begin_reached = 0;
922 		    moved = TRUE;
923 		}
924 		break;
925 	    case DLGK_BEGIN:	/* Beginning of line */
926 		if (obj.hscroll > 0) {
927 		    obj.hscroll = 0;
928 		    /* Reprint current page to scroll horizontally */
929 		    back_lines(&obj, obj.page_length);
930 		    moved = TRUE;
931 		}
932 		break;
933 	    case DLGK_GRID_LEFT:	/* Scroll left */
934 		if (obj.hscroll > 0) {
935 		    obj.hscroll--;
936 		    /* Reprint current page to scroll horizontally */
937 		    back_lines(&obj, obj.page_length);
938 		    moved = TRUE;
939 		}
940 		break;
941 	    case DLGK_GRID_RIGHT:	/* Scroll right */
942 		if (obj.hscroll < MAX_LEN) {
943 		    obj.hscroll++;
944 		    /* Reprint current page to scroll horizontally */
945 		    back_lines(&obj, obj.page_length);
946 		    moved = TRUE;
947 		}
948 		break;
949 #ifdef KEY_RESIZE
950 	    case KEY_RESIZE:
951 		dlg_will_resize(dialog);
952 		/* reset data */
953 		height = old_height;
954 		width = old_width;
955 		back_lines(&obj, obj.page_length);
956 		/* repaint */
957 		dlg_clear();
958 		dlg_del_window(dialog);
959 		dlg_mouse_free_regions();
960 		goto retry;
961 #endif
962 	    }
963 	} else {
964 	    switch (key) {
965 	    case '/':		/* Forward search */
966 	    case 'n':		/* Repeat forward search */
967 	    case '?':		/* Backward search */
968 	    case 'N':		/* Repeat backward search */
969 		moved = perform_search(&obj, height, width, key, search_term);
970 		fkey = FALSE;
971 		break;
972 	    default:
973 		beep();
974 		break;
975 	    }
976 	}
977     }
978 
979     dlg_del_window(dialog);
980     free(obj.buf);
981     (void) close(obj.fd);
982     dlg_mouse_free_regions();
983     return result;
984 }
985