missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / inserts.c
bloba12fa4011283d28a66bab5400bf8483e5e047ffd
1 /****************************************************************************
2 * Copyright (c) 2002-2009,2010 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 ****************************************************************************/
29 * $Id: inserts.c,v 1.23 2010/12/12 00:19:55 tom Exp $
31 * Demonstrate the winsstr() and winsch functions.
32 * Thomas Dickey - 2002/10/19
35 #include <test.priv.h>
37 #if HAVE_WINSSTR
39 #include <linedata.h>
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
50 #define InsCh insch
51 #define MvInsCh (void) mvinsch
52 #define MvWInsCh (void) mvwinsch
53 #define WInsCh winsch
55 #define MY_TABSIZE 8
57 typedef enum {
58 oDefault = 0,
59 oMove = 1,
60 oWindow = 2,
61 oMoveWindow = 3
62 } Options;
64 static bool m_opt = FALSE;
65 static bool w_opt = FALSE;
66 static int n_opt = -1;
68 static void
69 legend(WINDOW *win, int level, Options state, char *buffer, int length)
71 const char *showstate;
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;
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);
99 static int
100 ColOf(char *buffer, int length, int margin)
102 int n;
103 int result;
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.
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;
133 return result;
136 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
137 static void
138 test_inserts(int level)
140 static bool first = TRUE;
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));
160 if (first) {
161 static char cmd[80];
162 setlocale(LC_ALL, "");
164 putenv(strcpy(cmd, "TABSIZE=8"));
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);
172 limit = LINES - 5;
173 if (level > 0) {
174 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
175 work = newwin(limit - 2, COLS - (2 * level), 1, level);
176 show = newwin(4, COLS, limit + 1, 0);
177 box(look, 0, 0);
178 wnoutrefresh(look);
179 limit -= 2;
180 } else {
181 work = stdscr;
182 show = derwin(stdscr, 4, COLS, limit + 1, 0);
184 keypad(work, TRUE);
186 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
187 MvWVLine(work, row, col, '.', limit - 2);
189 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
190 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
191 limit /= 2;
193 MvWAddStr(work, 1, 2, "String");
194 MvWAddStr(work, limit + 1, 2, "Chars");
195 wnoutrefresh(work);
197 buffer[length = 0] = '\0';
198 legend(show, level, option, buffer, length);
199 wnoutrefresh(show);
201 doupdate();
204 * Show the characters inserted in color, to distinguish from those that
205 * are shifted.
207 if (has_colors()) {
208 start_color();
209 init_pair(1, COLOR_WHITE, COLOR_BLUE);
210 wbkgdset(work, COLOR_PAIR(1) | ' ');
213 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
214 wmove(work, row, margin + 1);
215 switch (ch) {
216 case key_RECUR:
217 test_inserts(level + 1);
219 touchwin(look);
220 touchwin(work);
221 touchwin(show);
223 wnoutrefresh(look);
224 wnoutrefresh(work);
225 wnoutrefresh(show);
227 doupdate();
228 break;
229 case key_NEWLINE:
230 if (row < limit) {
231 ++row;
232 /* put the whole string in, all at once */
233 col2 = margin + 1;
234 switch (option) {
235 case oDefault:
236 if (n_opt > 1) {
237 for (col = 0; col < length; col += n_opt) {
238 col2 = ColOf(buffer, col, margin);
239 if (move(row, col2) != ERR) {
240 InsNStr(buffer + col, LEN(col));
243 } else {
244 if (move(row, col2) != ERR) {
245 InsStr(buffer);
248 break;
249 case oMove:
250 if (n_opt > 1) {
251 for (col = 0; col < length; col += n_opt) {
252 col2 = ColOf(buffer, col, margin);
253 MvInsNStr(row, col2, buffer + col, LEN(col));
255 } else {
256 MvInsStr(row, col2, buffer);
258 break;
259 case oWindow:
260 if (n_opt > 1) {
261 for (col = 0; col < length; col += n_opt) {
262 col2 = ColOf(buffer, col, margin);
263 if (wmove(work, row, col2) != ERR) {
264 WInsNStr(work, buffer + col, LEN(col));
267 } else {
268 if (wmove(work, row, col2) != ERR) {
269 WInsStr(work, buffer);
272 break;
273 case oMoveWindow:
274 if (n_opt > 1) {
275 for (col = 0; col < length; col += n_opt) {
276 col2 = ColOf(buffer, col, margin);
277 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
279 } else {
280 MvWInsStr(work, row, col2, buffer);
282 break;
285 /* do the corresponding single-character insertion */
286 row2 = limit + row;
287 for (col = 0; col < length; ++col) {
288 col2 = ColOf(buffer, col, margin);
289 switch (option) {
290 case oDefault:
291 if (move(row2, col2) != ERR) {
292 InsCh(UChar(buffer[col]));
294 break;
295 case oMove:
296 MvInsCh(row2, col2, UChar(buffer[col]));
297 break;
298 case oWindow:
299 if (wmove(work, row2, col2) != ERR) {
300 WInsCh(work, UChar(buffer[col]));
302 break;
303 case oMoveWindow:
304 MvWInsCh(work, row2, col2, UChar(buffer[col]));
305 break;
308 } else {
309 beep();
311 break;
312 default:
313 if (ch <= 0 || ch > 255) {
314 beep();
315 break;
317 buffer[length++] = (char) ch;
318 buffer[length] = '\0';
320 /* put the string in, one character at a time */
321 col = ColOf(buffer, length - 1, margin);
322 switch (option) {
323 case oDefault:
324 if (move(row, col) != ERR) {
325 InsStr(buffer + length - 1);
327 break;
328 case oMove:
329 MvInsStr(row, col, buffer + length - 1);
330 break;
331 case oWindow:
332 if (wmove(work, row, col) != ERR) {
333 WInsStr(work, buffer + length - 1);
335 break;
336 case oMoveWindow:
337 MvWInsStr(work, row, col, buffer + length - 1);
338 break;
341 /* do the corresponding single-character insertion */
342 switch (option) {
343 case oDefault:
344 if (move(limit + row, col) != ERR) {
345 InsCh(UChar(ch));
347 break;
348 case oMove:
349 MvInsCh(limit + row, col, UChar(ch));
350 break;
351 case oWindow:
352 if (wmove(work, limit + row, col) != ERR) {
353 WInsCh(work, UChar(ch));
355 break;
356 case oMoveWindow:
357 MvWInsCh(work, limit + row, col, UChar(ch));
358 break;
361 wnoutrefresh(work);
363 legend(show, level, option, buffer, length);
364 wnoutrefresh(show);
366 doupdate();
367 break;
370 if (level > 0) {
371 delwin(show);
372 delwin(work);
373 delwin(look);
377 static void
378 usage(void)
380 static const char *tbl[] =
382 "Usage: inserts [options]"
384 ,"Options:"
385 ," -f FILE read data from given file"
386 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
387 ," -m perform wmove/move separately from insert-functions"
388 ," -w use window-parameter even when stdscr would be implied"
390 unsigned n;
391 for (n = 0; n < SIZEOF(tbl); ++n)
392 fprintf(stderr, "%s\n", tbl[n]);
393 ExitProgram(EXIT_FAILURE);
397 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
399 int ch;
401 setlocale(LC_ALL, "");
403 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
404 switch (ch) {
405 case 'f':
406 init_linedata(optarg);
407 break;
408 case 'm':
409 m_opt = TRUE;
410 break;
411 case 'n':
412 n_opt = atoi(optarg);
413 if (n_opt == 0)
414 n_opt = -1;
415 break;
416 case 'w':
417 w_opt = TRUE;
418 break;
419 default:
420 usage();
421 break;
424 if (optind < argc)
425 usage();
427 test_inserts(0);
428 endwin();
429 ExitProgram(EXIT_SUCCESS);
431 #else
433 main(void)
435 printf("This program requires the winsstr function\n");
436 ExitProgram(EXIT_FAILURE);
438 #endif /* HAVE_WINSSTR */