Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / console-tools / resize.c
blob4b0d63a03c8086a802e8d889aeeeb38b549f601f
1 /* vi: set sw=4 ts=4: */
2 /*
3 * resize - set terminal width and height.
5 * Copyright 2006 Bernhard Reutner-Fischer
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9 /* no options, no getopt */
11 //usage:#define resize_trivial_usage
12 //usage: ""
13 //usage:#define resize_full_usage "\n\n"
14 //usage: "Resize the screen"
16 #include "libbb.h"
18 #define ESC "\033"
20 #define old_termios_p ((struct termios*)&bb_common_bufsiz1)
22 static void
23 onintr(int sig UNUSED_PARAM)
25 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
26 _exit(EXIT_FAILURE);
29 int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
32 struct termios new;
33 struct winsize w = { 0, 0, 0, 0 };
34 int ret;
36 /* We use _stderr_ in order to make resize usable
37 * in shell backticks (those redirect stdout away from tty).
38 * NB: other versions of resize open "/dev/tty"
39 * and operate on it - should we do the same?
42 tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
43 memcpy(&new, old_termios_p, sizeof(new));
44 new.c_cflag |= (CLOCAL | CREAD);
45 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
46 bb_signals(0
47 + (1 << SIGINT)
48 + (1 << SIGQUIT)
49 + (1 << SIGTERM)
50 + (1 << SIGALRM)
51 , onintr);
52 tcsetattr(STDERR_FILENO, TCSANOW, &new);
54 /* save_cursor_pos 7
55 * scroll_whole_screen [r
56 * put_cursor_waaaay_off [$x;$yH
57 * get_cursor_pos [6n
58 * restore_cursor_pos 8
60 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
61 alarm(3); /* Just in case terminal won't answer */
62 //BUG: death by signal won't restore termios
63 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
64 fprintf(stderr, ESC"8");
66 /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
67 * by calculating character cell HxW from old values
68 * (gotten via TIOCGWINSZ) and recomputing *pixel values */
69 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
71 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
73 if (ENABLE_FEATURE_RESIZE_PRINT)
74 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
75 w.ws_col, w.ws_row);
77 return ret;