blob: d6e7f2afe31a5c43e7701aa7a0ef4801b53699fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * textbox.c -- implements the text box
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "dialog.h"
23
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010024static void back_lines(int n);
25static void print_page(WINDOW * win, int height, int width);
26static void print_line(WINDOW * win, int row, int width);
27static char *get_line(void);
28static void print_position(WINDOW * win, int height, int width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static int hscroll, fd, file_size, bytes_read;
31static int begin_reached = 1, end_reached, page_length;
32static char *buf, *page;
33
34/*
35 * Display text from a file in a dialog box.
36 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037int dialog_textbox(const char *title, const char *file, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010039 int i, x, y, cur_x, cur_y, fpos, key = 0;
40 int passed_end;
41 char search_term[MAX_LEN + 1];
42 WINDOW *dialog, *text;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010044 search_term[0] = '\0'; /* no search term entered yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010046 /* Open input file for reading */
47 if ((fd = open(file, O_RDONLY)) == -1) {
48 endwin();
49 fprintf(stderr,
50 "\nCan't open input file in dialog_textbox().\n");
51 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010053 /* Get file size. Actually, 'file_size' is the real file size - 1,
54 since it's only the last byte offset from the beginning */
55 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
56 endwin();
57 fprintf(stderr,
58 "\nError getting file size in dialog_textbox().\n");
59 exit(-1);
60 }
61 /* Restore file pointer to beginning of file after getting file size */
62 if (lseek(fd, 0, SEEK_SET) == -1) {
63 endwin();
64 fprintf(stderr,
65 "\nError moving file pointer in dialog_textbox().\n");
66 exit(-1);
67 }
68 /* Allocate space for read buffer */
69 if ((buf = malloc(BUF_SIZE + 1)) == NULL) {
70 endwin();
71 fprintf(stderr,
72 "\nCan't allocate memory in dialog_textbox().\n");
73 exit(-1);
74 }
75 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
76 endwin();
77 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
78 exit(-1);
79 }
80 buf[bytes_read] = '\0'; /* mark end of valid data */
81 page = buf; /* page is pointer to start of page to be displayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010083 /* center dialog box on screen */
84 x = (COLS - width) / 2;
85 y = (LINES - height) / 2;
86
87 draw_shadow(stdscr, y, x, height, width);
88
89 dialog = newwin(height, width, y, x);
90 keypad(dialog, TRUE);
91
92 /* Create window for text region, used for scrolling text */
93 text = subwin(dialog, height - 4, width - 2, y + 1, x + 1);
94 wattrset(text, dialog_attr);
95 wbkgdset(text, dialog_attr & A_COLOR);
96
97 keypad(text, TRUE);
98
99 /* register the new window, along with its borders */
100 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
101
102 wattrset(dialog, border_attr);
103 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
104 for (i = 0; i < width - 2; i++)
105 waddch(dialog, ACS_HLINE);
106 wattrset(dialog, dialog_attr);
107 wbkgdset(dialog, dialog_attr & A_COLOR);
108 waddch(dialog, ACS_RTEE);
109
110 if (title != NULL && strlen(title) >= width - 2) {
111 /* truncate long title -- mec */
112 char *title2 = malloc(width - 2 + 1);
113 memcpy(title2, title, width - 2);
114 title2[width - 2] = '\0';
115 title = title2;
116 }
117
118 if (title != NULL) {
119 wattrset(dialog, title_attr);
120 mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
121 waddstr(dialog, (char *)title);
122 waddch(dialog, ' ');
123 }
124 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
125 wnoutrefresh(dialog);
126 getyx(dialog, cur_y, cur_x); /* Save cursor position */
127
128 /* Print first page of text */
129 attr_clear(text, height - 4, width - 2, dialog_attr);
130 print_page(text, height - 4, width - 2);
131 print_position(dialog, height, width);
132 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
133 wrefresh(dialog);
134
135 while ((key != ESC) && (key != '\n')) {
136 key = wgetch(dialog);
137 switch (key) {
138 case 'E': /* Exit */
139 case 'e':
140 case 'X':
141 case 'x':
142 delwin(dialog);
143 free(buf);
144 close(fd);
145 return 0;
146 case 'g': /* First page */
147 case KEY_HOME:
148 if (!begin_reached) {
149 begin_reached = 1;
150 /* First page not in buffer? */
151 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
152 endwin();
153 fprintf(stderr,
154 "\nError moving file pointer in dialog_textbox().\n");
155 exit(-1);
156 }
157 if (fpos > bytes_read) { /* Yes, we have to read it in */
158 if (lseek(fd, 0, SEEK_SET) == -1) {
159 endwin();
160 fprintf(stderr,
161 "\nError moving file pointer in "
162 "dialog_textbox().\n");
163 exit(-1);
164 }
165 if ((bytes_read =
166 read(fd, buf, BUF_SIZE)) == -1) {
167 endwin();
168 fprintf(stderr,
169 "\nError reading file in dialog_textbox().\n");
170 exit(-1);
171 }
172 buf[bytes_read] = '\0';
173 }
174 page = buf;
175 print_page(text, height - 4, width - 2);
176 print_position(dialog, height, width);
177 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
178 wrefresh(dialog);
179 }
180 break;
181 case 'G': /* Last page */
182 case KEY_END:
183
184 end_reached = 1;
185 /* Last page not in buffer? */
186 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
187 endwin();
188 fprintf(stderr,
189 "\nError moving file pointer in dialog_textbox().\n");
190 exit(-1);
191 }
192 if (fpos < file_size) { /* Yes, we have to read it in */
193 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
194 endwin();
195 fprintf(stderr,
196 "\nError moving file pointer in dialog_textbox().\n");
197 exit(-1);
198 }
199 if ((bytes_read =
200 read(fd, buf, BUF_SIZE)) == -1) {
201 endwin();
202 fprintf(stderr,
203 "\nError reading file in dialog_textbox().\n");
204 exit(-1);
205 }
206 buf[bytes_read] = '\0';
207 }
208 page = buf + bytes_read;
209 back_lines(height - 4);
210 print_page(text, height - 4, width - 2);
211 print_position(dialog, height, width);
212 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
213 wrefresh(dialog);
214 break;
215 case 'K': /* Previous line */
216 case 'k':
217 case KEY_UP:
218 if (!begin_reached) {
219 back_lines(page_length + 1);
220
221 /* We don't call print_page() here but use scrolling to ensure
222 faster screen update. However, 'end_reached' and
223 'page_length' should still be updated, and 'page' should
224 point to start of next page. This is done by calling
225 get_line() in the following 'for' loop. */
226 scrollok(text, TRUE);
227 wscrl(text, -1); /* Scroll text region down one line */
228 scrollok(text, FALSE);
229 page_length = 0;
230 passed_end = 0;
231 for (i = 0; i < height - 4; i++) {
232 if (!i) {
233 /* print first line of page */
234 print_line(text, 0, width - 2);
235 wnoutrefresh(text);
236 } else
237 /* Called to update 'end_reached' and 'page' */
238 get_line();
239 if (!passed_end)
240 page_length++;
241 if (end_reached && !passed_end)
242 passed_end = 1;
243 }
244
245 print_position(dialog, height, width);
246 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
247 wrefresh(dialog);
248 }
249 break;
250 case 'B': /* Previous page */
251 case 'b':
252 case KEY_PPAGE:
253 if (begin_reached)
254 break;
255 back_lines(page_length + height - 4);
256 print_page(text, height - 4, width - 2);
257 print_position(dialog, height, width);
258 wmove(dialog, cur_y, cur_x);
259 wrefresh(dialog);
260 break;
261 case 'J': /* Next line */
262 case 'j':
263 case KEY_DOWN:
264 if (!end_reached) {
265 begin_reached = 0;
266 scrollok(text, TRUE);
267 scroll(text); /* Scroll text region up one line */
268 scrollok(text, FALSE);
269 print_line(text, height - 5, width - 2);
270 wnoutrefresh(text);
271 print_position(dialog, height, width);
272 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
273 wrefresh(dialog);
274 }
275 break;
276 case KEY_NPAGE: /* Next page */
277 case ' ':
278 if (end_reached)
279 break;
280
281 begin_reached = 0;
282 print_page(text, height - 4, width - 2);
283 print_position(dialog, height, width);
284 wmove(dialog, cur_y, cur_x);
285 wrefresh(dialog);
286 break;
287 case '0': /* Beginning of line */
288 case 'H': /* Scroll left */
289 case 'h':
290 case KEY_LEFT:
291 if (hscroll <= 0)
292 break;
293
294 if (key == '0')
295 hscroll = 0;
296 else
297 hscroll--;
298 /* Reprint current page to scroll horizontally */
299 back_lines(page_length);
300 print_page(text, height - 4, width - 2);
301 wmove(dialog, cur_y, cur_x);
302 wrefresh(dialog);
303 break;
304 case 'L': /* Scroll right */
305 case 'l':
306 case KEY_RIGHT:
307 if (hscroll >= MAX_LEN)
308 break;
309 hscroll++;
310 /* Reprint current page to scroll horizontally */
311 back_lines(page_length);
312 print_page(text, height - 4, width - 2);
313 wmove(dialog, cur_y, cur_x);
314 wrefresh(dialog);
315 break;
316 case ESC:
317 break;
318 }
319 }
320
321 delwin(dialog);
322 free(buf);
323 close(fd);
324 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/*
328 * Go back 'n' lines in text file. Called by dialog_textbox().
329 * 'page' will be updated to point to the desired line in 'buf'.
330 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100331static void back_lines(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100333 int i, fpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100335 begin_reached = 0;
336 /* We have to distinguish between end_reached and !end_reached
337 since at end of file, the line is not ended by a '\n'.
338 The code inside 'if' basically does a '--page' to move one
339 character backward so as to skip '\n' of the previous line */
340 if (!end_reached) {
341 /* Either beginning of buffer or beginning of file reached? */
342 if (page == buf) {
343 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
344 endwin();
345 fprintf(stderr,
346 "\nError moving file pointer in "
347 "back_lines().\n");
348 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100350 if (fpos > bytes_read) { /* Not beginning of file yet */
351 /* We've reached beginning of buffer, but not beginning of
352 file yet, so read previous part of file into buffer.
353 Note that we only move backward for BUF_SIZE/2 bytes,
354 but not BUF_SIZE bytes to avoid re-reading again in
355 print_page() later */
356 /* Really possible to move backward BUF_SIZE/2 bytes? */
357 if (fpos < BUF_SIZE / 2 + bytes_read) {
358 /* No, move less then */
359 if (lseek(fd, 0, SEEK_SET) == -1) {
360 endwin();
361 fprintf(stderr,
362 "\nError moving file pointer in "
363 "back_lines().\n");
364 exit(-1);
365 }
366 page = buf + fpos - bytes_read;
367 } else { /* Move backward BUF_SIZE/2 bytes */
368 if (lseek
369 (fd, -(BUF_SIZE / 2 + bytes_read),
370 SEEK_CUR)
371 == -1) {
372 endwin();
373 fprintf(stderr,
374 "\nError moving file pointer "
375 "in back_lines().\n");
376 exit(-1);
377 }
378 page = buf + BUF_SIZE / 2;
379 }
380 if ((bytes_read =
381 read(fd, buf, BUF_SIZE)) == -1) {
382 endwin();
383 fprintf(stderr,
384 "\nError reading file in back_lines().\n");
385 exit(-1);
386 }
387 buf[bytes_read] = '\0';
388 } else { /* Beginning of file reached */
389 begin_reached = 1;
390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100393 if (*(--page) != '\n') { /* '--page' here */
394 /* Something's wrong... */
395 endwin();
396 fprintf(stderr, "\nInternal error in back_lines().\n");
397 exit(-1);
398 }
399 }
400 /* Go back 'n' lines */
401 for (i = 0; i < n; i++)
402 do {
403 if (page == buf) {
404 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
405 endwin();
406 fprintf(stderr,
407 "\nError moving file pointer in back_lines().\n");
408 exit(-1);
409 }
410 if (fpos > bytes_read) {
411 /* Really possible to move backward BUF_SIZE/2 bytes? */
412 if (fpos < BUF_SIZE / 2 + bytes_read) {
413 /* No, move less then */
414 if (lseek(fd, 0, SEEK_SET) ==
415 -1) {
416 endwin();
417 fprintf(stderr,
418 "\nError moving file pointer "
419 "in back_lines().\n");
420 exit(-1);
421 }
422 page = buf + fpos - bytes_read;
423 } else { /* Move backward BUF_SIZE/2 bytes */
424 if (lseek
425 (fd,
426 -(BUF_SIZE / 2 +
427 bytes_read),
428 SEEK_CUR) == -1) {
429 endwin();
430 fprintf(stderr,
431 "\nError moving file pointer"
432 " in back_lines().\n");
433 exit(-1);
434 }
435 page = buf + BUF_SIZE / 2;
436 }
437 if ((bytes_read =
438 read(fd, buf, BUF_SIZE)) == -1) {
439 endwin();
440 fprintf(stderr,
441 "\nError reading file in "
442 "back_lines().\n");
443 exit(-1);
444 }
445 buf[bytes_read] = '\0';
446 } else { /* Beginning of file reached */
447 begin_reached = 1;
448 return;
449 }
450 }
451 } while (*(--page) != '\n');
452 page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/*
456 * Print a new page of text. Called by dialog_textbox().
457 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100458static void print_page(WINDOW * win, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100460 int i, passed_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100462 page_length = 0;
463 for (i = 0; i < height; i++) {
464 print_line(win, i, width);
465 if (!passed_end)
466 page_length++;
467 if (end_reached && !passed_end)
468 passed_end = 1;
469 }
470 wnoutrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/*
474 * Print a new line of text. Called by dialog_textbox() and print_page().
475 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100476static void print_line(WINDOW * win, int row, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100478 int y, x;
479 char *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100481 line = get_line();
482 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
483 wmove(win, row, 0); /* move cursor to correct line */
484 waddch(win, ' ');
485 waddnstr(win, line, MIN(strlen(line), width - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100487 getyx(win, y, x);
488 /* Clear 'residue' of previous line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#if OLD_NCURSES
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100490 {
491 int i;
492 for (i = 0; i < width - x; i++)
493 waddch(win, ' ');
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#else
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100496 wclrtoeol(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#endif
498}
499
500/*
501 * Return current line of text. Called by dialog_textbox() and print_line().
502 * 'page' should point to start of current line before calling, and will be
503 * updated to point to start of next line.
504 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100505static char *get_line(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100507 int i = 0, fpos;
508 static char line[MAX_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100510 end_reached = 0;
511 while (*page != '\n') {
512 if (*page == '\0') {
513 /* Either end of file or end of buffer reached */
514 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
515 endwin();
516 fprintf(stderr,
517 "\nError moving file pointer in "
518 "get_line().\n");
519 exit(-1);
520 }
521 if (fpos < file_size) { /* Not end of file yet */
522 /* We've reached end of buffer, but not end of file yet,
523 so read next part of file into buffer */
524 if ((bytes_read =
525 read(fd, buf, BUF_SIZE)) == -1) {
526 endwin();
527 fprintf(stderr,
528 "\nError reading file in get_line().\n");
529 exit(-1);
530 }
531 buf[bytes_read] = '\0';
532 page = buf;
533 } else {
534 if (!end_reached)
535 end_reached = 1;
536 break;
537 }
538 } else if (i < MAX_LEN)
539 line[i++] = *(page++);
540 else {
541 /* Truncate lines longer than MAX_LEN characters */
542 if (i == MAX_LEN)
543 line[i++] = '\0';
544 page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100547 if (i <= MAX_LEN)
548 line[i] = '\0';
549 if (!end_reached)
550 page++; /* move pass '\n' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100552 return line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
556 * Print current position
557 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100558static void print_position(WINDOW * win, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100560 int fpos, percent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100562 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
563 endwin();
564 fprintf(stderr,
565 "\nError moving file pointer in print_position().\n");
566 exit(-1);
567 }
568 wattrset(win, position_indicator_attr);
569 wbkgdset(win, position_indicator_attr & A_COLOR);
570 percent = !file_size ?
571 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
572 wmove(win, height - 3, width - 9);
573 wprintw(win, "(%3d%%)", percent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}