reset.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 (275 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. //applet:IF_RESET(APPLET_NOEXEC(reset, reset, BB_DIR_USR_BIN, BB_SUID_DROP, reset))
  17. //kbuild:lib-$(CONFIG_RESET) += reset.o
  18. //usage:#define reset_trivial_usage
  19. //usage: ""
  20. //usage:#define reset_full_usage "\n\n"
  21. //usage: "Reset the screen"
  22. /* "Standard" version of this tool is in ncurses package */
  23. #include "libbb.h"
  24. #define ESC "\033"
  25. #if ENABLE_STTY
  26. int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. #endif
  28. int reset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  29. int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  30. {
  31. static const char *const args[] = {
  32. "stty", "sane", NULL
  33. };
  34. /* no options, no getopt */
  35. if (/*isatty(STDIN_FILENO) &&*/ isatty(STDOUT_FILENO)) {
  36. /* See 'man 4 console_codes' for details:
  37. * "ESC c" -- Reset
  38. * "ESC ( B" -- Select G0 Character Set (B = US)
  39. * "ESC [ m" -- Reset all display attributes
  40. * "ESC [ J" -- Erase to the end of screen
  41. * "ESC [ ? 25 h" -- Make cursor visible
  42. */
  43. printf(ESC"c" ESC"(B" ESC"[m" ESC"[J" ESC"[?25h");
  44. /* http://bugs.busybox.net/view.php?id=1414:
  45. * people want it to reset echo etc: */
  46. #if ENABLE_STTY
  47. return stty_main(2, (char**)args);
  48. #else
  49. /* Make sure stdout gets drained before we execvp */
  50. fflush_all();
  51. execvp("stty", (char**)args);
  52. #endif
  53. }
  54. return EXIT_SUCCESS;
  55. }