aboutsummaryrefslogtreecommitdiff
path: root/console-tools
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-03-12 23:41:07 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-03-12 23:41:07 +0000
commitc9c893d4f59418c50c8eb42bd80390026e123dd8 (patch)
treecb0db6e84ffb11eaaf2ee4d34d15807af7350842 /console-tools
parent8a164052bfcf391368e267dae792a56b80174eba (diff)
downloadbusybox-w32-c9c893d4f59418c50c8eb42bd80390026e123dd8.tar.gz
busybox-w32-c9c893d4f59418c50c8eb42bd80390026e123dd8.tar.bz2
busybox-w32-c9c893d4f59418c50c8eb42bd80390026e123dd8.zip
resize: make it usable in in backticks; have a timeout (if display
doesn't respond to 'get cursor pos' ESC sequence...)
Diffstat (limited to 'console-tools')
-rw-r--r--console-tools/resize.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/console-tools/resize.c b/console-tools/resize.c
index 40a1b4ee0..64f318c76 100644
--- a/console-tools/resize.c
+++ b/console-tools/resize.c
@@ -11,31 +11,60 @@
11 11
12#define ESC "\033" 12#define ESC "\033"
13 13
14struct termios old;
15
16static void
17onintr(int sig ATTRIBUTE_UNUSED)
18{
19 tcsetattr(STDERR_FILENO, TCSANOW, &old);
20 exit(1);
21}
22
23
14int resize_main(int argc, char **argv); 24int resize_main(int argc, char **argv);
15int resize_main(int argc, char **argv) 25int resize_main(int argc, char **argv)
16{ 26{
17 struct termios old, new; 27 struct termios new;
18 struct winsize w = {0,0,0,0}; 28 struct winsize w = { 0,0,0,0 };
19 int ret; 29 int ret;
20 30
21 tcgetattr(STDOUT_FILENO, &old); /* fiddle echo */ 31 /* We use _stderr_ in order to make resize usable
32 * in shell backticks (those redirect stdout away from tty).
33 * NB: other versions of resize open "/dev/tty"
34 * and operate on it - should we do the same?
35 */
36
37 tcgetattr(STDERR_FILENO, &old); /* fiddle echo */
22 new = old; 38 new = old;
23 new.c_cflag |= (CLOCAL | CREAD); 39 new.c_cflag |= (CLOCAL | CREAD);
24 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 40 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
25 tcsetattr(STDOUT_FILENO, TCSANOW, &new); 41 signal(SIGINT, onintr);
42 signal(SIGQUIT, onintr);
43 signal(SIGTERM, onintr);
44 signal(SIGALRM, onintr);
45 tcsetattr(STDERR_FILENO, TCSANOW, &new);
46
26 /* save_cursor_pos 7 47 /* save_cursor_pos 7
27 * scroll_whole_screen [r 48 * scroll_whole_screen [r
28 * put_cursor_waaaay_off [$x;$yH 49 * put_cursor_waaaay_off [$x;$yH
29 * get_cursor_pos [6n 50 * get_cursor_pos [6n
30 * restore_cursor_pos 8 51 * restore_cursor_pos 8
31 */ 52 */
32 printf(ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n"); 53 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
54 alarm(3); /* Just in case terminal won't answer */
33 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col); 55 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
34 ret = ioctl(STDOUT_FILENO, TIOCSWINSZ, &w); 56 fprintf(stderr, ESC"8");
35 printf(ESC"8"); 57
36 tcsetattr(STDOUT_FILENO, TCSANOW, &old); 58 /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
59 * by calculating character cell HxW from old values
60 * (gotten via TIOCGWINSZ) and recomputing *pixel values */
61 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
62
63 tcsetattr(STDERR_FILENO, TCSANOW, &old);
64
37 if (ENABLE_FEATURE_RESIZE_PRINT) 65 if (ENABLE_FEATURE_RESIZE_PRINT)
38 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n", 66 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
39 w.ws_col, w.ws_row); 67 w.ws_col, w.ws_row);
68
40 return ret; 69 return ret;
41} 70}