kconfig: fixup after Lindent
[linux-2.6.git] / scripts / lxdialog / textbox.c
blobfa8d92ea02b6efbc1b7980f38a46534f4f477caa
1 /*
2 * textbox.c -- implements the text box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
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.
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.
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.
22 #include "dialog.h"
24 static void back_lines(int n);
25 static void print_page(WINDOW * win, int height, int width);
26 static void print_line(WINDOW * win, int row, int width);
27 static char *get_line(void);
28 static void print_position(WINDOW * win, int height, int width);
30 static int hscroll, fd, file_size, bytes_read;
31 static int begin_reached = 1, end_reached, page_length;
32 static char *buf, *page;
35 * Display text from a file in a dialog box.
37 int dialog_textbox(const char *title, const char *file, int height, int width)
39 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;
44 search_term[0] = '\0'; /* no search term entered yet */
46 /* Open input file for reading */
47 if ((fd = open(file, O_RDONLY)) == -1) {
48 endwin();
49 fprintf(stderr, "\nCan't open input file in dialog_textbox().\n");
50 exit(-1);
52 /* Get file size. Actually, 'file_size' is the real file size - 1,
53 since it's only the last byte offset from the beginning */
54 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
55 endwin();
56 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
57 exit(-1);
59 /* Restore file pointer to beginning of file after getting file size */
60 if (lseek(fd, 0, SEEK_SET) == -1) {
61 endwin();
62 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
63 exit(-1);
65 /* Allocate space for read buffer */
66 if ((buf = malloc(BUF_SIZE + 1)) == NULL) {
67 endwin();
68 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
69 exit(-1);
71 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
72 endwin();
73 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
74 exit(-1);
76 buf[bytes_read] = '\0'; /* mark end of valid data */
77 page = buf; /* page is pointer to start of page to be displayed */
79 /* center dialog box on screen */
80 x = (COLS - width) / 2;
81 y = (LINES - height) / 2;
83 draw_shadow(stdscr, y, x, height, width);
85 dialog = newwin(height, width, y, x);
86 keypad(dialog, TRUE);
88 /* Create window for text region, used for scrolling text */
89 text = subwin(dialog, height - 4, width - 2, y + 1, x + 1);
90 wattrset(text, dialog_attr);
91 wbkgdset(text, dialog_attr & A_COLOR);
93 keypad(text, TRUE);
95 /* register the new window, along with its borders */
96 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
98 wattrset(dialog, border_attr);
99 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
100 for (i = 0; i < width - 2; i++)
101 waddch(dialog, ACS_HLINE);
102 wattrset(dialog, dialog_attr);
103 wbkgdset(dialog, dialog_attr & A_COLOR);
104 waddch(dialog, ACS_RTEE);
106 if (title != NULL && strlen(title) >= width - 2) {
107 /* truncate long title -- mec */
108 char *title2 = malloc(width - 2 + 1);
109 memcpy(title2, title, width - 2);
110 title2[width - 2] = '\0';
111 title = title2;
114 if (title != NULL) {
115 wattrset(dialog, title_attr);
116 mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
117 waddstr(dialog, (char *)title);
118 waddch(dialog, ' ');
120 print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
121 wnoutrefresh(dialog);
122 getyx(dialog, cur_y, cur_x); /* Save cursor position */
124 /* Print first page of text */
125 attr_clear(text, height - 4, width - 2, dialog_attr);
126 print_page(text, height - 4, width - 2);
127 print_position(dialog, height, width);
128 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
129 wrefresh(dialog);
131 while ((key != ESC) && (key != '\n')) {
132 key = wgetch(dialog);
133 switch (key) {
134 case 'E': /* Exit */
135 case 'e':
136 case 'X':
137 case 'x':
138 delwin(dialog);
139 free(buf);
140 close(fd);
141 return 0;
142 case 'g': /* First page */
143 case KEY_HOME:
144 if (!begin_reached) {
145 begin_reached = 1;
146 /* First page not in buffer? */
147 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
148 endwin();
149 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
150 exit(-1);
152 if (fpos > bytes_read) { /* Yes, we have to read it in */
153 if (lseek(fd, 0, SEEK_SET) == -1) {
154 endwin();
155 fprintf(stderr, "\nError moving file pointer in "
156 "dialog_textbox().\n");
157 exit(-1);
159 if ((bytes_read =
160 read(fd, buf, BUF_SIZE)) == -1) {
161 endwin();
162 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
163 exit(-1);
165 buf[bytes_read] = '\0';
167 page = buf;
168 print_page(text, height - 4, width - 2);
169 print_position(dialog, height, width);
170 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
171 wrefresh(dialog);
173 break;
174 case 'G': /* Last page */
175 case KEY_END:
177 end_reached = 1;
178 /* Last page not in buffer? */
179 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
180 endwin();
181 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
182 exit(-1);
184 if (fpos < file_size) { /* Yes, we have to read it in */
185 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
186 endwin();
187 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
188 exit(-1);
190 if ((bytes_read =
191 read(fd, buf, BUF_SIZE)) == -1) {
192 endwin();
193 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
194 exit(-1);
196 buf[bytes_read] = '\0';
198 page = buf + bytes_read;
199 back_lines(height - 4);
200 print_page(text, height - 4, width - 2);
201 print_position(dialog, height, width);
202 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
203 wrefresh(dialog);
204 break;
205 case 'K': /* Previous line */
206 case 'k':
207 case KEY_UP:
208 if (!begin_reached) {
209 back_lines(page_length + 1);
211 /* We don't call print_page() here but use scrolling to ensure
212 faster screen update. However, 'end_reached' and
213 'page_length' should still be updated, and 'page' should
214 point to start of next page. This is done by calling
215 get_line() in the following 'for' loop. */
216 scrollok(text, TRUE);
217 wscrl(text, -1); /* Scroll text region down one line */
218 scrollok(text, FALSE);
219 page_length = 0;
220 passed_end = 0;
221 for (i = 0; i < height - 4; i++) {
222 if (!i) {
223 /* print first line of page */
224 print_line(text, 0, width - 2);
225 wnoutrefresh(text);
226 } else
227 /* Called to update 'end_reached' and 'page' */
228 get_line();
229 if (!passed_end)
230 page_length++;
231 if (end_reached && !passed_end)
232 passed_end = 1;
235 print_position(dialog, height, width);
236 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
237 wrefresh(dialog);
239 break;
240 case 'B': /* Previous page */
241 case 'b':
242 case KEY_PPAGE:
243 if (begin_reached)
244 break;
245 back_lines(page_length + height - 4);
246 print_page(text, height - 4, width - 2);
247 print_position(dialog, height, width);
248 wmove(dialog, cur_y, cur_x);
249 wrefresh(dialog);
250 break;
251 case 'J': /* Next line */
252 case 'j':
253 case KEY_DOWN:
254 if (!end_reached) {
255 begin_reached = 0;
256 scrollok(text, TRUE);
257 scroll(text); /* Scroll text region up one line */
258 scrollok(text, FALSE);
259 print_line(text, height - 5, width - 2);
260 wnoutrefresh(text);
261 print_position(dialog, height, width);
262 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
263 wrefresh(dialog);
265 break;
266 case KEY_NPAGE: /* Next page */
267 case ' ':
268 if (end_reached)
269 break;
271 begin_reached = 0;
272 print_page(text, height - 4, width - 2);
273 print_position(dialog, height, width);
274 wmove(dialog, cur_y, cur_x);
275 wrefresh(dialog);
276 break;
277 case '0': /* Beginning of line */
278 case 'H': /* Scroll left */
279 case 'h':
280 case KEY_LEFT:
281 if (hscroll <= 0)
282 break;
284 if (key == '0')
285 hscroll = 0;
286 else
287 hscroll--;
288 /* Reprint current page to scroll horizontally */
289 back_lines(page_length);
290 print_page(text, height - 4, width - 2);
291 wmove(dialog, cur_y, cur_x);
292 wrefresh(dialog);
293 break;
294 case 'L': /* Scroll right */
295 case 'l':
296 case KEY_RIGHT:
297 if (hscroll >= MAX_LEN)
298 break;
299 hscroll++;
300 /* Reprint current page to scroll horizontally */
301 back_lines(page_length);
302 print_page(text, height - 4, width - 2);
303 wmove(dialog, cur_y, cur_x);
304 wrefresh(dialog);
305 break;
306 case ESC:
307 break;
311 delwin(dialog);
312 free(buf);
313 close(fd);
314 return -1; /* ESC pressed */
318 * Go back 'n' lines in text file. Called by dialog_textbox().
319 * 'page' will be updated to point to the desired line in 'buf'.
321 static void back_lines(int n)
323 int i, fpos;
325 begin_reached = 0;
326 /* We have to distinguish between end_reached and !end_reached
327 since at end of file, the line is not ended by a '\n'.
328 The code inside 'if' basically does a '--page' to move one
329 character backward so as to skip '\n' of the previous line */
330 if (!end_reached) {
331 /* Either beginning of buffer or beginning of file reached? */
332 if (page == buf) {
333 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
334 endwin();
335 fprintf(stderr, "\nError moving file pointer in "
336 "back_lines().\n");
337 exit(-1);
339 if (fpos > bytes_read) { /* Not beginning of file yet */
340 /* We've reached beginning of buffer, but not beginning of
341 file yet, so read previous part of file into buffer.
342 Note that we only move backward for BUF_SIZE/2 bytes,
343 but not BUF_SIZE bytes to avoid re-reading again in
344 print_page() later */
345 /* Really possible to move backward BUF_SIZE/2 bytes? */
346 if (fpos < BUF_SIZE / 2 + bytes_read) {
347 /* No, move less then */
348 if (lseek(fd, 0, SEEK_SET) == -1) {
349 endwin();
350 fprintf(stderr, "\nError moving file pointer in "
351 "back_lines().\n");
352 exit(-1);
354 page = buf + fpos - bytes_read;
355 } else { /* Move backward BUF_SIZE/2 bytes */
356 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
357 endwin();
358 fprintf(stderr, "\nError moving file pointer "
359 "in back_lines().\n");
360 exit(-1);
362 page = buf + BUF_SIZE / 2;
364 if ((bytes_read =
365 read(fd, buf, BUF_SIZE)) == -1) {
366 endwin();
367 fprintf(stderr, "\nError reading file in back_lines().\n");
368 exit(-1);
370 buf[bytes_read] = '\0';
371 } else { /* Beginning of file reached */
372 begin_reached = 1;
373 return;
376 if (*(--page) != '\n') { /* '--page' here */
377 /* Something's wrong... */
378 endwin();
379 fprintf(stderr, "\nInternal error in back_lines().\n");
380 exit(-1);
383 /* Go back 'n' lines */
384 for (i = 0; i < n; i++)
385 do {
386 if (page == buf) {
387 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
388 endwin();
389 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
390 exit(-1);
392 if (fpos > bytes_read) {
393 /* Really possible to move backward BUF_SIZE/2 bytes? */
394 if (fpos < BUF_SIZE / 2 + bytes_read) {
395 /* No, move less then */
396 if (lseek(fd, 0, SEEK_SET) == -1) {
397 endwin();
398 fprintf(stderr, "\nError moving file pointer "
399 "in back_lines().\n");
400 exit(-1);
402 page = buf + fpos - bytes_read;
403 } else { /* Move backward BUF_SIZE/2 bytes */
404 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
405 endwin();
406 fprintf(stderr, "\nError moving file pointer"
407 " in back_lines().\n");
408 exit(-1);
410 page = buf + BUF_SIZE / 2;
412 if ((bytes_read =
413 read(fd, buf, BUF_SIZE)) == -1) {
414 endwin();
415 fprintf(stderr, "\nError reading file in "
416 "back_lines().\n");
417 exit(-1);
419 buf[bytes_read] = '\0';
420 } else { /* Beginning of file reached */
421 begin_reached = 1;
422 return;
425 } while (*(--page) != '\n');
426 page++;
430 * Print a new page of text. Called by dialog_textbox().
432 static void print_page(WINDOW * win, int height, int width)
434 int i, passed_end = 0;
436 page_length = 0;
437 for (i = 0; i < height; i++) {
438 print_line(win, i, width);
439 if (!passed_end)
440 page_length++;
441 if (end_reached && !passed_end)
442 passed_end = 1;
444 wnoutrefresh(win);
448 * Print a new line of text. Called by dialog_textbox() and print_page().
450 static void print_line(WINDOW * win, int row, int width)
452 int y, x;
453 char *line;
455 line = get_line();
456 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
457 wmove(win, row, 0); /* move cursor to correct line */
458 waddch(win, ' ');
459 waddnstr(win, line, MIN(strlen(line), width - 2));
461 getyx(win, y, x);
462 /* Clear 'residue' of previous line */
463 #if OLD_NCURSES
465 int i;
466 for (i = 0; i < width - x; i++)
467 waddch(win, ' ');
469 #else
470 wclrtoeol(win);
471 #endif
475 * Return current line of text. Called by dialog_textbox() and print_line().
476 * 'page' should point to start of current line before calling, and will be
477 * updated to point to start of next line.
479 static char *get_line(void)
481 int i = 0, fpos;
482 static char line[MAX_LEN + 1];
484 end_reached = 0;
485 while (*page != '\n') {
486 if (*page == '\0') {
487 /* Either end of file or end of buffer reached */
488 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
489 endwin();
490 fprintf(stderr, "\nError moving file pointer in "
491 "get_line().\n");
492 exit(-1);
494 if (fpos < file_size) { /* Not end of file yet */
495 /* We've reached end of buffer, but not end of file yet,
496 so read next part of file into buffer */
497 if ((bytes_read =
498 read(fd, buf, BUF_SIZE)) == -1) {
499 endwin();
500 fprintf(stderr, "\nError reading file in get_line().\n");
501 exit(-1);
503 buf[bytes_read] = '\0';
504 page = buf;
505 } else {
506 if (!end_reached)
507 end_reached = 1;
508 break;
510 } else if (i < MAX_LEN)
511 line[i++] = *(page++);
512 else {
513 /* Truncate lines longer than MAX_LEN characters */
514 if (i == MAX_LEN)
515 line[i++] = '\0';
516 page++;
519 if (i <= MAX_LEN)
520 line[i] = '\0';
521 if (!end_reached)
522 page++; /* move pass '\n' */
524 return line;
528 * Print current position
530 static void print_position(WINDOW * win, int height, int width)
532 int fpos, percent;
534 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
535 endwin();
536 fprintf(stderr, "\nError moving file pointer in print_position().\n");
537 exit(-1);
539 wattrset(win, position_indicator_attr);
540 wbkgdset(win, position_indicator_attr & A_COLOR);
541 percent = !file_size ?
542 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
543 wmove(win, height - 3, width - 9);
544 wprintw(win, "(%3d%%)", percent);