1 /****************************************************************************
2 * Copyright (c) 2002-2016,2017 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.29 2017/04/08 22:20:46 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 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
168 (void) cbreak(); /* take input chars one at a time, no wait for \n */
169 (void) noecho(); /* don't echo input */
170 keypad(stdscr, TRUE);
173 * Show the characters inserted in color, to distinguish from those
178 init_pair(1, COLOR_WHITE, COLOR_BLUE);
184 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
185 work = newwin(limit - 2, COLS - (2 * level), 1, level);
186 show = newwin(4, COLS, limit + 1, 0);
192 show = derwin(stdscr, 4, COLS, limit + 1, 0);
196 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
197 MvWVLine(work, row, col, '.', limit - 2);
199 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
200 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
203 MvWAddStr(work, 1, 2, "String");
204 MvWAddStr(work, limit + 1, 2, "Chars");
207 buffer[length = 0] = '\0';
208 legend(show, level, option, buffer, length);
214 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
217 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
218 wmove(work, row, margin + 1);
221 test_inserts(level + 1);
238 /* put the whole string in, all at once */
243 for (col = 0; col < length; col += n_opt) {
244 col2 = ColOf(buffer, col, margin);
245 if (move(row, col2) != ERR) {
246 InsNStr(buffer + col, LEN(col));
250 if (move(row, col2) != ERR) {
257 for (col = 0; col < length; col += n_opt) {
258 col2 = ColOf(buffer, col, margin);
259 MvInsNStr(row, col2, buffer + col, LEN(col));
262 MvInsStr(row, col2, buffer);
267 for (col = 0; col < length; col += n_opt) {
268 col2 = ColOf(buffer, col, margin);
269 if (wmove(work, row, col2) != ERR) {
270 WInsNStr(work, buffer + col, LEN(col));
274 if (wmove(work, row, col2) != ERR) {
275 WInsStr(work, buffer);
281 for (col = 0; col < length; col += n_opt) {
282 col2 = ColOf(buffer, col, margin);
283 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
286 MvWInsStr(work, row, col2, buffer);
291 /* do the corresponding single-character insertion */
293 for (col = 0; col < length; ++col) {
294 col2 = ColOf(buffer, col, margin);
297 if (move(row2, col2) != ERR) {
298 InsCh(UChar(buffer[col]));
302 MvInsCh(row2, col2, UChar(buffer[col]));
305 if (wmove(work, row2, col2) != ERR) {
306 WInsCh(work, UChar(buffer[col]));
310 MvWInsCh(work, row2, col2, UChar(buffer[col]));
319 if (ch <= 0 || ch > 255) {
323 if (length >= BUFSIZ - 2)
325 buffer[length++] = (char) ch;
326 buffer[length] = '\0';
328 /* put the string in, one character at a time */
329 col = ColOf(buffer, length - 1, margin);
332 if (move(row, col) != ERR) {
333 InsStr(buffer + length - 1);
337 MvInsStr(row, col, buffer + length - 1);
340 if (wmove(work, row, col) != ERR) {
341 WInsStr(work, buffer + length - 1);
345 MvWInsStr(work, row, col, buffer + length - 1);
349 /* do the corresponding single-character insertion */
352 if (move(limit + row, col) != ERR) {
357 MvInsCh(limit + row, col, UChar(ch));
360 if (wmove(work, limit + row, col) != ERR) {
361 WInsCh(work, UChar(ch));
365 MvWInsCh(work, limit + row, col, UChar(ch));
371 legend(show, level, option, buffer, length);
388 static const char *tbl[] =
390 "Usage: inserts [options]"
393 ," -f FILE read data from given file"
394 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
395 ," -m perform wmove/move separately from insert-functions"
396 ," -w use window-parameter even when stdscr would be implied"
399 for (n = 0; n < SIZEOF(tbl); ++n)
400 fprintf(stderr, "%s\n", tbl[n]);
401 ExitProgram(EXIT_FAILURE);
405 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
409 setlocale(LC_ALL, "");
411 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
414 init_linedata(optarg);
420 n_opt = atoi(optarg);
437 ExitProgram(EXIT_SUCCESS);
443 printf("This program requires the winsstr function\n");
444 ExitProgram(EXIT_FAILURE);
446 #endif /* HAVE_WINSSTR */