1 /****************************************************************************
2 * Copyright (c) 2007-2010,2017 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: test_instr.c,v 1.9 2017/09/06 09:27:45 tom Exp $
31 * Author: Thomas E Dickey
33 * Demonstrate the instr functions from the curses library.
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);
45 #include <test.priv.h>
53 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
57 show_1st(WINDOW *win, int line, char *buffer)
59 MvWAddStr(win, line, 5, buffer);
63 showmore(WINDOW *win, int line, char *buffer)
67 show_1st(win, line, buffer);
71 recursive_test(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
77 int txt_x = 0, txt_y = 0;
79 int limit = getmaxx(strwin) - 5;
81 char buffer[MAX_COLS];
83 if (argv[level] == 0) {
90 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
94 txtwin = derwin(txtbox,
104 keypad(txtwin, TRUE); /* enable keyboard mapping */
105 (void) cbreak(); /* take input chars one at a time, no wait for \n */
106 (void) noecho(); /* don't echo input */
110 wmove(txtwin, txt_y, txt_x);
112 if ((fp = fopen(argv[level], "r")) != 0) {
113 while ((ch = fgetc(fp)) != EOF) {
114 if (waddch(txtwin, UChar(ch)) != OK) {
120 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
123 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
127 if (txt_y < getmaxy(txtwin) - 1)
148 if (txt_x < getmaxx(txtwin) - 1)
154 recursive_test(level + 1, argv, chrwin, strwin);
157 wnoutrefresh(txtbox);
160 wnoutrefresh(txtwin);
178 MvWPrintw(chrwin, 0, 0, "line:");
181 if (txtwin != stdscr) {
182 wmove(txtwin, txt_y, txt_x);
184 if (winstr(txtwin, buffer) != ERR) {
185 show_1st(chrwin, 0, buffer);
187 if (mvwinstr(txtwin, txt_y, txt_x, buffer) != ERR) {
188 showmore(chrwin, 1, buffer);
193 if (instr(buffer) != ERR) {
194 show_1st(chrwin, 0, buffer);
196 if (mvinstr(txt_y, txt_x, buffer) != ERR) {
197 showmore(chrwin, 1, buffer);
200 wnoutrefresh(chrwin);
202 MvWPrintw(strwin, 0, 0, "%4d:", limit);
205 if (txtwin != stdscr) {
206 wmove(txtwin, txt_y, txt_x);
207 if (winnstr(txtwin, buffer, limit) != ERR) {
208 show_1st(strwin, 0, buffer);
211 if (mvwinnstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
212 showmore(strwin, 1, buffer);
216 if (innstr(buffer, limit) != ERR) {
217 show_1st(strwin, 0, buffer);
220 if (mvinnstr(txt_y, txt_x, buffer, limit) != ERR) {
221 showmore(strwin, 1, buffer);
225 wnoutrefresh(strwin);
235 main(int argc, char *argv[])
241 setlocale(LC_ALL, "");
244 fprintf(stderr, "usage: %s file\n", argv[0]);
250 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
252 wnoutrefresh(chrbox);
254 chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
255 strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
257 recursive_test(1, argv, chrwin, strwin);
260 ExitProgram(EXIT_SUCCESS);