blob: ae38fa21752167566de9d00a92f67521c916f50a [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
2 * Copyright (c) 2002-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: inserts.c,v 1.27 2012/12/16 00:35:27 tom Exp $
30 *
31 * Demonstrate the winsstr() and winsch functions.
32 * Thomas Dickey - 2002/10/19
33 */
34
35#include <test.priv.h>
36
37#if HAVE_WINSSTR
38
39#include <linedata.h>
40
41#define InsNStr insnstr
42#define InsStr insstr
43#define MvInsNStr (void) mvinsnstr
44#define MvInsStr (void) mvinsstr
45#define MvWInsNStr (void) mvwinsnstr
46#define MvWInsStr (void) mvwinsstr
47#define WInsNStr winsnstr
48#define WInsStr winsstr
49
50#define InsCh insch
51#define MvInsCh (void) mvinsch
52#define MvWInsCh (void) mvwinsch
53#define WInsCh winsch
54
55#define MY_TABSIZE 8
56
57typedef enum {
58 oDefault = 0,
59 oMove = 1,
60 oWindow = 2,
61 oMoveWindow = 3
62} Options;
63
64static bool m_opt = FALSE;
65static bool w_opt = FALSE;
66static int n_opt = -1;
67
68static void
69legend(WINDOW *win, int level, Options state, char *buffer, int length)
70{
71 const char *showstate;
72
73 switch (state) {
74 default:
75 case oDefault:
76 showstate = "";
77 break;
78 case oMove:
79 showstate = " (mvXXX)";
80 break;
81 case oWindow:
82 showstate = " (winXXX)";
83 break;
84 case oMoveWindow:
85 showstate = " (mvwinXXX)";
86 break;
87 }
88
89 wmove(win, 0, 0);
90 wprintw(win,
91 "The Strings/Chars displays should match. Enter any characters, except:\n");
92 wprintw(win,
93 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
94 wclrtoeol(win);
95 wprintw(win, "Level %d,%s inserted %d characters <%s>", level,
96 showstate, length, buffer);
97}
98
99static int
100ColOf(char *buffer, int length, int margin)
101{
102 int n;
103 int result;
104
105 for (n = 0, result = margin + 1; n < length; ++n) {
106 int ch = UChar(buffer[n]);
107 switch (ch) {
108 case '\n':
109 /* actually newline should clear the remainder of the line
110 * and move to the next line - but that seems a little awkward
111 * in this example.
112 */
113 case '\r':
114 result = 0;
115 break;
116 case '\b':
117 if (result > 0)
118 --result;
119 break;
120 case '\t':
121 result += (MY_TABSIZE - (result % MY_TABSIZE));
122 break;
123 case '\177':
124 result += 2;
125 break;
126 default:
127 ++result;
128 if (ch < 32)
129 ++result;
130 break;
131 }
132 }
133 return result;
134}
135
136#define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
137static void
138test_inserts(int level)
139{
140 static bool first = TRUE;
141
142 int ch;
143 int limit;
144 int row = 1;
145 int col;
146 int row2, col2;
147 int length;
148 char buffer[BUFSIZ];
149 WINDOW *look = 0;
150 WINDOW *work = 0;
151 WINDOW *show = 0;
152 int margin = (2 * MY_TABSIZE) - 1;
153 Options option = (Options) ((unsigned) (m_opt
154 ? oMove
155 : oDefault)
156 | (unsigned) ((w_opt || (level > 0))
157 ? oWindow
158 : oDefault));
159
160 if (first) {
161 static char cmd[80];
162 setlocale(LC_ALL, "");
163
164 putenv(strcpy(cmd, "TABSIZE=8"));
165
166 initscr();
167 (void) cbreak(); /* take input chars one at a time, no wait for \n */
168 (void) noecho(); /* don't echo input */
169 keypad(stdscr, TRUE);
170
171 /*
172 * Show the characters inserted in color, to distinguish from those
173 * that are shifted.
174 */
175 if (has_colors()) {
176 start_color();
177 init_pair(1, COLOR_WHITE, COLOR_BLUE);
178 }
179 }
180
181 limit = LINES - 5;
182 if (level > 0) {
183 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
184 work = newwin(limit - 2, COLS - (2 * level), 1, level);
185 show = newwin(4, COLS, limit + 1, 0);
186 box(look, 0, 0);
187 wnoutrefresh(look);
188 limit -= 2;
189 } else {
190 work = stdscr;
191 show = derwin(stdscr, 4, COLS, limit + 1, 0);
192 }
193 keypad(work, TRUE);
194
195 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
196 MvWVLine(work, row, col, '.', limit - 2);
197
198 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
199 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
200 limit /= 2;
201
202 MvWAddStr(work, 1, 2, "String");
203 MvWAddStr(work, limit + 1, 2, "Chars");
204 wnoutrefresh(work);
205
206 buffer[length = 0] = '\0';
207 legend(show, level, option, buffer, length);
208 wnoutrefresh(show);
209
210 doupdate();
211
212 if (has_colors()) {
213 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
214 }
215
216 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
217 wmove(work, row, margin + 1);
218 switch (ch) {
219 case key_RECUR:
220 test_inserts(level + 1);
221
222 if (look)
223 touchwin(look);
224 touchwin(work);
225 touchwin(show);
226
227 if (look)
228 wnoutrefresh(look);
229 wnoutrefresh(work);
230 wnoutrefresh(show);
231
232 doupdate();
233 break;
234 case key_NEWLINE:
235 if (row < limit) {
236 ++row;
237 /* put the whole string in, all at once */
238 col2 = margin + 1;
239 switch (option) {
240 case oDefault:
241 if (n_opt > 1) {
242 for (col = 0; col < length; col += n_opt) {
243 col2 = ColOf(buffer, col, margin);
244 if (move(row, col2) != ERR) {
245 InsNStr(buffer + col, LEN(col));
246 }
247 }
248 } else {
249 if (move(row, col2) != ERR) {
250 InsStr(buffer);
251 }
252 }
253 break;
254 case oMove:
255 if (n_opt > 1) {
256 for (col = 0; col < length; col += n_opt) {
257 col2 = ColOf(buffer, col, margin);
258 MvInsNStr(row, col2, buffer + col, LEN(col));
259 }
260 } else {
261 MvInsStr(row, col2, buffer);
262 }
263 break;
264 case oWindow:
265 if (n_opt > 1) {
266 for (col = 0; col < length; col += n_opt) {
267 col2 = ColOf(buffer, col, margin);
268 if (wmove(work, row, col2) != ERR) {
269 WInsNStr(work, buffer + col, LEN(col));
270 }
271 }
272 } else {
273 if (wmove(work, row, col2) != ERR) {
274 WInsStr(work, buffer);
275 }
276 }
277 break;
278 case oMoveWindow:
279 if (n_opt > 1) {
280 for (col = 0; col < length; col += n_opt) {
281 col2 = ColOf(buffer, col, margin);
282 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
283 }
284 } else {
285 MvWInsStr(work, row, col2, buffer);
286 }
287 break;
288 }
289
290 /* do the corresponding single-character insertion */
291 row2 = limit + row;
292 for (col = 0; col < length; ++col) {
293 col2 = ColOf(buffer, col, margin);
294 switch (option) {
295 case oDefault:
296 if (move(row2, col2) != ERR) {
297 InsCh(UChar(buffer[col]));
298 }
299 break;
300 case oMove:
301 MvInsCh(row2, col2, UChar(buffer[col]));
302 break;
303 case oWindow:
304 if (wmove(work, row2, col2) != ERR) {
305 WInsCh(work, UChar(buffer[col]));
306 }
307 break;
308 case oMoveWindow:
309 MvWInsCh(work, row2, col2, UChar(buffer[col]));
310 break;
311 }
312 }
313 } else {
314 beep();
315 }
316 break;
317 default:
318 if (ch <= 0 || ch > 255) {
319 beep();
320 break;
321 }
322 buffer[length++] = (char) ch;
323 buffer[length] = '\0';
324
325 /* put the string in, one character at a time */
326 col = ColOf(buffer, length - 1, margin);
327 switch (option) {
328 case oDefault:
329 if (move(row, col) != ERR) {
330 InsStr(buffer + length - 1);
331 }
332 break;
333 case oMove:
334 MvInsStr(row, col, buffer + length - 1);
335 break;
336 case oWindow:
337 if (wmove(work, row, col) != ERR) {
338 WInsStr(work, buffer + length - 1);
339 }
340 break;
341 case oMoveWindow:
342 MvWInsStr(work, row, col, buffer + length - 1);
343 break;
344 }
345
346 /* do the corresponding single-character insertion */
347 switch (option) {
348 case oDefault:
349 if (move(limit + row, col) != ERR) {
350 InsCh(UChar(ch));
351 }
352 break;
353 case oMove:
354 MvInsCh(limit + row, col, UChar(ch));
355 break;
356 case oWindow:
357 if (wmove(work, limit + row, col) != ERR) {
358 WInsCh(work, UChar(ch));
359 }
360 break;
361 case oMoveWindow:
362 MvWInsCh(work, limit + row, col, UChar(ch));
363 break;
364 }
365
366 wnoutrefresh(work);
367
368 legend(show, level, option, buffer, length);
369 wnoutrefresh(show);
370
371 doupdate();
372 break;
373 }
374 }
375 if (level > 0) {
376 delwin(work);
377 delwin(look);
378 }
379 delwin(show);
380}
381
382static void
383usage(void)
384{
385 static const char *tbl[] =
386 {
387 "Usage: inserts [options]"
388 ,""
389 ,"Options:"
390 ," -f FILE read data from given file"
391 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
392 ," -m perform wmove/move separately from insert-functions"
393 ," -w use window-parameter even when stdscr would be implied"
394 };
395 unsigned n;
396 for (n = 0; n < SIZEOF(tbl); ++n)
397 fprintf(stderr, "%s\n", tbl[n]);
398 ExitProgram(EXIT_FAILURE);
399}
400
401int
402main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
403{
404 int ch;
405
406 setlocale(LC_ALL, "");
407
408 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
409 switch (ch) {
410 case 'f':
411 init_linedata(optarg);
412 break;
413 case 'm':
414 m_opt = TRUE;
415 break;
416 case 'n':
417 n_opt = atoi(optarg);
418 if (n_opt == 0)
419 n_opt = -1;
420 break;
421 case 'w':
422 w_opt = TRUE;
423 break;
424 default:
425 usage();
426 break;
427 }
428 }
429 if (optind < argc)
430 usage();
431
432 test_inserts(0);
433 endwin();
434 ExitProgram(EXIT_SUCCESS);
435}
436#else
437int
438main(void)
439{
440 printf("This program requires the winsstr function\n");
441 ExitProgram(EXIT_FAILURE);
442}
443#endif /* HAVE_WINSSTR */