1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 2007-2012,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: inchs.c,v 1.18 2020/02/02 23:34:34 tom Exp $
32 * Author: Thomas E Dickey
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);
49 #include <test.priv.h>
50 #include <popup_msg.h>
62 ExitProgram(EXIT_FAILURE);
68 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
72 test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
74 static const char *help[] =
76 "Test input from screen using inch(), etc., in a moveable viewport.",
80 " h,j,k,l (and arrow-keys) - move viewport",
81 " w - recur to new window",
82 " for next input file",
89 int txt_x = 0, txt_y = 0;
91 chtype text[MAX_COLS];
93 if (argv[level] == 0) {
99 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
101 wnoutrefresh(txtbox);
103 txtwin = derwin(txtbox,
113 failed("cannot create txtwin");
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 */
121 wmove(txtwin, txt_y, txt_x);
123 if ((fp = fopen(argv[level], "r")) != 0) {
124 while ((j = fgetc(fp)) != EOF) {
125 if (waddch(txtwin, UChar(j)) != OK) {
131 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
134 while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
140 if (txt_y < getmaxy(txtwin) - 1)
161 if (txt_x < getmaxx(txtwin) - 1)
167 test_inchs(level + 1, argv, chrwin, strwin);
170 wnoutrefresh(txtbox);
173 wnoutrefresh(txtwin);
177 popup_msg(txtwin, help);
184 MvWPrintw(chrwin, 0, 0, "char:");
187 if (txtwin != stdscr) {
188 wmove(txtwin, txt_y, txt_x);
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) {
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) {
220 wnoutrefresh(chrwin);
222 MvWPrintw(strwin, 0, 0, "text:");
225 limit = getmaxx(strwin) - 5;
227 if (txtwin != stdscr) {
228 wmove(txtwin, txt_y, txt_x);
229 if (winchstr(txtwin, text) != ERR) {
230 MvWAddChStr(strwin, 0, 5, text);
233 wmove(txtwin, txt_y, txt_x);
234 if (winchnstr(txtwin, text, limit) != ERR) {
235 MvWAddChStr(strwin, 1, 5, text);
238 if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
239 MvWAddChStr(strwin, 2, 5, text);
242 if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
243 MvWAddChStr(strwin, 3, 5, text);
247 if (inchstr(text) != ERR) {
248 MvWAddChStr(strwin, 0, 5, text);
252 if (inchnstr(text, limit) != ERR) {
253 MvWAddChStr(strwin, 1, 5, text);
256 if (mvinchstr(txt_y, txt_x, text) != ERR) {
257 MvWAddChStr(strwin, 2, 5, text);
260 if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
261 MvWAddChStr(strwin, 3, 5, text);
265 wnoutrefresh(strwin);
275 main(int argc, char *argv[])
281 setlocale(LC_ALL, "");
284 fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]);
290 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
292 wnoutrefresh(chrbox);
294 chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
295 strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
297 test_inchs(1, argv, chrwin, strwin);
300 ExitProgram(EXIT_SUCCESS);