3
0

textbox.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. print_title(dialog, title, width);
  93. print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
  94. wnoutrefresh(dialog);
  95. getyx(dialog, cur_y, cur_x); /* Save cursor position */
  96. /* Print first page of text */
  97. attr_clear(text, height - 4, width - 2, dialog_attr);
  98. print_page(text, height - 4, width - 2);
  99. print_position(dialog, height, width);
  100. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  101. wrefresh(dialog);
  102. while ((key != ESC) && (key != '\n')) {
  103. key = wgetch(dialog);
  104. switch (key) {
  105. case 'E': /* Exit */
  106. case 'e':
  107. case 'X':
  108. case 'x':
  109. delwin(dialog);
  110. free(buf);
  111. close(fd);
  112. return 0;
  113. case 'g': /* First page */
  114. case KEY_HOME:
  115. if (!begin_reached) {
  116. begin_reached = 1;
  117. /* First page not in buffer? */
  118. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  119. endwin();
  120. fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
  121. exit(-1);
  122. }
  123. if (fpos > bytes_read) { /* Yes, we have to read it in */
  124. if (lseek(fd, 0, SEEK_SET) == -1) {
  125. endwin();
  126. fprintf(stderr, "\nError moving file pointer in "
  127. "dialog_textbox().\n");
  128. exit(-1);
  129. }
  130. if ((bytes_read =
  131. read(fd, buf, BUF_SIZE)) == -1) {
  132. endwin();
  133. fprintf(stderr, "\nError reading file in dialog_textbox().\n");
  134. exit(-1);
  135. }
  136. buf[bytes_read] = '\0';
  137. }
  138. page = buf;
  139. print_page(text, height - 4, width - 2);
  140. print_position(dialog, height, width);
  141. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  142. wrefresh(dialog);
  143. }
  144. break;
  145. case 'G': /* Last page */
  146. case KEY_END:
  147. end_reached = 1;
  148. /* Last page not in buffer? */
  149. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  150. endwin();
  151. fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
  152. exit(-1);
  153. }
  154. if (fpos < file_size) { /* Yes, we have to read it in */
  155. if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
  156. endwin();
  157. fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
  158. exit(-1);
  159. }
  160. if ((bytes_read =
  161. read(fd, buf, BUF_SIZE)) == -1) {
  162. endwin();
  163. fprintf(stderr, "\nError reading file in dialog_textbox().\n");
  164. exit(-1);
  165. }
  166. buf[bytes_read] = '\0';
  167. }
  168. page = buf + bytes_read;
  169. back_lines(height - 4);
  170. print_page(text, height - 4, width - 2);
  171. print_position(dialog, height, width);
  172. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  173. wrefresh(dialog);
  174. break;
  175. case 'K': /* Previous line */
  176. case 'k':
  177. case KEY_UP:
  178. if (!begin_reached) {
  179. back_lines(page_length + 1);
  180. /* We don't call print_page() here but use scrolling to ensure
  181. faster screen update. However, 'end_reached' and
  182. 'page_length' should still be updated, and 'page' should
  183. point to start of next page. This is done by calling
  184. get_line() in the following 'for' loop. */
  185. scrollok(text, TRUE);
  186. wscrl(text, -1); /* Scroll text region down one line */
  187. scrollok(text, FALSE);
  188. page_length = 0;
  189. passed_end = 0;
  190. for (i = 0; i < height - 4; i++) {
  191. if (!i) {
  192. /* print first line of page */
  193. print_line(text, 0, width - 2);
  194. wnoutrefresh(text);
  195. } else
  196. /* Called to update 'end_reached' and 'page' */
  197. get_line();
  198. if (!passed_end)
  199. page_length++;
  200. if (end_reached && !passed_end)
  201. passed_end = 1;
  202. }
  203. print_position(dialog, height, width);
  204. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  205. wrefresh(dialog);
  206. }
  207. break;
  208. case 'B': /* Previous page */
  209. case 'b':
  210. case KEY_PPAGE:
  211. if (begin_reached)
  212. break;
  213. back_lines(page_length + height - 4);
  214. print_page(text, height - 4, width - 2);
  215. print_position(dialog, height, width);
  216. wmove(dialog, cur_y, cur_x);
  217. wrefresh(dialog);
  218. break;
  219. case 'J': /* Next line */
  220. case 'j':
  221. case KEY_DOWN:
  222. if (!end_reached) {
  223. begin_reached = 0;
  224. scrollok(text, TRUE);
  225. scroll(text); /* Scroll text region up one line */
  226. scrollok(text, FALSE);
  227. print_line(text, height - 5, width - 2);
  228. wnoutrefresh(text);
  229. print_position(dialog, height, width);
  230. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  231. wrefresh(dialog);
  232. }
  233. break;
  234. case KEY_NPAGE: /* Next page */
  235. case ' ':
  236. if (end_reached)
  237. break;
  238. begin_reached = 0;
  239. print_page(text, height - 4, width - 2);
  240. print_position(dialog, height, width);
  241. wmove(dialog, cur_y, cur_x);
  242. wrefresh(dialog);
  243. break;
  244. case '0': /* Beginning of line */
  245. case 'H': /* Scroll left */
  246. case 'h':
  247. case KEY_LEFT:
  248. if (hscroll <= 0)
  249. break;
  250. if (key == '0')
  251. hscroll = 0;
  252. else
  253. hscroll--;
  254. /* Reprint current page to scroll horizontally */
  255. back_lines(page_length);
  256. print_page(text, height - 4, width - 2);
  257. wmove(dialog, cur_y, cur_x);
  258. wrefresh(dialog);
  259. break;
  260. case 'L': /* Scroll right */
  261. case 'l':
  262. case KEY_RIGHT:
  263. if (hscroll >= MAX_LEN)
  264. break;
  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 ESC:
  273. break;
  274. }
  275. }
  276. delwin(dialog);
  277. free(buf);
  278. close(fd);
  279. return -1; /* ESC pressed */
  280. }
  281. /*
  282. * Go back 'n' lines in text file. Called by dialog_textbox().
  283. * 'page' will be updated to point to the desired line in 'buf'.
  284. */
  285. static void back_lines(int n)
  286. {
  287. int i, fpos;
  288. begin_reached = 0;
  289. /* We have to distinguish between end_reached and !end_reached
  290. since at end of file, the line is not ended by a '\n'.
  291. The code inside 'if' basically does a '--page' to move one
  292. character backward so as to skip '\n' of the previous line */
  293. if (!end_reached) {
  294. /* Either beginning of buffer or beginning of file reached? */
  295. if (page == buf) {
  296. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  297. endwin();
  298. fprintf(stderr, "\nError moving file pointer in "
  299. "back_lines().\n");
  300. exit(-1);
  301. }
  302. if (fpos > bytes_read) { /* Not beginning of file yet */
  303. /* We've reached beginning of buffer, but not beginning of
  304. file yet, so read previous part of file into buffer.
  305. Note that we only move backward for BUF_SIZE/2 bytes,
  306. but not BUF_SIZE bytes to avoid re-reading again in
  307. print_page() later */
  308. /* Really possible to move backward BUF_SIZE/2 bytes? */
  309. if (fpos < BUF_SIZE / 2 + bytes_read) {
  310. /* No, move less then */
  311. if (lseek(fd, 0, SEEK_SET) == -1) {
  312. endwin();
  313. fprintf(stderr, "\nError moving file pointer in "
  314. "back_lines().\n");
  315. exit(-1);
  316. }
  317. page = buf + fpos - bytes_read;
  318. } else { /* Move backward BUF_SIZE/2 bytes */
  319. if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
  320. endwin();
  321. fprintf(stderr, "\nError moving file pointer "
  322. "in back_lines().\n");
  323. exit(-1);
  324. }
  325. page = buf + BUF_SIZE / 2;
  326. }
  327. if ((bytes_read =
  328. read(fd, buf, BUF_SIZE)) == -1) {
  329. endwin();
  330. fprintf(stderr, "\nError reading file in back_lines().\n");
  331. exit(-1);
  332. }
  333. buf[bytes_read] = '\0';
  334. } else { /* Beginning of file reached */
  335. begin_reached = 1;
  336. return;
  337. }
  338. }
  339. if (*(--page) != '\n') { /* '--page' here */
  340. /* Something's wrong... */
  341. endwin();
  342. fprintf(stderr, "\nInternal error in back_lines().\n");
  343. exit(-1);
  344. }
  345. }
  346. /* Go back 'n' lines */
  347. for (i = 0; i < n; i++)
  348. do {
  349. if (page == buf) {
  350. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  351. endwin();
  352. fprintf(stderr, "\nError moving file pointer in back_lines().\n");
  353. exit(-1);
  354. }
  355. if (fpos > bytes_read) {
  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, "\nError moving file pointer "
  362. "in back_lines().\n");
  363. exit(-1);
  364. }
  365. page = buf + fpos - bytes_read;
  366. } else { /* Move backward BUF_SIZE/2 bytes */
  367. if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
  368. endwin();
  369. fprintf(stderr, "\nError moving file pointer"
  370. " in back_lines().\n");
  371. exit(-1);
  372. }
  373. page = buf + BUF_SIZE / 2;
  374. }
  375. if ((bytes_read =
  376. read(fd, buf, BUF_SIZE)) == -1) {
  377. endwin();
  378. fprintf(stderr, "\nError reading file in "
  379. "back_lines().\n");
  380. exit(-1);
  381. }
  382. buf[bytes_read] = '\0';
  383. } else { /* Beginning of file reached */
  384. begin_reached = 1;
  385. return;
  386. }
  387. }
  388. } while (*(--page) != '\n');
  389. page++;
  390. }
  391. /*
  392. * Print a new page of text. Called by dialog_textbox().
  393. */
  394. static void print_page(WINDOW * win, int height, int width)
  395. {
  396. int i, passed_end = 0;
  397. page_length = 0;
  398. for (i = 0; i < height; i++) {
  399. print_line(win, i, width);
  400. if (!passed_end)
  401. page_length++;
  402. if (end_reached && !passed_end)
  403. passed_end = 1;
  404. }
  405. wnoutrefresh(win);
  406. }
  407. /*
  408. * Print a new line of text. Called by dialog_textbox() and print_page().
  409. */
  410. static void print_line(WINDOW * win, int row, int width)
  411. {
  412. int y, x;
  413. char *line;
  414. line = get_line();
  415. line += MIN(strlen(line), hscroll); /* Scroll horizontally */
  416. wmove(win, row, 0); /* move cursor to correct line */
  417. waddch(win, ' ');
  418. waddnstr(win, line, MIN(strlen(line), width - 2));
  419. getyx(win, y, x);
  420. /* Clear 'residue' of previous line */
  421. #if OLD_NCURSES
  422. {
  423. int i;
  424. for (i = 0; i < width - x; i++)
  425. waddch(win, ' ');
  426. }
  427. #else
  428. wclrtoeol(win);
  429. #endif
  430. }
  431. /*
  432. * Return current line of text. Called by dialog_textbox() and print_line().
  433. * 'page' should point to start of current line before calling, and will be
  434. * updated to point to start of next line.
  435. */
  436. static char *get_line(void)
  437. {
  438. int i = 0, fpos;
  439. static char line[MAX_LEN + 1];
  440. end_reached = 0;
  441. while (*page != '\n') {
  442. if (*page == '\0') {
  443. /* Either end of file or end of buffer reached */
  444. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  445. endwin();
  446. fprintf(stderr, "\nError moving file pointer in "
  447. "get_line().\n");
  448. exit(-1);
  449. }
  450. if (fpos < file_size) { /* Not end of file yet */
  451. /* We've reached end of buffer, but not end of file yet,
  452. so read next part of file into buffer */
  453. if ((bytes_read =
  454. read(fd, buf, BUF_SIZE)) == -1) {
  455. endwin();
  456. fprintf(stderr, "\nError reading file in get_line().\n");
  457. exit(-1);
  458. }
  459. buf[bytes_read] = '\0';
  460. page = buf;
  461. } else {
  462. if (!end_reached)
  463. end_reached = 1;
  464. break;
  465. }
  466. } else if (i < MAX_LEN)
  467. line[i++] = *(page++);
  468. else {
  469. /* Truncate lines longer than MAX_LEN characters */
  470. if (i == MAX_LEN)
  471. line[i++] = '\0';
  472. page++;
  473. }
  474. }
  475. if (i <= MAX_LEN)
  476. line[i] = '\0';
  477. if (!end_reached)
  478. page++; /* move pass '\n' */
  479. return line;
  480. }
  481. /*
  482. * Print current position
  483. */
  484. static void print_position(WINDOW * win, int height, int width)
  485. {
  486. int fpos, percent;
  487. if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
  488. endwin();
  489. fprintf(stderr, "\nError moving file pointer in print_position().\n");
  490. exit(-1);
  491. }
  492. wattrset(win, position_indicator_attr);
  493. wbkgdset(win, position_indicator_attr & A_COLOR);
  494. percent = !file_size ?
  495. 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
  496. wmove(win, height - 3, width - 9);
  497. wprintw(win, "(%3d%%)", percent);
  498. }