1 /****************************************************************************
2 * Copyright (c) 2007 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.4 2007/07/21 19:38:04 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 test_inchs(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) {
89 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
93 txtwin = derwin(txtbox,
103 keypad(txtwin, TRUE); /* enable keyboard mapping */
104 (void) cbreak(); /* take input chars one at a time, no wait for \n */
105 (void) noecho(); /* don't echo input */
109 wmove(txtwin, txt_y, txt_x);
111 if ((fp = fopen(argv[level], "r")) != 0) {
112 while ((ch = fgetc(fp)) != EOF) {
113 if (waddch(txtwin, UChar(ch)) != OK) {
119 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
122 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
126 if (txt_y < getmaxy(txtwin) - 1)
147 if (txt_x < getmaxx(txtwin) - 1)
153 test_inchs(level + 1, argv, chrwin, strwin);
156 wnoutrefresh(txtbox);
159 wnoutrefresh(txtwin);
177 mvwprintw(chrwin, 0, 0, "line:");
180 if (txtwin != stdscr) {
181 wmove(txtwin, txt_y, txt_x);
183 if (winstr(txtwin, buffer) != ERR) {
184 show_1st(chrwin, 0, buffer);
186 if (mvwinstr(txtwin, txt_y, txt_x, buffer) != ERR) {
187 showmore(chrwin, 1, buffer);
192 if (instr(buffer) != ERR) {
193 show_1st(chrwin, 0, buffer);
195 if (mvinstr(txt_y, txt_x, buffer) != ERR) {
196 showmore(chrwin, 1, buffer);
199 wnoutrefresh(chrwin);
201 mvwprintw(strwin, 0, 0, "%4d:", limit);
204 if (txtwin != stdscr) {
205 wmove(txtwin, txt_y, txt_x);
206 if (winnstr(txtwin, buffer, limit) != ERR) {
207 show_1st(strwin, 0, buffer);
210 if (mvwinnstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
211 showmore(strwin, 1, buffer);
215 if (innstr(buffer, limit) != ERR) {
216 show_1st(strwin, 0, buffer);
219 if (mvinnstr(txt_y, txt_x, buffer, limit) != ERR) {
220 showmore(strwin, 1, buffer);
224 wnoutrefresh(strwin);
234 main(int argc, char *argv[])
240 setlocale(LC_ALL, "");
243 fprintf(stderr, "usage: %s file\n", argv[0]);
249 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
251 wnoutrefresh(chrbox);
253 chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
254 strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
256 test_inchs(1, argv, chrwin, strwin);
259 ExitProgram(EXIT_SUCCESS);