1 /****************************************************************************
2 * Copyright 2020,2022 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_addchstr.c,v 1.29 2022/12/10 22:28:50 tom Exp $
32 * Demonstrate the waddchstr() and waddch functions.
33 * Thomas Dickey - 2009/9/12
36 #include <test.priv.h>
40 * redefinitions to simplify comparison between test_*str programs
45 #define AddNStr addchnstr
46 #define AddStr addchstr
47 #define MvAddNStr (void) mvaddchnstr
48 #define MvAddStr (void) mvaddchstr
49 #define MvWAddNStr (void) mvwaddchnstr
50 #define MvWAddStr (void) mvwaddchstr
51 #define WAddNStr waddchnstr
52 #define WAddStr waddchstr
63 static bool m_opt = FALSE;
64 static bool pass_ctls = FALSE;
65 static bool w_opt = FALSE;
66 static int n_opt = -1;
68 static chtype show_attr;
69 static chtype *temp_buffer;
70 static size_t temp_length;
72 #define TempBuffer(source_cast)
75 ChLen(const char *source)
77 size_t result = strlen(source);
83 for (n = 0; n < result; ++n) {
84 const char *s = unctrl(UChar(source[n]));
86 adjust += (strlen(s) - 1);
95 ChStr(const char *source)
98 size_t need = ChLen(source) + 1;
101 if (need > temp_length) {
102 temp_length = need * 2;
103 temp_buffer = typeRealloc(chtype, temp_length, temp_buffer);
105 failed("TempBuffer");
109 chtype ch = UChar(*source++);
110 if (!pass_ctls && (s = unctrl(ch)) != 0) {
112 temp_buffer[n++] = UChar(*s++);
115 temp_buffer[n++] = ch;
117 } while (source[0] != 0);
119 } else if (temp_buffer != 0) {
127 /* color the strings drawn in the workspace */
129 ChStr2(const char *source)
131 size_t len = ChLen(source);
133 chtype *result = ChStr(source);
134 for (n = 0; n < len; ++n) {
135 result[n] |= show_attr;
141 legend(WINDOW *win, int level, Options state, char *buffer, int length)
143 const char *showstate;
151 showstate = " (mvXXX)";
154 showstate = " (winXXX)";
157 showstate = " (mvwinXXX)";
163 "The Strings/Chars displays should match. Enter any characters, except:\n");
165 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
167 wprintw(win, "Level %d,%s added %d characters <%s>", level,
168 showstate, length, buffer);
172 ColOf(char *buffer, int length, int margin)
177 for (n = 0, result = margin + 1; n < length; ++n) {
178 int ch = UChar(buffer[n]);
181 /* actually newline should clear the remainder of the line
182 * and move to the next line - but that seems a little awkward
193 result += (MY_TABSIZE - (result % MY_TABSIZE));
208 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
210 recursive_test(int level)
212 static bool first = TRUE;
224 int margin = (2 * MY_TABSIZE) - 1;
225 Options option = (Options) ((unsigned) (m_opt
228 | (unsigned) ((w_opt || (level > 0))
234 setlocale(LC_ALL, "");
236 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
240 (void) cbreak(); /* take input chars one at a time, no wait for \n */
241 (void) noecho(); /* don't echo input */
242 keypad(stdscr, TRUE);
245 * Show the characters added in color, to distinguish from those that
250 init_pair(1, COLOR_WHITE, COLOR_BLUE);
256 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
257 work = newwin(limit - 2, COLS - (2 * level), 1, level);
258 show = newwin(4, COLS, limit + 1, 0);
264 show = derwin(stdscr, 4, COLS, limit + 1, 0);
268 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
269 MvWVLine(work, row, col, '.', limit - 2);
271 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
272 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
275 MvWAddChStr(work, 1, 2, ChStr("String"));
276 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
279 buffer[length = 0] = '\0';
280 legend(show, level, option, buffer, length);
286 show_attr = (chtype) COLOR_PAIR(1);
287 wbkgdset(work, show_attr | ' ');
289 show_attr = A_STANDOUT;
292 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
293 wmove(work, row, margin + 1);
296 recursive_test(level + 1);
313 /* put the whole string in, all at once */
318 for (col = 0; col < length; col += n_opt) {
319 col2 = ColOf(buffer, col, margin);
320 if (move(row, col2) != ERR) {
321 AddNStr(ChStr2(buffer + col), LEN(col));
325 if (move(row, col2) != ERR) {
326 AddStr(ChStr2(buffer));
332 for (col = 0; col < length; col += n_opt) {
333 col2 = ColOf(buffer, col, margin);
334 MvAddNStr(row, col2, ChStr2(buffer + col), LEN(col));
337 MvAddStr(row, col2, ChStr2(buffer));
342 for (col = 0; col < length; col += n_opt) {
343 col2 = ColOf(buffer, col, margin);
344 if (wmove(work, row, col2) != ERR) {
345 WAddNStr(work, ChStr2(buffer + col), LEN(col));
349 if (wmove(work, row, col2) != ERR) {
350 WAddStr(work, ChStr2(buffer));
356 for (col = 0; col < length; col += n_opt) {
357 col2 = ColOf(buffer, col, margin);
358 MvWAddNStr(work, row, col2, ChStr2(buffer + col),
362 MvWAddStr(work, row, col2, ChStr2(buffer));
367 /* do the corresponding single-character add */
369 for (col = 0; col < length; ++col) {
370 col2 = ColOf(buffer, col, margin);
373 if (move(row2, col2) != ERR) {
374 AddCh(UChar(buffer[col]));
378 MvAddCh(row2, col2, UChar(buffer[col]));
381 if (wmove(work, row2, col2) != ERR) {
382 WAddCh(work, UChar(buffer[col]));
386 MvWAddCh(work, row2, col2, UChar(buffer[col]));
398 if (ch <= 0 || ch > 255) {
402 buffer[length++] = (char) ch;
403 buffer[length] = '\0';
405 /* put the string in, one character at a time */
406 col = ColOf(buffer, length - 1, margin);
409 if (move(row, col) != ERR) {
410 AddStr(ChStr2(buffer + length - 1));
414 MvAddStr(row, col, ChStr2(buffer + length - 1));
417 if (wmove(work, row, col) != ERR) {
418 WAddStr(work, ChStr2(buffer + length - 1));
422 MvWAddStr(work, row, col, ChStr2(buffer + length - 1));
426 /* do the corresponding single-character add */
429 if (move(limit + row, col) != ERR) {
434 MvAddCh(limit + row, col, UChar(ch));
437 if (wmove(work, limit + row, col) != ERR) {
438 WAddCh(work, UChar(ch));
442 MvWAddCh(work, limit + row, col, UChar(ch));
448 legend(show, level, option, buffer, length);
465 static const char *tbl[] =
467 "Usage: test_addchstr [options]"
471 ," -f FILE read data from given file"
472 ," -n NUM limit string-adds to NUM bytes on ^N replay"
473 ," -m perform wmove/move separately from add-functions"
474 ," -p pass-thru control characters without using unctrl()"
475 ," -w use window-parameter even when stdscr would be implied"
478 for (n = 0; n < SIZEOF(tbl); ++n)
479 fprintf(stderr, "%s\n", tbl[n]);
480 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
487 main(int argc, char *argv[])
491 setlocale(LC_ALL, "");
493 while ((ch = getopt(argc, argv, OPTS_COMMON "f:mn:pw")) != -1) {
496 init_linedata(optarg);
502 n_opt = atoi(optarg);
514 ExitProgram(EXIT_SUCCESS);
516 usage(ch == OPTS_USAGE);
528 ExitProgram(EXIT_SUCCESS);