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_add_wchstr.c,v 1.27 2020/02/02 23:34:34 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);
137 for (n = 0; n < result; ++n) {
138 if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
139 adjust += (strlen(s) - 1);
148 ChStr(const char *source)
150 TempBuffer(strlen(source), UChar(*source++));
154 ChWStr(const wchar_t *source)
156 TempBuffer(ChWLen(source), *source++);
160 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
162 const char *showstate;
170 showstate = " (mvXXX)";
173 showstate = " (winXXX)";
176 showstate = " (mvwinXXX)";
182 "The Strings/Chars displays should match. Enter any characters, except:\n");
184 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
186 wprintw(win, "Level %d,%s added %d characters <", level,
188 waddwstr(win, buffer);
193 ColOf(wchar_t *buffer, int length, int margin)
198 for (n = 0, result = margin + 1; n < length; ++n) {
202 /* actually newline should clear the remainder of the line
203 * and move to the next line - but that seems a little awkward
214 result += (MY_TABSIZE - (result % MY_TABSIZE));
220 result += wcwidth((wchar_t) ch);
230 ConvertCh(chtype source, cchar_t *target)
232 wchar_t tmp_wchar[2];
234 tmp_wchar[0] = (wchar_t) source;
236 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
244 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
249 if (ConvertCh(ch, &tmp_cchar)) {
250 code = mvwadd_wch(win, y, x, &tmp_cchar);
252 code = mvwaddch(win, y, x, ch);
258 MvAddCh(int y, int x, chtype ch)
263 if (ConvertCh(ch, &tmp_cchar)) {
264 code = mvadd_wch(y, x, &tmp_cchar);
266 code = mvaddch(y, x, ch);
272 WAddCh(WINDOW *win, chtype ch)
277 if (ConvertCh(ch, &tmp_cchar)) {
278 code = wadd_wch(win, &tmp_cchar);
280 code = waddch(win, ch);
291 if (ConvertCh(ch, &tmp_cchar)) {
292 code = add_wch(&tmp_cchar);
299 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
301 recursive_test(int level)
303 static bool first = TRUE;
311 wchar_t buffer[BUFSIZ];
315 int margin = (2 * MY_TABSIZE) - 1;
316 Options option = (Options) ((unsigned) (m_opt
319 | (unsigned) ((w_opt || (level > 0))
325 setlocale(LC_ALL, "");
327 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
331 (void) cbreak(); /* take input chars one at a time, no wait for \n */
332 (void) noecho(); /* don't echo input */
333 keypad(stdscr, TRUE);
336 * Show the characters added in color, to distinguish from those that
341 init_pair(1, COLOR_WHITE, COLOR_BLUE);
347 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
348 work = newwin(limit - 2, COLS - (2 * level), 1, level);
349 show = newwin(4, COLS, limit + 1, 0);
355 show = derwin(stdscr, 4, COLS, limit + 1, 0);
359 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
360 MvWVLine(work, row, col, '.', limit - 2);
362 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
363 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
366 MvWAddChStr(work, 1, 2, ChStr("String"));
367 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
370 buffer[length = 0] = '\0';
371 legend(show, level, option, buffer, length);
377 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
380 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
381 wmove(work, row, margin + 1);
384 recursive_test(level + 1);
401 /* put the whole string in, all at once */
406 for (col = 0; col < length; col += n_opt) {
407 col2 = ColOf(buffer, col, margin);
408 if (move(row, col2) != ERR) {
409 AddNStr(ChWStr(buffer + col), LEN(col));
413 if (move(row, col2) != ERR) {
414 AddStr(ChWStr(buffer));
420 for (col = 0; col < length; col += n_opt) {
421 col2 = ColOf(buffer, col, margin);
422 MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
425 MvAddStr(row, col2, ChWStr(buffer));
430 for (col = 0; col < length; col += n_opt) {
431 col2 = ColOf(buffer, col, margin);
432 if (wmove(work, row, col2) != ERR) {
433 WAddNStr(work, ChWStr(buffer + col), LEN(col));
437 if (wmove(work, row, col2) != ERR) {
438 WAddStr(work, ChWStr(buffer));
444 for (col = 0; col < length; col += n_opt) {
445 col2 = ColOf(buffer, col, margin);
446 MvWAddNStr(work, row, col2, ChWStr(buffer +
450 MvWAddStr(work, row, col2, ChWStr(buffer));
455 /* do the corresponding single-character add */
457 for (col = 0; col < length; ++col) {
458 col2 = ColOf(buffer, col, margin);
461 if (move(row2, col2) != ERR) {
462 AddCh((chtype) buffer[col]);
466 MvAddCh(row2, col2, (chtype) buffer[col]);
469 if (wmove(work, row2, col2) != ERR) {
470 WAddCh(work, (chtype) buffer[col]);
474 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
483 buffer[length++] = (wchar_t) ch;
484 buffer[length] = '\0';
486 /* put the string in, one character at a time */
487 col = ColOf(buffer, length - 1, margin);
490 if (move(row, col) != ERR) {
491 AddStr(ChWStr(buffer + length - 1));
495 MvAddStr(row, col, ChWStr(buffer + length - 1));
498 if (wmove(work, row, col) != ERR) {
499 WAddStr(work, ChWStr(buffer + length - 1));
503 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
507 /* do the corresponding single-character add */
510 if (move(limit + row, col) != ERR) {
515 MvAddCh(limit + row, col, (chtype) ch);
518 if (wmove(work, limit + row, col) != ERR) {
519 WAddCh(work, (chtype) ch);
523 MvWAddCh(work, limit + row, col, (chtype) ch);
529 legend(show, level, option, buffer, length);
546 static const char *tbl[] =
548 "Usage: test_add_wchstr [options]"
551 ," -f FILE read data from given file"
552 ," -n NUM limit string-adds to NUM bytes on ^N replay"
553 ," -m perform wmove/move separately from add-functions"
554 ," -p pass-thru control characters without using unctrl()"
555 ," -w use window-parameter even when stdscr would be implied"
558 for (n = 0; n < SIZEOF(tbl); ++n)
559 fprintf(stderr, "%s\n", tbl[n]);
560 ExitProgram(EXIT_FAILURE);
564 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
568 setlocale(LC_ALL, "");
570 while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
573 init_linedata(optarg);
579 n_opt = atoi(optarg);
602 ExitProgram(EXIT_SUCCESS);
608 printf("This program requires the wide-ncurses library\n");
609 ExitProgram(EXIT_FAILURE);