NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / sys / share / ioctl.c
blobd0d764960a1c07632f6060059811df4d4bda58cb
1 /* aNetHack 0.0.1 ioctl.c $ANH-Date: 1432512788 2015/05/25 00:13:08 $ $ANH-Branch: master $:$ANH-Revision: 1.12 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* aNetHack may be freely redistributed. See license for details. */
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. */
9 #include "hack.h"
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
40 #ifdef POSIX_TYPES
41 #include <termios.h>
42 struct termios termio;
43 #if defined(BSD) || defined(_AIX32) || defined(__linux__)
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 #if defined(AMIX) || defined(__APPLE__)
58 #include <sys/ioctl.h>
59 #endif /* AMIX */
60 #endif
62 #ifdef SUSPEND /* BSD isn't alone anymore... */
63 #include <signal.h>
64 #endif
66 #if defined(TIOCGWINSZ) \
67 && (defined(BSD) || defined(ULTRIX) || defined(AIX_31) \
68 || defined(_BULL_SOURCE) || defined(SVR4))
69 #define USE_WIN_IOCTL
70 #include "tcap.h" /* for LI and CO */
71 #endif
73 #ifdef _M_UNIX
74 extern void NDECL(sco_mapon);
75 extern void NDECL(sco_mapoff);
76 #endif
77 #ifdef __linux__
78 extern void NDECL(linux_mapon);
79 extern void NDECL(linux_mapoff);
80 #endif
82 #ifdef AUX
83 void
84 catch_stp()
86 signal(SIGTSTP, SIG_DFL);
87 dosuspend();
89 #endif /* AUX */
91 void
92 getwindowsz()
94 #ifdef USE_WIN_IOCTL
96 * ttysize is found on Suns and BSD
97 * winsize is found on Suns, BSD, and Ultrix
99 struct winsize ttsz;
101 if (ioctl(fileno(stdin), (int) TIOCGWINSZ, (char *) &ttsz) != -1) {
103 * Use the kernel's values for lines and columns if it has
104 * any idea.
106 if (ttsz.ws_row)
107 LI = ttsz.ws_row;
108 if (ttsz.ws_col)
109 CO = ttsz.ws_col;
111 #endif
114 void
115 getioctls()
117 #ifdef BSD_JOB_CONTROL
118 (void) ioctl(fileno(stdin), (int) TIOCGLTC, (char *) &ltchars);
119 (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars0);
120 #else
121 #ifdef POSIX_TYPES
122 (void) tcgetattr(fileno(stdin), &termio);
123 #else
124 #if defined(TCSETS) && !defined(AIX_31)
125 (void) ioctl(fileno(stdin), (int) TCGETS, &termio);
126 #else
127 (void) ioctl(fileno(stdin), (int) TCGETA, &termio);
128 #endif
129 #endif
130 #endif
131 getwindowsz();
132 #ifdef AUX
133 (void) signal(SIGTSTP, catch_stp);
134 #endif
137 void
138 setioctls()
140 #ifdef BSD_JOB_CONTROL
141 (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars);
142 #else
143 #ifdef POSIX_TYPES
144 (void) tcsetattr(fileno(stdin), TCSADRAIN, &termio);
145 #else
146 #if defined(TCSETS) && !defined(AIX_31)
147 (void) ioctl(fileno(stdin), (int) TCSETSW, &termio);
148 #else
149 (void) ioctl(fileno(stdin), (int) TCSETAW, &termio);
150 #endif
151 #endif
152 #endif
155 #ifdef SUSPEND /* No longer implies BSD */
157 dosuspend()
159 #ifdef SYSCF
160 /* NB: check_user_string() is port-specific. */
161 if (!sysopt.shellers || !sysopt.shellers[0]
162 || !check_user_string(sysopt.shellers)) {
163 Norep("Suspend command not available.");
164 return 0;
166 #endif
167 #if defined(SIGTSTP) && !defined(NO_SIGNAL)
168 if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
169 suspend_nhwindows((char *) 0);
170 #ifdef _M_UNIX
171 sco_mapon();
172 #endif
173 #ifdef __linux__
174 linux_mapon();
175 #endif
176 (void) signal(SIGTSTP, SIG_DFL);
177 #ifdef AUX
178 (void) kill(0, SIGSTOP);
179 #else
180 (void) kill(0, SIGTSTP);
181 #endif
182 #ifdef _M_UNIX
183 sco_mapoff();
184 #endif
185 #ifdef __linux__
186 linux_mapoff();
187 #endif
188 resume_nhwindows();
189 } else {
190 pline("I don't think your shell has job control.");
192 #else
193 pline("Sorry, it seems we have no SIGTSTP here. Try ! or S.");
194 #endif
195 return (0);
197 #endif /* SUSPEND */