resize.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * resize - set terminal width and height.
  4. *
  5. * Copyright 2006 Bernhard Reutner-Fischer
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config RESIZE
  10. //config: bool "resize"
  11. //config: default y
  12. //config: help
  13. //config: This program is used to (re)set the width and height of your current
  14. //config: terminal.
  15. //config:
  16. //config:config FEATURE_RESIZE_PRINT
  17. //config: bool "Print environment variables"
  18. //config: default y
  19. //config: depends on RESIZE
  20. //config: help
  21. //config: Prints the newly set size (number of columns and rows) of
  22. //config: the terminal.
  23. //config: E.g.:
  24. //config: COLUMNS=80;LINES=44;export COLUMNS LINES;
  25. //applet:IF_RESIZE(APPLET(resize, BB_DIR_USR_BIN, BB_SUID_DROP))
  26. //kbuild:lib-$(CONFIG_RESIZE) += resize.o
  27. //usage:#define resize_trivial_usage
  28. //usage: ""
  29. //usage:#define resize_full_usage "\n\n"
  30. //usage: "Resize the screen"
  31. #include "libbb.h"
  32. #include "common_bufsiz.h"
  33. #define ESC "\033"
  34. #define old_termios_p ((struct termios*)bb_common_bufsiz1)
  35. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  36. static void
  37. onintr(int sig UNUSED_PARAM)
  38. {
  39. tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
  40. _exit(EXIT_FAILURE);
  41. }
  42. int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  43. int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  44. {
  45. struct termios new;
  46. struct winsize w = { 0, 0, 0, 0 };
  47. int ret;
  48. INIT_G();
  49. /* We use _stderr_ in order to make resize usable
  50. * in shell backticks (those redirect stdout away from tty).
  51. * NB: other versions of resize open "/dev/tty"
  52. * and operate on it - should we do the same?
  53. */
  54. tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
  55. memcpy(&new, old_termios_p, sizeof(new));
  56. new.c_cflag |= (CLOCAL | CREAD);
  57. new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  58. bb_signals(0
  59. + (1 << SIGINT)
  60. + (1 << SIGQUIT)
  61. + (1 << SIGTERM)
  62. + (1 << SIGALRM)
  63. , onintr);
  64. tcsetattr(STDERR_FILENO, TCSANOW, &new);
  65. /* save_cursor_pos 7
  66. * scroll_whole_screen [r
  67. * put_cursor_waaaay_off [$x;$yH
  68. * get_cursor_pos [6n
  69. * restore_cursor_pos 8
  70. */
  71. fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
  72. alarm(3); /* Just in case terminal won't answer */
  73. //BUG: death by signal won't restore termios
  74. scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
  75. fprintf(stderr, ESC"8");
  76. /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
  77. * by calculating character cell HxW from old values
  78. * (gotten via TIOCGWINSZ) and recomputing *pixel values */
  79. ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
  80. tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
  81. if (ENABLE_FEATURE_RESIZE_PRINT)
  82. printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
  83. w.ws_col, w.ws_row);
  84. return ret;
  85. }