1 /****************************************************************************
2 * Copyright (c) 2002-2010,2012 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.27 2012/12/16 00:35:27 tom Exp $
31 * Demonstrate the winsstr() and winsch functions.
32 * Thomas Dickey - 2002/10/19
35 #include <test.priv.h>
41 #define InsNStr insnstr
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
51 #define MvInsCh (void) mvinsch
52 #define MvWInsCh (void) mvwinsch
64 static bool m_opt = FALSE;
65 static bool w_opt = FALSE;
66 static int n_opt = -1;
69 legend(WINDOW *win, int level, Options state, char *buffer, int length)
71 const char *showstate;
79 showstate = " (mvXXX)";
82 showstate = " (winXXX)";
85 showstate = " (mvwinXXX)";
91 "The Strings/Chars displays should match. Enter any characters, except:\n");
93 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
95 wprintw(win, "Level %d,%s inserted %d characters <%s>", level,
96 showstate, length, buffer);
100 ColOf(char *buffer, int length, int margin)
105 for (n = 0, result = margin + 1; n < length; ++n) {
106 int ch = UChar(buffer[n]);
109 /* actually newline should clear the remainder of the line
110 * and move to the next line - but that seems a little awkward
121 result += (MY_TABSIZE - (result % MY_TABSIZE));
136 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
138 test_inserts(int level)
140 static bool first = TRUE;
152 int margin = (2 * MY_TABSIZE) - 1;
153 Options option = (Options) ((unsigned) (m_opt
156 | (unsigned) ((w_opt || (level > 0))
162 setlocale(LC_ALL, "");
164 putenv(strcpy(cmd, "TABSIZE=8"));
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 * Show the characters inserted in color, to distinguish from those
177 init_pair(1, COLOR_WHITE, COLOR_BLUE);
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);
191 show = derwin(stdscr, 4, COLS, limit + 1, 0);
195 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
196 MvWVLine(work, row, col, '.', limit - 2);
198 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
199 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
202 MvWAddStr(work, 1, 2, "String");
203 MvWAddStr(work, limit + 1, 2, "Chars");
206 buffer[length = 0] = '\0';
207 legend(show, level, option, buffer, length);
213 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
216 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
217 wmove(work, row, margin + 1);
220 test_inserts(level + 1);
237 /* put the whole string in, all at once */
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));
249 if (move(row, col2) != ERR) {
256 for (col = 0; col < length; col += n_opt) {
257 col2 = ColOf(buffer, col, margin);
258 MvInsNStr(row, col2, buffer + col, LEN(col));
261 MvInsStr(row, col2, buffer);
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));
273 if (wmove(work, row, col2) != ERR) {
274 WInsStr(work, buffer);
280 for (col = 0; col < length; col += n_opt) {
281 col2 = ColOf(buffer, col, margin);
282 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
285 MvWInsStr(work, row, col2, buffer);
290 /* do the corresponding single-character insertion */
292 for (col = 0; col < length; ++col) {
293 col2 = ColOf(buffer, col, margin);
296 if (move(row2, col2) != ERR) {
297 InsCh(UChar(buffer[col]));
301 MvInsCh(row2, col2, UChar(buffer[col]));
304 if (wmove(work, row2, col2) != ERR) {
305 WInsCh(work, UChar(buffer[col]));
309 MvWInsCh(work, row2, col2, UChar(buffer[col]));
318 if (ch <= 0 || ch > 255) {
322 buffer[length++] = (char) ch;
323 buffer[length] = '\0';
325 /* put the string in, one character at a time */
326 col = ColOf(buffer, length - 1, margin);
329 if (move(row, col) != ERR) {
330 InsStr(buffer + length - 1);
334 MvInsStr(row, col, buffer + length - 1);
337 if (wmove(work, row, col) != ERR) {
338 WInsStr(work, buffer + length - 1);
342 MvWInsStr(work, row, col, buffer + length - 1);
346 /* do the corresponding single-character insertion */
349 if (move(limit + row, col) != ERR) {
354 MvInsCh(limit + row, col, UChar(ch));
357 if (wmove(work, limit + row, col) != ERR) {
358 WInsCh(work, UChar(ch));
362 MvWInsCh(work, limit + row, col, UChar(ch));
368 legend(show, level, option, buffer, length);
385 static const char *tbl[] =
387 "Usage: inserts [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"
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, "f:mn:w")) != -1) {
411 init_linedata(optarg);
417 n_opt = atoi(optarg);
434 ExitProgram(EXIT_SUCCESS);
440 printf("This program requires the winsstr function\n");
441 ExitProgram(EXIT_FAILURE);
443 #endif /* HAVE_WINSSTR */