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