3
0

resize.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 (903 bytes)"
  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_NOEXEC(resize, resize, BB_DIR_USR_BIN, BB_SUID_DROP, resize))
  26. /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
  27. //kbuild:lib-$(CONFIG_RESIZE) += resize.o
  28. //usage:#define resize_trivial_usage
  29. //usage: ""
  30. //usage:#define resize_full_usage "\n\n"
  31. //usage: "Resize the screen"
  32. #include "libbb.h"
  33. #include "common_bufsiz.h"
  34. #define ESC "\033"
  35. #define old_termios_p ((struct termios*)bb_common_bufsiz1)
  36. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  37. static void
  38. onintr(int sig UNUSED_PARAM)
  39. {
  40. tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
  41. _exit(EXIT_FAILURE);
  42. }
  43. int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  44. int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  45. {
  46. struct termios new;
  47. struct winsize w = { 0, 0, 0, 0 };
  48. int ret;
  49. INIT_G();
  50. /* We use _stderr_ in order to make resize usable
  51. * in shell backticks (those redirect stdout away from tty).
  52. * NB: other versions of resize open "/dev/tty"
  53. * and operate on it - should we do the same?
  54. */
  55. tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
  56. //TODO: die if the above fails?
  57. memcpy(&new, old_termios_p, sizeof(new));
  58. new.c_cflag |= (CLOCAL | CREAD);
  59. new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  60. bb_signals(0
  61. + (1 << SIGINT)
  62. + (1 << SIGQUIT)
  63. + (1 << SIGTERM)
  64. + (1 << SIGALRM)
  65. , onintr);
  66. tcsetattr(STDERR_FILENO, TCSANOW, &new);
  67. /* save_cursor_pos 7
  68. * scroll_whole_screen [r
  69. * put_cursor_waaaay_off [$x;$yH
  70. * get_cursor_pos [6n
  71. * restore_cursor_pos 8
  72. */
  73. fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
  74. alarm(3); /* Just in case terminal won't answer */
  75. //BUG: death by signal won't restore termios
  76. scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
  77. fprintf(stderr, ESC"8");
  78. /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
  79. * by calculating character cell HxW from old values
  80. * (gotten via TIOCGWINSZ) and recomputing *pixel values */
  81. ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
  82. tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
  83. if (ENABLE_FEATURE_RESIZE_PRINT)
  84. printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
  85. w.ws_col, w.ws_row);
  86. return ret;
  87. }