1 /****************************************************************************
2 * Copyright 2019-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey 1998
33 * $Id: filter.c,v 1.38 2022/12/04 00:40:11 tom Exp $
35 * An example of the 'filter()' function in ncurses, this program prompts
36 * for commands and executes them (like a command shell). It illustrates
37 * how ncurses can be used to implement programs that are not full-screen.
39 * Ncurses differs slightly from SVr4 curses. The latter does not flush its
40 * state when exiting program mode, so the attributes on the command lines of
41 * this program 'bleed' onto the executed commands. Rather than use the
42 * reset_shell_mode() and reset_prog_mode() functions, we could invoke endwin()
43 * and refresh(), but that does not work any better.
45 #define NEED_KEY_EVENT
46 #include <test.priv.h>
53 show_prompt(int underline, bool clocked)
63 limit -= getcurx(stdscr);
67 time_t now = time((time_t *) 0);
68 struct tm *my = localtime(&now);
73 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%02d:%02d:%02d",
80 } else if (limit > 6) {
86 * Write the clock message on the right-margin so we can show the
87 * results of resizing the screen.
90 margin = (int) strlen(buffer) - skip;
92 move(0, COLS - margin);
102 new_command(char *buffer, int length, int underline, bool clocked, bool polled)
115 timeout(20); /* no one types 50CPS... */
122 limit = show_prompt(underline, clocked);
130 * if the screen is too narrow to show the whole buffer,
131 * shift the editing point left/right as needed.
134 if ((used + gap) > limit) {
135 while ((mark - left + gap) > limit) {
139 printw("%.*s", limit, buffer + left);
140 move(y, x + mark - left);
158 /* getnstr does not do this */
161 for (n = mark; n < used; ++n) {
162 buffer[n] = buffer[n + 1];
168 /* getnstr does this */
177 * Unlike getnstr, this function can move the cursor into the
178 * middle of the buffer and insert/delete at that point.
207 * Unlike getnstr, this function "knows" what the whole screen
208 * is supposed to look like, and can handle resize events.
221 /* getnstr does not do this... */
222 for (n = used + 1; n > mark; --n) {
223 buffer[n] = buffer[n - 1];
225 buffer[mark] = (char) ch;
229 /* getnstr does this part */
230 buffer[used] = (char) ch;
237 show_prompt(underline, clocked);
239 code = getnstr(buffer, length);
241 * If this returns anything except ERR/OK, it would be one of ncurses's
242 * extensions. Fill the buffer with something harmless that the shell
243 * will execute as a comment.
246 if (code == KEY_EVENT)
247 _nc_STRCPY(buffer, "# event!", length);
250 if (code == KEY_RESIZE) {
251 _nc_STRCPY(buffer, "# resize!", length);
263 #ifdef NCURSES_VERSION
265 * Cancel xterm's alternate-screen mode (from dialog -TD)
267 #define isprivate(s) ((s) != 0 && strstr(s, "\033[?") != 0)
269 cancel_altscreen(void)
271 if (isatty(fileno(stdout))
272 && key_mouse != 0 /* xterm and kindred */
273 && isprivate(enter_ca_mode)
274 && isprivate(exit_ca_mode)) {
276 * initscr() or newterm() already wrote enter_ca_mode as a side effect
277 * of initializing the screen. It would be nice to not even do that,
278 * but we do not really have access to the correct copy of the
279 * terminfo description until those functions have been invoked.
282 (void) putp(exit_ca_mode);
283 (void) fflush(stdout);
285 * Prevent ncurses from switching "back" to the normal screen when
286 * exiting from this program. That would move the cursor to the
287 * original location saved in xterm. Normally curses sets the cursor
288 * position to the first line after the display, but the alternate
289 * screen switching is done after that point.
291 * Cancelling the strings altogether also works around the buggy
292 * implementation of alternate-screen in rxvt, etc., which clear more
293 * of the display than they should.
304 static const char *msg[] =
306 "Usage: filter [options]"
310 #ifdef NCURSES_VERSION
311 ," -a suppress xterm alternate-screen by amending smcup/rmcup"
313 ," -c show current time on prompt line with \"Command\""
314 #if HAVE_USE_DEFAULT_COLORS
315 ," -d invoke use_default_colors"
317 ," -i use initscr() rather than newterm()"
318 ," -p poll for individual characters rather than using getnstr"
321 for (n = 0; n < SIZEOF(msg); n++)
322 fprintf(stderr, "%s\n", msg[n]);
323 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
330 main(int argc, char *argv[])
335 #ifdef NCURSES_VERSION
336 bool a_option = FALSE;
338 bool c_option = FALSE;
339 #if HAVE_USE_DEFAULT_COLORS
340 bool d_option = FALSE;
342 bool i_option = FALSE;
343 bool p_option = FALSE;
345 setlocale(LC_ALL, "");
347 while ((ch = getopt(argc, argv, OPTS_COMMON "adcip")) != -1) {
349 #ifdef NCURSES_VERSION
357 #if HAVE_USE_DEFAULT_COLORS
370 ExitProgram(EXIT_SUCCESS);
372 usage(ch == OPTS_USAGE);
377 printf("starting filter program using %s...\n",
378 i_option ? "initscr" : "newterm");
383 if (newterm((char *) 0, stdout, stdin) == 0) {
384 fprintf(stderr, "cannot initialize terminal\n");
385 ExitProgram(EXIT_FAILURE);
388 #ifdef NCURSES_VERSION
394 keypad(stdscr, TRUE);
397 int background = COLOR_BLACK;
399 #if HAVE_USE_DEFAULT_COLORS
400 if (d_option && (use_default_colors() != ERR))
403 init_pair(1, COLOR_CYAN, (short) background);
404 underline = COLOR_PAIR(1);
406 underline = A_UNDERLINE;
410 int code = new_command(buffer, sizeof(buffer) - 1,
411 underline, c_option, p_option);
412 if (code == ERR || *buffer == '\0')
417 IGNORE_RC(system(buffer));
426 ExitProgram(EXIT_SUCCESS);
432 printf("This program requires the filter function\n");
433 ExitProgram(EXIT_FAILURE);
435 #endif /* HAVE_FILTER */