]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inserts.c
ncurses 6.0 - patch 20170128
[ncurses.git] / test / inserts.c
1 /****************************************************************************
2  * Copyright (c) 2002-2012,2016 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.28 2016/09/10 21:34:42 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             buffer[length++] = (char) ch;
324             buffer[length] = '\0';
325
326             /* put the string in, one character at a time */
327             col = ColOf(buffer, length - 1, margin);
328             switch (option) {
329             case oDefault:
330                 if (move(row, col) != ERR) {
331                     InsStr(buffer + length - 1);
332                 }
333                 break;
334             case oMove:
335                 MvInsStr(row, col, buffer + length - 1);
336                 break;
337             case oWindow:
338                 if (wmove(work, row, col) != ERR) {
339                     WInsStr(work, buffer + length - 1);
340                 }
341                 break;
342             case oMoveWindow:
343                 MvWInsStr(work, row, col, buffer + length - 1);
344                 break;
345             }
346
347             /* do the corresponding single-character insertion */
348             switch (option) {
349             case oDefault:
350                 if (move(limit + row, col) != ERR) {
351                     InsCh(UChar(ch));
352                 }
353                 break;
354             case oMove:
355                 MvInsCh(limit + row, col, UChar(ch));
356                 break;
357             case oWindow:
358                 if (wmove(work, limit + row, col) != ERR) {
359                     WInsCh(work, UChar(ch));
360                 }
361                 break;
362             case oMoveWindow:
363                 MvWInsCh(work, limit + row, col, UChar(ch));
364                 break;
365             }
366
367             wnoutrefresh(work);
368
369             legend(show, level, option, buffer, length);
370             wnoutrefresh(show);
371
372             doupdate();
373             break;
374         }
375     }
376     if (level > 0) {
377         delwin(work);
378         delwin(look);
379     }
380     delwin(show);
381 }
382
383 static void
384 usage(void)
385 {
386     static const char *tbl[] =
387     {
388         "Usage: inserts [options]"
389         ,""
390         ,"Options:"
391         ,"  -f FILE read data from given file"
392         ,"  -n NUM  limit string-inserts to NUM bytes on ^N replay"
393         ,"  -m      perform wmove/move separately from insert-functions"
394         ,"  -w      use window-parameter even when stdscr would be implied"
395     };
396     unsigned n;
397     for (n = 0; n < SIZEOF(tbl); ++n)
398         fprintf(stderr, "%s\n", tbl[n]);
399     ExitProgram(EXIT_FAILURE);
400 }
401
402 int
403 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
404 {
405     int ch;
406
407     setlocale(LC_ALL, "");
408
409     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
410         switch (ch) {
411         case 'f':
412             init_linedata(optarg);
413             break;
414         case 'm':
415             m_opt = TRUE;
416             break;
417         case 'n':
418             n_opt = atoi(optarg);
419             if (n_opt == 0)
420                 n_opt = -1;
421             break;
422         case 'w':
423             w_opt = TRUE;
424             break;
425         default:
426             usage();
427             break;
428         }
429     }
430     if (optind < argc)
431         usage();
432
433     test_inserts(0);
434     endwin();
435     ExitProgram(EXIT_SUCCESS);
436 }
437 #else
438 int
439 main(void)
440 {
441     printf("This program requires the winsstr function\n");
442     ExitProgram(EXIT_FAILURE);
443 }
444 #endif /* HAVE_WINSSTR */