textbox.c 15 KB

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