test_instr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /****************************************************************************
  2. * Copyright (c) 2007 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: test_instr.c,v 1.4 2007/07/21 19:38:04 tom Exp $
  30. *
  31. * Author: Thomas E Dickey
  32. *
  33. * Demonstrate the instr functions from the curses library.
  34. int instr(char *str);
  35. int innstr(char *str, int n);
  36. int winstr(WINDOW *win, char *str);
  37. int winnstr(WINDOW *win, char *str, int n);
  38. int mvinstr(int y, int x, char *str);
  39. int mvinnstr(int y, int x, char *str, int n);
  40. int mvwinstr(WINDOW *win, int y, int x, char *str);
  41. int mvwinnstr(WINDOW *win, int y, int x, char *str, int n);
  42. */
  43. #include <test.priv.h>
  44. #define BASE_Y 6
  45. #define MAX_COLS 1024
  46. static bool
  47. Quit(int ch)
  48. {
  49. return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
  50. }
  51. static void
  52. show_1st(WINDOW *win, int line, char *buffer)
  53. {
  54. mvwaddstr(win, line, 5, buffer);
  55. }
  56. static void
  57. showmore(WINDOW *win, int line, char *buffer)
  58. {
  59. wmove(win, line, 0);
  60. wclrtoeol(win);
  61. show_1st(win, line, buffer);
  62. }
  63. static int
  64. test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
  65. {
  66. WINDOW *txtbox = 0;
  67. WINDOW *txtwin = 0;
  68. FILE *fp;
  69. int ch;
  70. int txt_x = 0, txt_y = 0;
  71. int base_y;
  72. int limit = getmaxx(strwin) - 5;
  73. char buffer[MAX_COLS];
  74. if (argv[level] == 0) {
  75. beep();
  76. return FALSE;
  77. }
  78. if (level > 1) {
  79. txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
  80. box(txtbox, 0, 0);
  81. wnoutrefresh(txtbox);
  82. txtwin = derwin(txtbox,
  83. getmaxy(txtbox) - 2,
  84. getmaxx(txtbox) - 2,
  85. 1, 1);
  86. base_y = 0;
  87. } else {
  88. txtwin = stdscr;
  89. base_y = BASE_Y;
  90. }
  91. keypad(txtwin, TRUE); /* enable keyboard mapping */
  92. (void) cbreak(); /* take input chars one at a time, no wait for \n */
  93. (void) noecho(); /* don't echo input */
  94. txt_y = base_y;
  95. txt_x = 0;
  96. wmove(txtwin, txt_y, txt_x);
  97. if ((fp = fopen(argv[level], "r")) != 0) {
  98. while ((ch = fgetc(fp)) != EOF) {
  99. if (waddch(txtwin, UChar(ch)) != OK) {
  100. break;
  101. }
  102. }
  103. fclose(fp);
  104. } else {
  105. wprintw(txtwin, "Cannot open:\n%s", argv[1]);
  106. }
  107. while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
  108. switch (ch) {
  109. case KEY_DOWN:
  110. case 'j':
  111. if (txt_y < getmaxy(txtwin) - 1)
  112. txt_y++;
  113. else
  114. beep();
  115. break;
  116. case KEY_UP:
  117. case 'k':
  118. if (txt_y > base_y)
  119. txt_y--;
  120. else
  121. beep();
  122. break;
  123. case KEY_LEFT:
  124. case 'h':
  125. if (txt_x > 0)
  126. txt_x--;
  127. else
  128. beep();
  129. break;
  130. case KEY_RIGHT:
  131. case 'l':
  132. if (txt_x < getmaxx(txtwin) - 1)
  133. txt_x++;
  134. else
  135. beep();
  136. break;
  137. case 'w':
  138. test_inchs(level + 1, argv, chrwin, strwin);
  139. if (txtbox != 0) {
  140. touchwin(txtbox);
  141. wnoutrefresh(txtbox);
  142. } else {
  143. touchwin(txtwin);
  144. wnoutrefresh(txtwin);
  145. }
  146. break;
  147. case '-':
  148. if (limit > 0) {
  149. --limit;
  150. } else {
  151. beep();
  152. }
  153. break;
  154. case '+':
  155. ++limit;
  156. break;
  157. default:
  158. beep();
  159. break;
  160. }
  161. mvwprintw(chrwin, 0, 0, "line:");
  162. wclrtoeol(chrwin);
  163. if (txtwin != stdscr) {
  164. wmove(txtwin, txt_y, txt_x);
  165. if (winstr(txtwin, buffer) != ERR) {
  166. show_1st(chrwin, 0, buffer);
  167. }
  168. if (mvwinstr(txtwin, txt_y, txt_x, buffer) != ERR) {
  169. showmore(chrwin, 1, buffer);
  170. }
  171. } else {
  172. move(txt_y, txt_x);
  173. if (instr(buffer) != ERR) {
  174. show_1st(chrwin, 0, buffer);
  175. }
  176. if (mvinstr(txt_y, txt_x, buffer) != ERR) {
  177. showmore(chrwin, 1, buffer);
  178. }
  179. }
  180. wnoutrefresh(chrwin);
  181. mvwprintw(strwin, 0, 0, "%4d:", limit);
  182. wclrtobot(strwin);
  183. if (txtwin != stdscr) {
  184. wmove(txtwin, txt_y, txt_x);
  185. if (winnstr(txtwin, buffer, limit) != ERR) {
  186. show_1st(strwin, 0, buffer);
  187. }
  188. if (mvwinnstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
  189. showmore(strwin, 1, buffer);
  190. }
  191. } else {
  192. move(txt_y, txt_x);
  193. if (innstr(buffer, limit) != ERR) {
  194. show_1st(strwin, 0, buffer);
  195. }
  196. if (mvinnstr(txt_y, txt_x, buffer, limit) != ERR) {
  197. showmore(strwin, 1, buffer);
  198. }
  199. }
  200. wnoutrefresh(strwin);
  201. }
  202. if (level > 1) {
  203. delwin(txtwin);
  204. delwin(txtbox);
  205. }
  206. return TRUE;
  207. }
  208. int
  209. main(int argc, char *argv[])
  210. {
  211. WINDOW *chrbox;
  212. WINDOW *chrwin;
  213. WINDOW *strwin;
  214. setlocale(LC_ALL, "");
  215. if (argc < 2) {
  216. fprintf(stderr, "usage: %s file\n", argv[0]);
  217. return EXIT_FAILURE;
  218. }
  219. initscr();
  220. chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
  221. box(chrbox, 0, 0);
  222. wnoutrefresh(chrbox);
  223. chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
  224. strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
  225. test_inchs(1, argv, chrwin, strwin);
  226. endwin();
  227. ExitProgram(EXIT_SUCCESS);
  228. }