1 | /*
|
---|
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 |
|
---|
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);
|
---|
29 |
|
---|
30 | static int hscroll, fd, file_size, bytes_read;
|
---|
31 | static int begin_reached = 1, end_reached, page_length;
|
---|
32 | static char *buf, *page;
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Display text from a file in a dialog box.
|
---|
36 | */
|
---|
37 | int
|
---|
38 | dialog_textbox (const char *title, const char *file, int height, int width)
|
---|
39 | {
|
---|
40 | int i, x, y, cur_x, cur_y, fpos, key = 0;
|
---|
41 | int passed_end;
|
---|
42 | WINDOW *dialog, *text;
|
---|
43 |
|
---|
44 | /* Open input file for reading */
|
---|
45 | if ((fd = open (file, O_RDONLY)) == -1) {
|
---|
46 | endwin ();
|
---|
47 | fprintf (stderr,
|
---|
48 | "\nCan't open input file in dialog_textbox().\n");
|
---|
49 | exit (-1);
|
---|
50 | }
|
---|
51 | /* Get file size. Actually, 'file_size' is the real file size - 1,
|
---|
52 | since it's only the last byte offset from the beginning */
|
---|
53 | if ((file_size = lseek (fd, 0, SEEK_END)) == -1) {
|
---|
54 | endwin ();
|
---|
55 | fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
|
---|
56 | exit (-1);
|
---|
57 | }
|
---|
58 | /* Restore file pointer to beginning of file after getting file size */
|
---|
59 | if (lseek (fd, 0, SEEK_SET) == -1) {
|
---|
60 | endwin ();
|
---|
61 | fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
|
---|
62 | exit (-1);
|
---|
63 | }
|
---|
64 | /* Allocate space for read buffer */
|
---|
65 | if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
|
---|
66 | endwin ();
|
---|
67 | fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
|
---|
68 | exit (-1);
|
---|
69 | }
|
---|
70 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
71 | endwin ();
|
---|
72 | fprintf (stderr, "\nError reading file in dialog_textbox().\n");
|
---|
73 | exit (-1);
|
---|
74 | }
|
---|
75 | buf[bytes_read] = '\0'; /* mark end of valid data */
|
---|
76 | page = buf; /* page is pointer to start of page to be displayed */
|
---|
77 |
|
---|
78 | /* center dialog box on screen */
|
---|
79 | x = (COLS - width) / 2;
|
---|
80 | y = (LINES - height) / 2;
|
---|
81 |
|
---|
82 |
|
---|
83 | draw_shadow (stdscr, y, x, height, width);
|
---|
84 |
|
---|
85 | dialog = newwin (height, width, y, x);
|
---|
86 | keypad (dialog, TRUE);
|
---|
87 |
|
---|
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);
|
---|
92 |
|
---|
93 | keypad (text, TRUE);
|
---|
94 |
|
---|
95 | /* register the new window, along with its borders */
|
---|
96 | draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
|
---|
97 |
|
---|
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);
|
---|
105 |
|
---|
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;
|
---|
112 | }
|
---|
113 |
|
---|
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, ' ');
|
---|
119 | }
|
---|
120 | wbkgdset (dialog, 0);
|
---|
121 | print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
|
---|
122 | wbkgdset (dialog, dialog_attr & A_COLOR);
|
---|
123 | wnoutrefresh (dialog);
|
---|
124 | getyx (dialog, cur_y, cur_x); /* Save cursor position */
|
---|
125 |
|
---|
126 | /* Print first page of text */
|
---|
127 | attr_clear (text, height - 4, width - 2, dialog_attr);
|
---|
128 | print_page (text, height - 4, width - 2);
|
---|
129 | print_position (dialog, height, width);
|
---|
130 | wmove (dialog, cur_y, cur_x); /* Restore cursor position */
|
---|
131 | wrefresh (dialog);
|
---|
132 |
|
---|
133 | while ((key != ESC) && (key != '\n')) {
|
---|
134 | key = wgetch (dialog);
|
---|
135 | switch (key) {
|
---|
136 | case 'E': /* Exit */
|
---|
137 | case 'e':
|
---|
138 | case 'X':
|
---|
139 | case 'x':
|
---|
140 | delwin (dialog);
|
---|
141 | free (buf);
|
---|
142 | close (fd);
|
---|
143 | return 0;
|
---|
144 | case 'g': /* First page */
|
---|
145 | case KEY_HOME:
|
---|
146 | if (!begin_reached) {
|
---|
147 | begin_reached = 1;
|
---|
148 | /* First page not in buffer? */
|
---|
149 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
150 | endwin ();
|
---|
151 | fprintf (stderr,
|
---|
152 | "\nError moving file pointer in dialog_textbox().\n");
|
---|
153 | exit (-1);
|
---|
154 | }
|
---|
155 | if (fpos > bytes_read) { /* Yes, we have to read it in */
|
---|
156 | if (lseek (fd, 0, SEEK_SET) == -1) {
|
---|
157 | endwin ();
|
---|
158 | fprintf (stderr, "\nError moving file pointer in "
|
---|
159 | "dialog_textbox().\n");
|
---|
160 | exit (-1);
|
---|
161 | }
|
---|
162 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
163 | endwin ();
|
---|
164 | fprintf (stderr,
|
---|
165 | "\nError reading file in dialog_textbox().\n");
|
---|
166 | exit (-1);
|
---|
167 | }
|
---|
168 | buf[bytes_read] = '\0';
|
---|
169 | }
|
---|
170 | page = buf;
|
---|
171 | print_page (text, height - 4, width - 2);
|
---|
172 | print_position (dialog, height, width);
|
---|
173 | wmove (dialog, cur_y, cur_x); /* Restore cursor position */
|
---|
174 | wrefresh (dialog);
|
---|
175 | }
|
---|
176 | break;
|
---|
177 | case 'G': /* Last page */
|
---|
178 | case KEY_END:
|
---|
179 |
|
---|
180 | end_reached = 1;
|
---|
181 | /* Last page not in buffer? */
|
---|
182 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
183 | endwin ();
|
---|
184 | fprintf (stderr,
|
---|
185 | "\nError moving file pointer in dialog_textbox().\n");
|
---|
186 | exit (-1);
|
---|
187 | }
|
---|
188 | if (fpos < file_size) { /* Yes, we have to read it in */
|
---|
189 | if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
|
---|
190 | endwin ();
|
---|
191 | fprintf (stderr,
|
---|
192 | "\nError moving file pointer in dialog_textbox().\n");
|
---|
193 | exit (-1);
|
---|
194 | }
|
---|
195 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
196 | endwin ();
|
---|
197 | fprintf (stderr,
|
---|
198 | "\nError reading file in dialog_textbox().\n");
|
---|
199 | exit (-1);
|
---|
200 | }
|
---|
201 | buf[bytes_read] = '\0';
|
---|
202 | }
|
---|
203 | page = buf + bytes_read;
|
---|
204 | back_lines (height - 4);
|
---|
205 | print_page (text, height - 4, width - 2);
|
---|
206 | print_position (dialog, height, width);
|
---|
207 | wmove (dialog, cur_y, cur_x); /* Restore cursor position */
|
---|
208 | wrefresh (dialog);
|
---|
209 | break;
|
---|
210 | case 'K': /* Previous line */
|
---|
211 | case 'k':
|
---|
212 | case KEY_UP:
|
---|
213 | if (!begin_reached) {
|
---|
214 | back_lines (page_length + 1);
|
---|
215 |
|
---|
216 | /* We don't call print_page() here but use scrolling to ensure
|
---|
217 | faster screen update. However, 'end_reached' and
|
---|
218 | 'page_length' should still be updated, and 'page' should
|
---|
219 | point to start of next page. This is done by calling
|
---|
220 | get_line() in the following 'for' loop. */
|
---|
221 | scrollok (text, TRUE);
|
---|
222 | wscrl (text, -1); /* Scroll text region down one line */
|
---|
223 | scrollok (text, FALSE);
|
---|
224 | page_length = 0;
|
---|
225 | passed_end = 0;
|
---|
226 | for (i = 0; i < height - 4; i++) {
|
---|
227 | if (!i) {
|
---|
228 | /* print first line of page */
|
---|
229 | print_line (text, 0, width - 2);
|
---|
230 | wnoutrefresh (text);
|
---|
231 | } else
|
---|
232 | /* Called to update 'end_reached' and 'page' */
|
---|
233 | get_line ();
|
---|
234 | if (!passed_end)
|
---|
235 | page_length++;
|
---|
236 | if (end_reached && !passed_end)
|
---|
237 | passed_end = 1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | print_position (dialog, height, width);
|
---|
241 | wmove (dialog, cur_y, cur_x); /* Restore cursor position */
|
---|
242 | wrefresh (dialog);
|
---|
243 | }
|
---|
244 | break;
|
---|
245 | case 'B': /* Previous page */
|
---|
246 | case 'b':
|
---|
247 | case KEY_PPAGE:
|
---|
248 | if (begin_reached)
|
---|
249 | break;
|
---|
250 | back_lines (page_length + height - 4);
|
---|
251 | print_page (text, height - 4, width - 2);
|
---|
252 | print_position (dialog, height, width);
|
---|
253 | wmove (dialog, cur_y, cur_x);
|
---|
254 | wrefresh (dialog);
|
---|
255 | break;
|
---|
256 | case 'J': /* Next line */
|
---|
257 | case 'j':
|
---|
258 | case KEY_DOWN:
|
---|
259 | if (!end_reached) {
|
---|
260 | begin_reached = 0;
|
---|
261 | scrollok (text, TRUE);
|
---|
262 | scroll (text); /* Scroll text region up one line */
|
---|
263 | scrollok (text, FALSE);
|
---|
264 | print_line (text, height - 5, width - 2);
|
---|
265 | wnoutrefresh (text);
|
---|
266 | print_position (dialog, height, width);
|
---|
267 | wmove (dialog, cur_y, cur_x); /* Restore cursor position */
|
---|
268 | wrefresh (dialog);
|
---|
269 | }
|
---|
270 | break;
|
---|
271 | case KEY_NPAGE: /* Next page */
|
---|
272 | case ' ':
|
---|
273 | if (end_reached)
|
---|
274 | break;
|
---|
275 |
|
---|
276 | begin_reached = 0;
|
---|
277 | print_page (text, height - 4, width - 2);
|
---|
278 | print_position (dialog, height, width);
|
---|
279 | wmove (dialog, cur_y, cur_x);
|
---|
280 | wrefresh (dialog);
|
---|
281 | break;
|
---|
282 | case '0': /* Beginning of line */
|
---|
283 | case 'H': /* Scroll left */
|
---|
284 | case 'h':
|
---|
285 | case KEY_LEFT:
|
---|
286 | if (hscroll <= 0)
|
---|
287 | break;
|
---|
288 |
|
---|
289 | if (key == '0')
|
---|
290 | hscroll = 0;
|
---|
291 | else
|
---|
292 | hscroll--;
|
---|
293 | /* Reprint current page to scroll horizontally */
|
---|
294 | back_lines (page_length);
|
---|
295 | print_page (text, height - 4, width - 2);
|
---|
296 | wmove (dialog, cur_y, cur_x);
|
---|
297 | wrefresh (dialog);
|
---|
298 | break;
|
---|
299 | case 'L': /* Scroll right */
|
---|
300 | case 'l':
|
---|
301 | case KEY_RIGHT:
|
---|
302 | if (hscroll >= MAX_LEN)
|
---|
303 | break;
|
---|
304 | hscroll++;
|
---|
305 | /* Reprint current page to scroll horizontally */
|
---|
306 | back_lines (page_length);
|
---|
307 | print_page (text, height - 4, width - 2);
|
---|
308 | wmove (dialog, cur_y, cur_x);
|
---|
309 | wrefresh (dialog);
|
---|
310 | break;
|
---|
311 | case ESC:
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | delwin (dialog);
|
---|
317 | free (buf);
|
---|
318 | close (fd);
|
---|
319 | return 1; /* ESC pressed */
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * Go back 'n' lines in text file. Called by dialog_textbox().
|
---|
324 | * 'page' will be updated to point to the desired line in 'buf'.
|
---|
325 | */
|
---|
326 | static void
|
---|
327 | back_lines (int n)
|
---|
328 | {
|
---|
329 | int i, fpos;
|
---|
330 |
|
---|
331 | begin_reached = 0;
|
---|
332 | /* We have to distinguish between end_reached and !end_reached
|
---|
333 | since at end of file, the line is not ended by a '\n'.
|
---|
334 | The code inside 'if' basically does a '--page' to move one
|
---|
335 | character backward so as to skip '\n' of the previous line */
|
---|
336 | if (!end_reached) {
|
---|
337 | /* Either beginning of buffer or beginning of file reached? */
|
---|
338 | if (page == buf) {
|
---|
339 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
340 | endwin ();
|
---|
341 | fprintf (stderr, "\nError moving file pointer in "
|
---|
342 | "back_lines().\n");
|
---|
343 | exit (-1);
|
---|
344 | }
|
---|
345 | if (fpos > bytes_read) { /* Not beginning of file yet */
|
---|
346 | /* We've reached beginning of buffer, but not beginning of
|
---|
347 | file yet, so read previous part of file into buffer.
|
---|
348 | Note that we only move backward for BUF_SIZE/2 bytes,
|
---|
349 | but not BUF_SIZE bytes to avoid re-reading again in
|
---|
350 | print_page() later */
|
---|
351 | /* Really possible to move backward BUF_SIZE/2 bytes? */
|
---|
352 | if (fpos < BUF_SIZE / 2 + bytes_read) {
|
---|
353 | /* No, move less then */
|
---|
354 | if (lseek (fd, 0, SEEK_SET) == -1) {
|
---|
355 | endwin ();
|
---|
356 | fprintf (stderr, "\nError moving file pointer in "
|
---|
357 | "back_lines().\n");
|
---|
358 | exit (-1);
|
---|
359 | }
|
---|
360 | page = buf + fpos - bytes_read;
|
---|
361 | } else { /* Move backward BUF_SIZE/2 bytes */
|
---|
362 | if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR)
|
---|
363 | == -1) {
|
---|
364 | endwin ();
|
---|
365 | fprintf (stderr, "\nError moving file pointer "
|
---|
366 | "in back_lines().\n");
|
---|
367 | exit (-1);
|
---|
368 | }
|
---|
369 | page = buf + BUF_SIZE / 2;
|
---|
370 | }
|
---|
371 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
372 | endwin ();
|
---|
373 | fprintf (stderr, "\nError reading file in back_lines().\n");
|
---|
374 | exit (-1);
|
---|
375 | }
|
---|
376 | buf[bytes_read] = '\0';
|
---|
377 | } else { /* Beginning of file reached */
|
---|
378 | begin_reached = 1;
|
---|
379 | return;
|
---|
380 | }
|
---|
381 | }
|
---|
382 | if (*(--page) != '\n') { /* '--page' here */
|
---|
383 | /* Something's wrong... */
|
---|
384 | endwin ();
|
---|
385 | fprintf (stderr, "\nInternal error in back_lines().\n");
|
---|
386 | exit (-1);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | /* Go back 'n' lines */
|
---|
390 | for (i = 0; i < n; i++)
|
---|
391 | do {
|
---|
392 | if (page == buf) {
|
---|
393 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
394 | endwin ();
|
---|
395 | fprintf (stderr,
|
---|
396 | "\nError moving file pointer in back_lines().\n");
|
---|
397 | exit (-1);
|
---|
398 | }
|
---|
399 | if (fpos > bytes_read) {
|
---|
400 | /* Really possible to move backward BUF_SIZE/2 bytes? */
|
---|
401 | if (fpos < BUF_SIZE / 2 + bytes_read) {
|
---|
402 | /* No, move less then */
|
---|
403 | if (lseek (fd, 0, SEEK_SET) == -1) {
|
---|
404 | endwin ();
|
---|
405 | fprintf (stderr, "\nError moving file pointer "
|
---|
406 | "in back_lines().\n");
|
---|
407 | exit (-1);
|
---|
408 | }
|
---|
409 | page = buf + fpos - bytes_read;
|
---|
410 | } else { /* Move backward BUF_SIZE/2 bytes */
|
---|
411 | if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
|
---|
412 | SEEK_CUR) == -1) {
|
---|
413 | endwin ();
|
---|
414 | fprintf (stderr, "\nError moving file pointer"
|
---|
415 | " in back_lines().\n");
|
---|
416 | exit (-1);
|
---|
417 | }
|
---|
418 | page = buf + BUF_SIZE / 2;
|
---|
419 | }
|
---|
420 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
421 | endwin ();
|
---|
422 | fprintf (stderr, "\nError reading file in "
|
---|
423 | "back_lines().\n");
|
---|
424 | exit (-1);
|
---|
425 | }
|
---|
426 | buf[bytes_read] = '\0';
|
---|
427 | } else { /* Beginning of file reached */
|
---|
428 | begin_reached = 1;
|
---|
429 | return;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | } while (*(--page) != '\n');
|
---|
433 | page++;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Print a new page of text. Called by dialog_textbox().
|
---|
438 | */
|
---|
439 | static void
|
---|
440 | print_page (WINDOW * win, int height, int width)
|
---|
441 | {
|
---|
442 | int i, passed_end = 0;
|
---|
443 |
|
---|
444 | page_length = 0;
|
---|
445 | for (i = 0; i < height; i++) {
|
---|
446 | print_line (win, i, width);
|
---|
447 | if (!passed_end)
|
---|
448 | page_length++;
|
---|
449 | if (end_reached && !passed_end)
|
---|
450 | passed_end = 1;
|
---|
451 | }
|
---|
452 | wnoutrefresh (win);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /*
|
---|
456 | * Print a new line of text. Called by dialog_textbox() and print_page().
|
---|
457 | */
|
---|
458 | static void
|
---|
459 | print_line (WINDOW * win, int row, int width)
|
---|
460 | {
|
---|
461 | char *line;
|
---|
462 |
|
---|
463 | line = get_line ();
|
---|
464 | line += MIN (strlen (line), hscroll); /* Scroll horizontally */
|
---|
465 | wmove (win, row, 0); /* move cursor to correct line */
|
---|
466 | waddch (win, ' ');
|
---|
467 | waddnstr (win, line, MIN (strlen (line), width - 2));
|
---|
468 |
|
---|
469 | /* Clear 'residue' of previous line */
|
---|
470 | #if OLD_NCURSES
|
---|
471 | {
|
---|
472 | int y, x;
|
---|
473 | int i;
|
---|
474 | getyx (win, y, x);
|
---|
475 | for (i = 0; i < width - x; i++)
|
---|
476 | waddch (win, ' ');
|
---|
477 | }
|
---|
478 | #else
|
---|
479 | wclrtoeol(win);
|
---|
480 | #endif
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Return current line of text. Called by dialog_textbox() and print_line().
|
---|
485 | * 'page' should point to start of current line before calling, and will be
|
---|
486 | * updated to point to start of next line.
|
---|
487 | */
|
---|
488 | static char *
|
---|
489 | get_line (void)
|
---|
490 | {
|
---|
491 | int i = 0, fpos;
|
---|
492 | static char line[MAX_LEN + 1];
|
---|
493 |
|
---|
494 | end_reached = 0;
|
---|
495 | while (*page != '\n') {
|
---|
496 | if (*page == '\0') {
|
---|
497 | /* Either end of file or end of buffer reached */
|
---|
498 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
499 | endwin ();
|
---|
500 | fprintf (stderr, "\nError moving file pointer in "
|
---|
501 | "get_line().\n");
|
---|
502 | exit (-1);
|
---|
503 | }
|
---|
504 | if (fpos < file_size) { /* Not end of file yet */
|
---|
505 | /* We've reached end of buffer, but not end of file yet,
|
---|
506 | so read next part of file into buffer */
|
---|
507 | if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
|
---|
508 | endwin ();
|
---|
509 | fprintf (stderr, "\nError reading file in get_line().\n");
|
---|
510 | exit (-1);
|
---|
511 | }
|
---|
512 | buf[bytes_read] = '\0';
|
---|
513 | page = buf;
|
---|
514 | } else {
|
---|
515 | if (!end_reached)
|
---|
516 | end_reached = 1;
|
---|
517 | break;
|
---|
518 | }
|
---|
519 | } else if (i < MAX_LEN)
|
---|
520 | line[i++] = *(page++);
|
---|
521 | else {
|
---|
522 | /* Truncate lines longer than MAX_LEN characters */
|
---|
523 | if (i == MAX_LEN)
|
---|
524 | line[i++] = '\0';
|
---|
525 | page++;
|
---|
526 | }
|
---|
527 | }
|
---|
528 | if (i <= MAX_LEN)
|
---|
529 | line[i] = '\0';
|
---|
530 | if (!end_reached)
|
---|
531 | page++; /* move pass '\n' */
|
---|
532 |
|
---|
533 | return line;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /*
|
---|
537 | * Print current position
|
---|
538 | */
|
---|
539 | static void
|
---|
540 | print_position (WINDOW * win, int height, int width)
|
---|
541 | {
|
---|
542 | int fpos, percent;
|
---|
543 |
|
---|
544 | if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
|
---|
545 | endwin ();
|
---|
546 | fprintf (stderr, "\nError moving file pointer in print_position().\n");
|
---|
547 | exit (-1);
|
---|
548 | }
|
---|
549 | wattrset (win, position_indicator_attr);
|
---|
550 | wbkgdset (win, position_indicator_attr & A_COLOR);
|
---|
551 | percent = !file_size ?
|
---|
552 | 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
|
---|
553 | wmove (win, height - 3, width - 9);
|
---|
554 | wprintw (win, "(%3d%%)", percent);
|
---|
555 | }
|
---|