1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2007-2010,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: test_instr.c,v 1.10 2020/02/02 23:34:34 tom Exp $
32 * Author: Thomas E Dickey
34 * Demonstrate the instr functions from the curses library.
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);
46 #include <test.priv.h>
54 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
58 show_1st(WINDOW *win, int line, char *buffer)
60 MvWAddStr(win, line, 5, buffer);
64 showmore(WINDOW *win, int line, char *buffer)
68 show_1st(win, line, buffer);
72 recursive_test(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
78 int txt_x = 0, txt_y = 0;
80 int limit = getmaxx(strwin) - 5;
82 char buffer[MAX_COLS];
84 if (argv[level] == 0) {
91 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
95 txtwin = derwin(txtbox,
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 */
111 wmove(txtwin, txt_y, txt_x);
113 if ((fp = fopen(argv[level], "r")) != 0) {
114 while ((ch = fgetc(fp)) != EOF) {
115 if (waddch(txtwin, UChar(ch)) != OK) {
121 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
124 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
128 if (txt_y < getmaxy(txtwin) - 1)
149 if (txt_x < getmaxx(txtwin) - 1)
155 recursive_test(level + 1, argv, chrwin, strwin);
158 wnoutrefresh(txtbox);
161 wnoutrefresh(txtwin);
179 MvWPrintw(chrwin, 0, 0, "line:");
182 if (txtwin != stdscr) {
183 wmove(txtwin, txt_y, txt_x);
185 if (winstr(txtwin, buffer) != ERR) {
186 show_1st(chrwin, 0, buffer);
188 if (mvwinstr(txtwin, txt_y, txt_x, buffer) != ERR) {
189 showmore(chrwin, 1, buffer);
194 if (instr(buffer) != ERR) {
195 show_1st(chrwin, 0, buffer);
197 if (mvinstr(txt_y, txt_x, buffer) != ERR) {
198 showmore(chrwin, 1, buffer);
201 wnoutrefresh(chrwin);
203 MvWPrintw(strwin, 0, 0, "%4d:", limit);
206 if (txtwin != stdscr) {
207 wmove(txtwin, txt_y, txt_x);
208 if (winnstr(txtwin, buffer, limit) != ERR) {
209 show_1st(strwin, 0, buffer);
212 if (mvwinnstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
213 showmore(strwin, 1, buffer);
217 if (innstr(buffer, limit) != ERR) {
218 show_1st(strwin, 0, buffer);
221 if (mvinnstr(txt_y, txt_x, buffer, limit) != ERR) {
222 showmore(strwin, 1, buffer);
226 wnoutrefresh(strwin);
236 main(int argc, char *argv[])
242 setlocale(LC_ALL, "");
245 fprintf(stderr, "usage: %s file\n", argv[0]);
251 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
253 wnoutrefresh(chrbox);
255 chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
256 strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
258 recursive_test(1, argv, chrwin, strwin);
261 ExitProgram(EXIT_SUCCESS);