81ebb378c0c6bde52766d0136c6e8f16ad69a9bb
[platform/upstream/bash.git] / lib / sh / winsize.c
1 /* winsize.c - handle window size changes and information. */
2
3 /* Copyright (C) 2005 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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.
11
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.
16
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 */
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
33 #if !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
34 /* For struct winsize on SCO */
35 /*   sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
36 #  if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
37 #    if defined (HAVE_SYS_STREAM_H)
38 #      include <sys/stream.h>
39 #    endif
40 #    include <sys/ptem.h>
41 #  endif /* HAVE_SYS_PTEM_H && TIOCGWINSZ && SIGWINCH */
42 #endif /* !STRUCT_WINSIZE_IN_SYS_IOCTL */
43
44 #include <stdio.h>
45
46 /* Return the fd from which we are actually getting input. */
47 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
48
49 #if !defined (errno)
50 extern int errno;
51 #endif /* !errno */
52
53 extern int shell_tty;
54
55 #if defined (READLINE)
56 extern void rl_set_screen_size __P((int, int));
57 #endif
58 extern void sh_set_lines_and_columns __P((int, int));
59
60 void
61 get_new_window_size (from_sig, rp, cp)
62      int from_sig;
63      int *rp, *cp;
64 {
65 #if defined (TIOCGWINSZ)
66   struct winsize win;
67   int tty;
68
69   tty = input_tty ();
70   if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
71       win.ws_row > 0 && win.ws_col > 0)
72     {
73       sh_set_lines_and_columns (win.ws_row, win.ws_col);
74 #if defined (READLINE)
75       rl_set_screen_size (win.ws_row, win.ws_col);
76       if (rp)
77         *rp = win.ws_row;
78       if (cp)
79         *cp = win.ws_col;
80 #endif
81     }
82 #endif
83 }