1 /****************************************************************************
2 * Copyright (c) 2002-2006,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: inserts.c,v 1.17 2007/03/11 00:19:00 tom Exp $
31 * Demonstrate the winsstr() and winsch functions.
32 * Thomas Dickey - 2002/10/19
35 #include <test.priv.h>
39 #define InsNStr insnstr
41 #define MvInsNStr mvinsnstr
42 #define MvInsStr mvinsstr
43 #define MvWInsNStr mvwinsnstr
44 #define MvWInsStr mvwinsstr
45 #define WInsNStr winsnstr
46 #define WInsStr winsstr
49 #define MvInsCh mvinsch
50 #define MvWInsCh mvwinsch
62 static bool m_opt = FALSE;
63 static bool w_opt = FALSE;
64 static int n_opt = -1;
67 legend(WINDOW *win, int level, Options state, char *buffer, int length)
69 NCURSES_CONST char *showstate;
77 showstate = " (mvXXX)";
80 showstate = " (winXXX)";
83 showstate = " (mvwinXXX)";
89 "The Strings/Chars displays should match. Enter any characters, except:\n");
91 "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
93 wprintw(win, "Level %d,%s inserted %d characters <%s>", level,
94 showstate, length, buffer);
98 ColOf(char *buffer, int length, int margin)
103 for (n = 0, result = margin + 1; n < length; ++n) {
104 int ch = UChar(buffer[n]);
107 /* actually newline should clear the remainder of the line
108 * and move to the next line - but that seems a little awkward
119 result += (MY_TABSIZE - (result % MY_TABSIZE));
134 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
136 test_inserts(int level)
138 static bool first = TRUE;
150 int margin = (2 * MY_TABSIZE) - 1;
151 Options option = (Options) ((unsigned) (m_opt
154 | (unsigned) ((w_opt || (level > 0))
160 setlocale(LC_ALL, "");
162 putenv(strcpy(cmd, "TABSIZE=8"));
165 (void) cbreak(); /* take input chars one at a time, no wait for \n */
166 (void) noecho(); /* don't echo input */
167 keypad(stdscr, TRUE);
172 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
173 work = newwin(limit - 2, COLS - (2 * level), 1, level);
174 show = newwin(4, COLS, limit + 1, 0);
180 show = derwin(stdscr, 4, COLS, limit + 1, 0);
184 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
185 mvwvline(work, row, col, '.', limit - 2);
187 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
188 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
191 mvwaddstr(work, 1, 2, "String");
192 mvwaddstr(work, limit + 1, 2, "Chars");
195 buffer[length = 0] = '\0';
196 legend(show, level, option, buffer, length);
202 * Show the characters inserted in color, to distinguish from those that
207 init_pair(1, COLOR_WHITE, COLOR_BLUE);
208 wbkgdset(work, COLOR_PAIR(1) | ' ');
211 while ((ch = wgetch(work)) != 'q') {
216 wmove(work, row, margin + 1);
219 test_inserts(level + 1);
235 /* put the whole string in, all at once */
240 for (col = 0; col < length; col += n_opt) {
241 col2 = ColOf(buffer, col, margin);
242 if (move(row, col2) != ERR) {
243 InsNStr(buffer + col, LEN(col));
247 if (move(row, col2) != ERR) {
254 for (col = 0; col < length; col += n_opt) {
255 col2 = ColOf(buffer, col, margin);
256 MvInsNStr(row, col2, buffer + col, LEN(col));
259 MvInsStr(row, col2, buffer);
264 for (col = 0; col < length; col += n_opt) {
265 col2 = ColOf(buffer, col, margin);
266 if (wmove(work, row, col2) != ERR) {
267 WInsNStr(work, buffer + col, LEN(col));
271 if (wmove(work, row, col2) != ERR) {
272 WInsStr(work, buffer);
278 for (col = 0; col < length; col += n_opt) {
279 col2 = ColOf(buffer, col, margin);
280 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
283 MvWInsStr(work, row, col2, buffer);
288 /* do the corresponding single-character insertion */
290 for (col = 0; col < length; ++col) {
291 col2 = ColOf(buffer, col, margin);
294 if (move(row2, col2) != ERR) {
295 InsCh(UChar(buffer[col]));
299 MvInsCh(row2, col2, UChar(buffer[col]));
302 if (wmove(work, row2, col2) != ERR) {
303 WInsCh(work, UChar(buffer[col]));
307 MvWInsCh(work, row2, col2, UChar(buffer[col]));
319 if (ch <= 0 || ch > 255) {
323 buffer[length++] = ch;
324 buffer[length] = '\0';
326 /* put the string in, one character at a time */
327 col = ColOf(buffer, length - 1, margin);
330 if (move(row, col) != ERR) {
331 InsStr(buffer + length - 1);
335 MvInsStr(row, col, buffer + length - 1);
338 if (wmove(work, row, col) != ERR) {
339 WInsStr(work, buffer + length - 1);
343 MvWInsStr(work, row, col, buffer + length - 1);
347 /* do the corresponding single-character insertion */
350 if (move(limit + row, col) != ERR) {
355 MvInsCh(limit + row, col, UChar(ch));
358 if (wmove(work, limit + row, col) != ERR) {
359 WInsCh(work, UChar(ch));
363 MvWInsCh(work, limit + row, col, UChar(ch));
369 legend(show, level, option, buffer, length);
386 static const char *tbl[] =
388 "Usage: inserts [options]"
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"
396 for (n = 0; n < SIZEOF(tbl); ++n)
397 fprintf(stderr, "%s\n", tbl[n]);
398 ExitProgram(EXIT_FAILURE);
402 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
406 setlocale(LC_ALL, "");
408 while ((ch = getopt(argc, argv, "mn:w")) != EOF) {
414 n_opt = atoi(optarg);
431 ExitProgram(EXIT_SUCCESS);
437 printf("This program requires the winsstr function\n");
438 ExitProgram(EXIT_FAILURE);
440 #endif /* HAVE_WINSSTR */