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