textbox.c 15 KB

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