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