1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2009-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: test_addstr.c,v 1.16 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate the waddstr() and waddch functions.
33 * Thomas Dickey - 2009/9/12
36 #include <test.priv.h>
40 * redefinitions to simplify comparison between test_*str programs
42 #define AddNStr addnstr
44 #define MvAddNStr (void) mvaddnstr
45 #define MvWAddNStr (void) mvwaddnstr
46 #define WAddNStr waddnstr
47 #define WAddStr waddstr
58 static bool m_opt = FALSE;
59 static bool w_opt = FALSE;
60 static int n_opt = -1;
63 legend(WINDOW *win, int level, Options state, char *buffer, int length)
65 const char *showstate;
73 showstate = " (mvXXX)";
76 showstate = " (winXXX)";
79 showstate = " (mvwinXXX)";
85 "The Strings/Chars displays should match. Enter any characters, except:\n");
87 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
89 wprintw(win, "Level %d,%s added %d characters <%s>", level,
90 showstate, length, buffer);
94 ColOf(char *buffer, int length, int margin)
99 for (n = 0, result = margin + 1; n < length; ++n) {
100 int ch = UChar(buffer[n]);
103 /* actually newline should clear the remainder of the line
104 * and move to the next line - but that seems a little awkward
115 result += (MY_TABSIZE - (result % MY_TABSIZE));
130 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
132 recursive_test(int level)
134 static bool first = TRUE;
146 int margin = (2 * MY_TABSIZE) - 1;
147 Options option = (Options) ((unsigned) (m_opt
150 | (unsigned) ((w_opt || (level > 0))
156 setlocale(LC_ALL, "");
158 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
162 (void) cbreak(); /* take input chars one at a time, no wait for \n */
163 (void) noecho(); /* don't echo input */
164 keypad(stdscr, TRUE);
167 * Show the characters added in color, to distinguish from those that
172 init_pair(1, COLOR_WHITE, COLOR_BLUE);
178 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
179 work = newwin(limit - 2, COLS - (2 * level), 1, level);
180 show = newwin(4, COLS, limit + 1, 0);
186 show = derwin(stdscr, 4, COLS, limit + 1, 0);
190 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
191 MvWVLine(work, row, col, '.', limit - 2);
193 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
194 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
197 MvWAddStr(work, 1, 2, "String");
198 MvWAddStr(work, limit + 1, 2, "Chars");
201 buffer[length = 0] = '\0';
202 legend(show, level, option, buffer, length);
208 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
211 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
212 wmove(work, row, margin + 1);
215 recursive_test(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 AddNStr(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 MvAddNStr(row, col2, buffer + col, LEN(col));
256 MvAddStr(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 WAddNStr(work, buffer + col, LEN(col));
268 if (wmove(work, row, col2) != ERR) {
269 WAddStr(work, buffer);
275 for (col = 0; col < length; col += n_opt) {
276 col2 = ColOf(buffer, col, margin);
277 MvWAddNStr(work, row, col2, buffer + col, LEN(col));
280 MvWAddStr(work, row, col2, buffer);
285 /* do the corresponding single-character add */
287 for (col = 0; col < length; ++col) {
288 col2 = ColOf(buffer, col, margin);
291 if (move(row2, col2) != ERR) {
292 AddCh(UChar(buffer[col]));
296 MvAddCh(row2, col2, UChar(buffer[col]));
299 if (wmove(work, row2, col2) != ERR) {
300 WAddCh(work, UChar(buffer[col]));
304 MvWAddCh(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 AddStr(buffer + length - 1);
329 MvAddStr(row, col, buffer + length - 1);
332 if (wmove(work, row, col) != ERR) {
333 WAddStr(work, buffer + length - 1);
337 MvWAddStr(work, row, col, buffer + length - 1);
341 /* do the corresponding single-character add */
344 if (move(limit + row, col) != ERR) {
349 MvAddCh(limit + row, col, UChar(ch));
352 if (wmove(work, limit + row, col) != ERR) {
353 WAddCh(work, UChar(ch));
357 MvWAddCh(work, limit + row, col, UChar(ch));
363 legend(show, level, option, buffer, length);
380 static const char *tbl[] =
382 "Usage: test_addstr [options]"
385 ," -f FILE read data from given file"
386 ," -n NUM limit string-adds to NUM bytes on ^N replay"
387 ," -m perform wmove/move separately from add-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);