blob: a3342a1956bc43550450a98e850e8d79777afaee [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 */
9/* no options, no getopt */
Pere Orga55068c42011-03-27 23:42:28 +020010
11//usage:#define resize_trivial_usage
12//usage: ""
13//usage:#define resize_full_usage "\n\n"
14//usage: "Resize the screen"
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020017#include "common_bufsiz.h"
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000018
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000019#define ESC "\033"
20
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020021#define old_termios_p ((struct termios*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020022#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000023
24static void
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000025onintr(int sig UNUSED_PARAM)
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000026{
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010027 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
Marek Polacek7b181072010-10-28 21:34:56 +020028 _exit(EXIT_FAILURE);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000029}
30
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000031int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000032int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000033{
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000034 struct termios new;
Denis Vlasenko68404f12008-03-17 09:00:54 +000035 struct winsize w = { 0, 0, 0, 0 };
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000036 int ret;
37
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020038 INIT_G();
39
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000040 /* We use _stderr_ in order to make resize usable
41 * in shell backticks (those redirect stdout away from tty).
42 * NB: other versions of resize open "/dev/tty"
43 * and operate on it - should we do the same?
44 */
45
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010046 tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
47 memcpy(&new, old_termios_p, sizeof(new));
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000048 new.c_cflag |= (CLOCAL | CREAD);
49 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Denis Vlasenko25591c32008-02-16 22:58:56 +000050 bb_signals(0
51 + (1 << SIGINT)
52 + (1 << SIGQUIT)
53 + (1 << SIGTERM)
54 + (1 << SIGALRM)
55 , onintr);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000056 tcsetattr(STDERR_FILENO, TCSANOW, &new);
57
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000058 /* save_cursor_pos 7
59 * scroll_whole_screen [r
60 * put_cursor_waaaay_off [$x;$yH
61 * get_cursor_pos [6n
62 * restore_cursor_pos 8
63 */
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000064 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
65 alarm(3); /* Just in case terminal won't answer */
Denys Vlasenkod8494932011-01-26 03:26:38 +010066//BUG: death by signal won't restore termios
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000067 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000068 fprintf(stderr, ESC"8");
69
70 /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel
71 * by calculating character cell HxW from old values
72 * (gotten via TIOCGWINSZ) and recomputing *pixel values */
73 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
74
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010075 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000076
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000077 if (ENABLE_FEATURE_RESIZE_PRINT)
78 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
79 w.ws_col, w.ws_row);
Denis Vlasenkoc9c893d2007-03-12 23:41:07 +000080
Bernhard Reutner-Fischere63a0de2006-10-25 15:07:56 +000081 return ret;
82}