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.26 2012/12/09 00:51:51 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);
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]));
316 if (ch <= 0 || ch > 255) {
320 buffer[length++] = (char) ch;
321 buffer[length] = '\0';
323 /* put the string in, one character at a time */
324 col = ColOf(buffer, length - 1, margin);
327 if (move(row, col) != ERR) {
328 InsStr(buffer + length - 1);
332 MvInsStr(row, col, buffer + length - 1);
335 if (wmove(work, row, col) != ERR) {
336 WInsStr(work, buffer + length - 1);
340 MvWInsStr(work, row, col, buffer + length - 1);
344 /* do the corresponding single-character insertion */
347 if (move(limit + row, col) != ERR) {
352 MvInsCh(limit + row, col, UChar(ch));
355 if (wmove(work, limit + row, col) != ERR) {
356 WInsCh(work, UChar(ch));
360 MvWInsCh(work, limit + row, col, UChar(ch));
366 legend(show, level, option, buffer, length);
383 static const char *tbl[] =
385 "Usage: inserts [options]"
388 ," -f FILE read data from given file"
389 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
390 ," -m perform wmove/move separately from insert-functions"
391 ," -w use window-parameter even when stdscr would be implied"
394 for (n = 0; n < SIZEOF(tbl); ++n)
395 fprintf(stderr, "%s\n", tbl[n]);
396 ExitProgram(EXIT_FAILURE);
400 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
404 setlocale(LC_ALL, "");
406 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
409 init_linedata(optarg);
415 n_opt = atoi(optarg);
432 ExitProgram(EXIT_SUCCESS);
438 printf("This program requires the winsstr function\n");
439 ExitProgram(EXIT_FAILURE);
441 #endif /* HAVE_WINSSTR */