]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inserts.c
ncurses 5.9 - patch 20121208
[ncurses.git] / test / inserts.c
1 /****************************************************************************
2  * Copyright (c) 2002-2010,2012 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.26 2012/12/09 00:51:51 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         putenv(strcpy(cmd, "TABSIZE=8"));
165
166         initscr();
167         (void) cbreak();        /* take input chars one at a time, no wait for \n */
168         (void) noecho();        /* don't echo input */
169         keypad(stdscr, TRUE);
170
171         /*
172          * Show the characters inserted in color, to distinguish from those
173          * that are shifted.
174          */
175         if (has_colors()) {
176             start_color();
177             init_pair(1, COLOR_WHITE, COLOR_BLUE);
178         }
179     }
180
181     limit = LINES - 5;
182     if (level > 0) {
183         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
184         work = newwin(limit - 2, COLS - (2 * level), 1, level);
185         show = newwin(4, COLS, limit + 1, 0);
186         box(look, 0, 0);
187         wnoutrefresh(look);
188         limit -= 2;
189     } else {
190         work = stdscr;
191         show = derwin(stdscr, 4, COLS, limit + 1, 0);
192     }
193     keypad(work, TRUE);
194
195     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
196         MvWVLine(work, row, col, '.', limit - 2);
197
198     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
199     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
200     limit /= 2;
201
202     MvWAddStr(work, 1, 2, "String");
203     MvWAddStr(work, limit + 1, 2, "Chars");
204     wnoutrefresh(work);
205
206     buffer[length = 0] = '\0';
207     legend(show, level, option, buffer, length);
208     wnoutrefresh(show);
209
210     doupdate();
211
212     if (has_colors()) {
213         wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
214     }
215
216     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
217         wmove(work, row, margin + 1);
218         switch (ch) {
219         case key_RECUR:
220             test_inserts(level + 1);
221
222             touchwin(look);
223             touchwin(work);
224             touchwin(show);
225
226             wnoutrefresh(look);
227             wnoutrefresh(work);
228             wnoutrefresh(show);
229
230             doupdate();
231             break;
232         case key_NEWLINE:
233             if (row < limit) {
234                 ++row;
235                 /* put the whole string in, all at once */
236                 col2 = margin + 1;
237                 switch (option) {
238                 case oDefault:
239                     if (n_opt > 1) {
240                         for (col = 0; col < length; col += n_opt) {
241                             col2 = ColOf(buffer, col, margin);
242                             if (move(row, col2) != ERR) {
243                                 InsNStr(buffer + col, LEN(col));
244                             }
245                         }
246                     } else {
247                         if (move(row, col2) != ERR) {
248                             InsStr(buffer);
249                         }
250                     }
251                     break;
252                 case oMove:
253                     if (n_opt > 1) {
254                         for (col = 0; col < length; col += n_opt) {
255                             col2 = ColOf(buffer, col, margin);
256                             MvInsNStr(row, col2, buffer + col, LEN(col));
257                         }
258                     } else {
259                         MvInsStr(row, col2, buffer);
260                     }
261                     break;
262                 case oWindow:
263                     if (n_opt > 1) {
264                         for (col = 0; col < length; col += n_opt) {
265                             col2 = ColOf(buffer, col, margin);
266                             if (wmove(work, row, col2) != ERR) {
267                                 WInsNStr(work, buffer + col, LEN(col));
268                             }
269                         }
270                     } else {
271                         if (wmove(work, row, col2) != ERR) {
272                             WInsStr(work, buffer);
273                         }
274                     }
275                     break;
276                 case oMoveWindow:
277                     if (n_opt > 1) {
278                         for (col = 0; col < length; col += n_opt) {
279                             col2 = ColOf(buffer, col, margin);
280                             MvWInsNStr(work, row, col2, buffer + col, LEN(col));
281                         }
282                     } else {
283                         MvWInsStr(work, row, col2, buffer);
284                     }
285                     break;
286                 }
287
288                 /* do the corresponding single-character insertion */
289                 row2 = limit + row;
290                 for (col = 0; col < length; ++col) {
291                     col2 = ColOf(buffer, col, margin);
292                     switch (option) {
293                     case oDefault:
294                         if (move(row2, col2) != ERR) {
295                             InsCh(UChar(buffer[col]));
296                         }
297                         break;
298                     case oMove:
299                         MvInsCh(row2, col2, UChar(buffer[col]));
300                         break;
301                     case oWindow:
302                         if (wmove(work, row2, col2) != ERR) {
303                             WInsCh(work, UChar(buffer[col]));
304                         }
305                         break;
306                     case oMoveWindow:
307                         MvWInsCh(work, row2, col2, UChar(buffer[col]));
308                         break;
309                     }
310                 }
311             } else {
312                 beep();
313             }
314             break;
315         default:
316             if (ch <= 0 || ch > 255) {
317                 beep();
318                 break;
319             }
320             buffer[length++] = (char) ch;
321             buffer[length] = '\0';
322
323             /* put the string in, one character at a time */
324             col = ColOf(buffer, length - 1, margin);
325             switch (option) {
326             case oDefault:
327                 if (move(row, col) != ERR) {
328                     InsStr(buffer + length - 1);
329                 }
330                 break;
331             case oMove:
332                 MvInsStr(row, col, buffer + length - 1);
333                 break;
334             case oWindow:
335                 if (wmove(work, row, col) != ERR) {
336                     WInsStr(work, buffer + length - 1);
337                 }
338                 break;
339             case oMoveWindow:
340                 MvWInsStr(work, row, col, buffer + length - 1);
341                 break;
342             }
343
344             /* do the corresponding single-character insertion */
345             switch (option) {
346             case oDefault:
347                 if (move(limit + row, col) != ERR) {
348                     InsCh(UChar(ch));
349                 }
350                 break;
351             case oMove:
352                 MvInsCh(limit + row, col, UChar(ch));
353                 break;
354             case oWindow:
355                 if (wmove(work, limit + row, col) != ERR) {
356                     WInsCh(work, UChar(ch));
357                 }
358                 break;
359             case oMoveWindow:
360                 MvWInsCh(work, limit + row, col, UChar(ch));
361                 break;
362             }
363
364             wnoutrefresh(work);
365
366             legend(show, level, option, buffer, length);
367             wnoutrefresh(show);
368
369             doupdate();
370             break;
371         }
372     }
373     if (level > 0) {
374         delwin(work);
375         delwin(look);
376     }
377     delwin(show);
378 }
379
380 static void
381 usage(void)
382 {
383     static const char *tbl[] =
384     {
385         "Usage: inserts [options]"
386         ,""
387         ,"Options:"
388         ,"  -f FILE read data from given file"
389         ,"  -n NUM  limit string-inserts to NUM bytes on ^N replay"
390         ,"  -m      perform wmove/move separately from insert-functions"
391         ,"  -w      use window-parameter even when stdscr would be implied"
392     };
393     unsigned n;
394     for (n = 0; n < SIZEOF(tbl); ++n)
395         fprintf(stderr, "%s\n", tbl[n]);
396     ExitProgram(EXIT_FAILURE);
397 }
398
399 int
400 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
401 {
402     int ch;
403
404     setlocale(LC_ALL, "");
405
406     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
407         switch (ch) {
408         case 'f':
409             init_linedata(optarg);
410             break;
411         case 'm':
412             m_opt = TRUE;
413             break;
414         case 'n':
415             n_opt = atoi(optarg);
416             if (n_opt == 0)
417                 n_opt = -1;
418             break;
419         case 'w':
420             w_opt = TRUE;
421             break;
422         default:
423             usage();
424             break;
425         }
426     }
427     if (optind < argc)
428         usage();
429
430     test_inserts(0);
431     endwin();
432     ExitProgram(EXIT_SUCCESS);
433 }
434 #else
435 int
436 main(void)
437 {
438     printf("This program requires the winsstr function\n");
439     ExitProgram(EXIT_FAILURE);
440 }
441 #endif /* HAVE_WINSSTR */