1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2002-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: inserts.c,v 1.30 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate the winsstr() and winsch functions.
33 * Thomas Dickey - 2002/10/19
36 #include <test.priv.h>
42 #define InsNStr insnstr
44 #define MvInsNStr (void) mvinsnstr
45 #define MvInsStr (void) mvinsstr
46 #define MvWInsNStr (void) mvwinsnstr
47 #define MvWInsStr (void) mvwinsstr
48 #define WInsNStr winsnstr
49 #define WInsStr winsstr
52 #define MvInsCh (void) mvinsch
53 #define MvWInsCh (void) mvwinsch
65 static bool m_opt = FALSE;
66 static bool w_opt = FALSE;
67 static int n_opt = -1;
70 legend(WINDOW *win, int level, Options state, char *buffer, int length)
72 const char *showstate;
80 showstate = " (mvXXX)";
83 showstate = " (winXXX)";
86 showstate = " (mvwinXXX)";
92 "The Strings/Chars displays should match. Enter any characters, except:\n");
94 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
96 wprintw(win, "Level %d,%s inserted %d characters <%s>", level,
97 showstate, length, buffer);
101 ColOf(char *buffer, int length, int margin)
106 for (n = 0, result = margin + 1; n < length; ++n) {
107 int ch = UChar(buffer[n]);
110 /* actually newline should clear the remainder of the line
111 * and move to the next line - but that seems a little awkward
122 result += (MY_TABSIZE - (result % MY_TABSIZE));
137 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
139 test_inserts(int level)
141 static bool first = TRUE;
153 int margin = (2 * MY_TABSIZE) - 1;
154 Options option = (Options) ((unsigned) (m_opt
157 | (unsigned) ((w_opt || (level > 0))
163 setlocale(LC_ALL, "");
165 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
169 (void) cbreak(); /* take input chars one at a time, no wait for \n */
170 (void) noecho(); /* don't echo input */
171 keypad(stdscr, TRUE);
174 * Show the characters inserted in color, to distinguish from those
179 init_pair(1, COLOR_WHITE, COLOR_BLUE);
185 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
186 work = newwin(limit - 2, COLS - (2 * level), 1, level);
187 show = newwin(4, COLS, limit + 1, 0);
193 show = derwin(stdscr, 4, COLS, limit + 1, 0);
197 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
198 MvWVLine(work, row, col, '.', limit - 2);
200 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
201 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
204 MvWAddStr(work, 1, 2, "String");
205 MvWAddStr(work, limit + 1, 2, "Chars");
208 buffer[length = 0] = '\0';
209 legend(show, level, option, buffer, length);
215 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
218 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
219 wmove(work, row, margin + 1);
222 test_inserts(level + 1);
239 /* put the whole string in, all at once */
244 for (col = 0; col < length; col += n_opt) {
245 col2 = ColOf(buffer, col, margin);
246 if (move(row, col2) != ERR) {
247 InsNStr(buffer + col, LEN(col));
251 if (move(row, col2) != ERR) {
258 for (col = 0; col < length; col += n_opt) {
259 col2 = ColOf(buffer, col, margin);
260 MvInsNStr(row, col2, buffer + col, LEN(col));
263 MvInsStr(row, col2, buffer);
268 for (col = 0; col < length; col += n_opt) {
269 col2 = ColOf(buffer, col, margin);
270 if (wmove(work, row, col2) != ERR) {
271 WInsNStr(work, buffer + col, LEN(col));
275 if (wmove(work, row, col2) != ERR) {
276 WInsStr(work, buffer);
282 for (col = 0; col < length; col += n_opt) {
283 col2 = ColOf(buffer, col, margin);
284 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
287 MvWInsStr(work, row, col2, buffer);
292 /* do the corresponding single-character insertion */
294 for (col = 0; col < length; ++col) {
295 col2 = ColOf(buffer, col, margin);
298 if (move(row2, col2) != ERR) {
299 InsCh(UChar(buffer[col]));
303 MvInsCh(row2, col2, UChar(buffer[col]));
306 if (wmove(work, row2, col2) != ERR) {
307 WInsCh(work, UChar(buffer[col]));
311 MvWInsCh(work, row2, col2, UChar(buffer[col]));
320 if (ch <= 0 || ch > 255) {
324 if (length >= BUFSIZ - 2)
326 buffer[length++] = (char) ch;
327 buffer[length] = '\0';
329 /* put the string in, one character at a time */
330 col = ColOf(buffer, length - 1, margin);
333 if (move(row, col) != ERR) {
334 InsStr(buffer + length - 1);
338 MvInsStr(row, col, buffer + length - 1);
341 if (wmove(work, row, col) != ERR) {
342 WInsStr(work, buffer + length - 1);
346 MvWInsStr(work, row, col, buffer + length - 1);
350 /* do the corresponding single-character insertion */
353 if (move(limit + row, col) != ERR) {
358 MvInsCh(limit + row, col, UChar(ch));
361 if (wmove(work, limit + row, col) != ERR) {
362 WInsCh(work, UChar(ch));
366 MvWInsCh(work, limit + row, col, UChar(ch));
372 legend(show, level, option, buffer, length);
389 static const char *tbl[] =
391 "Usage: inserts [options]"
394 ," -f FILE read data from given file"
395 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
396 ," -m perform wmove/move separately from insert-functions"
397 ," -w use window-parameter even when stdscr would be implied"
400 for (n = 0; n < SIZEOF(tbl); ++n)
401 fprintf(stderr, "%s\n", tbl[n]);
402 ExitProgram(EXIT_FAILURE);
406 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
410 setlocale(LC_ALL, "");
412 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
415 init_linedata(optarg);
421 n_opt = atoi(optarg);
438 ExitProgram(EXIT_SUCCESS);
444 printf("This program requires the winsstr function\n");
445 ExitProgram(EXIT_FAILURE);
447 #endif /* HAVE_WINSSTR */