]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_add_wchstr.c
58ff87d5af212b0e9a1517ad63a76222ab1404eb
[ncurses.git] / test / test_add_wchstr.c
1 /****************************************************************************
2  * Copyright (c) 2009-2016,2017 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  ****************************************************************************/
28 /*
29  * $Id: test_add_wchstr.c,v 1.23 2017/04/08 23:10:35 tom Exp $
30  *
31  * Demonstrate the waddwchstr() and wadd_wch functions.
32  * Thomas Dickey - 2009/9/12
33  *
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.
42  */
43
44 #include <test.priv.h>
45
46 #if USE_WIDEC_SUPPORT
47
48 #define WIDE_LINEDATA
49 #include <linedata.h>
50
51 #undef MvAddCh
52 #undef MvAddStr
53 #undef MvWAddCh
54 #undef MvWAddStr
55
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
65
66 #define MY_TABSIZE 8
67
68 typedef enum {
69     oDefault = 0,
70     oMove = 1,
71     oWindow = 2,
72     oMoveWindow = 3
73 } Options;
74
75 static bool m_opt = FALSE;
76 static bool pass_ctls = FALSE;
77 static bool w_opt = FALSE;
78 static int n_opt = -1;
79
80 static cchar_t *temp_buffer;
81 static size_t temp_length;
82
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; \
89  \
90         if (need > temp_length) { \
91             temp_length = need * 2; \
92             temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
93             if (!temp_buffer) \
94                 failed("TempBuffer"); \
95         } \
96         have[0] = 0; \
97         have[1] = 0; \
98         do { \
99             have[0] = source_cast; \
100             if (!pass_ctls \
101              && have[0] != 0 \
102              && have[0] < 256 \
103              && (temp = unctrl((chtype) have[0])) != 0 \
104              && strlen(temp) > 1) { \
105                 while (*temp != '\0') { \
106                     have[0] = (wchar_t) *temp++; \
107                     setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
108                 } \
109             } else { \
110                 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
111             } \
112         } while (have[0] != 0); \
113     } else if (temp_buffer != 0) { \
114         free(temp_buffer); \
115         temp_buffer = 0; \
116         temp_length = 0; \
117     } \
118     return temp_buffer;
119
120 static size_t
121 ChWLen(const wchar_t *source)
122 {
123     size_t result = wcslen(source);
124
125     if (!pass_ctls) {
126         size_t adjust = 0;
127         size_t n;
128         const char *s;
129
130         for (n = 0; n < result; ++n) {
131             if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
132                 adjust += (strlen(s) - 1);
133             }
134         }
135         result += adjust;
136     }
137     return result;
138 }
139
140 static cchar_t *
141 ChStr(const char *source)
142 {
143     TempBuffer(strlen(source), UChar(*source++));
144 }
145
146 static cchar_t *
147 ChWStr(const wchar_t *source)
148 {
149     TempBuffer(ChWLen(source), *source++);
150 }
151
152 static void
153 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
154 {
155     const char *showstate;
156
157     switch (state) {
158     default:
159     case oDefault:
160         showstate = "";
161         break;
162     case oMove:
163         showstate = " (mvXXX)";
164         break;
165     case oWindow:
166         showstate = " (winXXX)";
167         break;
168     case oMoveWindow:
169         showstate = " (mvwinXXX)";
170         break;
171     }
172
173     wmove(win, 0, 0);
174     wprintw(win,
175             "The Strings/Chars displays should match.  Enter any characters, except:\n");
176     wprintw(win,
177             "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
178     wclrtoeol(win);
179     wprintw(win, "Level %d,%s added %d characters <", level,
180             showstate, length);
181     waddwstr(win, buffer);
182     waddstr(win, ">");
183 }
184
185 static int
186 ColOf(wchar_t *buffer, int length, int margin)
187 {
188     int n;
189     int result;
190
191     for (n = 0, result = margin + 1; n < length; ++n) {
192         int ch = buffer[n];
193         switch (ch) {
194         case '\n':
195             /* actually newline should clear the remainder of the line
196              * and move to the next line - but that seems a little awkward
197              * in this example.
198              */
199         case '\r':
200             result = 0;
201             break;
202         case '\b':
203             if (result > 0)
204                 --result;
205             break;
206         case '\t':
207             result += (MY_TABSIZE - (result % MY_TABSIZE));
208             break;
209         case '\177':
210             result += 2;
211             break;
212         default:
213             result += wcwidth((wchar_t) ch);
214             if (ch < 32)
215                 ++result;
216             break;
217         }
218     }
219     return result;
220 }
221
222 static int
223 ConvertCh(chtype source, cchar_t *target)
224 {
225     wchar_t tmp_wchar[2];
226
227     tmp_wchar[0] = (wchar_t) source;
228     tmp_wchar[1] = 0;
229     if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
230         beep();
231         return FALSE;
232     }
233     return TRUE;
234 }
235
236 static int
237 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
238 {
239     int code;
240     cchar_t tmp_cchar;
241
242     if (ConvertCh(ch, &tmp_cchar)) {
243         code = mvwadd_wch(win, y, x, &tmp_cchar);
244     } else {
245         code = mvwaddch(win, y, x, ch);
246     }
247     return code;
248 }
249
250 static int
251 MvAddCh(int y, int x, chtype ch)
252 {
253     int code;
254     cchar_t tmp_cchar;
255
256     if (ConvertCh(ch, &tmp_cchar)) {
257         code = mvadd_wch(y, x, &tmp_cchar);
258     } else {
259         code = mvaddch(y, x, ch);
260     }
261     return code;
262 }
263
264 static int
265 WAddCh(WINDOW *win, chtype ch)
266 {
267     int code;
268     cchar_t tmp_cchar;
269
270     if (ConvertCh(ch, &tmp_cchar)) {
271         code = wadd_wch(win, &tmp_cchar);
272     } else {
273         code = waddch(win, ch);
274     }
275     return code;
276 }
277
278 static int
279 AddCh(chtype ch)
280 {
281     int code;
282     cchar_t tmp_cchar;
283
284     if (ConvertCh(ch, &tmp_cchar)) {
285         code = add_wch(&tmp_cchar);
286     } else {
287         code = addch(ch);
288     }
289     return code;
290 }
291
292 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
293 static void
294 test_add_wchstr(int level)
295 {
296     static bool first = TRUE;
297
298     int ch;
299     int limit;
300     int row = 1;
301     int col;
302     int row2, col2;
303     int length;
304     wchar_t buffer[BUFSIZ];
305     WINDOW *look = 0;
306     WINDOW *work = 0;
307     WINDOW *show = 0;
308     int margin = (2 * MY_TABSIZE) - 1;
309     Options option = ((m_opt ? oMove : oDefault)
310                       | ((w_opt || (level > 0)) ? oWindow : oDefault));
311
312     if (first) {
313         static char cmd[80];
314         setlocale(LC_ALL, "");
315
316         _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
317         putenv(cmd);
318
319         initscr();
320         (void) cbreak();        /* take input chars one at a time, no wait for \n */
321         (void) noecho();        /* don't echo input */
322         keypad(stdscr, TRUE);
323
324         /*
325          * Show the characters added in color, to distinguish from those that
326          * are shifted.
327          */
328         if (has_colors()) {
329             start_color();
330             init_pair(1, COLOR_WHITE, COLOR_BLUE);
331         }
332     }
333
334     limit = LINES - 5;
335     if (level > 0) {
336         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
337         work = newwin(limit - 2, COLS - (2 * level), 1, level);
338         show = newwin(4, COLS, limit + 1, 0);
339         box(look, 0, 0);
340         wnoutrefresh(look);
341         limit -= 2;
342     } else {
343         work = stdscr;
344         show = derwin(stdscr, 4, COLS, limit + 1, 0);
345     }
346     keypad(work, TRUE);
347
348     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
349         MvWVLine(work, row, col, '.', limit - 2);
350
351     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
352     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
353     limit /= 2;
354
355     (void) mvwadd_wchstr(work, 1, 2, ChStr("String"));
356     (void) mvwadd_wchstr(work, limit + 1, 2, ChStr("Chars"));
357     wnoutrefresh(work);
358
359     buffer[length = 0] = '\0';
360     legend(show, level, option, buffer, length);
361     wnoutrefresh(show);
362
363     doupdate();
364
365     if (has_colors()) {
366         wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
367     }
368
369     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
370         wmove(work, row, margin + 1);
371         switch (ch) {
372         case key_RECUR:
373             test_add_wchstr(level + 1);
374
375             if (look)
376                 touchwin(look);
377             touchwin(work);
378             touchwin(show);
379
380             if (look)
381                 wnoutrefresh(look);
382             wnoutrefresh(work);
383             wnoutrefresh(show);
384
385             doupdate();
386             break;
387         case key_NEWLINE:
388             if (row < limit) {
389                 ++row;
390                 /* put the whole string in, all at once */
391                 col2 = margin + 1;
392                 switch (option) {
393                 case oDefault:
394                     if (n_opt > 1) {
395                         for (col = 0; col < length; col += n_opt) {
396                             col2 = ColOf(buffer, col, margin);
397                             if (move(row, col2) != ERR) {
398                                 AddNStr(ChWStr(buffer + col), LEN(col));
399                             }
400                         }
401                     } else {
402                         if (move(row, col2) != ERR) {
403                             AddStr(ChWStr(buffer));
404                         }
405                     }
406                     break;
407                 case oMove:
408                     if (n_opt > 1) {
409                         for (col = 0; col < length; col += n_opt) {
410                             col2 = ColOf(buffer, col, margin);
411                             MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
412                         }
413                     } else {
414                         MvAddStr(row, col2, ChWStr(buffer));
415                     }
416                     break;
417                 case oWindow:
418                     if (n_opt > 1) {
419                         for (col = 0; col < length; col += n_opt) {
420                             col2 = ColOf(buffer, col, margin);
421                             if (wmove(work, row, col2) != ERR) {
422                                 WAddNStr(work, ChWStr(buffer + col), LEN(col));
423                             }
424                         }
425                     } else {
426                         if (wmove(work, row, col2) != ERR) {
427                             WAddStr(work, ChWStr(buffer));
428                         }
429                     }
430                     break;
431                 case oMoveWindow:
432                     if (n_opt > 1) {
433                         for (col = 0; col < length; col += n_opt) {
434                             col2 = ColOf(buffer, col, margin);
435                             MvWAddNStr(work, row, col2, ChWStr(buffer +
436                                                                col), LEN(col));
437                         }
438                     } else {
439                         MvWAddStr(work, row, col2, ChWStr(buffer));
440                     }
441                     break;
442                 }
443
444                 /* do the corresponding single-character add */
445                 row2 = limit + row;
446                 for (col = 0; col < length; ++col) {
447                     col2 = ColOf(buffer, col, margin);
448                     switch (option) {
449                     case oDefault:
450                         if (move(row2, col2) != ERR) {
451                             AddCh((chtype) buffer[col]);
452                         }
453                         break;
454                     case oMove:
455                         MvAddCh(row2, col2, (chtype) buffer[col]);
456                         break;
457                     case oWindow:
458                         if (wmove(work, row2, col2) != ERR) {
459                             WAddCh(work, (chtype) buffer[col]);
460                         }
461                         break;
462                     case oMoveWindow:
463                         MvWAddCh(work, row2, col2, (chtype) buffer[col]);
464                         break;
465                     }
466                 }
467             } else {
468                 beep();
469             }
470             break;
471         default:
472             buffer[length++] = (wchar_t) ch;
473             buffer[length] = '\0';
474
475             /* put the string in, one character at a time */
476             col = ColOf(buffer, length - 1, margin);
477             switch (option) {
478             case oDefault:
479                 if (move(row, col) != ERR) {
480                     AddStr(ChWStr(buffer + length - 1));
481                 }
482                 break;
483             case oMove:
484                 MvAddStr(row, col, ChWStr(buffer + length - 1));
485                 break;
486             case oWindow:
487                 if (wmove(work, row, col) != ERR) {
488                     WAddStr(work, ChWStr(buffer + length - 1));
489                 }
490                 break;
491             case oMoveWindow:
492                 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
493                 break;
494             }
495
496             /* do the corresponding single-character add */
497             switch (option) {
498             case oDefault:
499                 if (move(limit + row, col) != ERR) {
500                     AddCh((chtype) ch);
501                 }
502                 break;
503             case oMove:
504                 MvAddCh(limit + row, col, (chtype) ch);
505                 break;
506             case oWindow:
507                 if (wmove(work, limit + row, col) != ERR) {
508                     WAddCh(work, (chtype) ch);
509                 }
510                 break;
511             case oMoveWindow:
512                 MvWAddCh(work, limit + row, col, (chtype) ch);
513                 break;
514             }
515
516             wnoutrefresh(work);
517
518             legend(show, level, option, buffer, length);
519             wnoutrefresh(show);
520
521             doupdate();
522             break;
523         }
524     }
525     delwin(show);
526     if (level > 0) {
527         delwin(work);
528         delwin(look);
529     }
530 }
531
532 static void
533 usage(void)
534 {
535     static const char *tbl[] =
536     {
537         "Usage: test_add_wchstr [options]"
538         ,""
539         ,"Options:"
540         ,"  -f FILE read data from given file"
541         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
542         ,"  -m      perform wmove/move separately from add-functions"
543         ,"  -p      pass-thru control characters without using unctrl()"
544         ,"  -w      use window-parameter even when stdscr would be implied"
545     };
546     unsigned n;
547     for (n = 0; n < SIZEOF(tbl); ++n)
548         fprintf(stderr, "%s\n", tbl[n]);
549     ExitProgram(EXIT_FAILURE);
550 }
551
552 int
553 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
554 {
555     int ch;
556
557     setlocale(LC_ALL, "");
558
559     while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
560         switch (ch) {
561         case 'f':
562             init_linedata(optarg);
563             break;
564         case 'm':
565             m_opt = TRUE;
566             break;
567         case 'n':
568             n_opt = atoi(optarg);
569             if (n_opt == 0)
570                 n_opt = -1;
571             break;
572         case 'p':
573             pass_ctls = TRUE;
574             break;
575         case 'w':
576             w_opt = TRUE;
577             break;
578         default:
579             usage();
580             break;
581         }
582     }
583     if (optind < argc)
584         usage();
585
586     test_add_wchstr(0);
587     endwin();
588 #if NO_LEAKS
589     free(temp_buffer);
590 #endif
591     ExitProgram(EXIT_SUCCESS);
592 }
593 #else
594 int
595 main(void)
596 {
597     printf("This program requires the wide-ncurses library\n");
598     ExitProgram(EXIT_FAILURE);
599 }
600 #endif