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 | * |
| 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 */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 12 | #define ESC "\033" |
| 13 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 14 | #define old_termios (*(struct termios*)&bb_common_bufsiz1) |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 15 | |
| 16 | static void |
| 17 | onintr(int sig ATTRIBUTE_UNUSED) |
| 18 | { |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 19 | tcsetattr(STDERR_FILENO, TCSANOW, &old_termios); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 20 | exit(1); |
| 21 | } |
| 22 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 23 | int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 24 | int resize_main(int argc, char **argv) |
| 25 | { |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 26 | struct termios new; |
| 27 | struct winsize w = { 0,0,0,0 }; |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 28 | int ret; |
| 29 | |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 30 | /* 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 Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 36 | tcgetattr(STDERR_FILENO, &old_termios); /* fiddle echo */ |
| 37 | new = old_termios; |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 38 | new.c_cflag |= (CLOCAL | CREAD); |
| 39 | new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 40 | signal(SIGINT, onintr); |
| 41 | signal(SIGQUIT, onintr); |
| 42 | signal(SIGTERM, onintr); |
| 43 | signal(SIGALRM, onintr); |
| 44 | tcsetattr(STDERR_FILENO, TCSANOW, &new); |
| 45 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 46 | /* save_cursor_pos 7 |
| 47 | * scroll_whole_screen [r |
| 48 | * put_cursor_waaaay_off [$x;$yH |
| 49 | * get_cursor_pos [6n |
| 50 | * restore_cursor_pos 8 |
| 51 | */ |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 52 | fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n"); |
| 53 | alarm(3); /* Just in case terminal won't answer */ |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 54 | scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 55 | fprintf(stderr, ESC"8"); |
| 56 | |
| 57 | /* BTW, other versions of resize recalculate w.ws_xpixel, ws.ws_ypixel |
| 58 | * by calculating character cell HxW from old values |
| 59 | * (gotten via TIOCGWINSZ) and recomputing *pixel values */ |
| 60 | ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w); |
| 61 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 62 | tcsetattr(STDERR_FILENO, TCSANOW, &old_termios); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 63 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 64 | if (ENABLE_FEATURE_RESIZE_PRINT) |
| 65 | printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n", |
| 66 | w.ws_col, w.ws_row); |
Denis Vlasenko | c9c893d | 2007-03-12 23:41:07 +0000 | [diff] [blame] | 67 | |
Bernhard Reutner-Fischer | e63a0de | 2006-10-25 15:07:56 +0000 | [diff] [blame] | 68 | return ret; |
| 69 | } |