missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / test_add_wchstr.c
bloba48f2c49e1c26f6c5b563c655c9b9eb9768bd9fb
1 /****************************************************************************
2 * Copyright (c) 2009,2010,2011 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
29 * $Id: test_add_wchstr.c,v 1.15 2011/01/15 18:15:11 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>
46 #if USE_WIDEC_SUPPORT
48 #define WIDE_LINEDATA
49 #include <linedata.h>
51 #undef MvAddCh
52 #undef MvAddStr
53 #undef MvWAddCh
54 #undef MvWAddStr
56 /* definitions to make it simpler to compare with test_addstr.c */
57 #define AddNStr add_wchnstr
58 #define AddStr add_wchstr
59 #define MvAddNStr (void) mvadd_wchnstr
60 #define MvAddStr (void) mvadd_wchstr
61 #define MvWAddNStr (void) mvwadd_wchnstr
62 #define MvWAddStr (void) mvwadd_wchstr
63 #define WAddNStr wadd_wchnstr
64 #define WAddStr wadd_wchstr
66 #define MY_TABSIZE 8
68 typedef enum {
69 oDefault = 0,
70 oMove = 1,
71 oWindow = 2,
72 oMoveWindow = 3
73 } Options;
75 static bool m_opt = FALSE;
76 static bool pass_ctls = FALSE;
77 static bool w_opt = FALSE;
78 static int n_opt = -1;
80 static cchar_t *temp_buffer;
81 static size_t temp_length;
83 #define TempBuffer(source_len, source_cast) \
84 if (source != 0) { \
85 const char *temp; \
86 size_t need = source_len + 1; \
87 wchar_t have[2]; \
88 int n = 0; \
90 if (need > temp_length) { \
91 temp_length = need * 2; \
92 temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
93 } \
94 have[0] = 0; \
95 have[1] = 0; \
96 do { \
97 have[0] = source_cast; \
98 if (!pass_ctls \
99 && have[0] != 0 \
100 && have[0] < 256 \
101 && (temp = unctrl((chtype) have[0])) != 0 \
102 && strlen(temp) > 1) { \
103 while (*temp != '\0') { \
104 have[0] = *temp++; \
105 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
107 } else { \
108 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
110 } while (have[0] != 0); \
111 } else if (temp_buffer != 0) { \
112 free(temp_buffer); \
113 temp_buffer = 0; \
114 temp_length = 0; \
116 return temp_buffer;
118 static size_t
119 ChWLen(const wchar_t *source)
121 size_t result = wcslen(source);
123 if (!pass_ctls) {
124 size_t adjust = 0;
125 size_t n;
126 const char *s;
128 for (n = 0; n < result; ++n) {
129 if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
130 adjust += (strlen(s) - 1);
133 result += adjust;
135 return result;
138 static cchar_t *
139 ChStr(const char *source)
141 TempBuffer(strlen(source), UChar(*source++));
144 static cchar_t *
145 ChWStr(const wchar_t *source)
147 TempBuffer(ChWLen(source), *source++);
150 static void
151 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
153 const char *showstate;
155 switch (state) {
156 default:
157 case oDefault:
158 showstate = "";
159 break;
160 case oMove:
161 showstate = " (mvXXX)";
162 break;
163 case oWindow:
164 showstate = " (winXXX)";
165 break;
166 case oMoveWindow:
167 showstate = " (mvwinXXX)";
168 break;
171 wmove(win, 0, 0);
172 wprintw(win,
173 "The Strings/Chars displays should match. Enter any characters, except:\n");
174 wprintw(win,
175 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
176 wclrtoeol(win);
177 wprintw(win, "Level %d,%s added %d characters <", level,
178 showstate, length);
179 waddwstr(win, buffer);
180 waddstr(win, ">");
183 static int
184 ColOf(wchar_t *buffer, int length, int margin)
186 int n;
187 int result;
189 for (n = 0, result = margin + 1; n < length; ++n) {
190 int ch = buffer[n];
191 switch (ch) {
192 case '\n':
193 /* actually newline should clear the remainder of the line
194 * and move to the next line - but that seems a little awkward
195 * in this example.
197 case '\r':
198 result = 0;
199 break;
200 case '\b':
201 if (result > 0)
202 --result;
203 break;
204 case '\t':
205 result += (MY_TABSIZE - (result % MY_TABSIZE));
206 break;
207 case '\177':
208 result += 2;
209 break;
210 default:
211 result += wcwidth(ch);
212 if (ch < 32)
213 ++result;
214 break;
217 return result;
220 static int
221 ConvertCh(chtype source, cchar_t *target)
223 wchar_t tmp_wchar[2];
225 tmp_wchar[0] = (wchar_t) source;
226 tmp_wchar[1] = 0;
227 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
228 beep();
229 return FALSE;
231 return TRUE;
234 static int
235 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
237 int code;
238 cchar_t tmp_cchar;
240 if (ConvertCh(ch, &tmp_cchar)) {
241 code = mvwadd_wch(win, y, x, &tmp_cchar);
242 } else {
243 code = mvwaddch(win, y, x, ch);
245 return code;
248 static int
249 MvAddCh(int y, int x, chtype ch)
251 int code;
252 cchar_t tmp_cchar;
254 if (ConvertCh(ch, &tmp_cchar)) {
255 code = mvadd_wch(y, x, &tmp_cchar);
256 } else {
257 code = mvaddch(y, x, ch);
259 return code;
262 static int
263 WAddCh(WINDOW *win, chtype ch)
265 int code;
266 cchar_t tmp_cchar;
268 if (ConvertCh(ch, &tmp_cchar)) {
269 code = wadd_wch(win, &tmp_cchar);
270 } else {
271 code = waddch(win, ch);
273 return code;
276 static int
277 AddCh(chtype ch)
279 int code;
280 cchar_t tmp_cchar;
282 if (ConvertCh(ch, &tmp_cchar)) {
283 code = add_wch(&tmp_cchar);
284 } else {
285 code = addch(ch);
287 return code;
290 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
291 static void
292 test_add_wchstr(int level)
294 static bool first = TRUE;
296 int ch;
297 int limit;
298 int row = 1;
299 int col;
300 int row2, col2;
301 int length;
302 wchar_t buffer[BUFSIZ];
303 WINDOW *look = 0;
304 WINDOW *work = 0;
305 WINDOW *show = 0;
306 int margin = (2 * MY_TABSIZE) - 1;
307 Options option = ((m_opt ? oMove : oDefault)
308 | ((w_opt || (level > 0)) ? oWindow : oDefault));
310 if (first) {
311 static char cmd[80];
312 setlocale(LC_ALL, "");
314 putenv(strcpy(cmd, "TABSIZE=8"));
316 initscr();
317 (void) cbreak(); /* take input chars one at a time, no wait for \n */
318 (void) noecho(); /* don't echo input */
319 keypad(stdscr, TRUE);
322 limit = LINES - 5;
323 if (level > 0) {
324 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
325 work = newwin(limit - 2, COLS - (2 * level), 1, level);
326 show = newwin(4, COLS, limit + 1, 0);
327 box(look, 0, 0);
328 wnoutrefresh(look);
329 limit -= 2;
330 } else {
331 work = stdscr;
332 show = derwin(stdscr, 4, COLS, limit + 1, 0);
334 keypad(work, TRUE);
336 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
337 MvWVLine(work, row, col, '.', limit - 2);
339 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
340 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
341 limit /= 2;
343 (void) mvwadd_wchstr(work, 1, 2, ChStr("String"));
344 (void) mvwadd_wchstr(work, limit + 1, 2, ChStr("Chars"));
345 wnoutrefresh(work);
347 buffer[length = 0] = '\0';
348 legend(show, level, option, buffer, length);
349 wnoutrefresh(show);
351 doupdate();
354 * Show the characters added in color, to distinguish from those that
355 * are shifted.
357 if (has_colors()) {
358 start_color();
359 init_pair(1, COLOR_WHITE, COLOR_BLUE);
360 wbkgdset(work, COLOR_PAIR(1) | ' ');
363 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
364 wmove(work, row, margin + 1);
365 switch (ch) {
366 case key_RECUR:
367 test_add_wchstr(level + 1);
369 touchwin(look);
370 touchwin(work);
371 touchwin(show);
373 wnoutrefresh(look);
374 wnoutrefresh(work);
375 wnoutrefresh(show);
377 doupdate();
378 break;
379 case key_NEWLINE:
380 if (row < limit) {
381 ++row;
382 /* put the whole string in, all at once */
383 col2 = margin + 1;
384 switch (option) {
385 case oDefault:
386 if (n_opt > 1) {
387 for (col = 0; col < length; col += n_opt) {
388 col2 = ColOf(buffer, col, margin);
389 if (move(row, col2) != ERR) {
390 AddNStr(ChWStr(buffer + col), LEN(col));
393 } else {
394 if (move(row, col2) != ERR) {
395 AddStr(ChWStr(buffer));
398 break;
399 case oMove:
400 if (n_opt > 1) {
401 for (col = 0; col < length; col += n_opt) {
402 col2 = ColOf(buffer, col, margin);
403 MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
405 } else {
406 MvAddStr(row, col2, ChWStr(buffer));
408 break;
409 case oWindow:
410 if (n_opt > 1) {
411 for (col = 0; col < length; col += n_opt) {
412 col2 = ColOf(buffer, col, margin);
413 if (wmove(work, row, col2) != ERR) {
414 WAddNStr(work, ChWStr(buffer + col), LEN(col));
417 } else {
418 if (wmove(work, row, col2) != ERR) {
419 WAddStr(work, ChWStr(buffer));
422 break;
423 case oMoveWindow:
424 if (n_opt > 1) {
425 for (col = 0; col < length; col += n_opt) {
426 col2 = ColOf(buffer, col, margin);
427 MvWAddNStr(work, row, col2, ChWStr(buffer +
428 col), LEN(col));
430 } else {
431 MvWAddStr(work, row, col2, ChWStr(buffer));
433 break;
436 /* do the corresponding single-character add */
437 row2 = limit + row;
438 for (col = 0; col < length; ++col) {
439 col2 = ColOf(buffer, col, margin);
440 switch (option) {
441 case oDefault:
442 if (move(row2, col2) != ERR) {
443 AddCh((chtype) buffer[col]);
445 break;
446 case oMove:
447 MvAddCh(row2, col2, (chtype) buffer[col]);
448 break;
449 case oWindow:
450 if (wmove(work, row2, col2) != ERR) {
451 WAddCh(work, (chtype) buffer[col]);
453 break;
454 case oMoveWindow:
455 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
456 break;
459 } else {
460 beep();
462 break;
463 default:
464 buffer[length++] = ch;
465 buffer[length] = '\0';
467 /* put the string in, one character at a time */
468 col = ColOf(buffer, length - 1, margin);
469 switch (option) {
470 case oDefault:
471 if (move(row, col) != ERR) {
472 AddStr(ChWStr(buffer + length - 1));
474 break;
475 case oMove:
476 MvAddStr(row, col, ChWStr(buffer + length - 1));
477 break;
478 case oWindow:
479 if (wmove(work, row, col) != ERR) {
480 WAddStr(work, ChWStr(buffer + length - 1));
482 break;
483 case oMoveWindow:
484 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
485 break;
488 /* do the corresponding single-character add */
489 switch (option) {
490 case oDefault:
491 if (move(limit + row, col) != ERR) {
492 AddCh((chtype) ch);
494 break;
495 case oMove:
496 MvAddCh(limit + row, col, (chtype) ch);
497 break;
498 case oWindow:
499 if (wmove(work, limit + row, col) != ERR) {
500 WAddCh(work, (chtype) ch);
502 break;
503 case oMoveWindow:
504 MvWAddCh(work, limit + row, col, (chtype) ch);
505 break;
508 wnoutrefresh(work);
510 legend(show, level, option, buffer, length);
511 wnoutrefresh(show);
513 doupdate();
514 break;
517 if (level > 0) {
518 delwin(show);
519 delwin(work);
520 delwin(look);
524 static void
525 usage(void)
527 static const char *tbl[] =
529 "Usage: test_add_wchstr [options]"
531 ,"Options:"
532 ," -f FILE read data from given file"
533 ," -n NUM limit string-adds to NUM bytes on ^N replay"
534 ," -m perform wmove/move separately from add-functions"
535 ," -p pass-thru control characters without using unctrl()"
536 ," -w use window-parameter even when stdscr would be implied"
538 unsigned n;
539 for (n = 0; n < SIZEOF(tbl); ++n)
540 fprintf(stderr, "%s\n", tbl[n]);
541 ExitProgram(EXIT_FAILURE);
545 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
547 int ch;
549 setlocale(LC_ALL, "");
551 while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
552 switch (ch) {
553 case 'f':
554 init_linedata(optarg);
555 break;
556 case 'm':
557 m_opt = TRUE;
558 break;
559 case 'n':
560 n_opt = atoi(optarg);
561 if (n_opt == 0)
562 n_opt = -1;
563 break;
564 case 'p':
565 pass_ctls = TRUE;
566 break;
567 case 'w':
568 w_opt = TRUE;
569 break;
570 default:
571 usage();
572 break;
575 if (optind < argc)
576 usage();
578 test_add_wchstr(0);
579 endwin();
580 ExitProgram(EXIT_SUCCESS);
582 #else
584 main(void)
586 printf("This program requires the wide-ncurses library\n");
587 ExitProgram(EXIT_FAILURE);
589 #endif