1 /****************************************************************************
2 * Copyright (c) 1998-2014,2016 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey 1998
32 * $Id: filter.c,v 1.24 2016/04/16 22:11:03 tom Exp $
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.
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.
44 #include <test.priv.h>
51 show_prompt(int underline, bool clocked)
61 limit -= getcurx(stdscr);
65 time_t now = time((time_t *) 0);
66 struct tm *my = localtime(&now);
71 sprintf(buffer, "%02d:%02d:%02d",
78 } else if (limit > 6) {
84 * Write the clock message on the right-margin so we can show the
85 * results of resizing the screen.
88 margin = (int) strlen(buffer) - skip;
90 move(0, COLS - margin);
100 new_command(char *buffer, int length, int underline, bool clocked, bool polled)
114 timeout(20); /* no one types 50CPS... */
120 limit = show_prompt(underline, clocked);
128 * if the screen is too narrow to show the whole buffer,
129 * shift the editing point left/right as needed.
132 if ((used + gap) > limit) {
133 while ((mark - left + gap) > limit) {
137 printw("%.*s", limit, buffer + left);
138 move(y, x + mark - left);
156 /* getnstr does not do this */
159 for (n = mark; n < used; ++n) {
160 buffer[n] = buffer[n + 1];
166 /* getnstr does this */
175 * Unlike getnstr, this function can move the cursor into the
176 * middle of the buffer and insert/delete at that point.
205 * Unlike getnstr, this function "knows" what the whole screen
206 * is supposed to look like, and can handle resize events.
219 /* getnstr does not do this... */
220 for (n = used + 1; n > mark; --n) {
221 buffer[n] = buffer[n - 1];
223 buffer[mark] = (char) ch;
227 /* getnstr does this part */
228 buffer[used] = (char) ch;
235 show_prompt(underline, clocked);
237 code = getnstr(buffer, length);
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.
244 if (code == KEY_EVENT)
245 strcpy(buffer, "# event!");
248 if (code == KEY_RESIZE) {
249 strcpy(buffer, "# resize!");
264 static const char *msg[] =
266 "Usage: filter [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"
274 for (n = 0; n < SIZEOF(msg); n++)
275 fprintf(stderr, "%s\n", msg[n]);
276 ExitProgram(EXIT_FAILURE);
280 main(int argc, char *argv[])
285 bool c_option = FALSE;
286 bool i_option = FALSE;
287 bool p_option = FALSE;
289 setlocale(LC_ALL, "");
291 while ((ch = getopt(argc, argv, "cip")) != -1) {
307 printf("starting filter program using %s...\n",
308 i_option ? "initscr" : "newterm");
313 (void) newterm((char *) 0, stdout, stdin);
316 keypad(stdscr, TRUE);
319 int background = COLOR_BLACK;
321 #if HAVE_USE_DEFAULT_COLORS
322 if (use_default_colors() != ERR)
325 init_pair(1, COLOR_CYAN, (short) background);
326 underline = COLOR_PAIR(1);
328 underline = A_UNDERLINE;
332 int code = new_command(buffer, sizeof(buffer) - 1,
333 underline, c_option, p_option);
334 if (code == ERR || *buffer == '\0')
339 IGNORE_RC(system(buffer));
348 ExitProgram(EXIT_SUCCESS);
354 printf("This program requires the filter function\n");
355 ExitProgram(EXIT_FAILURE);
357 #endif /* HAVE_FILTER */