blob: 614806ba754e8a0f8f45fe4a5d20e10004adee19 [file] [log] [blame]
Eric Andersendc746162000-08-21 21:26:33 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini reset implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 * Written by Erik Andersen and Kent Robotti <robotti@metconnect.com>
Eric Andersendc746162000-08-21 21:26:33 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersendc746162000-08-21 21:26:33 +00009 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010010//config:config RESET
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "reset (275 bytes)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: This program is used to reset the terminal screen, if it
15//config: gets messed up.
Denys Vlasenko6d932992016-11-23 10:39:27 +010016
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020017//applet:IF_RESET(APPLET_NOEXEC(reset, reset, BB_DIR_USR_BIN, BB_SUID_DROP, reset))
Denys Vlasenko6d932992016-11-23 10:39:27 +010018
19//kbuild:lib-$(CONFIG_RESET) += reset.o
Denis Vlasenkod4f0b942008-02-26 15:33:10 +000020
Pere Orga55068c42011-03-27 23:42:28 +020021//usage:#define reset_trivial_usage
22//usage: ""
23//usage:#define reset_full_usage "\n\n"
24//usage: "Reset the screen"
25
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020026/* "Standard" version of this tool is in ncurses package */
27
Marek Polacek7b181072010-10-28 21:34:56 +020028#include "libbb.h"
29
30#define ESC "\033"
31
Denis Vlasenkod4f0b942008-02-26 15:33:10 +000032#if ENABLE_STTY
33int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34#endif
35
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int reset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000037int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersendc746162000-08-21 21:26:33 +000038{
Denis Vlasenkod4f0b942008-02-26 15:33:10 +000039 static const char *const args[] = {
40 "stty", "sane", NULL
41 };
42
43 /* no options, no getopt */
44
Marek Polacek7b181072010-10-28 21:34:56 +020045 if (/*isatty(STDIN_FILENO) &&*/ isatty(STDOUT_FILENO)) {
Eric Andersen08573e02003-12-20 07:07:22 +000046 /* See 'man 4 console_codes' for details:
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020047 * "ESC c" -- Reset
Denys Vlasenkob2f00ac2011-02-10 10:18:22 +010048 * "ESC ( B" -- Select G0 Character Set (B = US)
Denys Vlasenko8187e012017-09-13 22:48:30 +020049 * "ESC [ m" -- Reset all display attributes
Marek Polacek7b181072010-10-28 21:34:56 +020050 * "ESC [ J" -- Erase to the end of screen
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020051 * "ESC [ ? 25 h" -- Make cursor visible
Eric Andersen08573e02003-12-20 07:07:22 +000052 */
Denys Vlasenko8187e012017-09-13 22:48:30 +020053 printf(ESC"c" ESC"(B" ESC"[m" ESC"[J" ESC"[?25h");
Denis Vlasenkod4f0b942008-02-26 15:33:10 +000054 /* 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
Glenn Matthews02a2a272017-02-17 23:01:13 +010059 /* Make sure stdout gets drained before we execvp */
60 fflush_all();
Denis Vlasenko85c24712008-03-17 09:04:04 +000061 execvp("stty", (char**)args);
Denis Vlasenkod4f0b942008-02-26 15:33:10 +000062#endif
Eric Andersen08573e02003-12-20 07:07:22 +000063 }
Eric Andersen7f6295f2003-10-22 10:34:15 +000064 return EXIT_SUCCESS;
Eric Andersendc746162000-08-21 21:26:33 +000065}