blob: 97866673a21fbcd722d538fb88aa015e5f76478e [file] [log] [blame]
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +00001/* vi: set sw=4 ts=4: */
2/*
3 * resize - set terminal width and height.
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright 2006 Bernhard Reutner-Fischer
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +00008 */
Denys Vlasenko6d932992016-11-23 10:39:27 +01009//config:config RESIZE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "resize (756 bytes)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: This program is used to (re)set the width and height of your current
14//config: terminal.
Denys Vlasenko6d932992016-11-23 10:39:27 +010015//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//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;
Denys Vlasenko6d932992016-11-23 10:39:27 +010025
Denys Vlasenko39194f02017-08-03 19:00:01 +020026//applet:IF_RESIZE(APPLET_NOEXEC(resize, resize, BB_DIR_USR_BIN, BB_SUID_DROP, resize))
Denys Vlasenko6d932992016-11-23 10:39:27 +010027
28//kbuild:lib-$(CONFIG_RESIZE) += resize.o
Pere Orga55068c42011-03-27 23:42:28 +020029
30//usage:#define resize_trivial_usage
31//usage: ""
32//usage:#define resize_full_usage "\n\n"
33//usage: "Resize the screen"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020036#include "common_bufsiz.h"
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000037
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000038#define ESC "\033"
39
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020040#define old_termios_p ((struct termios*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020041#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000042
43static void
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044onintr(int sig UNUSED_PARAM)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000045{
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010046 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
Marek Polacek7b181072010-10-28 21:34:56 +020047 _exit(EXIT_FAILURE);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000048}
49
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000050int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000051int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000052{
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000053 struct termios new;
Denis Vlasenko68404f12008-03-17 09:00:54 +000054 struct winsize w = { 0, 0, 0, 0 };
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000055 int ret;
56
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020057 INIT_G();
58
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000059 /* We use _stderr_ in order to make resize usable
60 * in shell backticks (those redirect stdout away from tty).
61 * NB: other versions of resize open "/dev/tty"
62 * and operate on it - should we do the same?
63 */
64
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010065 tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
66 memcpy(&new, old_termios_p, sizeof(new));
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000067 new.c_cflag |= (CLOCAL | CREAD);
68 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Denis Vlasenko25591c32008-02-16 22:58:56 +000069 bb_signals(0
70 + (1 << SIGINT)
71 + (1 << SIGQUIT)
72 + (1 << SIGTERM)
73 + (1 << SIGALRM)
74 , onintr);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000075 tcsetattr(STDERR_FILENO, TCSANOW, &new);
76
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000077 /* save_cursor_pos 7
78 * scroll_whole_screen [r
79 * put_cursor_waaaay_off [$x;$yH
80 * get_cursor_pos [6n
81 * restore_cursor_pos 8
82 */
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000083 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
84 alarm(3); /* Just in case terminal won't answer */
Denys Vlasenkod8494932011-01-26 03:26:38 +010085//BUG: death by signal won't restore termios
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000086 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000087 fprintf(stderr, ESC"8");
88
89 /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
90 * by calculating character cell HxW from old values
91 * (gotten via TIOCGWINSZ) and recomputing *pixel values */
92 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
93
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010094 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000095
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000096 if (ENABLE_FEATURE_RESIZE_PRINT)
97 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
98 w.ws_col, w.ws_row);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000099
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +0000100 return ret;
101}