1 /****************************************************************************
2 * Copyright 2020-2021,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_add_wchstr.c,v 1.34 2022/12/10 22:28:50 tom Exp $
32 * Demonstrate the waddwchstr() and wadd_wch functions.
33 * Thomas Dickey - 2009/9/12
35 * Note: to provide inputs for *add_wch(), we use setcchar(). A quirk of the
36 * X/Open definition for that function is that the string contains no
37 * characters with negative width. Any control character (such as tab) falls
38 * into that category. So it follows that *add_wch() cannot render a tab
39 * character because there is no legal way to construct a cchar_t containing
40 * one. X/Open does not document this, and it would be logical to assume that
41 * *addwchstr() has the same limitation, but it uses a wchar_t string directly,
42 * and does not document how tabs are handled.
45 #include <test.priv.h>
61 * redefinitions to simplify comparison between test_*str programs
63 #define AddNStr add_wchnstr
64 #define AddStr add_wchstr
65 #define MvAddNStr (void) mvadd_wchnstr
66 #define MvAddStr (void) mvadd_wchstr
67 #define MvWAddNStr (void) mvwadd_wchnstr
68 #define MvWAddStr (void) mvwadd_wchstr
69 #define MvWAddChStr(w,y,x,s) (void) mvwadd_wchstr((w),(y),(x),(s))
70 #define WAddNStr wadd_wchnstr
71 #define WAddStr wadd_wchstr
82 static bool m_opt = FALSE;
83 static bool pass_ctls = FALSE;
84 static bool w_opt = FALSE;
85 static int n_opt = -1;
87 static cchar_t *temp_buffer;
88 static size_t temp_length;
90 #define TempBuffer(source_len, source_cast) \
93 size_t need = source_len + 1; \
97 if (need > temp_length) { \
98 temp_length = need * 2; \
99 temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
101 failed("TempBuffer"); \
106 have[0] = source_cast; \
110 && (temp = unctrl((chtype) have[0])) != 0 \
111 && strlen(temp) > 1) { \
112 while (*temp != '\0') { \
113 have[0] = (wchar_t) *temp++; \
114 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
117 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
119 } while (have[0] != 0); \
120 } else if (temp_buffer != 0) { \
128 ChWLen(const wchar_t *source)
130 size_t result = wcslen(source);
136 for (n = 0; source[n] != 0; ++n) {
139 if ((source[n] < 256) && (s = unctrl((chtype) source[n])) != 0) {
140 adjust += (strlen(s) - 1);
149 ChStr(const char *source)
151 TempBuffer(strlen(source), UChar(*source++));
155 ChWStr(const wchar_t *source)
157 TempBuffer(ChWLen(source), *source++);
161 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
163 const char *showstate;
171 showstate = " (mvXXX)";
174 showstate = " (winXXX)";
177 showstate = " (mvwinXXX)";
183 "The Strings/Chars displays should match. Enter any characters, except:\n");
185 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
187 wprintw(win, "Level %d,%s added %d characters <", level,
189 waddwstr(win, buffer);
194 ColOf(const wchar_t *buffer, int length, int margin)
199 for (n = 0, result = margin + 1; n < length; ++n) {
203 /* actually newline should clear the remainder of the line
204 * and move to the next line - but that seems a little awkward
215 result += (MY_TABSIZE - (result % MY_TABSIZE));
221 result += wcwidth((wchar_t) ch);
231 ConvertCh(chtype source, cchar_t *target)
233 wchar_t tmp_wchar[2];
235 tmp_wchar[0] = (wchar_t) source;
237 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
245 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
250 if (ConvertCh(ch, &tmp_cchar)) {
251 code = mvwadd_wch(win, y, x, &tmp_cchar);
253 code = mvwaddch(win, y, x, ch);
259 MvAddCh(int y, int x, chtype ch)
264 if (ConvertCh(ch, &tmp_cchar)) {
265 code = mvadd_wch(y, x, &tmp_cchar);
267 code = mvaddch(y, x, ch);
273 WAddCh(WINDOW *win, chtype ch)
278 if (ConvertCh(ch, &tmp_cchar)) {
279 code = wadd_wch(win, &tmp_cchar);
281 code = waddch(win, ch);
292 if (ConvertCh(ch, &tmp_cchar)) {
293 code = add_wch(&tmp_cchar);
300 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
302 recursive_test(int level)
304 static bool first = TRUE;
312 wchar_t buffer[BUFSIZ];
316 int margin = (2 * MY_TABSIZE) - 1;
317 Options option = (Options) ((unsigned) (m_opt
320 | (unsigned) ((w_opt || (level > 0))
326 setlocale(LC_ALL, "");
328 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
332 (void) cbreak(); /* take input chars one at a time, no wait for \n */
333 (void) noecho(); /* don't echo input */
334 keypad(stdscr, TRUE);
337 * Show the characters added in color, to distinguish from those that
342 init_pair(1, COLOR_WHITE, COLOR_BLUE);
348 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
349 work = newwin(limit - 2, COLS - (2 * level), 1, level);
350 show = newwin(4, COLS, limit + 1, 0);
356 show = derwin(stdscr, 4, COLS, limit + 1, 0);
360 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
361 MvWVLine(work, row, col, '.', limit - 2);
363 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
364 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
367 MvWAddChStr(work, 1, 2, ChStr("String"));
368 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
371 buffer[length = 0] = '\0';
372 legend(show, level, option, buffer, length);
378 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
381 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
382 wmove(work, row, margin + 1);
385 recursive_test(level + 1);
402 /* put the whole string in, all at once */
407 for (col = 0; col < length; col += n_opt) {
408 col2 = ColOf(buffer, col, margin);
409 if (move(row, col2) != ERR) {
410 AddNStr(ChWStr(buffer + col), LEN(col));
414 if (move(row, col2) != ERR) {
415 AddStr(ChWStr(buffer));
421 for (col = 0; col < length; col += n_opt) {
422 col2 = ColOf(buffer, col, margin);
423 MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
426 MvAddStr(row, col2, ChWStr(buffer));
431 for (col = 0; col < length; col += n_opt) {
432 col2 = ColOf(buffer, col, margin);
433 if (wmove(work, row, col2) != ERR) {
434 WAddNStr(work, ChWStr(buffer + col), LEN(col));
438 if (wmove(work, row, col2) != ERR) {
439 WAddStr(work, ChWStr(buffer));
445 for (col = 0; col < length; col += n_opt) {
446 col2 = ColOf(buffer, col, margin);
447 MvWAddNStr(work, row, col2, ChWStr(buffer +
451 MvWAddStr(work, row, col2, ChWStr(buffer));
456 /* do the corresponding single-character add */
458 for (col = 0; col < length; ++col) {
459 col2 = ColOf(buffer, col, margin);
462 if (move(row2, col2) != ERR) {
463 AddCh((chtype) buffer[col]);
467 MvAddCh(row2, col2, (chtype) buffer[col]);
470 if (wmove(work, row2, col2) != ERR) {
471 WAddCh(work, (chtype) buffer[col]);
475 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
484 buffer[length++] = (wchar_t) ch;
485 buffer[length] = '\0';
487 /* put the string in, one character at a time */
488 col = ColOf(buffer, length - 1, margin);
491 if (move(row, col) != ERR) {
492 AddStr(ChWStr(buffer + length - 1));
496 MvAddStr(row, col, ChWStr(buffer + length - 1));
499 if (wmove(work, row, col) != ERR) {
500 WAddStr(work, ChWStr(buffer + length - 1));
504 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
508 /* do the corresponding single-character add */
511 if (move(limit + row, col) != ERR) {
516 MvAddCh(limit + row, col, (chtype) ch);
519 if (wmove(work, limit + row, col) != ERR) {
520 WAddCh(work, (chtype) ch);
524 MvWAddCh(work, limit + row, col, (chtype) ch);
530 legend(show, level, option, buffer, length);
547 static const char *tbl[] =
549 "Usage: test_add_wchstr [options]"
553 ," -f FILE read data from given file"
554 ," -n NUM limit string-adds to NUM bytes on ^N replay"
555 ," -m perform wmove/move separately from add-functions"
556 ," -p pass-thru control characters without using unctrl()"
557 ," -w use window-parameter even when stdscr would be implied"
560 for (n = 0; n < SIZEOF(tbl); ++n)
561 fprintf(stderr, "%s\n", tbl[n]);
562 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
569 main(int argc, char *argv[])
573 setlocale(LC_ALL, "");
575 while ((ch = getopt(argc, argv, OPTS_COMMON "f:mn:pw")) != -1) {
578 init_linedata(optarg);
584 n_opt = atoi(optarg);
596 ExitProgram(EXIT_SUCCESS);
598 usage(ch == OPTS_USAGE);
610 ExitProgram(EXIT_SUCCESS);
616 printf("This program requires the wide-ncurses library\n");
617 ExitProgram(EXIT_FAILURE);