]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_addstr.c
ncurses 6.1 - patch 20180602
[ncurses.git] / test / test_addstr.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_addstr.c,v 1.15 2017/09/06 09:27:34 tom Exp $
30  *
31  * Demonstrate the waddstr() 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 #define AddNStr    addnstr
42 #define AddStr     addstr
43 #define MvAddNStr  (void) mvaddnstr
44 #define MvWAddNStr (void) mvwaddnstr
45 #define WAddNStr   waddnstr
46 #define WAddStr    waddstr
47
48 #define MY_TABSIZE 8
49
50 typedef enum {
51     oDefault = 0,
52     oMove = 1,
53     oWindow = 2,
54     oMoveWindow = 3
55 } Options;
56
57 static bool m_opt = FALSE;
58 static bool w_opt = FALSE;
59 static int n_opt = -1;
60
61 static void
62 legend(WINDOW *win, int level, Options state, char *buffer, int length)
63 {
64     const char *showstate;
65
66     switch (state) {
67     default:
68     case oDefault:
69         showstate = "";
70         break;
71     case oMove:
72         showstate = " (mvXXX)";
73         break;
74     case oWindow:
75         showstate = " (winXXX)";
76         break;
77     case oMoveWindow:
78         showstate = " (mvwinXXX)";
79         break;
80     }
81
82     wmove(win, 0, 0);
83     wprintw(win,
84             "The Strings/Chars displays should match.  Enter any characters, except:\n");
85     wprintw(win,
86             "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
87     wclrtoeol(win);
88     wprintw(win, "Level %d,%s added %d characters <%s>", level,
89             showstate, length, buffer);
90 }
91
92 static int
93 ColOf(char *buffer, int length, int margin)
94 {
95     int n;
96     int result;
97
98     for (n = 0, result = margin + 1; n < length; ++n) {
99         int ch = UChar(buffer[n]);
100         switch (ch) {
101         case '\n':
102             /* actually newline should clear the remainder of the line
103              * and move to the next line - but that seems a little awkward
104              * in this example.
105              */
106         case '\r':
107             result = 0;
108             break;
109         case '\b':
110             if (result > 0)
111                 --result;
112             break;
113         case '\t':
114             result += (MY_TABSIZE - (result % MY_TABSIZE));
115             break;
116         case '\177':
117             result += 2;
118             break;
119         default:
120             ++result;
121             if (ch < 32)
122                 ++result;
123             break;
124         }
125     }
126     return result;
127 }
128
129 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
130 static void
131 recursive_test(int level)
132 {
133     static bool first = TRUE;
134
135     int ch;
136     int limit;
137     int row = 1;
138     int col;
139     int row2, col2;
140     int length;
141     char buffer[BUFSIZ];
142     WINDOW *look = 0;
143     WINDOW *work = 0;
144     WINDOW *show = 0;
145     int margin = (2 * MY_TABSIZE) - 1;
146     Options option = (Options) ((unsigned) (m_opt
147                                             ? oMove
148                                             : oDefault)
149                                 | (unsigned) ((w_opt || (level > 0))
150                                               ? oWindow
151                                               : oDefault));
152
153     if (first) {
154         static char cmd[80];
155         setlocale(LC_ALL, "");
156
157         _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
158         putenv(cmd);
159
160         initscr();
161         (void) cbreak();        /* take input chars one at a time, no wait for \n */
162         (void) noecho();        /* don't echo input */
163         keypad(stdscr, TRUE);
164
165         /*
166          * Show the characters added in color, to distinguish from those that
167          * are shifted.
168          */
169         if (has_colors()) {
170             start_color();
171             init_pair(1, COLOR_WHITE, COLOR_BLUE);
172         }
173     }
174
175     limit = LINES - 5;
176     if (level > 0) {
177         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
178         work = newwin(limit - 2, COLS - (2 * level), 1, level);
179         show = newwin(4, COLS, limit + 1, 0);
180         box(look, 0, 0);
181         wnoutrefresh(look);
182         limit -= 2;
183     } else {
184         work = stdscr;
185         show = derwin(stdscr, 4, COLS, limit + 1, 0);
186     }
187     keypad(work, TRUE);
188
189     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
190         MvWVLine(work, row, col, '.', limit - 2);
191
192     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
193     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
194     limit /= 2;
195
196     MvWAddStr(work, 1, 2, "String");
197     MvWAddStr(work, limit + 1, 2, "Chars");
198     wnoutrefresh(work);
199
200     buffer[length = 0] = '\0';
201     legend(show, level, option, buffer, length);
202     wnoutrefresh(show);
203
204     doupdate();
205
206     if (has_colors()) {
207         wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
208     }
209
210     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
211         wmove(work, row, margin + 1);
212         switch (ch) {
213         case key_RECUR:
214             recursive_test(level + 1);
215
216             if (look)
217                 touchwin(look);
218             touchwin(work);
219             touchwin(show);
220
221             if (look)
222                 wnoutrefresh(look);
223             wnoutrefresh(work);
224             wnoutrefresh(show);
225
226             doupdate();
227             break;
228         case key_NEWLINE:
229             if (row < limit) {
230                 ++row;
231                 /* put the whole string in, all at once */
232                 col2 = margin + 1;
233                 switch (option) {
234                 case oDefault:
235                     if (n_opt > 1) {
236                         for (col = 0; col < length; col += n_opt) {
237                             col2 = ColOf(buffer, col, margin);
238                             if (move(row, col2) != ERR) {
239                                 AddNStr(buffer + col, LEN(col));
240                             }
241                         }
242                     } else {
243                         if (move(row, col2) != ERR) {
244                             AddStr(buffer);
245                         }
246                     }
247                     break;
248                 case oMove:
249                     if (n_opt > 1) {
250                         for (col = 0; col < length; col += n_opt) {
251                             col2 = ColOf(buffer, col, margin);
252                             MvAddNStr(row, col2, buffer + col, LEN(col));
253                         }
254                     } else {
255                         MvAddStr(row, col2, buffer);
256                     }
257                     break;
258                 case oWindow:
259                     if (n_opt > 1) {
260                         for (col = 0; col < length; col += n_opt) {
261                             col2 = ColOf(buffer, col, margin);
262                             if (wmove(work, row, col2) != ERR) {
263                                 WAddNStr(work, buffer + col, LEN(col));
264                             }
265                         }
266                     } else {
267                         if (wmove(work, row, col2) != ERR) {
268                             WAddStr(work, buffer);
269                         }
270                     }
271                     break;
272                 case oMoveWindow:
273                     if (n_opt > 1) {
274                         for (col = 0; col < length; col += n_opt) {
275                             col2 = ColOf(buffer, col, margin);
276                             MvWAddNStr(work, row, col2, buffer + col, LEN(col));
277                         }
278                     } else {
279                         MvWAddStr(work, row, col2, buffer);
280                     }
281                     break;
282                 }
283
284                 /* do the corresponding single-character add */
285                 row2 = limit + row;
286                 for (col = 0; col < length; ++col) {
287                     col2 = ColOf(buffer, col, margin);
288                     switch (option) {
289                     case oDefault:
290                         if (move(row2, col2) != ERR) {
291                             AddCh(UChar(buffer[col]));
292                         }
293                         break;
294                     case oMove:
295                         MvAddCh(row2, col2, UChar(buffer[col]));
296                         break;
297                     case oWindow:
298                         if (wmove(work, row2, col2) != ERR) {
299                             WAddCh(work, UChar(buffer[col]));
300                         }
301                         break;
302                     case oMoveWindow:
303                         MvWAddCh(work, row2, col2, UChar(buffer[col]));
304                         break;
305                     }
306                 }
307             } else {
308                 beep();
309             }
310             break;
311         default:
312             if (ch <= 0 || ch > 255) {
313                 beep();
314                 break;
315             }
316             buffer[length++] = (char) ch;
317             buffer[length] = '\0';
318
319             /* put the string in, one character at a time */
320             col = ColOf(buffer, length - 1, margin);
321             switch (option) {
322             case oDefault:
323                 if (move(row, col) != ERR) {
324                     AddStr(buffer + length - 1);
325                 }
326                 break;
327             case oMove:
328                 MvAddStr(row, col, buffer + length - 1);
329                 break;
330             case oWindow:
331                 if (wmove(work, row, col) != ERR) {
332                     WAddStr(work, buffer + length - 1);
333                 }
334                 break;
335             case oMoveWindow:
336                 MvWAddStr(work, row, col, buffer + length - 1);
337                 break;
338             }
339
340             /* do the corresponding single-character add */
341             switch (option) {
342             case oDefault:
343                 if (move(limit + row, col) != ERR) {
344                     AddCh(UChar(ch));
345                 }
346                 break;
347             case oMove:
348                 MvAddCh(limit + row, col, UChar(ch));
349                 break;
350             case oWindow:
351                 if (wmove(work, limit + row, col) != ERR) {
352                     WAddCh(work, UChar(ch));
353                 }
354                 break;
355             case oMoveWindow:
356                 MvWAddCh(work, limit + row, col, UChar(ch));
357                 break;
358             }
359
360             wnoutrefresh(work);
361
362             legend(show, level, option, buffer, length);
363             wnoutrefresh(show);
364
365             doupdate();
366             break;
367         }
368     }
369     delwin(show);
370     if (level > 0) {
371         delwin(work);
372         delwin(look);
373     }
374 }
375
376 static void
377 usage(void)
378 {
379     static const char *tbl[] =
380     {
381         "Usage: test_addstr [options]"
382         ,""
383         ,"Options:"
384         ,"  -f FILE read data from given file"
385         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
386         ,"  -m      perform wmove/move separately from add-functions"
387         ,"  -w      use window-parameter even when stdscr would be implied"
388     };
389     unsigned n;
390     for (n = 0; n < SIZEOF(tbl); ++n)
391         fprintf(stderr, "%s\n", tbl[n]);
392     ExitProgram(EXIT_FAILURE);
393 }
394
395 int
396 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
397 {
398     int ch;
399
400     setlocale(LC_ALL, "");
401
402     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
403         switch (ch) {
404         case 'f':
405             init_linedata(optarg);
406             break;
407         case 'm':
408             m_opt = TRUE;
409             break;
410         case 'n':
411             n_opt = atoi(optarg);
412             if (n_opt == 0)
413                 n_opt = -1;
414             break;
415         case 'w':
416             w_opt = TRUE;
417             break;
418         default:
419             usage();
420             break;
421         }
422     }
423     if (optind < argc)
424         usage();
425
426     recursive_test(0);
427     endwin();
428     ExitProgram(EXIT_SUCCESS);
429 }