Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * resize - set terminal width and height. |
| 4 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 5 | * Copyright 2006 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 8 | */ |
| 9 | /* no options, no getopt */ |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 10 | |
| 11 | //usage:#define resize_trivial_usage |
| 12 | //usage: "" |
| 13 | //usage:#define resize_full_usage "\n\n" |
| 14 | //usage: "Resize the screen" |
| 15 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 17 | #include "common_bufsiz.h" |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 19 | #define ESC "\033" |
| 20 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 21 | #define old_termios_p ((struct termios*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 22 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 23 | |
| 24 | static void |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 25 | onintr(int sig UNUSED_PARAM) |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 26 | { |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 27 | tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p); |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 28 | _exit(EXIT_FAILURE); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 31 | int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 32 | int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 33 | { |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 34 | struct termios new; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 35 | struct winsize w = { 0, 0, 0, 0 }; |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 36 | int ret; |
| 37 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 38 | INIT_G(); |
| 39 | |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 40 | /* 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 Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 46 | tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */ |
| 47 | memcpy(&new, old_termios_p, sizeof(new)); |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 48 | new.c_cflag |= (CLOCAL | CREAD); |
| 49 | new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 50 | bb_signals(0 |
| 51 | + (1 << SIGINT) |
| 52 | + (1 << SIGQUIT) |
| 53 | + (1 << SIGTERM) |
| 54 | + (1 << SIGALRM) |
| 55 | , onintr); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 56 | tcsetattr(STDERR_FILENO, TCSANOW, &new); |
| 57 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 58 | /* 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 Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 64 | fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n"); |
| 65 | alarm(3); /* Just in case terminal won't answer */ |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 66 | //BUG: death by signal won't restore termios |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 67 | scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 68 | 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 Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 75 | tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 76 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 77 | if (ENABLE_FEATURE_RESIZE_PRINT) |
| 78 | printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n", |
| 79 | w.ws_col, w.ws_row); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 80 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 81 | return ret; |
| 82 | } |