[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * resize - set terminal width and height.
|
---|
| 4 | *
|
---|
[2725] | 5 | * Copyright 2006 Bernhard Reutner-Fischer
|
---|
[1765] | 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[1765] | 8 | */
|
---|
| 9 | /* no options, no getopt */
|
---|
[3232] | 10 |
|
---|
| 11 | //usage:#define resize_trivial_usage
|
---|
| 12 | //usage: ""
|
---|
| 13 | //usage:#define resize_full_usage "\n\n"
|
---|
| 14 | //usage: "Resize the screen"
|
---|
| 15 |
|
---|
[1765] | 16 | #include "libbb.h"
|
---|
[3621] | 17 | #include "common_bufsiz.h"
|
---|
[1765] | 18 |
|
---|
| 19 | #define ESC "\033"
|
---|
| 20 |
|
---|
[3621] | 21 | #define old_termios_p ((struct termios*)bb_common_bufsiz1)
|
---|
| 22 | #define INIT_G() do { setup_common_bufsiz(); } while (0)
|
---|
[1765] | 23 |
|
---|
| 24 | static void
|
---|
[2725] | 25 | onintr(int sig UNUSED_PARAM)
|
---|
[1765] | 26 | {
|
---|
[2725] | 27 | tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
|
---|
| 28 | _exit(EXIT_FAILURE);
|
---|
[1765] | 29 | }
|
---|
| 30 |
|
---|
[2725] | 31 | int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 32 | int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
---|
[1765] | 33 | {
|
---|
| 34 | struct termios new;
|
---|
[2725] | 35 | struct winsize w = { 0, 0, 0, 0 };
|
---|
[1765] | 36 | int ret;
|
---|
| 37 |
|
---|
[3621] | 38 | INIT_G();
|
---|
| 39 |
|
---|
[1765] | 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 |
|
---|
[2725] | 46 | tcgetattr(STDERR_FILENO, old_termios_p); /* fiddle echo */
|
---|
| 47 | memcpy(&new, old_termios_p, sizeof(new));
|
---|
[1765] | 48 | new.c_cflag |= (CLOCAL | CREAD);
|
---|
| 49 | new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
---|
[2725] | 50 | bb_signals(0
|
---|
| 51 | + (1 << SIGINT)
|
---|
| 52 | + (1 << SIGQUIT)
|
---|
| 53 | + (1 << SIGTERM)
|
---|
| 54 | + (1 << SIGALRM)
|
---|
| 55 | , onintr);
|
---|
[1765] | 56 | tcsetattr(STDERR_FILENO, TCSANOW, &new);
|
---|
| 57 |
|
---|
| 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 | */
|
---|
| 64 | fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
|
---|
| 65 | alarm(3); /* Just in case terminal won't answer */
|
---|
[3232] | 66 | //BUG: death by signal won't restore termios
|
---|
[1765] | 67 | scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
|
---|
| 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 |
|
---|
[2725] | 75 | tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
|
---|
[1765] | 76 |
|
---|
| 77 | if (ENABLE_FEATURE_RESIZE_PRINT)
|
---|
| 78 | printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
|
---|
| 79 | w.ws_col, w.ws_row);
|
---|
| 80 |
|
---|
| 81 | return ret;
|
---|
| 82 | }
|
---|