1 /****************************************************************************
2 * Copyright (c) 2002-2009,2010 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.21 2010/05/01 19:12:26 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 NCURSES_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, 'q' 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);
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);
182 show = derwin(stdscr, 4, COLS, limit + 1, 0);
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);
193 MvWAddStr(work, 1, 2, "String");
194 MvWAddStr(work, limit + 1, 2, "Chars");
197 buffer[length = 0] = '\0';
198 legend(show, level, option, buffer, length);
204 * Show the characters inserted in color, to distinguish from those that
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);
217 test_inserts(level + 1);
232 /* put the whole string in, all at once */
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));
244 if (move(row, col2) != ERR) {
251 for (col = 0; col < length; col += n_opt) {
252 col2 = ColOf(buffer, col, margin);
253 MvInsNStr(row, col2, buffer + col, LEN(col));
256 MvInsStr(row, col2, buffer);
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));
268 if (wmove(work, row, col2) != ERR) {
269 WInsStr(work, buffer);
275 for (col = 0; col < length; col += n_opt) {
276 col2 = ColOf(buffer, col, margin);
277 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
280 MvWInsStr(work, row, col2, buffer);
285 /* do the corresponding single-character insertion */
287 for (col = 0; col < length; ++col) {
288 col2 = ColOf(buffer, col, margin);
291 if (move(row2, col2) != ERR) {
292 InsCh(UChar(buffer[col]));
296 MvInsCh(row2, col2, UChar(buffer[col]));
299 if (wmove(work, row2, col2) != ERR) {
300 WInsCh(work, UChar(buffer[col]));
304 MvWInsCh(work, row2, col2, UChar(buffer[col]));
313 if (ch <= 0 || ch > 255) {
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);
324 if (move(row, col) != ERR) {
325 InsStr(buffer + length - 1);
329 MvInsStr(row, col, buffer + length - 1);
332 if (wmove(work, row, col) != ERR) {
333 WInsStr(work, buffer + length - 1);
337 MvWInsStr(work, row, col, buffer + length - 1);
341 /* do the corresponding single-character insertion */
344 if (move(limit + row, col) != ERR) {
349 MvInsCh(limit + row, col, UChar(ch));
352 if (wmove(work, limit + row, col) != ERR) {
353 WInsCh(work, UChar(ch));
357 MvWInsCh(work, limit + row, col, UChar(ch));
363 legend(show, level, option, buffer, length);
380 static const char *tbl[] =
382 "Usage: inserts [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"
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)
401 setlocale(LC_ALL, "");
403 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
406 init_linedata(optarg);
412 n_opt = atoi(optarg);
429 ExitProgram(EXIT_SUCCESS);
435 printf("This program requires the winsstr function\n");
436 ExitProgram(EXIT_FAILURE);
438 #endif /* HAVE_WINSSTR */