1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini reset implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * Written by Erik Andersen and Kent Robotti <robotti@metconnect.com>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 //config:config RESET
11 //config:	bool "reset (345 bytes)"
12 //config:	default y
13 //config:	help
14 //config:	This program is used to reset the terminal screen, if it
15 //config:	gets messed up.
16 
17 //applet:IF_RESET(APPLET_NOEXEC(reset, reset, BB_DIR_USR_BIN, BB_SUID_DROP, reset))
18 
19 //kbuild:lib-$(CONFIG_RESET) += reset.o
20 
21 //usage:#define reset_trivial_usage
22 //usage:       ""
23 //usage:#define reset_full_usage "\n\n"
24 //usage:       "Reset the screen"
25 
26 /* "Standard" version of this tool is in ncurses package */
27 
28 #include "libbb.h"
29 
30 #define ESC "\033"
31 
32 #if ENABLE_STTY
33 int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 #endif
35 
36 int reset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
reset_main(int argc UNUSED_PARAM,char ** argv UNUSED_PARAM)37 int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
38 {
39 	static const char *const args[] = {
40 		"stty", "sane", NULL
41 	};
42 
43 	/* no options, no getopt */
44 
45 	if (/*isatty(STDIN_FILENO) &&*/ isatty(STDOUT_FILENO)) {
46 		/* See 'man 4 console_codes' for details:
47 		 * "ESC c"        -- Reset
48 		 * "ESC ( B"      -- Select G0 Character Set (B = US)
49 		 * "ESC [ m"      -- Reset all display attributes
50 		 * "ESC [ J"      -- Erase to the end of screen
51 		 * "ESC [ ? 25 h" -- Make cursor visible
52 		 */
53 		printf(ESC"c" ESC"(B" ESC"[m" ESC"[J" ESC"[?25h");
54 		/* http://bugs.busybox.net/view.php?id=1414:
55 		 * people want it to reset echo etc: */
56 #if ENABLE_STTY
57 		return stty_main(2, (char**)args);
58 #else
59 		/* Make sure stdout gets drained before we execvp */
60 		fflush_all();
61 		execvp("stty", (char**)args);
62 #endif
63 	}
64 	return EXIT_SUCCESS;
65 }
66