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