Mercurial > hg > early-roguelike
comparison arogue5/mdport.c @ 63:0ed67132cf10
Import Advanced Rogue 5.8 from the Roguelike Restoration Project (r1490)
author | elwin |
---|---|
date | Thu, 09 Aug 2012 22:58:48 +0000 |
parents | |
children | c49f7927b0fa |
comparison
equal
deleted
inserted
replaced
62:0ef99244acb8 | 63:0ed67132cf10 |
---|---|
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 #if defined(_WIN32) | |
33 #include <Windows.h> | |
34 #include <Lmcons.h> | |
35 #include <process.h> | |
36 #include <shlobj.h> | |
37 #include <sys/types.h> | |
38 #undef MOUSE_MOVED | |
39 #elif defined(__DJGPP__) | |
40 #include <process.h> | |
41 #else | |
42 #include <pwd.h> | |
43 #include <sys/utsname.h> | |
44 #include <unistd.h> | |
45 #endif | |
46 | |
47 #include <stdlib.h> | |
48 | |
49 #if defined(_WIN32) && !defined(__MINGW32__) | |
50 #define PATH_MAX MAX_PATH | |
51 #endif | |
52 | |
53 #include <curses.h> | |
54 | |
55 #if defined(__INTERIX) || defined(__MSYS__) | |
56 #include <term.h> | |
57 #else | |
58 #ifdef NCURSES_VERSION | |
59 #include <ncurses/term.h> | |
60 #endif | |
61 #endif | |
62 | |
63 #include <stdio.h> | |
64 #include <fcntl.h> | |
65 #include <limits.h> | |
66 #include <sys/stat.h> | |
67 #include <signal.h> | |
68 | |
69 #define MOD_MOVE(c) (toupper(c) ) | |
70 | |
71 void | |
72 md_init() | |
73 { | |
74 #ifdef __INTERIX | |
75 char *term; | |
76 | |
77 term = getenv("TERM"); | |
78 | |
79 if (term == NULL) | |
80 setenv("TERM","interix"); | |
81 #endif | |
82 #if defined(__DJGPP__) || defined(_WIN32) | |
83 _fmode = _O_BINARY; | |
84 #endif | |
85 #if defined(__CYGWIN__) || defined(__MSYS__) | |
86 ESCDELAY=250; | |
87 #endif | |
88 } | |
89 | |
90 int | |
91 md_hasclreol() | |
92 { | |
93 #ifndef attron | |
94 return(!CE); | |
95 #elif !defined(__PDCURSES__) | |
96 return(clr_eol != NULL); | |
97 #else | |
98 return(TRUE); | |
99 #endif | |
100 } | |
101 | |
102 #ifdef attron | |
103 # define _puts(s) tputs(s, 0, md_putchar); | |
104 # define SO enter_standout_mode | |
105 # define SE exit_standout_mode | |
106 #endif | |
107 | |
108 int | |
109 md_putchar(int c) | |
110 { | |
111 putchar(c); | |
112 } | |
113 | |
114 static int md_standout_mode = 0; | |
115 | |
116 int | |
117 md_raw_standout() | |
118 { | |
119 #ifdef _WIN32 | |
120 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
121 HANDLE hStdout; | |
122 int fgattr,bgattr; | |
123 | |
124 if (md_standout_mode == 0) | |
125 { | |
126 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
127 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
128 fgattr = (csbiInfo.wAttributes & 0xF); | |
129 bgattr = (csbiInfo.wAttributes & 0xF0); | |
130 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
131 md_standout_mode = 1; | |
132 } | |
133 #elif !defined(__PDCURSES__) | |
134 _puts(SO); | |
135 fflush(stdout); | |
136 #endif | |
137 } | |
138 | |
139 int | |
140 md_raw_standend() | |
141 { | |
142 #ifdef _WIN32 | |
143 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
144 HANDLE hStdout; | |
145 int fgattr,bgattr; | |
146 | |
147 if (md_standout_mode == 1) | |
148 { | |
149 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
150 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
151 fgattr = (csbiInfo.wAttributes & 0xF); | |
152 bgattr = (csbiInfo.wAttributes & 0xF0); | |
153 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
154 md_standout_mode = 0; | |
155 } | |
156 #elif !defined(__PDCURSES__) | |
157 _puts(SE); | |
158 fflush(stdout); | |
159 #endif | |
160 } | |
161 | |
162 int | |
163 md_unlink_open_file(char *file, int inf) | |
164 { | |
165 #ifdef _WIN32 | |
166 close(inf); | |
167 chmod(file, 0600); | |
168 return( _unlink(file) ); | |
169 #else | |
170 return(unlink(file)); | |
171 #endif | |
172 } | |
173 | |
174 int | |
175 md_unlink(char *file) | |
176 { | |
177 #ifdef _WIN32 | |
178 chmod(file, 0600); | |
179 return( _unlink(file) ); | |
180 #else | |
181 return(unlink(file)); | |
182 #endif | |
183 } | |
184 | |
185 int | |
186 md_creat(char *file, int mode) | |
187 { | |
188 int fd; | |
189 #ifdef _WIN32 | |
190 mode = _S_IREAD | _S_IWRITE; | |
191 #endif | |
192 fd = open(file,O_CREAT | O_EXCL | O_WRONLY, mode); | |
193 | |
194 return(fd); | |
195 } | |
196 | |
197 | |
198 int | |
199 md_normaluser() | |
200 { | |
201 #ifndef _WIN32 | |
202 setuid(getuid()); | |
203 setgid(getgid()); | |
204 #endif | |
205 } | |
206 | |
207 int | |
208 md_getuid() | |
209 { | |
210 #ifndef _WIN32 | |
211 return( getuid() ); | |
212 #else | |
213 return(42); | |
214 #endif | |
215 } | |
216 | |
217 char * | |
218 md_getusername() | |
219 { | |
220 static char login[80]; | |
221 char *l = NULL; | |
222 #ifdef _WIN32 | |
223 LPSTR mybuffer; | |
224 DWORD size = UNLEN + 1; | |
225 TCHAR buffer[UNLEN + 1]; | |
226 | |
227 mybuffer = buffer; | |
228 GetUserName(mybuffer,&size); | |
229 l = mybuffer; | |
230 #endif | |
231 #if !defined(_WIN32) && !defined(DJGPP) | |
232 struct passwd *pw; | |
233 | |
234 pw = getpwuid(getuid()); | |
235 | |
236 l = pw->pw_name; | |
237 #endif | |
238 | |
239 if ((l == NULL) || (*l == '\0')) | |
240 if ( (l = getenv("USERNAME")) == NULL ) | |
241 if ( (l = getenv("LOGNAME")) == NULL ) | |
242 if ( (l = getenv("USER")) == NULL ) | |
243 l = "nobody"; | |
244 | |
245 strncpy(login,l,80); | |
246 login[79] = 0; | |
247 | |
248 return(login); | |
249 } | |
250 | |
251 char * | |
252 md_gethomedir() | |
253 { | |
254 static char homedir[PATH_MAX]; | |
255 char *h = NULL; | |
256 size_t len; | |
257 #if defined(_WIN32) | |
258 TCHAR szPath[PATH_MAX]; | |
259 #endif | |
260 #if defined(_WIN32) || defined(DJGPP) | |
261 char slash = '\\'; | |
262 #else | |
263 char slash = '/'; | |
264 struct passwd *pw; | |
265 pw = getpwuid(getuid()); | |
266 | |
267 h = pw->pw_dir; | |
268 | |
269 if (strcmp(h,"/") == 0) | |
270 h = NULL; | |
271 #endif | |
272 homedir[0] = 0; | |
273 #ifdef _WIN32 | |
274 if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath))) | |
275 h = szPath; | |
276 #endif | |
277 | |
278 if ( (h == NULL) || (*h == '\0') ) | |
279 if ( (h = getenv("HOME")) == NULL ) | |
280 if ( (h = getenv("HOMEDRIVE")) == NULL) | |
281 h = ""; | |
282 else | |
283 { | |
284 strncpy(homedir,h,PATH_MAX-1); | |
285 homedir[PATH_MAX-1] = 0; | |
286 | |
287 if ( (h = getenv("HOMEPATH")) == NULL) | |
288 h = ""; | |
289 } | |
290 | |
291 | |
292 len = strlen(homedir); | |
293 strncat(homedir,h,PATH_MAX-len-1); | |
294 len = strlen(homedir); | |
295 | |
296 if ((len > 0) && (homedir[len-1] != slash)) { | |
297 homedir[len] = slash; | |
298 homedir[len+1] = 0; | |
299 } | |
300 | |
301 return(homedir); | |
302 } | |
303 | |
304 int | |
305 md_sleep(int s) | |
306 { | |
307 #ifdef _WIN32 | |
308 _sleep(s); | |
309 #else | |
310 sleep(s); | |
311 #endif | |
312 } | |
313 | |
314 char * | |
315 md_getshell() | |
316 { | |
317 static char shell[PATH_MAX]; | |
318 char *s = NULL; | |
319 #ifdef _WIN32 | |
320 char *def = "C:\\WINDOWS\\SYSTEM32\\CMD.EXE"; | |
321 #elif defined(__DJGPP__) | |
322 char *def = "C:\\COMMAND.COM"; | |
323 #else | |
324 char *def = "/bin/sh"; | |
325 struct passwd *pw; | |
326 pw = getpwuid(getuid()); | |
327 s = pw->pw_shell; | |
328 #endif | |
329 if ((s == NULL) || (*s == '\0')) | |
330 if ( (s = getenv("COMSPEC")) == NULL) | |
331 if ( (s = getenv("SHELL")) == NULL) | |
332 if ( (s = getenv("SystemRoot")) == NULL) | |
333 s = def; | |
334 | |
335 strncpy(shell,s,PATH_MAX); | |
336 shell[PATH_MAX-1] = 0; | |
337 | |
338 return(shell); | |
339 } | |
340 | |
341 int | |
342 md_shellescape() | |
343 { | |
344 #if (!defined(_WIN32) && !defined(__DJGPP__)) | |
345 int ret_status; | |
346 int pid; | |
347 void (*myquit)(int); | |
348 void (*myend)(int); | |
349 #endif | |
350 char *sh; | |
351 | |
352 sh = md_getshell(); | |
353 | |
354 #if defined(_WIN32) | |
355 return((int)_spawnl(_P_WAIT,sh,"shell",NULL,0)); | |
356 #elif defined(__DJGPP__) | |
357 return ( spawnl(P_WAIT,sh,"shell",NULL,0) ); | |
358 #else | |
359 while((pid = fork()) < 0) | |
360 sleep(1); | |
361 | |
362 if (pid == 0) /* Shell Process */ | |
363 { | |
364 /* | |
365 * Set back to original user, just in case | |
366 */ | |
367 setuid(getuid()); | |
368 setgid(getgid()); | |
369 execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0); | |
370 perror("No shelly"); | |
371 _exit(-1); | |
372 } | |
373 else /* Application */ | |
374 { | |
375 myend = signal(SIGINT, SIG_IGN); | |
376 #ifdef SIGQUIT | |
377 myquit = signal(SIGQUIT, SIG_IGN); | |
378 #endif | |
379 while (wait(&ret_status) != pid) | |
380 continue; | |
381 | |
382 signal(SIGINT, myquit); | |
383 #ifdef SIGQUIT | |
384 signal(SIGQUIT, myend); | |