Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 37 | int dialog_textbox(const char *title, const char *file, int height, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 44 | search_term[0] = '\0'; /* no search term entered yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 46 | /* Open input file for reading */ |
| 47 | if ((fd = open(file, O_RDONLY)) == -1) { |
| 48 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 49 | fprintf(stderr, "\nCan't open input file in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 50 | exit(-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 56 | fprintf(stderr, "\nError getting file size in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 57 | exit(-1); |
| 58 | } |
| 59 | /* Restore file pointer to beginning of file after getting file size */ |
| 60 | if (lseek(fd, 0, SEEK_SET) == -1) { |
| 61 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 62 | fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 63 | exit(-1); |
| 64 | } |
| 65 | /* Allocate space for read buffer */ |
| 66 | if ((buf = malloc(BUF_SIZE + 1)) == NULL) { |
| 67 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 68 | fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 69 | exit(-1); |
| 70 | } |
| 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); |
| 75 | } |
| 76 | buf[bytes_read] = '\0'; /* mark end of valid data */ |
| 77 | page = buf; /* page is pointer to start of page to be displayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 79 | /* center dialog box on screen */ |
| 80 | x = (COLS - width) / 2; |
| 81 | y = (LINES - height) / 2; |
| 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 | print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE); |
| 121 | wnoutrefresh(dialog); |
| 122 | getyx(dialog, cur_y, cur_x); /* Save cursor position */ |
| 123 | |
| 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); |
| 130 | |
| 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 149 | fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 150 | exit(-1); |
| 151 | } |
| 152 | if (fpos > bytes_read) { /* Yes, we have to read it in */ |
| 153 | if (lseek(fd, 0, SEEK_SET) == -1) { |
| 154 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 155 | fprintf(stderr, "\nError moving file pointer in " |
| 156 | "dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 157 | exit(-1); |
| 158 | } |
| 159 | if ((bytes_read = |
| 160 | read(fd, buf, BUF_SIZE)) == -1) { |
| 161 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 162 | fprintf(stderr, "\nError reading file in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 163 | exit(-1); |
| 164 | } |
| 165 | buf[bytes_read] = '\0'; |
| 166 | } |
| 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); |
| 172 | } |
| 173 | break; |
| 174 | case 'G': /* Last page */ |
| 175 | case KEY_END: |
| 176 | |
| 177 | end_reached = 1; |
| 178 | /* Last page not in buffer? */ |
| 179 | if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { |
| 180 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 181 | fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 182 | exit(-1); |
| 183 | } |
| 184 | if (fpos < file_size) { /* Yes, we have to read it in */ |
| 185 | if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) { |
| 186 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 187 | fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 188 | exit(-1); |
| 189 | } |
| 190 | if ((bytes_read = |
| 191 | read(fd, buf, BUF_SIZE)) == -1) { |
| 192 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 193 | fprintf(stderr, "\nError reading file in dialog_textbox().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 194 | exit(-1); |
| 195 | } |
| 196 | buf[bytes_read] = '\0'; |
| 197 | } |
| 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); |
| 210 | |
| 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; |
| 233 | } |
| 234 | |
| 235 | print_position(dialog, height, width); |
| 236 | wmove(dialog, cur_y, cur_x); /* Restore cursor position */ |
| 237 | wrefresh(dialog); |
| 238 | } |
| 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); |
| 264 | } |
| 265 | break; |
| 266 | case KEY_NPAGE: /* Next page */ |
| 267 | case ' ': |
| 268 | if (end_reached) |
| 269 | break; |
| 270 | |
| 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; |
| 283 | |
| 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; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | delwin(dialog); |
| 312 | free(buf); |
| 313 | close(fd); |
| 314 | return -1; /* ESC pressed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
| 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'. |
| 320 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 321 | static void back_lines(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 323 | int i, fpos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 335 | fprintf(stderr, "\nError moving file pointer in " |
| 336 | "back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 337 | exit(-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 350 | fprintf(stderr, "\nError moving file pointer in " |
| 351 | "back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 352 | exit(-1); |
| 353 | } |
| 354 | page = buf + fpos - bytes_read; |
| 355 | } else { /* Move backward BUF_SIZE/2 bytes */ |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 356 | if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 357 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 358 | fprintf(stderr, "\nError moving file pointer " |
| 359 | "in back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 360 | exit(-1); |
| 361 | } |
| 362 | page = buf + BUF_SIZE / 2; |
| 363 | } |
| 364 | if ((bytes_read = |
| 365 | read(fd, buf, BUF_SIZE)) == -1) { |
| 366 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 367 | fprintf(stderr, "\nError reading file in back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 368 | exit(-1); |
| 369 | } |
| 370 | buf[bytes_read] = '\0'; |
| 371 | } else { /* Beginning of file reached */ |
| 372 | begin_reached = 1; |
| 373 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 376 | if (*(--page) != '\n') { /* '--page' here */ |
| 377 | /* Something's wrong... */ |
| 378 | endwin(); |
| 379 | fprintf(stderr, "\nInternal error in back_lines().\n"); |
| 380 | exit(-1); |
| 381 | } |
| 382 | } |
| 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 389 | fprintf(stderr, "\nError moving file pointer in back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 390 | exit(-1); |
| 391 | } |
| 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 */ |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 396 | if (lseek(fd, 0, SEEK_SET) == -1) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 397 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 398 | fprintf(stderr, "\nError moving file pointer " |
| 399 | "in back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 400 | exit(-1); |
| 401 | } |
| 402 | page = buf + fpos - bytes_read; |
| 403 | } else { /* Move backward BUF_SIZE/2 bytes */ |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 404 | if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 405 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 406 | fprintf(stderr, "\nError moving file pointer" |
| 407 | " in back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 408 | exit(-1); |
| 409 | } |
| 410 | page = buf + BUF_SIZE / 2; |
| 411 | } |
| 412 | if ((bytes_read = |
| 413 | read(fd, buf, BUF_SIZE)) == -1) { |
| 414 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 415 | fprintf(stderr, "\nError reading file in " |
| 416 | "back_lines().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 417 | exit(-1); |
| 418 | } |
| 419 | buf[bytes_read] = '\0'; |
| 420 | } else { /* Beginning of file reached */ |
| 421 | begin_reached = 1; |
| 422 | return; |
| 423 | } |
| 424 | } |
| 425 | } while (*(--page) != '\n'); |
| 426 | page++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Print a new page of text. Called by dialog_textbox(). |
| 431 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 432 | static void print_page(WINDOW * win, int height, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 434 | int i, passed_end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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; |
| 443 | } |
| 444 | wnoutrefresh(win); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Print a new line of text. Called by dialog_textbox() and print_page(). |
| 449 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 450 | static void print_line(WINDOW * win, int row, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 452 | int y, x; |
| 453 | char *line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 461 | getyx(win, y, x); |
| 462 | /* Clear 'residue' of previous line */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | #if OLD_NCURSES |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 464 | { |
| 465 | int i; |
| 466 | for (i = 0; i < width - x; i++) |
| 467 | waddch(win, ' '); |
| 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | #else |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 470 | wclrtoeol(win); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | #endif |
| 472 | } |
| 473 | |
| 474 | /* |
| 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. |
| 478 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 479 | static char *get_line(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 481 | int i = 0, fpos; |
| 482 | static char line[MAX_LEN + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 490 | fprintf(stderr, "\nError moving file pointer in " |
| 491 | "get_line().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 492 | exit(-1); |
| 493 | } |
| 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(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 500 | fprintf(stderr, "\nError reading file in get_line().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 501 | exit(-1); |
| 502 | } |
| 503 | buf[bytes_read] = '\0'; |
| 504 | page = buf; |
| 505 | } else { |
| 506 | if (!end_reached) |
| 507 | end_reached = 1; |
| 508 | break; |
| 509 | } |
| 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++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 519 | if (i <= MAX_LEN) |
| 520 | line[i] = '\0'; |
| 521 | if (!end_reached) |
| 522 | page++; /* move pass '\n' */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 524 | return line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Print current position |
| 529 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 530 | static void print_position(WINDOW * win, int height, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 532 | int fpos, percent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 534 | if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { |
| 535 | endwin(); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 536 | fprintf(stderr, "\nError moving file pointer in print_position().\n"); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 537 | exit(-1); |
| 538 | } |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |