textbox.c 15 KB

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