1 /****************************************************************************
2 * Copyright (c) 2009-2016,2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: test_add_wchstr.c,v 1.26 2017/04/15 15:15:25 tom Exp $
31 * Demonstrate the waddwchstr() and wadd_wch functions.
32 * Thomas Dickey - 2009/9/12
34 * Note: to provide inputs for *add_wch(), we use setcchar(). A quirk of the
35 * X/Open definition for that function is that the string contains no
36 * characters with negative width. Any control character (such as tab) falls
37 * into that category. So it follows that *add_wch() cannot render a tab
38 * character because there is no legal way to construct a cchar_t containing
39 * one. X/Open does not document this, and it would be logical to assume that
40 * *addwchstr() has the same limitation, but it uses a wchar_t string directly,
41 * and does not document how tabs are handled.
44 #include <test.priv.h>
60 * redefinitions to simplify comparison between test_*str programs
62 #define AddNStr add_wchnstr
63 #define AddStr add_wchstr
64 #define MvAddNStr (void) mvadd_wchnstr
65 #define MvAddStr (void) mvadd_wchstr
66 #define MvWAddNStr (void) mvwadd_wchnstr
67 #define MvWAddStr (void) mvwadd_wchstr
68 #define MvWAddChStr(w,y,x,s) (void) mvwadd_wchstr((w),(y),(x),(s))
69 #define WAddNStr wadd_wchnstr
70 #define WAddStr wadd_wchstr
81 static bool m_opt = FALSE;
82 static bool pass_ctls = FALSE;
83 static bool w_opt = FALSE;
84 static int n_opt = -1;
86 static cchar_t *temp_buffer;
87 static size_t temp_length;
89 #define TempBuffer(source_len, source_cast) \
92 size_t need = source_len + 1; \
96 if (need > temp_length) { \
97 temp_length = need * 2; \
98 temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
100 failed("TempBuffer"); \
105 have[0] = source_cast; \
109 && (temp = unctrl((chtype) have[0])) != 0 \
110 && strlen(temp) > 1) { \
111 while (*temp != '\0') { \
112 have[0] = (wchar_t) *temp++; \
113 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
116 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
118 } while (have[0] != 0); \
119 } else if (temp_buffer != 0) { \
127 ChWLen(const wchar_t *source)
129 size_t result = wcslen(source);
136 for (n = 0; n < result; ++n) {
137 if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
138 adjust += (strlen(s) - 1);
147 ChStr(const char *source)
149 TempBuffer(strlen(source), UChar(*source++));
153 ChWStr(const wchar_t *source)
155 TempBuffer(ChWLen(source), *source++);
159 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
161 const char *showstate;
169 showstate = " (mvXXX)";
172 showstate = " (winXXX)";
175 showstate = " (mvwinXXX)";
181 "The Strings/Chars displays should match. Enter any characters, except:\n");
183 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
185 wprintw(win, "Level %d,%s added %d characters <", level,
187 waddwstr(win, buffer);
192 ColOf(wchar_t *buffer, int length, int margin)
197 for (n = 0, result = margin + 1; n < length; ++n) {
201 /* actually newline should clear the remainder of the line
202 * and move to the next line - but that seems a little awkward
213 result += (MY_TABSIZE - (result % MY_TABSIZE));
219 result += wcwidth((wchar_t) ch);
229 ConvertCh(chtype source, cchar_t *target)
231 wchar_t tmp_wchar[2];
233 tmp_wchar[0] = (wchar_t) source;
235 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
243 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
248 if (ConvertCh(ch, &tmp_cchar)) {
249 code = mvwadd_wch(win, y, x, &tmp_cchar);
251 code = mvwaddch(win, y, x, ch);
257 MvAddCh(int y, int x, chtype ch)
262 if (ConvertCh(ch, &tmp_cchar)) {
263 code = mvadd_wch(y, x, &tmp_cchar);
265 code = mvaddch(y, x, ch);
271 WAddCh(WINDOW *win, chtype ch)
276 if (ConvertCh(ch, &tmp_cchar)) {
277 code = wadd_wch(win, &tmp_cchar);
279 code = waddch(win, ch);
290 if (ConvertCh(ch, &tmp_cchar)) {
291 code = add_wch(&tmp_cchar);
298 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
300 recursive_test(int level)
302 static bool first = TRUE;
310 wchar_t buffer[BUFSIZ];
314 int margin = (2 * MY_TABSIZE) - 1;
315 Options option = (Options) ((unsigned) (m_opt
318 | (unsigned) ((w_opt || (level > 0))
324 setlocale(LC_ALL, "");
326 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
330 (void) cbreak(); /* take input chars one at a time, no wait for \n */
331 (void) noecho(); /* don't echo input */
332 keypad(stdscr, TRUE);
335 * Show the characters added in color, to distinguish from those that
340 init_pair(1, COLOR_WHITE, COLOR_BLUE);
346 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
347 work = newwin(limit - 2, COLS - (2 * level), 1, level);
348 show = newwin(4, COLS, limit + 1, 0);
354 show = derwin(stdscr, 4, COLS, limit + 1, 0);
358 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
359 MvWVLine(work, row, col, '.', limit - 2);
361 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
362 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
365 MvWAddChStr(work, 1, 2, ChStr("String"));
366 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
369 buffer[length = 0] = '\0';
370 legend(show, level, option, buffer, length);
376 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
379 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
380 wmove(work, row, margin + 1);
383 recursive_test(level + 1);
400 /* put the whole string in, all at once */
405 for (col = 0; col < length; col += n_opt) {
406 col2 = ColOf(buffer, col, margin);
407 if (move(row, col2) != ERR) {
408 AddNStr(ChWStr(buffer + col), LEN(col));
412 if (move(row, col2) != ERR) {
413 AddStr(ChWStr(buffer));
419 for (col = 0; col < length; col += n_opt) {
420 col2 = ColOf(buffer, col, margin);
421 MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
424 MvAddStr(row, col2, ChWStr(buffer));
429 for (col = 0; col < length; col += n_opt) {
430 col2 = ColOf(buffer, col, margin);
431 if (wmove(work, row, col2) != ERR) {
432 WAddNStr(work, ChWStr(buffer + col), LEN(col));
436 if (wmove(work, row, col2) != ERR) {
437 WAddStr(work, ChWStr(buffer));
443 for (col = 0; col < length; col += n_opt) {
444 col2 = ColOf(buffer, col, margin);
445 MvWAddNStr(work, row, col2, ChWStr(buffer +
449 MvWAddStr(work, row, col2, ChWStr(buffer));
454 /* do the corresponding single-character add */
456 for (col = 0; col < length; ++col) {
457 col2 = ColOf(buffer, col, margin);
460 if (move(row2, col2) != ERR) {
461 AddCh((chtype) buffer[col]);
465 MvAddCh(row2, col2, (chtype) buffer[col]);
468 if (wmove(work, row2, col2) != ERR) {
469 WAddCh(work, (chtype) buffer[col]);
473 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
482 buffer[length++] = (wchar_t) ch;
483 buffer[length] = '\0';
485 /* put the string in, one character at a time */
486 col = ColOf(buffer, length - 1, margin);
489 if (move(row, col) != ERR) {
490 AddStr(ChWStr(buffer + length - 1));
494 MvAddStr(row, col, ChWStr(buffer + length - 1));
497 if (wmove(work, row, col) != ERR) {
498 WAddStr(work, ChWStr(buffer + length - 1));
502 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
506 /* do the corresponding single-character add */
509 if (move(limit + row, col) != ERR) {
514 MvAddCh(limit + row, col, (chtype) ch);
517 if (wmove(work, limit + row, col) != ERR) {
518 WAddCh(work, (chtype) ch);
522 MvWAddCh(work, limit + row, col, (chtype) ch);
528 legend(show, level, option, buffer, length);
545 static const char *tbl[] =
547 "Usage: test_add_wchstr [options]"
550 ," -f FILE read data from given file"
551 ," -n NUM limit string-adds to NUM bytes on ^N replay"
552 ," -m perform wmove/move separately from add-functions"
553 ," -p pass-thru control characters without using unctrl()"
554 ," -w use window-parameter even when stdscr would be implied"
557 for (n = 0; n < SIZEOF(tbl); ++n)
558 fprintf(stderr, "%s\n", tbl[n]);
559 ExitProgram(EXIT_FAILURE);
563 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
567 setlocale(LC_ALL, "");
569 while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
572 init_linedata(optarg);
578 n_opt = atoi(optarg);
601 ExitProgram(EXIT_SUCCESS);
607 printf("This program requires the wide-ncurses library\n");
608 ExitProgram(EXIT_FAILURE);