blob: 4504cc85d61a9261e33e1321b7adc90dac118e2b [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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9/* no options, no getopt */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000011
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000012#define ESC "\033"
13
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000014#define old_termios (*(struct termios*)&bb_common_bufsiz1)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000015
16static void
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000017onintr(int sig UNUSED_PARAM)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000018{
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000019 tcsetattr(STDERR_FILENO, TCSANOW, &old_termios);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000020 exit(EXIT_FAILURE);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000021}
22
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000023int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000024int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000025{
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000026 struct termios new;
Denis Vlasenko68404f12008-03-17 09:00:54 +000027 struct winsize w = { 0, 0, 0, 0 };
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000028 int ret;
29
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000030 /* We use _stderr_ in order to make resize usable
31 * in shell backticks (those redirect stdout away from tty).
32 * NB: other versions of resize open "/dev/tty"
33 * and operate on it - should we do the same?
34 */
35
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000036 tcgetattr(STDERR_FILENO, &old_termios); /* fiddle echo */
37 new = old_termios;
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000038 new.c_cflag |= (CLOCAL | CREAD);
39 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Denis Vlasenko25591c32008-02-16 22:58:56 +000040 bb_signals(0
41 + (1 << SIGINT)
42 + (1 << SIGQUIT)
43 + (1 << SIGTERM)
44 + (1 << SIGALRM)
45 , onintr);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000046 tcsetattr(STDERR_FILENO, TCSANOW, &new);
47
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000048 /* save_cursor_pos 7
49 * scroll_whole_screen [r
50 * put_cursor_waaaay_off [$x;$yH
51 * get_cursor_pos [6n
52 * restore_cursor_pos 8
53 */
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000054 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
55 alarm(3); /* Just in case terminal won't answer */
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000056 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000057 fprintf(stderr, ESC"8");
58
59 /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
60 * by calculating character cell HxW from old values
61 * (gotten via TIOCGWINSZ) and recomputing *pixel values */
62 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
63
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000064 tcsetattr(STDERR_FILENO, TCSANOW, &old_termios);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000065
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000066 if (ENABLE_FEATURE_RESIZE_PRINT)
67 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
68 w.ws_col, w.ws_row);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000069
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000070 return ret;
71}