blob: be3aab86f57829499c1b4d4156414023868dd040 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
2 * Copyright (c) 2007-2010,2012 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
28/*
29 * $Id: inchs.c,v 1.12 2012/11/18 01:58:15 tom Exp $
30 *
31 * Author: Thomas E Dickey
32 */
33/*
34 chtype inch(void);
35 chtype winch(WINDOW *win);
36 chtype mvinch(int y, int x);
37 chtype mvwinch(WINDOW *win, int y, int x);
38 int inchstr(chtype *chstr);
39 int inchnstr(chtype *chstr, int n);
40 int winchstr(WINDOW *win, chtype *chstr);
41 int winchnstr(WINDOW *win, chtype *chstr, int n);
42 int mvinchstr(int y, int x, chtype *chstr);
43 int mvinchnstr(int y, int x, chtype *chstr, int n);
44 int mvwinchstr(WINDOW *win, int y, int x, chtype *chstr);
45 int mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n);
46*/
47
48#include <test.priv.h>
49
50#define BASE_Y 7
51#define MAX_COLS 1024
52
53static void
54failed(const char *s)
55{
56 int save = errno;
57 endwin();
58 errno = save;
59 perror(s);
60 ExitProgram(EXIT_FAILURE);
61}
62
63static bool
64Quit(int ch)
65{
66 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
67}
68
69static int
70test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
71{
72 WINDOW *txtbox = 0;
73 WINDOW *txtwin = 0;
74 FILE *fp;
75 int ch, j;
76 int txt_x = 0, txt_y = 0;
77 int base_y;
78 int limit;
79 chtype text[MAX_COLS];
80
81 if (argv[level] == 0) {
82 beep();
83 return FALSE;
84 }
85
86 if (level > 1) {
87 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
88 box(txtbox, 0, 0);
89 wnoutrefresh(txtbox);
90
91 txtwin = derwin(txtbox,
92 getmaxy(txtbox) - 2,
93 getmaxx(txtbox) - 2,
94 1, 1);
95 base_y = 0;
96 } else {
97 txtwin = stdscr;
98 base_y = BASE_Y;
99 }
100 if (txtwin == 0)
101 failed("cannot create txtwin");
102
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 */
106
107 txt_y = base_y;
108 txt_x = 0;
109 wmove(txtwin, txt_y, txt_x);
110
111 if ((fp = fopen(argv[level], "r")) != 0) {
112 while ((j = fgetc(fp)) != EOF) {
113 if (waddch(txtwin, UChar(j)) != OK) {
114 break;
115 }
116 }
117 fclose(fp);
118 } else {
119 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
120 }
121
122 while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
123 switch (j) {
124 case KEY_DOWN:
125 case 'j':
126 if (txt_y < getmaxy(txtwin) - 1)
127 txt_y++;
128 else
129 beep();
130 break;
131 case KEY_UP:
132 case 'k':
133 if (txt_y > base_y)
134 txt_y--;
135 else
136 beep();
137 break;
138 case KEY_LEFT:
139 case 'h':
140 if (txt_x > 0)
141 txt_x--;
142 else
143 beep();
144 break;
145 case KEY_RIGHT:
146 case 'l':
147 if (txt_x < getmaxx(txtwin) - 1)
148 txt_x++;
149 else
150 beep();
151 break;
152 case 'w':
153 test_inchs(level + 1, argv, chrwin, strwin);
154 if (txtbox != 0) {
155 touchwin(txtbox);
156 wnoutrefresh(txtbox);
157 } else {
158 touchwin(txtwin);
159 wnoutrefresh(txtwin);
160 }
161 break;
162 default:
163 beep();
164 break;
165 }
166
167 MvWPrintw(chrwin, 0, 0, "char:");
168 wclrtoeol(chrwin);
169
170 if (txtwin != stdscr) {
171 wmove(txtwin, txt_y, txt_x);
172
173 if ((ch = (int) winch(txtwin)) != ERR) {
174 if (waddch(chrwin, (chtype) ch) != ERR) {
175 for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
176 if ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) {
177 if (waddch(chrwin, (chtype) ch) == ERR) {
178 break;
179 }
180 } else {
181 break;
182 }
183 }
184 }
185 }
186 } else {
187 move(txt_y, txt_x);
188
189 if ((ch = (int) inch()) != ERR) {
190 if (waddch(chrwin, (chtype) ch) != ERR) {
191 for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
192 if ((ch = (int) mvinch(txt_y, j)) != ERR) {
193 if (waddch(chrwin, (chtype) ch) == ERR) {
194 break;
195 }
196 } else {
197 break;
198 }
199 }
200 }
201 }
202 }
203 wnoutrefresh(chrwin);
204
205 MvWPrintw(strwin, 0, 0, "text:");
206 wclrtobot(strwin);
207
208 limit = getmaxx(strwin) - 5;
209
210 if (txtwin != stdscr) {
211 wmove(txtwin, txt_y, txt_x);
212 if (winchstr(txtwin, text) != ERR) {
213 MvWAddChStr(strwin, 0, 5, text);
214 }
215
216 wmove(txtwin, txt_y, txt_x);
217 if (winchnstr(txtwin, text, limit) != ERR) {
218 MvWAddChStr(strwin, 1, 5, text);
219 }
220
221 if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
222 MvWAddChStr(strwin, 2, 5, text);
223 }
224
225 if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
226 MvWAddChStr(strwin, 3, 5, text);
227 }
228 } else {
229 move(txt_y, txt_x);
230 if (inchstr(text) != ERR) {
231 MvWAddChStr(strwin, 0, 5, text);
232 }
233
234 move(txt_y, txt_x);
235 if (inchnstr(text, limit) != ERR) {
236 MvWAddChStr(strwin, 1, 5, text);
237 }
238
239 if (mvinchstr(txt_y, txt_x, text) != ERR) {
240 MvWAddChStr(strwin, 2, 5, text);
241 }
242
243 if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
244 MvWAddChStr(strwin, 3, 5, text);
245 }
246 }
247
248 wnoutrefresh(strwin);
249 }
250 if (level > 1) {
251 delwin(txtwin);
252 delwin(txtbox);
253 }
254 return TRUE;
255}
256
257int
258main(int argc, char *argv[])
259{
260 WINDOW *chrbox;
261 WINDOW *chrwin;
262 WINDOW *strwin;
263
264 setlocale(LC_ALL, "");
265
266 if (argc < 2) {
267 fprintf(stderr, "usage: %s file\n", argv[0]);
268 return EXIT_FAILURE;
269 }
270
271 initscr();
272
273 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
274 box(chrbox, 0, 0);
275 wnoutrefresh(chrbox);
276
277 chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
278 strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
279
280 test_inchs(1, argv, chrwin, strwin);
281
282 endwin();
283 ExitProgram(EXIT_SUCCESS);
284}