16f6046cfSSam Ravnborg /*
26f6046cfSSam Ravnborg  *  textbox.c -- implements the text box
36f6046cfSSam Ravnborg  *
46f6046cfSSam Ravnborg  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
56f6046cfSSam Ravnborg  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
66f6046cfSSam Ravnborg  *
76f6046cfSSam Ravnborg  *  This program is free software; you can redistribute it and/or
86f6046cfSSam Ravnborg  *  modify it under the terms of the GNU General Public License
96f6046cfSSam Ravnborg  *  as published by the Free Software Foundation; either version 2
106f6046cfSSam Ravnborg  *  of the License, or (at your option) any later version.
116f6046cfSSam Ravnborg  *
126f6046cfSSam Ravnborg  *  This program is distributed in the hope that it will be useful,
136f6046cfSSam Ravnborg  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
146f6046cfSSam Ravnborg  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156f6046cfSSam Ravnborg  *  GNU General Public License for more details.
166f6046cfSSam Ravnborg  *
176f6046cfSSam Ravnborg  *  You should have received a copy of the GNU General Public License
186f6046cfSSam Ravnborg  *  along with this program; if not, write to the Free Software
196f6046cfSSam Ravnborg  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
206f6046cfSSam Ravnborg  */
216f6046cfSSam Ravnborg 
226f6046cfSSam Ravnborg #include "dialog.h"
236f6046cfSSam Ravnborg 
246f6046cfSSam Ravnborg static void back_lines(int n);
2595ac9b3bSBenjamin Poirier static void print_page(WINDOW *win, int height, int width, update_text_fn
2695ac9b3bSBenjamin Poirier 		       update_text, void *data);
276f6046cfSSam Ravnborg static void print_line(WINDOW *win, int row, int width);
286f6046cfSSam Ravnborg static char *get_line(void);
29c8dc68adSSam Ravnborg static void print_position(WINDOW * win);
306f6046cfSSam Ravnborg 
312982de69SSam Ravnborg static int hscroll;
322982de69SSam Ravnborg static int begin_reached, end_reached, page_length;
3395ac9b3bSBenjamin Poirier static char *buf;
3495ac9b3bSBenjamin Poirier static char *page;
356f6046cfSSam Ravnborg 
366f6046cfSSam Ravnborg /*
37c8dc68adSSam Ravnborg  * refresh window content
38c8dc68adSSam Ravnborg  */
39c8dc68adSSam Ravnborg static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
4095ac9b3bSBenjamin Poirier 			     int cur_y, int cur_x, update_text_fn update_text,
4195ac9b3bSBenjamin Poirier 			     void *data)
42c8dc68adSSam Ravnborg {
4395ac9b3bSBenjamin Poirier 	print_page(box, boxh, boxw, update_text, data);
44c8dc68adSSam Ravnborg 	print_position(dialog);
45c8dc68adSSam Ravnborg 	wmove(dialog, cur_y, cur_x);	/* Restore cursor position */
46c8dc68adSSam Ravnborg 	wrefresh(dialog);
47c8dc68adSSam Ravnborg }
48c8dc68adSSam Ravnborg 
49c8dc68adSSam Ravnborg 
50c8dc68adSSam Ravnborg /*
516f6046cfSSam Ravnborg  * Display text from a file in a dialog box.
52537ddae7SBenjamin Poirier  *
53537ddae7SBenjamin Poirier  * keys is a null-terminated array
5495ac9b3bSBenjamin Poirier  * update_text() may not add or remove any '\n' or '\0' in tbuf
556f6046cfSSam Ravnborg  */
5695ac9b3bSBenjamin Poirier int dialog_textbox(const char *title, char *tbuf, int initial_height,
5795ac9b3bSBenjamin Poirier 		   int initial_width, int *keys, int *_vscroll, int *_hscroll,
5895ac9b3bSBenjamin Poirier 		   update_text_fn update_text, void *data)
596f6046cfSSam Ravnborg {
602982de69SSam Ravnborg 	int i, x, y, cur_x, cur_y, key = 0;
61c8dc68adSSam Ravnborg 	int height, width, boxh, boxw;
62c8dc68adSSam Ravnborg 	WINDOW *dialog, *box;
63537ddae7SBenjamin Poirier 	bool done = false;
646f6046cfSSam Ravnborg 
652982de69SSam Ravnborg 	begin_reached = 1;
662982de69SSam Ravnborg 	end_reached = 0;
672982de69SSam Ravnborg 	page_length = 0;
682982de69SSam Ravnborg 	hscroll = 0;
692982de69SSam Ravnborg 	buf = tbuf;
706f6046cfSSam Ravnborg 	page = buf;	/* page is pointer to start of page to be displayed */
716f6046cfSSam Ravnborg 
721d1e2caeSBenjamin Poirier 	if (_vscroll && *_vscroll) {
731d1e2caeSBenjamin Poirier 		begin_reached = 0;
741d1e2caeSBenjamin Poirier 
751d1e2caeSBenjamin Poirier 		for (i = 0; i < *_vscroll; i++)
761d1e2caeSBenjamin Poirier 			get_line();
771d1e2caeSBenjamin Poirier 	}
781d1e2caeSBenjamin Poirier 	if (_hscroll)
791d1e2caeSBenjamin Poirier 		hscroll = *_hscroll;
801d1e2caeSBenjamin Poirier 
81c8dc68adSSam Ravnborg do_resize:
82c8dc68adSSam Ravnborg 	getmaxyx(stdscr, height, width);
83c8dc68adSSam Ravnborg 	if (height < 8 || width < 8)
84c8dc68adSSam Ravnborg 		return -ERRDISPLAYTOOSMALL;
85c8dc68adSSam Ravnborg 	if (initial_height != 0)
86c8dc68adSSam Ravnborg 		height = initial_height;
87c8dc68adSSam Ravnborg 	else
88c8dc68adSSam Ravnborg 		if (height > 4)
89c8dc68adSSam Ravnborg 			height -= 4;
90c8dc68adSSam Ravnborg 		else
91c8dc68adSSam Ravnborg 			height = 0;
92c8dc68adSSam Ravnborg 	if (initial_width != 0)
93c8dc68adSSam Ravnborg 		width = initial_width;
94c8dc68adSSam Ravnborg 	else
95c8dc68adSSam Ravnborg 		if (width > 5)
96c8dc68adSSam Ravnborg 			width -= 5;
97c8dc68adSSam Ravnborg 		else
98c8dc68adSSam Ravnborg 			width = 0;
99c8dc68adSSam Ravnborg 
1006f6046cfSSam Ravnborg 	/* center dialog box on screen */
1016f6046cfSSam Ravnborg 	x = (COLS - width) / 2;
1026f6046cfSSam Ravnborg 	y = (LINES - height) / 2;
1036f6046cfSSam Ravnborg 
1046f6046cfSSam Ravnborg 	draw_shadow(stdscr, y, x, height, width);
1056f6046cfSSam Ravnborg 
1066f6046cfSSam Ravnborg 	dialog = newwin(height, width, y, x);
1076f6046cfSSam Ravnborg 	keypad(dialog, TRUE);
1086f6046cfSSam Ravnborg 
109c8dc68adSSam Ravnborg 	/* Create window for box region, used for scrolling text */
110c8dc68adSSam Ravnborg 	boxh = height - 4;
111c8dc68adSSam Ravnborg 	boxw = width - 2;
112c8dc68adSSam Ravnborg 	box = subwin(dialog, boxh, boxw, y + 1, x + 1);
113c8dc68adSSam Ravnborg 	wattrset(box, dlg.dialog.atr);
114c8dc68adSSam Ravnborg 	wbkgdset(box, dlg.dialog.atr & A_COLOR);
1156f6046cfSSam Ravnborg 
116c8dc68adSSam Ravnborg 	keypad(box, TRUE);
1176f6046cfSSam Ravnborg 
1186f6046cfSSam Ravnborg 	/* register the new window, along with its borders */
11998e5a157SSam Ravnborg 	draw_box(dialog, 0, 0, height, width,
12098e5a157SSam Ravnborg 		 dlg.dialog.atr, dlg.border.atr);
1216f6046cfSSam Ravnborg 
12298e5a157SSam Ravnborg 	wattrset(dialog, dlg.border.atr);
1236f6046cfSSam Ravnborg 	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
1246f6046cfSSam Ravnborg 	for (i = 0; i < width - 2; i++)
1256f6046cfSSam Ravnborg 		waddch(dialog, ACS_HLINE);
12698e5a157SSam Ravnborg 	wattrset(dialog, dlg.dialog.atr);
12798e5a157SSam Ravnborg 	wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
1286f6046cfSSam Ravnborg 	waddch(dialog, ACS_RTEE);
1296f6046cfSSam Ravnborg 
1306f6046cfSSam Ravnborg 	print_title(dialog, title, width);
1316f6046cfSSam Ravnborg 
13275c0a8a5SEGRY Gabor 	print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE);
1336f6046cfSSam Ravnborg 	wnoutrefresh(dialog);
1346f6046cfSSam Ravnborg 	getyx(dialog, cur_y, cur_x);	/* Save cursor position */
1356f6046cfSSam Ravnborg 
1366f6046cfSSam Ravnborg 	/* Print first page of text */
137c8dc68adSSam Ravnborg 	attr_clear(box, boxh, boxw, dlg.dialog.atr);
13895ac9b3bSBenjamin Poirier 	refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x, update_text,
13995ac9b3bSBenjamin Poirier 			 data);
1406f6046cfSSam Ravnborg 
141537ddae7SBenjamin Poirier 	while (!done) {
1426f6046cfSSam Ravnborg 		key = wgetch(dialog);
1436f6046cfSSam Ravnborg 		switch (key) {
1446f6046cfSSam Ravnborg 		case 'E':	/* Exit */
1456f6046cfSSam Ravnborg 		case 'e':
1466f6046cfSSam Ravnborg 		case 'X':
1476f6046cfSSam Ravnborg 		case 'x':
1489d4792c9SBenjamin Poirier 		case 'q':
149537ddae7SBenjamin Poirier 		case '\n':
150537ddae7SBenjamin Poirier 			done = true;
151537ddae7SBenjamin Poirier 			break;
1526f6046cfSSam Ravnborg 		case 'g':	/* First page */
1536f6046cfSSam Ravnborg 		case KEY_HOME:
1546f6046cfSSam Ravnborg 			if (!begin_reached) {
1556f6046cfSSam Ravnborg 				begin_reached = 1;
1566f6046cfSSam Ravnborg 				page = buf;
157c8dc68adSSam Ravnborg 				refresh_text_box(dialog, box, boxh, boxw,
15895ac9b3bSBenjamin Poirier 						 cur_y, cur_x, update_text,
15995ac9b3bSBenjamin Poirier 						 data);
1606f6046cfSSam Ravnborg 			}
1616f6046cfSSam Ravnborg 			break;
1626f6046cfSSam Ravnborg 		case 'G':	/* Last page */
1636f6046cfSSam Ravnborg 		case KEY_END:
1646f6046cfSSam Ravnborg 
1656f6046cfSSam Ravnborg 			end_reached = 1;
1662982de69SSam Ravnborg 			/* point to last char in buf */
1672982de69SSam Ravnborg 			page = buf + strlen(buf);
168c8dc68adSSam Ravnborg 			back_lines(boxh);
16995ac9b3bSBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
17095ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
1716f6046cfSSam Ravnborg 			break;
1726f6046cfSSam Ravnborg 		case 'K':	/* Previous line */
1736f6046cfSSam Ravnborg 		case 'k':
1746f6046cfSSam Ravnborg 		case KEY_UP:
1751a374ae6SBenjamin Poirier 			if (begin_reached)
1761a374ae6SBenjamin Poirier 				break;
177537ddae7SBenjamin Poirier 
1786f6046cfSSam Ravnborg 			back_lines(page_length + 1);
1791a374ae6SBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
18095ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
1816f6046cfSSam Ravnborg 			break;
1826f6046cfSSam Ravnborg 		case 'B':	/* Previous page */
1836f6046cfSSam Ravnborg 		case 'b':
1849d4792c9SBenjamin Poirier 		case 'u':
1856f6046cfSSam Ravnborg 		case KEY_PPAGE:
1866f6046cfSSam Ravnborg 			if (begin_reached)
1876f6046cfSSam Ravnborg 				break;
188c8dc68adSSam Ravnborg 			back_lines(page_length + boxh);
18995ac9b3bSBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
19095ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
1916f6046cfSSam Ravnborg 			break;
1926f6046cfSSam Ravnborg 		case 'J':	/* Next line */
1936f6046cfSSam Ravnborg 		case 'j':
1946f6046cfSSam Ravnborg 		case KEY_DOWN:
1951a374ae6SBenjamin Poirier 			if (end_reached)
1961a374ae6SBenjamin Poirier 				break;
1971a374ae6SBenjamin Poirier 
1981a374ae6SBenjamin Poirier 			back_lines(page_length - 1);
1991a374ae6SBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
20095ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
2016f6046cfSSam Ravnborg 			break;
2026f6046cfSSam Ravnborg 		case KEY_NPAGE:	/* Next page */
2036f6046cfSSam Ravnborg 		case ' ':
2049d4792c9SBenjamin Poirier 		case 'd':
2056f6046cfSSam Ravnborg 			if (end_reached)
2066f6046cfSSam Ravnborg 				break;
2076f6046cfSSam Ravnborg 
2086f6046cfSSam Ravnborg 			begin_reached = 0;
20995ac9b3bSBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
21095ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
2116f6046cfSSam Ravnborg 			break;
2126f6046cfSSam Ravnborg 		case '0':	/* Beginning of line */
2136f6046cfSSam Ravnborg 		case 'H':	/* Scroll left */
2146f6046cfSSam Ravnborg 		case 'h':
2156f6046cfSSam Ravnborg 		case KEY_LEFT:
2166f6046cfSSam Ravnborg 			if (hscroll <= 0)
2176f6046cfSSam Ravnborg 				break;
2186f6046cfSSam Ravnborg 
2196f6046cfSSam Ravnborg 			if (key == '0')
2206f6046cfSSam Ravnborg 				hscroll = 0;
2216f6046cfSSam Ravnborg 			else
2226f6046cfSSam Ravnborg 				hscroll--;
2236f6046cfSSam Ravnborg 			/* Reprint current page to scroll horizontally */
2246f6046cfSSam Ravnborg 			back_lines(page_length);
22595ac9b3bSBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
22695ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
2276f6046cfSSam Ravnborg 			break;
2286f6046cfSSam Ravnborg 		case 'L':	/* Scroll right */
2296f6046cfSSam Ravnborg 		case 'l':
2306f6046cfSSam Ravnborg 		case KEY_RIGHT:
2316f6046cfSSam Ravnborg 			if (hscroll >= MAX_LEN)
2326f6046cfSSam Ravnborg 				break;
2336f6046cfSSam Ravnborg 			hscroll++;
2346f6046cfSSam Ravnborg 			/* Reprint current page to scroll horizontally */
2356f6046cfSSam Ravnborg 			back_lines(page_length);
23695ac9b3bSBenjamin Poirier 			refresh_text_box(dialog, box, boxh, boxw, cur_y,
23795ac9b3bSBenjamin Poirier 					 cur_x, update_text, data);
2386f6046cfSSam Ravnborg 			break;
239f3cbcdc9SSam Ravnborg 		case KEY_ESC:
240537ddae7SBenjamin Poirier 			if (on_key_esc(dialog) == KEY_ESC)
241537ddae7SBenjamin Poirier 				done = true;
2426f6046cfSSam Ravnborg 			break;
243c8dc68adSSam Ravnborg 		case KEY_RESIZE:
244c8dc68adSSam Ravnborg 			back_lines(height);
245c8dc68adSSam Ravnborg 			delwin(box);
246c8dc68adSSam Ravnborg 			delwin(dialog);
247c8dc68adSSam Ravnborg 			on_key_resize();
248c8dc68adSSam Ravnborg 			goto do_resize;
249537ddae7SBenjamin Poirier 		default:
250537ddae7SBenjamin Poirier 			for (i = 0; keys[i]; i++) {
251537ddae7SBenjamin Poirier 				if (key == keys[i]) {
252537ddae7SBenjamin Poirier 					done = true;
253537ddae7SBenjamin Poirier 					break;
254537ddae7SBenjamin Poirier 				}
255537ddae7SBenjamin Poirier 			}
2566f6046cfSSam Ravnborg 		}
2576f6046cfSSam Ravnborg 	}
258c8dc68adSSam Ravnborg 	delwin(box);
2596f6046cfSSam Ravnborg 	delwin(dialog);
2601d1e2caeSBenjamin Poirier 	if (_vscroll) {
2611d1e2caeSBenjamin Poirier 		const char *s;
2621d1e2caeSBenjamin Poirier 
2631d1e2caeSBenjamin Poirier 		s = buf;
2641d1e2caeSBenjamin Poirier 		*_vscroll = 0;
2651d1e2caeSBenjamin Poirier 		back_lines(page_length);
2661d1e2caeSBenjamin Poirier 		while (s < page && (s = strchr(s, '\n'))) {
2671d1e2caeSBenjamin Poirier 			(*_vscroll)++;
2681d1e2caeSBenjamin Poirier 			s++;
2691d1e2caeSBenjamin Poirier 		}
2701d1e2caeSBenjamin Poirier 	}
2711d1e2caeSBenjamin Poirier 	if (_hscroll)
2721d1e2caeSBenjamin Poirier 		*_hscroll = hscroll;
273537ddae7SBenjamin Poirier 	return key;
2746f6046cfSSam Ravnborg }
2756f6046cfSSam Ravnborg 
2766f6046cfSSam Ravnborg /*
2772982de69SSam Ravnborg  * Go back 'n' lines in text. Called by dialog_textbox().
2786f6046cfSSam Ravnborg  * 'page' will be updated to point to the desired line in 'buf'.
2796f6046cfSSam Ravnborg  */
2806f6046cfSSam Ravnborg static void back_lines(int n)
2816f6046cfSSam Ravnborg {
2822982de69SSam Ravnborg 	int i;
2836f6046cfSSam Ravnborg 
2846f6046cfSSam Ravnborg 	begin_reached = 0;
2852982de69SSam Ravnborg 	/* Go back 'n' lines */
2862982de69SSam Ravnborg 	for (i = 0; i < n; i++) {
2872982de69SSam Ravnborg 		if (*page == '\0') {
2882982de69SSam Ravnborg 			if (end_reached) {
2892982de69SSam Ravnborg 				end_reached = 0;
2902982de69SSam Ravnborg 				continue;
2912982de69SSam Ravnborg 			}
2922982de69SSam Ravnborg 		}
2936f6046cfSSam Ravnborg 		if (page == buf) {
2946f6046cfSSam Ravnborg 			begin_reached = 1;
2956f6046cfSSam Ravnborg 			return;
2966f6046cfSSam Ravnborg 		}
2972982de69SSam Ravnborg 		page--;
2986f6046cfSSam Ravnborg 		do {
2996f6046cfSSam Ravnborg 			if (page == buf) {
3006f6046cfSSam Ravnborg 				begin_reached = 1;
3016f6046cfSSam Ravnborg 				return;
3026f6046cfSSam Ravnborg 			}
3032982de69SSam Ravnborg 			page--;
3042982de69SSam Ravnborg 		} while (*page != '\n');
3056f6046cfSSam Ravnborg 		page++;
3066f6046cfSSam Ravnborg 	}
3072982de69SSam Ravnborg }
3086f6046cfSSam Ravnborg 
3096f6046cfSSam Ravnborg /*
31095ac9b3bSBenjamin Poirier  * Print a new page of text.
3116f6046cfSSam Ravnborg  */
31295ac9b3bSBenjamin Poirier static void print_page(WINDOW *win, int height, int width, update_text_fn
31395ac9b3bSBenjamin Poirier 		       update_text, void *data)
3146f6046cfSSam Ravnborg {
3156f6046cfSSam Ravnborg 	int i, passed_end = 0;
3166f6046cfSSam Ravnborg 
31795ac9b3bSBenjamin Poirier 	if (update_text) {
31895ac9b3bSBenjamin Poirier 		char *end;
31995ac9b3bSBenjamin Poirier 
32095ac9b3bSBenjamin Poirier 		for (i = 0; i < height; i++)
32195ac9b3bSBenjamin Poirier 			get_line();
32295ac9b3bSBenjamin Poirier 		end = page;
32395ac9b3bSBenjamin Poirier 		back_lines(height);
32495ac9b3bSBenjamin Poirier 		update_text(buf, page - buf, end - buf, data);
32595ac9b3bSBenjamin Poirier 	}
32695ac9b3bSBenjamin Poirier 
3276f6046cfSSam Ravnborg 	page_length = 0;
3286f6046cfSSam Ravnborg 	for (i = 0; i < height; i++) {
3296f6046cfSSam Ravnborg 		print_line(win, i, width);
3306f6046cfSSam Ravnborg 		if (!passed_end)
3316f6046cfSSam Ravnborg 			page_length++;
3326f6046cfSSam Ravnborg 		if (end_reached && !passed_end)
3336f6046cfSSam Ravnborg 			passed_end = 1;
3346f6046cfSSam Ravnborg 	}
3356f6046cfSSam Ravnborg 	wnoutrefresh(win);
3366f6046cfSSam Ravnborg }
3376f6046cfSSam Ravnborg 
3386f6046cfSSam Ravnborg /*
33995ac9b3bSBenjamin Poirier  * Print a new line of text.
3406f6046cfSSam Ravnborg  */
3416f6046cfSSam Ravnborg static void print_line(WINDOW * win, int row, int width)
3426f6046cfSSam Ravnborg {
3436f6046cfSSam Ravnborg 	char *line;
3446f6046cfSSam Ravnborg 
3456f6046cfSSam Ravnborg 	line = get_line();
3466f6046cfSSam Ravnborg 	line += MIN(strlen(line), hscroll);	/* Scroll horizontally */
3476f6046cfSSam Ravnborg 	wmove(win, row, 0);	/* move cursor to correct line */
3486f6046cfSSam Ravnborg 	waddch(win, ' ');
3496f6046cfSSam Ravnborg 	waddnstr(win, line, MIN(strlen(line), width - 2));
3506f6046cfSSam Ravnborg 
3516f6046cfSSam Ravnborg 	/* Clear 'residue' of previous line */
3526f6046cfSSam Ravnborg #if OLD_NCURSES
3536f6046cfSSam Ravnborg 	{
354702a9450SLucas De Marchi 		int x = getcurx(win);
3556f6046cfSSam Ravnborg 		int i;
3566f6046cfSSam Ravnborg 		for (i = 0; i < width - x; i++)
3576f6046cfSSam Ravnborg 			waddch(win, ' ');
3586f6046cfSSam Ravnborg 	}
3596f6046cfSSam Ravnborg #else
3606f6046cfSSam Ravnborg 	wclrtoeol(win);
3616f6046cfSSam Ravnborg #endif
3626f6046cfSSam Ravnborg }
3636f6046cfSSam Ravnborg 
3646f6046cfSSam Ravnborg /*
3656f6046cfSSam Ravnborg  * Return current line of text. Called by dialog_textbox() and print_line().
3666f6046cfSSam Ravnborg  * 'page' should point to start of current line before calling, and will be
3676f6046cfSSam Ravnborg  * updated to point to start of next line.
3686f6046cfSSam Ravnborg  */
3696f6046cfSSam Ravnborg static char *get_line(void)
3706f6046cfSSam Ravnborg {
3712982de69SSam Ravnborg 	int i = 0;
3726f6046cfSSam Ravnborg 	static char line[MAX_LEN + 1];
3736f6046cfSSam Ravnborg 
3746f6046cfSSam Ravnborg 	end_reached = 0;
3756f6046cfSSam Ravnborg 	while (*page != '\n') {
3766f6046cfSSam Ravnborg 		if (*page == '\0') {
3776f6046cfSSam Ravnborg 			end_reached = 1;
3786f6046cfSSam Ravnborg 			break;
3796f6046cfSSam Ravnborg 		} else if (i < MAX_LEN)
3806f6046cfSSam Ravnborg 			line[i++] = *(page++);
3816f6046cfSSam Ravnborg 		else {
3826f6046cfSSam Ravnborg 			/* Truncate lines longer than MAX_LEN characters */
3836f6046cfSSam Ravnborg 			if (i == MAX_LEN)
3846f6046cfSSam Ravnborg 				line[i++] = '\0';
3856f6046cfSSam Ravnborg 			page++;
3866f6046cfSSam Ravnborg 		}
3876f6046cfSSam Ravnborg 	}
3886f6046cfSSam Ravnborg 	if (i <= MAX_LEN)
3896f6046cfSSam Ravnborg 		line[i] = '\0';
3906f6046cfSSam Ravnborg 	if (!end_reached)
391b9d29abdSBenjamin Poirier 		page++;		/* move past '\n' */
3926f6046cfSSam Ravnborg 
3936f6046cfSSam Ravnborg 	return line;
3946f6046cfSSam Ravnborg }
3956f6046cfSSam Ravnborg 
3966f6046cfSSam Ravnborg /*
3976f6046cfSSam Ravnborg  * Print current position
3986f6046cfSSam Ravnborg  */
399c8dc68adSSam Ravnborg static void print_position(WINDOW * win)
4006f6046cfSSam Ravnborg {
4012982de69SSam Ravnborg 	int percent;
4026f6046cfSSam Ravnborg 
40398e5a157SSam Ravnborg 	wattrset(win, dlg.position_indicator.atr);
40498e5a157SSam Ravnborg 	wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
4052982de69SSam Ravnborg 	percent = (page - buf) * 100 / strlen(buf);
406c8dc68adSSam Ravnborg 	wmove(win, getmaxy(win) - 3, getmaxx(win) - 9);
4076f6046cfSSam Ravnborg 	wprintw(win, "(%3d%%)", percent);
4086f6046cfSSam Ravnborg }
409