blob: 64f318c76ca38d90ebcd608c5e74b5d87212ed2c [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 *
5 * Copyright 2006 Bernhard Fischer
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9/* no options, no getopt */
10#include "busybox.h"
11
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000012#define ESC "\033"
13
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000014struct termios old;
15
16static void
17onintr(int sig ATTRIBUTE_UNUSED)
18{
19 tcsetattr(STDERR_FILENO, TCSANOW, &old);
20 exit(1);
21}
22
23
Denis Vlasenko06af2162007-02-03 17:28:39 +000024int resize_main(int argc, char **argv);
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000025int resize_main(int argc, char **argv)
26{
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000027 struct termios new;
28 struct winsize w = { 0,0,0,0 };
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000029 int ret;
30
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000031 /* 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 */
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000038 new = old;
39 new.c_cflag |= (CLOCAL | CREAD);
40 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000041 signal(SIGINT, onintr);
42 signal(SIGQUIT, onintr);
43 signal(SIGTERM, onintr);
44 signal(SIGALRM, onintr);
45 tcsetattr(STDERR_FILENO, TCSANOW, &new);
46
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000047 /* save_cursor_pos 7
48 * scroll_whole_screen [r
49 * put_cursor_waaaay_off [$x;$yH
50 * get_cursor_pos [6n
51 * restore_cursor_pos 8
52 */
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000053 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
54 alarm(3); /* Just in case terminal won't answer */
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000055 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000056 fprintf(stderr, ESC"8");
57
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
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000065 if (ENABLE_FEATURE_RESIZE_PRINT)
66 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
67 w.ws_col, w.ws_row);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000068
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000069 return ret;
70}