xref: /dflybsd-src/contrib/ncurses/progs/clear.c (revision 0cadad7e49c6219b0de0675ef6a6f44683d177d4)
15f4613f2SJohn Marino /****************************************************************************
2*32bb5217SDaniel Fojt  * Copyright 2020 Thomas E. Dickey                                          *
3*32bb5217SDaniel Fojt  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
45f4613f2SJohn Marino  *                                                                          *
55f4613f2SJohn Marino  * Permission is hereby granted, free of charge, to any person obtaining a  *
65f4613f2SJohn Marino  * copy of this software and associated documentation files (the            *
75f4613f2SJohn Marino  * "Software"), to deal in the Software without restriction, including      *
85f4613f2SJohn Marino  * without limitation the rights to use, copy, modify, merge, publish,      *
95f4613f2SJohn Marino  * distribute, distribute with modifications, sublicense, and/or sell       *
105f4613f2SJohn Marino  * copies of the Software, and to permit persons to whom the Software is    *
115f4613f2SJohn Marino  * furnished to do so, subject to the following conditions:                 *
125f4613f2SJohn Marino  *                                                                          *
135f4613f2SJohn Marino  * The above copyright notice and this permission notice shall be included  *
145f4613f2SJohn Marino  * in all copies or substantial portions of the Software.                   *
155f4613f2SJohn Marino  *                                                                          *
165f4613f2SJohn Marino  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
175f4613f2SJohn Marino  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
185f4613f2SJohn Marino  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
195f4613f2SJohn Marino  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
205f4613f2SJohn Marino  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
215f4613f2SJohn Marino  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
225f4613f2SJohn Marino  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
235f4613f2SJohn Marino  *                                                                          *
245f4613f2SJohn Marino  * Except as contained in this notice, the name(s) of the above copyright   *
255f4613f2SJohn Marino  * holders shall not be used in advertising or otherwise to promote the     *
265f4613f2SJohn Marino  * sale, use or other dealings in this Software without prior written       *
275f4613f2SJohn Marino  * authorization.                                                           *
285f4613f2SJohn Marino  ****************************************************************************/
295f4613f2SJohn Marino 
305f4613f2SJohn Marino /****************************************************************************
315f4613f2SJohn Marino  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
325f4613f2SJohn Marino  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
335f4613f2SJohn Marino  *     and: Thomas E. Dickey                        1996-on                 *
345f4613f2SJohn Marino  ****************************************************************************/
355f4613f2SJohn Marino 
365f4613f2SJohn Marino /*
375f4613f2SJohn Marino  * clear.c --  clears the terminal's screen
385f4613f2SJohn Marino  */
395f4613f2SJohn Marino 
405f4613f2SJohn Marino #define USE_LIBTINFO
41*32bb5217SDaniel Fojt #include <clear_cmd.h>
42*32bb5217SDaniel Fojt #include <tty_settings.h>
435f4613f2SJohn Marino 
44*32bb5217SDaniel Fojt MODULE_ID("$Id: clear.c,v 1.23 2020/02/02 23:34:34 tom Exp $")
455f4613f2SJohn Marino 
46*32bb5217SDaniel Fojt const char *_nc_progname = "clear";
47*32bb5217SDaniel Fojt 
48*32bb5217SDaniel Fojt static void
usage(void)49*32bb5217SDaniel Fojt usage(void)
505f4613f2SJohn Marino {
51*32bb5217SDaniel Fojt #define KEEP(s) s "\n"
52*32bb5217SDaniel Fojt     static const char msg[] =
53*32bb5217SDaniel Fojt     {
54*32bb5217SDaniel Fojt 	KEEP("")
55*32bb5217SDaniel Fojt 	KEEP("Options:")
56*32bb5217SDaniel Fojt 	KEEP("  -T TERM     use this instead of $TERM")
57*32bb5217SDaniel Fojt 	KEEP("  -V          print curses-version")
58*32bb5217SDaniel Fojt 	KEEP("  -x          do not try to clear scrollback")
59*32bb5217SDaniel Fojt     };
60*32bb5217SDaniel Fojt #undef KEEP
61*32bb5217SDaniel Fojt     (void) fprintf(stderr, "Usage: %s [options]\n", _nc_progname);
62*32bb5217SDaniel Fojt     fputs(msg, stderr);
63*32bb5217SDaniel Fojt     ExitProgram(EXIT_FAILURE);
645f4613f2SJohn Marino }
655f4613f2SJohn Marino 
665f4613f2SJohn Marino int
main(int argc GCC_UNUSED,char * argv[]GCC_UNUSED)675f4613f2SJohn Marino main(
685f4613f2SJohn Marino 	int argc GCC_UNUSED,
695f4613f2SJohn Marino 	char *argv[]GCC_UNUSED)
705f4613f2SJohn Marino {
71*32bb5217SDaniel Fojt     TTY tty_settings;
72*32bb5217SDaniel Fojt     int fd;
73*32bb5217SDaniel Fojt     int c;
74*32bb5217SDaniel Fojt     char *term;
75*32bb5217SDaniel Fojt     bool opt_x = FALSE;		/* clear scrollback if possible */
763468e90cSJohn Marino 
77*32bb5217SDaniel Fojt     _nc_progname = _nc_rootname(argv[0]);
78*32bb5217SDaniel Fojt     term = getenv("TERM");
793468e90cSJohn Marino 
80*32bb5217SDaniel Fojt     while ((c = getopt(argc, argv, "T:Vx")) != -1) {
81*32bb5217SDaniel Fojt 	switch (c) {
82*32bb5217SDaniel Fojt 	case 'T':
83*32bb5217SDaniel Fojt 	    use_env(FALSE);
84*32bb5217SDaniel Fojt 	    use_tioctl(TRUE);
85*32bb5217SDaniel Fojt 	    term = optarg;
86*32bb5217SDaniel Fojt 	    break;
87*32bb5217SDaniel Fojt 	case 'V':
88*32bb5217SDaniel Fojt 	    puts(curses_version());
89*32bb5217SDaniel Fojt 	    ExitProgram(EXIT_SUCCESS);
90*32bb5217SDaniel Fojt 	case 'x':		/* do not try to clear scrollback */
91*32bb5217SDaniel Fojt 	    opt_x = TRUE;
92*32bb5217SDaniel Fojt 	    break;
93*32bb5217SDaniel Fojt 	default:
94*32bb5217SDaniel Fojt 	    usage();
95*32bb5217SDaniel Fojt 	    /* NOTREACHED */
96*32bb5217SDaniel Fojt 	}
97*32bb5217SDaniel Fojt     }
98*32bb5217SDaniel Fojt     if (optind < argc)
99*32bb5217SDaniel Fojt 	usage();
1003468e90cSJohn Marino 
101*32bb5217SDaniel Fojt     fd = save_tty_settings(&tty_settings, FALSE);
102*32bb5217SDaniel Fojt 
103*32bb5217SDaniel Fojt     setupterm(term, fd, (int *) 0);
104*32bb5217SDaniel Fojt 
105*32bb5217SDaniel Fojt     ExitProgram((clear_cmd(opt_x) == ERR)
1065f4613f2SJohn Marino 		? EXIT_FAILURE
1075f4613f2SJohn Marino 		: EXIT_SUCCESS);
1085f4613f2SJohn Marino }
109