textbox.c 15 KB

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