1 /****************************************************************************
2 * Copyright 2019,2020 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.34 2020/02/02 23:34:34 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 #include <test.priv.h>
52 show_prompt(int underline, bool clocked)
62 limit -= getcurx(stdscr);
66 time_t now = time((time_t *) 0);
67 struct tm *my = localtime(&now);
72 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%02d:%02d:%02d",
79 } else if (limit > 6) {
85 * Write the clock message on the right-margin so we can show the
86 * results of resizing the screen.
89 margin = (int) strlen(buffer) - skip;
91 move(0, COLS - margin);
101 new_command(char *buffer, int length, int underline, bool clocked, bool polled)
114 timeout(20); /* no one types 50CPS... */
121 limit = show_prompt(underline, clocked);
129 * if the screen is too narrow to show the whole buffer,
130 * shift the editing point left/right as needed.
133 if ((used + gap) > limit) {
134 while ((mark - left + gap) > limit) {
138 printw("%.*s", limit, buffer + left);
139 move(y, x + mark - left);
157 /* getnstr does not do this */
160 for (n = mark; n < used; ++n) {
161 buffer[n] = buffer[n + 1];
167 /* getnstr does this */
176 * Unlike getnstr, this function can move the cursor into the
177 * middle of the buffer and insert/delete at that point.
206 * Unlike getnstr, this function "knows" what the whole screen
207 * is supposed to look like, and can handle resize events.
220 /* getnstr does not do this... */
221 for (n = used + 1; n > mark; --n) {
222 buffer[n] = buffer[n - 1];
224 buffer[mark] = (char) ch;
228 /* getnstr does this part */
229 buffer[used] = (char) ch;
236 show_prompt(underline, clocked);
238 code = getnstr(buffer, length);
240 * If this returns anything except ERR/OK, it would be one of ncurses's
241 * extensions. Fill the buffer with something harmless that the shell
242 * will execute as a comment.
245 if (code == KEY_EVENT)
246 _nc_STRCPY(buffer, "# event!", length);
249 if (code == KEY_RESIZE) {
250 _nc_STRCPY(buffer, "# resize!", length);
262 #ifdef NCURSES_VERSION
264 * Cancel xterm's alternate-screen mode (from dialog -TD)
266 #define isprivate(s) ((s) != 0 && strstr(s, "\033[?") != 0)
268 cancel_altscreen(void)
270 if (isatty(fileno(stdout))
271 && key_mouse != 0 /* xterm and kindred */
272 && isprivate(enter_ca_mode)
273 && isprivate(exit_ca_mode)) {
275 * initscr() or newterm() already wrote enter_ca_mode as a side effect
276 * of initializing the screen. It would be nice to not even do that,
277 * but we do not really have access to the correct copy of the
278 * terminfo description until those functions have been invoked.
281 (void) putp(exit_ca_mode);
282 (void) fflush(stdout);
284 * Prevent ncurses from switching "back" to the normal screen when
285 * exiting from this program. That would move the cursor to the
286 * original location saved in xterm. Normally curses sets the cursor
287 * position to the first line after the display, but the alternate
288 * screen switching is done after that point.
290 * Cancelling the strings altogether also works around the buggy
291 * implementation of alternate-screen in rxvt, etc., which clear more
292 * of the display than they should.
303 static const char *msg[] =
305 "Usage: filter [options]"
308 #ifdef NCURSES_VERSION
309 ," -a suppress xterm alternate-screen by amending smcup/rmcup"
311 ," -c show current time on prompt line with \"Command\""
312 #if HAVE_USE_DEFAULT_COLORS
313 ," -d invoke use_default_colors"
315 ," -i use initscr() rather than newterm()"
316 ," -p poll for individual characters rather than using getnstr"
319 for (n = 0; n < SIZEOF(msg); n++)
320 fprintf(stderr, "%s\n", msg[n]);
321 ExitProgram(EXIT_FAILURE);
325 main(int argc, char *argv[])
330 #ifdef NCURSES_VERSION
331 bool a_option = FALSE;
333 bool c_option = FALSE;
334 #if HAVE_USE_DEFAULT_COLORS
335 bool d_option = FALSE;
337 bool i_option = FALSE;
338 bool p_option = FALSE;
340 setlocale(LC_ALL, "");
342 while ((ch = getopt(argc, argv, "adcip")) != -1) {
344 #ifdef NCURSES_VERSION
352 #if HAVE_USE_DEFAULT_COLORS
368 printf("starting filter program using %s...\n",
369 i_option ? "initscr" : "newterm");
374 if (newterm((char *) 0, stdout, stdin) == 0) {
375 fprintf(stderr, "cannot initialize terminal\n");
376 ExitProgram(EXIT_FAILURE);
379 #ifdef NCURSES_VERSION
385 keypad(stdscr, TRUE);
388 int background = COLOR_BLACK;
390 #if HAVE_USE_DEFAULT_COLORS
391 if (d_option && (use_default_colors() != ERR))
394 init_pair(1, COLOR_CYAN, (short) background);
395 underline = COLOR_PAIR(1);
397 underline = A_UNDERLINE;
401 int code = new_command(buffer, sizeof(buffer) - 1,
402 underline, c_option, p_option);
403 if (code == ERR || *buffer == '\0')
408 IGNORE_RC(system(buffer));
417 ExitProgram(EXIT_SUCCESS);
423 printf("This program requires the filter function\n");
424 ExitProgram(EXIT_FAILURE);
426 #endif /* HAVE_FILTER */