Mercurial > hg > early-roguelike
comparison rogue3/mdport.c @ 0:527e2150eaf0
Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
author | edwarj4 |
---|---|
date | Tue, 13 Oct 2009 13:33:34 +0000 |
parents | |
children | 9a2c0c60c386 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:527e2150eaf0 |
---|---|
1 /* | |
2 mdport.c - Machine Dependent Code for Porting Unix/Curses games | |
3 | |
4 Copyright (C) 2005 Nicholas J. Kisseberth | |
5 All rights reserved. | |
6 | |
7 Redistribution and use in source and binary forms, with or without | |
8 modification, are permitted provided that the following conditions | |
9 are met: | |
10 1. Redistributions of source code must retain the above copyright | |
11 notice, this list of conditions and the following disclaimer. | |
12 2. Redistributions in binary form must reproduce the above copyright | |
13 notice, this list of conditions and the following disclaimer in the | |
14 documentation and/or other materials provided with the distribution. | |
15 3. Neither the name(s) of the author(s) nor the names of other contributors | |
16 may be used to endorse or promote products derived from this software | |
17 without specific prior written permission. | |
18 | |
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND | |
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE | |
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 SUCH DAMAGE. | |
30 */ | |
31 | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 | |
35 #if defined(_WIN32) | |
36 #include <Windows.h> | |
37 #include <io.h> | |
38 #include <sys/locking.h> | |
39 #include <Lmcons.h> | |
40 #include <conio.h> | |
41 #pragma warning( disable: 4201 ) | |
42 #include <shlobj.h> | |
43 #pragma warning( default: 4201 ) | |
44 #include <Shlwapi.h> | |
45 #undef MOUSE_MOVED | |
46 #endif | |
47 | |
48 #include <curses.h> | |
49 | |
50 #include "mdport.h" | |
51 | |
52 #if defined(HAVE_SYS_TYPES) | |
53 #include <sys/types.h> | |
54 #endif | |
55 | |
56 #if defined(HAVE_PROCESS_H) | |
57 #include <process.h> | |
58 #endif | |
59 | |
60 #if defined(HAVE_PWD_H) | |
61 #include <pwd.h> | |
62 #endif | |
63 | |
64 #if defined(HAVE_SYS_UTSNAME) | |
65 #include <sys/utsname.h> | |
66 #endif | |
67 | |
68 #if defined(HAVE_ARPA_INET_H) | |
69 #include <arpa/inet.h> /* Solaris 2.8 required this for htonl() and ntohl() */ | |
70 #endif | |
71 | |
72 #if defined(HAVE_TERMIOS_H) | |
73 #include <termios.h> | |
74 #endif | |
75 | |
76 #if defined(HAVE_UNISTD_H) | |
77 #ifndef __USE_GNU | |
78 #define __USE_GNU | |
79 #include <unistd.h> | |
80 #undef __USE_GNU | |
81 #else | |
82 #include <unistd.h> | |
83 #endif | |
84 #endif | |
85 | |
86 #if defined(HAVE_TERM_H) | |
87 #include <term.h> | |
88 #elif defined(HAVE_NCURSES_TERM_H) | |
89 #include <ncurses/term.h> | |
90 #endif | |
91 | |
92 #if defined(HAVE_WORKING_FORK) | |
93 #include <sys/wait.h> | |
94 #endif | |
95 | |
96 #ifdef HAVE_UTMPX_H | |
97 #include <utmpx.h> | |
98 #endif | |
99 | |
100 #ifdef HAVE_ERRNO_H | |
101 #include <errno.h> | |
102 #endif | |
103 | |
104 #include <ctype.h> | |
105 #include <fcntl.h> | |
106 #include <limits.h> | |
107 #include <sys/stat.h> | |
108 #include <signal.h> | |
109 | |
110 #define NOOP(x) (x += 0) | |
111 | |
112 static int pass_ctrl_keypad = 1; | |
113 | |
114 void | |
115 md_init(int options) | |
116 { | |
117 #if defined(__INTERIX) | |
118 char *term; | |
119 | |
120 term = getenv("TERM"); | |
121 | |
122 if (term == NULL) | |
123 setenv("TERM","interix"); | |
124 #elif defined(__DJGPP__) | |
125 _fmode = _O_BINARY; | |
126 #elif defined(_WIN32) | |
127 _fmode = _O_BINARY; | |
128 #endif | |
129 | |
130 #if defined(HAVE_ESCDELAY) || defined(NCURSES_VERSION) | |
131 ESCDELAY=64; | |
132 #endif | |
133 | |
134 #if defined(DUMP) | |
135 md_onsignal_default(); | |
136 #else | |
137 md_onsignal_exit(); | |
138 #endif | |
139 | |
140 if (options & MD_STRIP_CTRL_KEYPAD) | |
141 pass_ctrl_keypad = 0; | |
142 else | |
143 pass_ctrl_keypad = 1; | |
144 } | |
145 | |
146 void | |
147 md_onsignal_default(void) | |
148 { | |
149 #ifdef SIGHUP | |
150 signal(SIGHUP, SIG_DFL); | |
151 #endif | |
152 #ifdef SIGQUIT | |
153 signal(SIGQUIT, SIG_DFL); | |
154 #endif | |
155 #ifdef SIGILL | |
156 signal(SIGILL, SIG_DFL); | |
157 #endif | |
158 #ifdef SIGTRAP | |
159 signal(SIGTRAP, SIG_DFL); | |
160 #endif | |
161 #ifdef SIGIOT | |
162 signal(SIGIOT, SIG_DFL); | |
163 #endif | |
164 #ifdef SIGEMT | |
165 signal(SIGEMT, SIG_DFL); | |
166 #endif | |
167 #ifdef SIGFPE | |
168 signal(SIGFPE, SIG_DFL); | |
169 #endif | |
170 #ifdef SIGBUS | |
171 signal(SIGBUS, SIG_DFL); | |
172 #endif | |
173 #ifdef SIGSEGV | |
174 signal(SIGSEGV, SIG_DFL); | |
175 #endif | |
176 #ifdef SIGSYS | |
177 signal(SIGSYS, SIG_DFL); | |
178 #endif | |
179 #ifdef SIGTERM | |
180 signal(SIGTERM, SIG_DFL); | |
181 #endif | |
182 } | |
183 | |
184 void | |
185 md_onsignal_exit(void) | |
186 { | |
187 #ifdef SIGHUP | |
188 signal(SIGHUP, SIG_DFL); | |
189 #endif | |
190 #ifdef SIGQUIT | |
191 signal(SIGQUIT, exit); | |
192 #endif | |
193 #ifdef SIGILL | |
194 signal(SIGILL, exit); | |
195 #endif | |
196 #ifdef SIGTRAP | |
197 signal(SIGTRAP, exit); | |
198 #endif | |
199 #ifdef SIGIOT | |
200 signal(SIGIOT, exit); | |
201 #endif | |
202 #ifdef SIGEMT | |
203 signal(SIGEMT, exit); | |
204 #endif | |
205 #ifdef SIGFPE | |
206 signal(SIGFPE, exit); | |
207 #endif | |
208 #ifdef SIGBUS | |
209 signal(SIGBUS, exit); | |
210 #endif | |
211 #ifdef SIGSEGV | |
212 signal(SIGSEGV, exit); | |
213 #endif | |
214 #ifdef SIGSYS | |
215 signal(SIGSYS, exit); | |
216 #endif | |
217 #ifdef SIGTERM | |
218 signal(SIGTERM, exit); | |
219 #endif | |
220 } | |
221 | |
222 extern void auto_save(int sig); | |
223 extern void endit(int sig); | |
224 extern void quit(int sig); | |
225 | |
226 void | |
227 md_onsignal_autosave(void) | |
228 { | |
229 | |
230 #ifdef SIGHUP | |
231 signal(SIGHUP, auto_save); | |
232 #endif | |
233 #ifdef SIGQUIT | |
234 signal(SIGQUIT, endit); | |
235 #endif | |
236 #ifdef SIGILL | |
237 signal(SIGILL, auto_save); | |
238 #endif | |
239 #ifdef SIGTRAP | |
240 signal(SIGTRAP, auto_save); | |
241 #endif | |
242 #ifdef SIGIOT | |
243 signal(SIGIOT, auto_save); | |
244 #endif | |
245 #ifdef SIGEMT | |
246 signal(SIGEMT, auto_save); | |
247 #endif | |
248 #ifdef SIGFPE | |
249 signal(SIGFPE, auto_save); | |
250 #endif | |
251 #ifdef SIGBUS | |
252 signal(SIGBUS, auto_save); | |
253 #endif | |
254 #ifdef SIGSEGV | |
255 signal(SIGSEGV, auto_save); | |
256 #endif | |
257 #ifdef SIGSYS | |
258 signal(SIGSYS, auto_save); | |
259 #endif | |
260 #ifdef SIGTERM | |
261 signal(SIGTERM, auto_save); | |
262 #endif | |
263 #ifdef SIGINT | |
264 signal(SIGINT, quit); | |
265 #endif | |
266 } | |
267 | |
268 int | |
269 md_hasclreol(void) | |
270 { | |
271 #if defined(clr_eol) | |
272 #ifdef NCURSES_VERSION | |
273 if (cur_term == NULL) | |
274 return(0); | |
275 if (cur_term->type.Strings == NULL) | |
276 return(0); | |
277 #endif | |
278 return((clr_eol != NULL) && (*clr_eol != 0)); | |
279 #elif defined(__PDCURSES__) | |
280 return(TRUE); | |
281 #else | |
282 return((CE != NULL) && (*CE != 0)); | |
283 #endif | |
284 } | |
285 | |
286 void | |
287 md_putchar(int c) | |
288 { | |
289 putchar(c); | |
290 } | |
291 | |
292 #ifdef _WIN32 | |
293 static int md_standout_mode = 0; | |
294 #endif | |
295 | |
296 void | |
297 md_raw_standout(void) | |
298 { | |
299 #ifdef _WIN32 | |
300 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
301 HANDLE hStdout; | |
302 WORD fgattr,bgattr; | |
303 | |
304 if (md_standout_mode == 0) | |
305 { | |
306 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
307 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
308 fgattr = (csbiInfo.wAttributes & 0xF); | |
309 bgattr = (csbiInfo.wAttributes & 0xF0); | |
310 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
311 md_standout_mode = 1; | |
312 } | |
313 #elif defined(SO) | |
314 tputs(SO,0,md_putchar); | |
315 fflush(stdout); | |
316 #endif | |
317 } | |
318 | |
319 void | |
320 md_raw_standend(void) | |
321 { | |
322 #ifdef _WIN32 | |
323 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
324 HANDLE hStdout; | |
325 WORD fgattr,bgattr; | |
326 | |
327 if (md_standout_mode == 1) | |
328 { | |
329 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
330 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
331 fgattr = (csbiInfo.wAttributes & 0xF); | |
332 bgattr = (csbiInfo.wAttributes & 0xF0); | |
333 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
334 md_standout_mode = 0; | |
335 } | |
336 #elif defined(SE) | |
337 tputs(SE,0,md_putchar); | |
338 fflush(stdout); | |
339 #endif | |
340 } | |
341 | |
342 int | |
343 md_unlink_open_file(const char *file, FILE *inf) | |
344 { | |
345 #ifdef _WIN32 | |
346 fclose(inf); | |
347 (void) _chmod(file, 0600); | |
348 return( _unlink(file) ); | |
349 #else | |
350 return(unlink(file)); | |
351 #endif | |
352 } | |
353 | |
354 int | |
355 md_unlink(char *file) | |
356 { | |
357 #ifdef _WIN32 | |
358 (void) _chmod(file, 0600); | |
359 return( _unlink(file) ); | |
360 #else | |
361 return(unlink(file)); | |
362 #endif | |
363 } | |
364 | |
365 int | |
366 md_chmod(const char *filename, int mode) | |
367 { | |
368 #ifdef _WIN32 | |
369 return( _chmod(filename, mode) ); | |
370 #else | |
371 return( chmod(filename, mode) ); | |
372 #endif | |
373 } | |
374 | |
375 void | |
376 md_normaluser(void) | |
377 { | |
378 #if defined(HAVE_GETGID) && defined(HAVE_GETUID) | |
379 gid_t realgid = getgid(); | |
380 uid_t realuid = getuid(); | |
381 | |
382 #if defined(HAVE_SETRESGID) | |
383 if (setresgid(-1, realgid, realgid) != 0) { | |
384 #elif defined (HAVE_SETREGID) | |
385 if (setregid(realgid, realgid) != 0) { | |
386 #elif defined (HAVE_SETGID) | |
387 if (setgid(realgid) != 0) { | |
388 #else | |