]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/filter.c
ncurses 6.0 - patch 20160409
[ncurses.git] / test / filter.c
1 /****************************************************************************
2  * Copyright (c) 1998-2014,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 /*
30  * Author:  Thomas E. Dickey 1998
31  *
32  * $Id: filter.c,v 1.23 2016/04/09 23:52:22 tom Exp $
33  *
34  * An example of the 'filter()' function in ncurses, this program prompts
35  * for commands and executes them (like a command shell).  It illustrates
36  * how ncurses can be used to implement programs that are not full-screen.
37  *
38  * Ncurses differs slightly from SVr4 curses.  The latter does not flush its
39  * state when exiting program mode, so the attributes on the command lines of
40  * this program 'bleed' onto the executed commands.  Rather than use the
41  * reset_shell_mode() and reset_prog_mode() functions, we could invoke endwin()
42  * and refresh(), but that does not work any better.
43  */
44 #include <test.priv.h>
45
46 #if HAVE_FILTER
47
48 #include <time.h>
49
50 static int
51 show_prompt(int underline, bool clocked)
52 {
53     int limit = COLS;
54
55     move(0, 0);
56     attrset(A_NORMAL);
57     clrtoeol();
58     attrset(A_BOLD);
59     addstr("Command: ");
60
61     limit -= getcurx(stdscr);
62
63     if (clocked) {
64         if (limit >= 3) {
65             time_t now = time((time_t *) 0);
66             struct tm *my = localtime(&now);
67             char buffer[80];
68             int skip, y, x;
69             int margin;
70
71             sprintf(buffer, "%02d:%02d:%02d",
72                     my->tm_hour,
73                     my->tm_min,
74                     my->tm_sec);
75
76             if (limit > 9) {
77                 skip = 0;
78             } else if (limit > 6) {
79                 skip = 3;
80             } else {
81                 skip = 6;
82             }
83             /*
84              * Write the clock message on the right-margin so we can show the
85              * results of resizing the screen.
86              */
87             getyx(stdscr, y, x);
88             margin = (int) strlen(buffer) - skip;
89             limit -= margin;
90             move(0, COLS - margin);
91             addstr(buffer);
92             move(y, x);
93         }
94     }
95     attron((chtype) underline);
96     return limit;
97 }
98
99 static int
100 new_command(char *buffer, int length, int underline, bool clocked, bool polled)
101 {
102     int code = OK;
103     int limit;
104
105     if (polled) {
106         bool done = FALSE;
107         bool first = TRUE;
108         int y, x;
109         int n;
110         int mark = 0;
111         int used = 0;
112         const int gap = 2;
113
114         timeout(20);            /* no one types 50CPS... */
115         while (!done) {
116             int ch = getch();
117
118             buffer[used] = '\0';
119
120             limit = show_prompt(underline, clocked);
121             if (first) {
122                 getyx(stdscr, y, x);
123                 first = FALSE;
124             } else {
125                 int left = 0;
126
127                 /*
128                  * if the screen is too narrow to show the whole buffer,
129                  * shift the editing point left/right as needed.
130                  */
131                 move(y, x);
132                 if ((used + gap) > limit) {
133                     while ((mark - left + gap) > limit) {
134                         left += limit / 2;
135                     }
136                 }
137                 printw("%.*s", limit, buffer + left);
138                 move(y, x + mark - left);
139             }
140
141             switch (ch) {
142             case ERR:
143                 continue;
144             case '\004':
145                 code = ERR;
146                 done = TRUE;
147                 break;
148             case KEY_ENTER:
149             case '\n':
150                 done = TRUE;
151                 break;
152             case KEY_BACKSPACE:
153             case '\b':
154                 if (used) {
155                     if (mark < used) {
156                         /* getnstr does not do this */
157                         if (mark > 0) {
158                             --mark;
159                             for (n = mark; n < used; ++n) {
160                                 buffer[n] = buffer[n + 1];
161                             }
162                         } else {
163                             flash();
164                         }
165                     } else {
166                         /* getnstr does this */
167                         mark = --used;
168                         buffer[used] = '\0';
169                     }
170                 } else {
171                     flash();
172                 }
173                 break;
174                 /*
175                  * Unlike getnstr, this function can move the cursor into the
176                  * middle of the buffer and insert/delete at that point.
177                  */
178             case KEY_HOME:
179                 mark = 0;
180                 break;
181             case KEY_END:
182                 mark = used;
183                 break;
184             case KEY_LEFT:
185                 if (mark > 0) {
186                     mark--;
187                 } else {
188                     flash();
189                 }
190                 break;
191             case KEY_RIGHT:
192                 if (mark < used) {
193                     mark++;
194                 } else {
195                     flash();
196                 }
197                 break;
198 #ifdef KEY_EVENT
199             case KEY_EVENT:
200                 continue;
201 #endif
202 #ifdef KEY_RESIZE
203             case KEY_RESIZE:
204                 /*
205                  * Unlike getnstr, this function "knows" what the whole screen
206                  * is supposed to look like, and can handle resize events.
207                  */
208                 continue;
209 #endif
210             case '\t':
211                 ch = ' ';
212                 /* FALLTHRU */
213             default:
214                 if (ch >= KEY_MIN) {
215                     flash();
216                     continue;
217                 }
218                 if (mark < used) {
219                     /* getnstr does not do this... */
220                     for (n = used + 1; n > mark; --n) {
221                         buffer[n] = buffer[n - 1];
222                     }
223                     buffer[mark] = (char) ch;
224                     used++;
225                     mark++;
226                 } else {
227                     /* getnstr does this part */
228                     buffer[used] = (char) ch;
229                     mark = ++used;
230                 }
231                 break;
232             }
233         }
234     } else {
235         show_prompt(underline, clocked);
236
237         code = getnstr(buffer, length);
238         /*
239          * If this returns anything except ERR/OK, it would be one of ncurses's
240          * extensions.  Fill the buffer with something harmless that the shell
241          * will execute as a comment.
242          */
243 #ifdef KEY_EVENT
244         if (code == KEY_EVENT)
245             strcpy(buffer, "# event!");
246 #endif
247 #ifdef KEY_RESIZE
248         if (code == KEY_RESIZE) {
249             strcpy(buffer, "# resize!");
250             getch();
251         }
252 #endif
253     }
254     attroff((chtype) underline);
255     attroff(A_BOLD);
256     printw("\n");
257
258     return code;
259 }
260
261 static void
262 usage(void)
263 {
264     static const char *msg[] =
265     {
266         "Usage: filter [options]"
267         ,""
268         ,"Options:"
269         ,"  -c   show current time on prompt line with \"Command\""
270         ,"  -i   use initscr() rather than newterm()"
271         ,"  -p   poll for individual characters rather than using getnstr"
272     };
273     unsigned n;
274     for (n = 0; n < SIZEOF(msg); n++)
275         fprintf(stderr, "%s\n", msg[n]);
276     ExitProgram(EXIT_FAILURE);
277 }
278
279 int
280 main(int argc, char *argv[])
281 {
282     int ch;
283     char buffer[80];
284     int underline;
285     bool c_option = FALSE;
286     bool i_option = FALSE;
287     bool p_option = FALSE;
288
289     setlocale(LC_ALL, "");
290
291     while ((ch = getopt(argc, argv, "cip")) != -1) {
292         switch (ch) {
293         case 'c':
294             c_option = TRUE;
295             break;
296         case 'i':
297             i_option = TRUE;
298             break;
299         case 'p':
300             p_option = TRUE;
301             break;
302         default:
303             usage();
304         }
305     }
306
307     printf("starting filter program using %s...\n",
308            i_option ? "initscr" : "newterm");
309     filter();
310     if (i_option) {
311         initscr();
312     } else {
313         (void) newterm((char *) 0, stdout, stdin);
314     }
315     cbreak();
316     keypad(stdscr, TRUE);
317
318     if (has_colors()) {
319         int background = COLOR_BLACK;
320         start_color();
321 #if HAVE_USE_DEFAULT_COLORS
322         if (use_default_colors() != ERR)
323             background = -1;
324 #endif
325         init_pair(1, COLOR_CYAN, (short) background);
326         underline = COLOR_PAIR(1);
327     } else {
328         underline = A_UNDERLINE;
329     }
330
331     for (;;) {
332         int code = new_command(buffer, sizeof(buffer) - 1,
333                                underline, c_option, p_option);
334         if (code == ERR || *buffer == '\0')
335             break;
336         reset_shell_mode();
337         printf("\n");
338         fflush(stdout);
339         IGNORE_RC(system(buffer));
340         reset_prog_mode();
341         touchwin(stdscr);
342         erase();
343         refresh();
344     }
345     printw("done");
346     refresh();
347     endwin();
348     ExitProgram(EXIT_SUCCESS);
349 }
350 #else
351 int
352 main(void)
353 {
354     printf("This program requires the filter function\n");
355     ExitProgram(EXIT_FAILURE);
356 }
357 #endif /* HAVE_FILTER */