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 2019-2020,2022 Thomas E. Dickey * |
| 3 | * Copyright 2007-2012,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: inchs.c,v 1.19 2022/12/11 00:01:39 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | * |
| 32 | * Author: Thomas E Dickey |
| 33 | */ |
| 34 | /* |
| 35 | chtype inch(void); |
| 36 | chtype winch(WINDOW *win); |
| 37 | chtype mvinch(int y, int x); |
| 38 | chtype mvwinch(WINDOW *win, int y, int x); |
| 39 | int inchstr(chtype *chstr); |
| 40 | int inchnstr(chtype *chstr, int n); |
| 41 | int winchstr(WINDOW *win, chtype *chstr); |
| 42 | int winchnstr(WINDOW *win, chtype *chstr, int n); |
| 43 | int mvinchstr(int y, int x, chtype *chstr); |
| 44 | int mvinchnstr(int y, int x, chtype *chstr, int n); |
| 45 | int mvwinchstr(WINDOW *win, int y, int x, chtype *chstr); |
| 46 | int mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n); |
| 47 | */ |
| 48 | |
| 49 | #include <test.priv.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 50 | #include <popup_msg.h> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 51 | |
| 52 | #define BASE_Y 7 |
| 53 | #define MAX_COLS 1024 |
| 54 | |
| 55 | static void |
| 56 | failed(const char *s) |
| 57 | { |
| 58 | int save = errno; |
| 59 | endwin(); |
| 60 | errno = save; |
| 61 | perror(s); |
| 62 | ExitProgram(EXIT_FAILURE); |
| 63 | } |
| 64 | |
| 65 | static bool |
| 66 | Quit(int ch) |
| 67 | { |
| 68 | return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE); |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin) |
| 73 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 74 | static const char *help[] = |
| 75 | { |
| 76 | "Test input from screen using inch(), etc., in a moveable viewport.", |
| 77 | "", |
| 78 | "Commands:", |
| 79 | " ESC/^Q - quit", |
| 80 | " h,j,k,l (and arrow-keys) - move viewport", |
| 81 | " w - recur to new window", |
| 82 | " for next input file", |
| 83 | 0 |
| 84 | }; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 85 | WINDOW *txtbox = 0; |
| 86 | WINDOW *txtwin = 0; |
| 87 | FILE *fp; |
| 88 | int ch, j; |
| 89 | int txt_x = 0, txt_y = 0; |
| 90 | int base_y; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 91 | chtype text[MAX_COLS]; |
| 92 | |
| 93 | if (argv[level] == 0) { |
| 94 | beep(); |
| 95 | return FALSE; |
| 96 | } |
| 97 | |
| 98 | if (level > 1) { |
| 99 | txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); |
| 100 | box(txtbox, 0, 0); |
| 101 | wnoutrefresh(txtbox); |
| 102 | |
| 103 | txtwin = derwin(txtbox, |
| 104 | getmaxy(txtbox) - 2, |
| 105 | getmaxx(txtbox) - 2, |
| 106 | 1, 1); |
| 107 | base_y = 0; |
| 108 | } else { |
| 109 | txtwin = stdscr; |
| 110 | base_y = BASE_Y; |
| 111 | } |
| 112 | if (txtwin == 0) |
| 113 | failed("cannot create txtwin"); |
| 114 | |
| 115 | keypad(txtwin, TRUE); /* enable keyboard mapping */ |
| 116 | (void) cbreak(); /* take input chars one at a time, no wait for \n */ |
| 117 | (void) noecho(); /* don't echo input */ |
| 118 | |
| 119 | txt_y = base_y; |
| 120 | txt_x = 0; |
| 121 | wmove(txtwin, txt_y, txt_x); |
| 122 | |
| 123 | if ((fp = fopen(argv[level], "r")) != 0) { |
| 124 | while ((j = fgetc(fp)) != EOF) { |
| 125 | if (waddch(txtwin, UChar(j)) != OK) { |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | fclose(fp); |
| 130 | } else { |
| 131 | wprintw(txtwin, "Cannot open:\n%s", argv[1]); |
| 132 | } |
| 133 | |
| 134 | while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 135 | int limit; |
| 136 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 137 | switch (j) { |
| 138 | case KEY_DOWN: |
| 139 | case 'j': |
| 140 | if (txt_y < getmaxy(txtwin) - 1) |
| 141 | txt_y++; |
| 142 | else |
| 143 | beep(); |
| 144 | break; |
| 145 | case KEY_UP: |
| 146 | case 'k': |
| 147 | if (txt_y > base_y) |
| 148 | txt_y--; |
| 149 | else |
| 150 | beep(); |
| 151 | break; |
| 152 | case KEY_LEFT: |
| 153 | case 'h': |
| 154 | if (txt_x > 0) |
| 155 | txt_x--; |
| 156 | else |
| 157 | beep(); |
| 158 | break; |
| 159 | case KEY_RIGHT: |
| 160 | case 'l': |
| 161 | if (txt_x < getmaxx(txtwin) - 1) |
| 162 | txt_x++; |
| 163 | else |
| 164 | beep(); |
| 165 | break; |
| 166 | case 'w': |
| 167 | test_inchs(level + 1, argv, chrwin, strwin); |
| 168 | if (txtbox != 0) { |
| 169 | touchwin(txtbox); |
| 170 | wnoutrefresh(txtbox); |
| 171 | } else { |
| 172 | touchwin(txtwin); |
| 173 | wnoutrefresh(txtwin); |
| 174 | } |
| 175 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 176 | case HELP_KEY_1: |
| 177 | popup_msg(txtwin, help); |
| 178 | break; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 179 | default: |
| 180 | beep(); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | MvWPrintw(chrwin, 0, 0, "char:"); |
| 185 | wclrtoeol(chrwin); |
| 186 | |
| 187 | if (txtwin != stdscr) { |
| 188 | wmove(txtwin, txt_y, txt_x); |
| 189 | |
| 190 | if ((ch = (int) winch(txtwin)) != ERR) { |
| 191 | if (waddch(chrwin, (chtype) ch) != ERR) { |
| 192 | for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { |
| 193 | if ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) { |
| 194 | if (waddch(chrwin, (chtype) ch) == ERR) { |
| 195 | break; |
| 196 | } |
| 197 | } else { |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } else { |
| 204 | move(txt_y, txt_x); |
| 205 | |
| 206 | if ((ch = (int) inch()) != ERR) { |
| 207 | if (waddch(chrwin, (chtype) ch) != ERR) { |
| 208 | for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { |
| 209 | if ((ch = (int) mvinch(txt_y, j)) != ERR) { |
| 210 | if (waddch(chrwin, (chtype) ch) == ERR) { |
| 211 | break; |
| 212 | } |
| 213 | } else { |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | wnoutrefresh(chrwin); |
| 221 | |
| 222 | MvWPrintw(strwin, 0, 0, "text:"); |
| 223 | wclrtobot(strwin); |
| 224 | |
| 225 | limit = getmaxx(strwin) - 5; |
| 226 | |
| 227 | if (txtwin != stdscr) { |
| 228 | wmove(txtwin, txt_y, txt_x); |
| 229 | if (winchstr(txtwin, text) != ERR) { |
| 230 | MvWAddChStr(strwin, 0, 5, text); |
| 231 | } |
| 232 | |
| 233 | wmove(txtwin, txt_y, txt_x); |
| 234 | if (winchnstr(txtwin, text, limit) != ERR) { |
| 235 | MvWAddChStr(strwin, 1, 5, text); |
| 236 | } |
| 237 | |
| 238 | if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) { |
| 239 | MvWAddChStr(strwin, 2, 5, text); |
| 240 | } |
| 241 | |
| 242 | if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) { |
| 243 | MvWAddChStr(strwin, 3, 5, text); |
| 244 | } |
| 245 | } else { |
| 246 | move(txt_y, txt_x); |
| 247 | if (inchstr(text) != ERR) { |
| 248 | MvWAddChStr(strwin, 0, 5, text); |
| 249 | } |
| 250 | |
| 251 | move(txt_y, txt_x); |
| 252 | if (inchnstr(text, limit) != ERR) { |
| 253 | MvWAddChStr(strwin, 1, 5, text); |
| 254 | } |
| 255 | |
| 256 | if (mvinchstr(txt_y, txt_x, text) != ERR) { |
| 257 | MvWAddChStr(strwin, 2, 5, text); |
| 258 | } |
| 259 | |
| 260 | if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) { |
| 261 | MvWAddChStr(strwin, 3, 5, text); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | wnoutrefresh(strwin); |
| 266 | } |
| 267 | if (level > 1) { |
| 268 | delwin(txtwin); |
| 269 | delwin(txtbox); |
| 270 | } |
| 271 | return TRUE; |
| 272 | } |
| 273 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 274 | static void |
| 275 | usage(int ok) |
| 276 | { |
| 277 | static const char *msg[] = |
| 278 | { |
| 279 | "Usage: inchs [options] file1 [file2 [...]]" |
| 280 | ,"" |
| 281 | ,USAGE_COMMON |
| 282 | }; |
| 283 | size_t n; |
| 284 | |
| 285 | for (n = 0; n < SIZEOF(msg); n++) |
| 286 | fprintf(stderr, "%s\n", msg[n]); |
| 287 | |
| 288 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 289 | } |
| 290 | /* *INDENT-OFF* */ |
| 291 | VERSION_COMMON() |
| 292 | /* *INDENT-ON* */ |
| 293 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 294 | int |
| 295 | main(int argc, char *argv[]) |
| 296 | { |
| 297 | WINDOW *chrbox; |
| 298 | WINDOW *chrwin; |
| 299 | WINDOW *strwin; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 300 | int ch; |
| 301 | |
| 302 | while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) { |
| 303 | switch (ch) { |
| 304 | case OPTS_VERSION: |
| 305 | show_version(argv); |
| 306 | ExitProgram(EXIT_SUCCESS); |
| 307 | default: |
| 308 | usage(ch == OPTS_USAGE); |
| 309 | /* NOTREACHED */ |
| 310 | } |
| 311 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 312 | |
| 313 | setlocale(LC_ALL, ""); |
| 314 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 315 | if (optind + 1 > argc) |
| 316 | usage(FALSE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 317 | |
| 318 | initscr(); |
| 319 | |
| 320 | chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0); |
| 321 | box(chrbox, 0, 0); |
| 322 | wnoutrefresh(chrbox); |
| 323 | |
| 324 | chrwin = derwin(chrbox, 1, COLS - 2, 1, 1); |
| 325 | strwin = derwin(chrbox, 4, COLS - 2, 2, 1); |
| 326 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 327 | test_inchs(optind, argv, chrwin, strwin); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 328 | |
| 329 | endwin(); |
| 330 | ExitProgram(EXIT_SUCCESS); |
| 331 | } |