Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2007,2010 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.5 2010/05/01 19:13:46 tom Exp $ |
| 30 | * |
| 31 | * Author: Thomas E Dickey |
| 32 | * |
| 33 | * Demonstrate the instr functions from the curses library. |
| 34 | |
| 35 | int instr(char *str); |
| 36 | int innstr(char *str, int n); |
| 37 | int winstr(WINDOW *win, char *str); |
| 38 | int winnstr(WINDOW *win, char *str, int n); |
| 39 | int mvinstr(int y, int x, char *str); |
| 40 | int mvinnstr(int y, int x, char *str, int n); |
| 41 | int mvwinstr(WINDOW *win, int y, int x, char *str); |
| 42 | int mvwinnstr(WINDOW *win, int y, int x, char *str, int n); |
| 43 | */ |
| 44 | |
| 45 | #include <test.priv.h> |
| 46 | |
| 47 | #define BASE_Y 6 |
| 48 | #define MAX_COLS 1024 |
| 49 | |
| 50 | static bool |
| 51 | Quit(int ch) |
| 52 | { |
| 53 | return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE); |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | show_1st(WINDOW *win, int line, char *buffer) |
| 58 | { |
| 59 | MvWAddStr(win, line, 5, buffer); |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | showmore(WINDOW *win, int line, char *buffer) |
| 64 | { |
| 65 | wmove(win, line, 0); |
| 66 | wclrtoeol(win); |
| 67 | show_1st(win, line, buffer); |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin) |
| 72 | { |
| 73 | WINDOW *txtbox = 0; |
| 74 | WINDOW *txtwin = 0; |
| 75 | FILE *fp; |
| 76 | int ch; |
| 77 | int txt_x = 0, txt_y = 0; |
| 78 | int base_y; |
| 79 | int limit = getmaxx(strwin) - 5; |
| 80 | |
| 81 | char buffer[MAX_COLS]; |
| 82 | |
| 83 | if (argv[level] == 0) { |
| 84 | beep(); |
| 85 | return FALSE; |
| 86 | } |
| 87 | |
| 88 | if (level > 1) { |
| 89 | txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); |
| 90 | box(txtbox, 0, 0); |
| 91 | wnoutrefresh(txtbox); |
| 92 | |
| 93 | txtwin = derwin(txtbox, |
| 94 | getmaxy(txtbox) - 2, |
| 95 | getmaxx(txtbox) - 2, |
| 96 | 1, 1); |
| 97 | base_y = 0; |
| 98 | } else { |
| 99 | txtwin = stdscr; |
| 100 | base_y = BASE_Y; |
| 101 | } |
| 102 | |
| 103 | keypad(txtwin, TRUE); /* enable keyboard mapping */ |
| 104 | (void) cbreak(); /* take input chars one at a time, no wait for \n */ |
| 105 | (void) noecho(); /* don't echo input */ |
| 106 | |
| 107 | txt_y = base_y; |
| 108 | txt_x = 0; |
| 109 | wmove(txtwin, txt_y, txt_x); |
| 110 | |
| 111 | if ((fp = fopen(argv[level], "r")) != 0) { |
| 112 | while ((ch = fgetc(fp)) != EOF) { |
| 113 | if (waddch(txtwin, UChar(ch)) != OK) { |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | fclose(fp); |
| 118 | } else { |
| 119 | wprintw(txtwin, "Cannot open:\n%s", argv[1]); |
| 120 | } |
| 121 | |
| 122 | while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) { |
| 123 | switch (ch) { |
| 124 | case KEY_DOWN: |
| 125 | case 'j': |
| 126 | if (txt_y < getmaxy(txtwin) - 1) |
| 127 | txt_y++; |
| 128 | else |
| 129 | beep(); |
| 130 | break; |
| 131 | case KEY_UP: |
| 132 | case 'k': |
| 133 | if (txt_y > base_y) |
| 134 | txt_y--; |
| 135 | else |
| 136 | beep(); |
| 137 | break; |
| 138 | case KEY_LEFT: |
| 139 | case 'h': |
| 140 | if (txt_x > 0) |
| 141 | txt_x--; |
| 142 | else |
| 143 | beep(); |
| 144 | break; |
| 145 | case KEY_RIGHT: |
| 146 | case 'l': |
| 147 | if (txt_x < getmaxx(txtwin) - 1) |
| 148 | txt_x++; |
| 149 | else |
| 150 | beep(); |
| 151 | break; |
| 152 | case 'w': |
| 153 | test_inchs(level + 1, argv, chrwin, strwin); |
| 154 | if (txtbox != 0) { |
| 155 | touchwin(txtbox); |
| 156 | wnoutrefresh(txtbox); |
| 157 | } else { |
| 158 | touchwin(txtwin); |
| 159 | wnoutrefresh(txtwin); |
| 160 | } |
| 161 | break; |
| 162 | case '-': |
| 163 | if (limit > 0) { |
| 164 | --limit; |
| 165 | } else { |
| 166 | beep(); |
| 167 | } |
| 168 | break; |
| 169 | case '+': |
| 170 | ++limit; |
| 171 | break; |
| 172 | default: |
| 173 | beep(); |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | MvWPrintw(chrwin, 0, 0, "line:"); |
| 178 | wclrtoeol(chrwin); |
| 179 | |
| 180 | if (txtwin != stdscr) { |
| 181 | wmove(txtwin, txt_y, txt_x); |
| 182 | |
| 183 | if (winstr(txtwin, buffer) != ERR) { |
| 184 | show_1st(chrwin, 0, buffer); |
| 185 | } |
| 186 | if (mvwinstr(txtwin, txt_y, txt_x, buffer) != ERR) { |
| 187 | showmore(chrwin, 1, buffer); |
| 188 | } |
| 189 | } else { |
| 190 | move(txt_y, txt_x); |
| 191 | |
| 192 | if (instr(buffer) != ERR) { |
| 193 | show_1st(chrwin, 0, buffer); |
| 194 | } |
| 195 | if (mvinstr(txt_y, txt_x, buffer) != ERR) { |
| 196 | showmore(chrwin, 1, buffer); |
| 197 | } |
| 198 | } |
| 199 | wnoutrefresh(chrwin); |
| 200 | |
| 201 | MvWPrintw(strwin, 0, 0, "%4d:", limit); |
| 202 | wclrtobot(strwin); |
| 203 | |
| 204 | if (txtwin != stdscr) { |
| 205 | wmove(txtwin, txt_y, txt_x); |
| 206 | if (winnstr(txtwin, buffer, limit) != ERR) { |
| 207 | show_1st(strwin, 0, buffer); |
| 208 | } |
| 209 | |
| 210 | if (mvwinnstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) { |
| 211 | showmore(strwin, 1, buffer); |
| 212 | } |
| 213 | } else { |
| 214 | move(txt_y, txt_x); |
| 215 | if (innstr(buffer, limit) != ERR) { |
| 216 | show_1st(strwin, 0, buffer); |
| 217 | } |
| 218 | |
| 219 | if (mvinnstr(txt_y, txt_x, buffer, limit) != ERR) { |
| 220 | showmore(strwin, 1, buffer); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | wnoutrefresh(strwin); |
| 225 | } |
| 226 | if (level > 1) { |
| 227 | delwin(txtwin); |
| 228 | delwin(txtbox); |
| 229 | } |
| 230 | return TRUE; |
| 231 | } |
| 232 | |
| 233 | int |
| 234 | main(int argc, char *argv[]) |
| 235 | { |
| 236 | WINDOW *chrbox; |
| 237 | WINDOW *chrwin; |
| 238 | WINDOW *strwin; |
| 239 | |
| 240 | setlocale(LC_ALL, ""); |
| 241 | |
| 242 | if (argc < 2) { |
| 243 | fprintf(stderr, "usage: %s file\n", argv[0]); |
| 244 | return EXIT_FAILURE; |
| 245 | } |
| 246 | |
| 247 | initscr(); |
| 248 | |
| 249 | chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0); |
| 250 | box(chrbox, 0, 0); |
| 251 | wnoutrefresh(chrbox); |
| 252 | |
| 253 | chrwin = derwin(chrbox, 2, COLS - 2, 1, 1); |
| 254 | strwin = derwin(chrbox, 2, COLS - 2, 3, 1); |
| 255 | |
| 256 | test_inchs(1, argv, chrwin, strwin); |
| 257 | |
| 258 | endwin(); |
| 259 | ExitProgram(EXIT_SUCCESS); |
| 260 | } |