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