]>
Commit | Line | Data |
---|---|---|
3185942a | 1 | /* winsize.c - handle window size changes and information. */ |
95732b49 | 2 | |
a0c0a00f | 3 | /* Copyright (C) 2005-2015 Free Software Foundation, Inc. |
95732b49 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
7 | Bash is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
95732b49 | 11 | |
3185942a JA |
12 | Bash is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
95732b49 | 16 | |
3185942a JA |
17 | You should have received a copy of the GNU General Public License |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
95732b49 JA |
20 | |
21 | #include "config.h" | |
22 | ||
23 | #include <stdc.h> | |
24 | ||
25 | #include "bashtypes.h" | |
26 | ||
27 | #if defined (HAVE_UNISTD_H) | |
28 | # include <unistd.h> | |
29 | #endif | |
30 | ||
31 | #include <sys/ioctl.h> | |
32 | ||
89a92869 CR |
33 | /* Try to find the definitions of `struct winsize' and TIOGCWINSZ */ |
34 | ||
a0c0a00f | 35 | #if 0 |
89a92869 CR |
36 | #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ) |
37 | # include <sys/ioctl.h> | |
38 | #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */ | |
a0c0a00f | 39 | #endif |
89a92869 CR |
40 | |
41 | #if defined (STRUCT_WINSIZE_IN_TERMIOS) && !defined (STRUCT_WINSIZE_IN_SYS_IOCTL) | |
42 | # include <termios.h> | |
43 | #endif /* STRUCT_WINSIZE_IN_TERMIOS && !STRUCT_WINSIZE_IN_SYS_IOCTL */ | |
44 | ||
45 | /* Not in either of the standard places, look around. */ | |
46 | #if !defined (STRUCT_WINSIZE_IN_TERMIOS) && !defined (STRUCT_WINSIZE_IN_SYS_IOCTL) | |
47 | # if defined (HAVE_SYS_STREAM_H) | |
48 | # include <sys/stream.h> | |
49 | # endif /* HAVE_SYS_STREAM_H */ | |
50 | # if defined (HAVE_SYS_PTEM_H) /* SVR4.2, at least, has it here */ | |
95732b49 | 51 | # include <sys/ptem.h> |
89a92869 CR |
52 | # define _IO_PTEM_H /* work around SVR4.2 1.1.4 bug */ |
53 | # endif /* HAVE_SYS_PTEM_H */ | |
54 | # if defined (HAVE_SYS_PTE_H) /* ??? */ | |
55 | # include <sys/pte.h> | |
56 | # endif /* HAVE_SYS_PTE_H */ | |
57 | #endif /* !STRUCT_WINSIZE_IN_TERMIOS && !STRUCT_WINSIZE_IN_SYS_IOCTL */ | |
95732b49 JA |
58 | |
59 | #include <stdio.h> | |
60 | ||
61 | /* Return the fd from which we are actually getting input. */ | |
62 | #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr) | |
63 | ||
64 | #if !defined (errno) | |
65 | extern int errno; | |
66 | #endif /* !errno */ | |
67 | ||
68 | extern int shell_tty; | |
69 | ||
70 | #if defined (READLINE) | |
71 | extern void rl_set_screen_size __P((int, int)); | |
72 | #endif | |
0628567a | 73 | extern void sh_set_lines_and_columns __P((int, int)); |
95732b49 JA |
74 | |
75 | void | |
76 | get_new_window_size (from_sig, rp, cp) | |
77 | int from_sig; | |
78 | int *rp, *cp; | |
79 | { | |
80 | #if defined (TIOCGWINSZ) | |
81 | struct winsize win; | |
82 | int tty; | |
83 | ||
84 | tty = input_tty (); | |
85 | if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) && | |
86 | win.ws_row > 0 && win.ws_col > 0) | |
87 | { | |
88 | sh_set_lines_and_columns (win.ws_row, win.ws_col); | |
89 | #if defined (READLINE) | |
90 | rl_set_screen_size (win.ws_row, win.ws_col); | |
91 | if (rp) | |
92 | *rp = win.ws_row; | |
93 | if (cp) | |
94 | *cp = win.ws_col; | |
95 | #endif | |
96 | } | |
97 | #endif | |
98 | } |