1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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.32 2017/09/28 23:40:39 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 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(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 _nc_STRCPY(buffer, "# event!", length);
248 if (code == KEY_RESIZE) {
249 _nc_STRCPY(buffer, "# resize!", length);
261 #ifdef NCURSES_VERSION
263 * Cancel xterm's alternate-screen mode (from dialog -TD)
265 #define isprivate(s) ((s) != 0 && strstr(s, "\033[?") != 0)
267 cancel_altscreen(void)
269 if (isatty(fileno(stdout))
270 && key_mouse != 0 /* xterm and kindred */
271 && isprivate(enter_ca_mode)
272 && isprivate(exit_ca_mode)) {
274 * initscr() or newterm() already wrote enter_ca_mode as a side effect
275 * of initializing the screen. It would be nice to not even do that,
276 * but we do not really have access to the correct copy of the
277 * terminfo description until those functions have been invoked.
280 (void) putp(exit_ca_mode);
281 (void) fflush(stdout);
283 * Prevent ncurses from switching "back" to the normal screen when
284 * exiting from this program. That would move the cursor to the
285 * original location saved in xterm. Normally curses sets the cursor
286 * position to the first line after the display, but the alternate
287 * screen switching is done after that point.
289 * Cancelling the strings altogether also works around the buggy
290 * implementation of alternate-screen in rxvt, etc., which clear more
291 * of the display than they should.
302 static const char *msg[] =
304 "Usage: filter [options]"
307 #ifdef NCURSES_VERSION
308 ," -a suppress xterm alternate-screen by amending smcup/rmcup"
310 ," -c show current time on prompt line with \"Command\""
311 #if HAVE_USE_DEFAULT_COLORS
312 ," -d invoke use_default_colors"
314 ," -i use initscr() rather than newterm()"
315 ," -p poll for individual characters rather than using getnstr"
318 for (n = 0; n < SIZEOF(msg); n++)
319 fprintf(stderr, "%s\n", msg[n]);
320 ExitProgram(EXIT_FAILURE);
324 main(int argc, char *argv[])
329 #ifdef NCURSES_VERSION
330 bool a_option = FALSE;
332 bool c_option = FALSE;
333 #if HAVE_USE_DEFAULT_COLORS
334 bool d_option = FALSE;
336 bool i_option = FALSE;
337 bool p_option = FALSE;
339 setlocale(LC_ALL, "");
341 while ((ch = getopt(argc, argv, "adcip")) != -1) {
343 #ifdef NCURSES_VERSION
351 #if HAVE_USE_DEFAULT_COLORS
367 printf("starting filter program using %s...\n",
368 i_option ? "initscr" : "newterm");
373 if (newterm((char *) 0, stdout, stdin) == 0) {
374 fprintf(stderr, "cannot initialize terminal\n");
375 ExitProgram(EXIT_FAILURE);
378 #ifdef NCURSES_VERSION
384 keypad(stdscr, TRUE);
387 int background = COLOR_BLACK;
389 #if HAVE_USE_DEFAULT_COLORS
390 if (d_option && (use_default_colors() != ERR))
393 init_pair(1, COLOR_CYAN, (short) background);
394 underline = COLOR_PAIR(1);
396 underline = A_UNDERLINE;
400 int code = new_command(buffer, sizeof(buffer) - 1,
401 underline, c_option, p_option);
402 if (code == ERR || *buffer == '\0')
407 IGNORE_RC(system(buffer));
416 ExitProgram(EXIT_SUCCESS);
422 printf("This program requires the filter function\n");
423 ExitProgram(EXIT_FAILURE);
425 #endif /* HAVE_FILTER */