OSDN Git Service

Initial Import
[nethackexpress/trunk.git] / sys / share / ioctl.c
1 /*      SCCS Id: @(#)ioctl.c    3.4     1990/22/02 */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 /* This cannot be part of hack.tty.c (as it was earlier) since on some
6    systems (e.g. MUNIX) the include files <termio.h> and <sgtty.h>
7    define the same constants, and the C preprocessor complains. */
8
9 #include "hack.h"
10
11 #if defined(BSD_JOB_CONTROL) || defined(_BULL_SOURCE)
12 # ifdef HPUX
13 #include <bsdtty.h>
14 # else
15 #  if defined(AIX_31) && !defined(_ALL_SOURCE)
16 #   define _ALL_SOURCE  /* causes struct winsize to be present */
17 #   ifdef _AIX32
18 #    include <sys/ioctl.h>
19 #   endif
20 #  endif
21 #  if defined(_BULL_SOURCE)
22 #   include <termios.h>
23 struct termios termio;
24 #   undef TIMEOUT               /* defined in you.h and sys/tty.h */
25 #   include <sys/tty.h>         /* define winsize */
26 #   include <sys/ttold.h>       /* define struct ltchars */
27 #   include <sys/bsdioctl.h>    /* define TIOGWINSZ */
28 #  else
29 #   ifdef LINUX
30 #    include <bsd/sgtty.h>
31 #   else
32 #    include <sgtty.h>
33 #   endif
34 #  endif
35 # endif
36 struct ltchars ltchars;
37 struct ltchars ltchars0 = { -1, -1, -1, -1, -1, -1 }; /* turn all off */
38 #else
39
40 # ifdef POSIX_TYPES
41 #include <termios.h>
42 struct termios termio;
43 #  if defined(BSD) || defined(_AIX32)
44 #   if defined(_AIX32) && !defined(_ALL_SOURCE)
45 #    define _ALL_SOURCE
46 #   endif
47 #include <sys/ioctl.h>
48 #  endif
49 # else
50 #include <termio.h>     /* also includes part of <sgtty.h> */
51 #  if defined(TCSETS) && !defined(AIX_31)
52 struct termios termio;
53 #  else
54 struct termio termio;
55 #  endif
56 # endif
57 # ifdef AMIX
58 #include <sys/ioctl.h>
59 # endif /* AMIX */
60 #endif
61
62 #ifdef SUSPEND  /* BSD isn't alone anymore... */
63 #include        <signal.h>
64 #endif
65
66 #if defined(TIOCGWINSZ) && (defined(BSD) || defined(ULTRIX) || defined(AIX_31) || defined(_BULL_SOURCE) || defined(SVR4))
67 #define USE_WIN_IOCTL
68 #include "tcap.h"       /* for LI and CO */
69 #endif
70
71 #ifdef _M_UNIX
72 extern void NDECL(sco_mapon);
73 extern void NDECL(sco_mapoff);
74 #endif
75 #ifdef __linux__
76 extern void NDECL(linux_mapon);
77 extern void NDECL(linux_mapoff);
78 #endif
79
80 #ifdef AUX
81 void
82 catch_stp()
83 {
84     signal(SIGTSTP, SIG_DFL);
85     dosuspend();
86 }
87 #endif /* AUX */
88
89 void
90 getwindowsz()
91 {
92 #ifdef USE_WIN_IOCTL
93     /*
94      * ttysize is found on Suns and BSD
95      * winsize is found on Suns, BSD, and Ultrix
96      */
97     struct winsize ttsz;
98
99     if (ioctl(fileno(stdin), (int)TIOCGWINSZ, (char *)&ttsz) != -1) {
100         /*
101          * Use the kernel's values for lines and columns if it has
102          * any idea.
103          */
104         if (ttsz.ws_row)
105             LI = ttsz.ws_row;
106         if (ttsz.ws_col)
107             CO = ttsz.ws_col;
108     }
109 #endif
110 }
111
112 void
113 getioctls()
114 {
115 #ifdef BSD_JOB_CONTROL
116         (void) ioctl(fileno(stdin), (int) TIOCGLTC, (char *) &ltchars);
117         (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars0);
118 #else
119 # ifdef POSIX_TYPES
120         (void) tcgetattr(fileno(stdin), &termio);
121 # else
122 #  if defined(TCSETS) && !defined(AIX_31)
123         (void) ioctl(fileno(stdin), (int) TCGETS, &termio);
124 #  else
125         (void) ioctl(fileno(stdin), (int) TCGETA, &termio);
126 #  endif
127 # endif
128 #endif
129         getwindowsz();
130 #ifdef AUX
131         ( void ) signal ( SIGTSTP , catch_stp ) ;
132 #endif
133 }
134
135 void
136 setioctls()
137 {
138 #ifdef BSD_JOB_CONTROL
139         (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars);
140 #else
141 # ifdef POSIX_TYPES
142         (void) tcsetattr(fileno(stdin), TCSADRAIN, &termio);
143 # else
144 #  if defined(TCSETS) && !defined(AIX_31)
145         (void) ioctl(fileno(stdin), (int) TCSETSW, &termio);
146 #  else
147         (void) ioctl(fileno(stdin), (int) TCSETAW, &termio);
148 #  endif
149 # endif
150 #endif
151 }
152
153 #ifdef SUSPEND          /* No longer implies BSD */
154 int
155 dosuspend()
156 {
157 # ifdef SIGTSTP
158         if(signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
159                 suspend_nhwindows((char *)0);
160 #  ifdef _M_UNIX
161                 sco_mapon();
162 #  endif
163 #  ifdef __linux__
164                 linux_mapon();
165 #  endif
166                 (void) signal(SIGTSTP, SIG_DFL);
167 #  ifdef AUX
168                 ( void ) kill ( 0 , SIGSTOP ) ;
169 #  else
170                 (void) kill(0, SIGTSTP);
171 #  endif
172 #  ifdef _M_UNIX
173                 sco_mapoff();
174 #  endif
175 #  ifdef __linux__
176                 linux_mapoff();
177 #  endif
178                 resume_nhwindows();
179         } else {
180                 pline("I don't think your shell has job control.");
181         }
182 # else
183         pline("Sorry, it seems we have no SIGTSTP here.  Try ! or S.");
184 # endif
185         return(0);
186 }
187 #endif /* SUSPEND */