]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/machdep.c
968489102670b3f11f7dec3192fd9620c7c46b54
[bsdgames-darwin.git] / rogue / machdep.c
1 /* $NetBSD: machdep.c,v 1.4 1995/04/24 12:25:01 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93";
42 #else
43 static char rcsid[] = "$NetBSD: machdep.c,v 1.4 1995/04/24 12:25:01 cgd Exp $";
44 #endif
45 #endif /* not lint */
46
47 /*
48 * machdep.c
49 *
50 * This source herein may be modified and/or distributed by anybody who
51 * so desires, with the following restrictions:
52 * 1.) No portion of this notice shall be removed.
53 * 2.) Credit shall not be taken for the creation of this source.
54 * 3.) This code is not to be traded, sold, or used for personal
55 * gain or profit.
56 *
57 */
58
59 /* Included in this file are all system dependent routines. Extensive use
60 * of #ifdef's will be used to compile the appropriate code on each system:
61 *
62 * UNIX: all UNIX systems.
63 * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
64 * UNIX_SYSV: UNIX system V
65 * UNIX_V7: UNIX version 7
66 *
67 * All UNIX code should be included between the single "#ifdef UNIX" at the
68 * top of this file, and the "#endif" at the bottom.
69 *
70 * To change a routine to include a new UNIX system, simply #ifdef the
71 * existing routine, as in the following example:
72 *
73 * To make a routine compatible with UNIX system 5, change the first
74 * function to the second:
75 *
76 * md_function()
77 * {
78 * code;
79 * }
80 *
81 * md_function()
82 * {
83 * #ifdef UNIX_SYSV
84 * sys5code;
85 * #else
86 * code;
87 * #endif
88 * }
89 *
90 * Appropriate variations of this are of course acceptible.
91 * The use of "#elseif" is discouraged because of non-portability.
92 * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
93 * and insert it in the list at the top of the file. Alter the CFLAGS
94 * in you Makefile appropriately.
95 *
96 */
97
98 #ifdef UNIX
99
100 #include <stdio.h>
101 #include <sys/types.h>
102 #include <sys/file.h>
103 #include <sys/stat.h>
104 #include <pwd.h>
105
106 #ifdef UNIX_BSD4_2
107 #include <sys/time.h>
108 #include <sgtty.h>
109 #endif
110
111 #ifdef UNIX_SYSV
112 #include <time.h>
113 #include <termio.h>
114 #endif
115
116 #include <signal.h>
117 #include "rogue.h"
118 #include "pathnames.h"
119
120 /* md_slurp:
121 *
122 * This routine throws away all keyboard input that has not
123 * yet been read. It is used to get rid of input that the user may have
124 * typed-ahead.
125 *
126 * This function is not necessary, so it may be stubbed. The might cause
127 * message-line output to flash by because the game has continued to read
128 * input without waiting for the user to read the message. Not such a
129 * big deal.
130 */
131
132 md_slurp()
133 {
134 (void)fpurge(stdin);
135 }
136
137 /* md_control_keyboard():
138 *
139 * This routine is much like md_cbreak_no_echo_nonl() below. It sets up the
140 * keyboard for appropriate input. Specifically, it prevents the tty driver
141 * from stealing characters. For example, ^Y is needed as a command
142 * character, but the tty driver intercepts it for another purpose. Any
143 * such behavior should be stopped. This routine could be avoided if
144 * we used RAW mode instead of CBREAK. But RAW mode does not allow the
145 * generation of keyboard signals, which the program uses.
146 *
147 * The parameter 'mode' when true, indicates that the keyboard should
148 * be set up to play rogue. When false, it should be restored if
149 * necessary.
150 *
151 * This routine is not strictly necessary and may be stubbed. This may
152 * cause certain command characters to be unavailable.
153 */
154
155 md_control_keybord(mode)
156 boolean mode;
157 {
158 static boolean called_before = 0;
159 #ifdef UNIX_BSD4_2
160 static struct ltchars ltc_orig;
161 static struct tchars tc_orig;
162 struct ltchars ltc_temp;
163 struct tchars tc_temp;
164 #endif
165 #ifdef UNIX_SYSV
166 static struct termio _oldtty;
167 struct termio _tty;
168 #endif
169
170 if (!called_before) {
171 called_before = 1;
172 #ifdef UNIX_BSD4_2
173 ioctl(0, TIOCGETC, &tc_orig);
174 ioctl(0, TIOCGLTC, &ltc_orig);
175 #endif
176 #ifdef UNIX_SYSV
177 ioctl(0, TCGETA, &_oldtty);
178 #endif
179 }
180 #ifdef UNIX_BSD4_2
181 ltc_temp = ltc_orig;
182 tc_temp = tc_orig;
183 #endif
184 #ifdef UNIX_SYSV
185 _tty = _oldtty;
186 #endif
187
188 if (!mode) {
189 #ifdef UNIX_BSD4_2
190 ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
191 ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
192 ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
193 tc_temp.t_startc = tc_temp.t_stopc = -1;
194 #endif
195 #ifdef UNIX_SYSV
196 _tty.c_cc[VSWTCH] = CNSWTCH;
197 #endif
198 }
199 #ifdef UNIX_BSD4_2
200 ioctl(0, TIOCSETC, &tc_temp);
201 ioctl(0, TIOCSLTC, &ltc_temp);
202 #endif
203 #ifdef UNIX_SYSV
204 ioctl(0, TCSETA, &_tty);
205 #endif
206 }
207
208 /* md_heed_signals():
209 *
210 * This routine tells the program to call particular routines when
211 * certain interrupts/events occur:
212 *
213 * SIGINT: call onintr() to interrupt fight with monster or long rest.
214 * SIGQUIT: call byebye() to check for game termination.
215 * SIGHUP: call error_save() to save game when terminal hangs up.
216 *
217 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
218 *
219 * This routine is not strictly necessary and can be stubbed. This will
220 * mean that the game cannot be interrupted properly with keyboard
221 * input, this is not usually critical.
222 */
223
224 md_heed_signals()
225 {
226 signal(SIGINT, onintr);
227 signal(SIGQUIT, byebye);
228 signal(SIGHUP, error_save);
229 }
230
231 /* md_ignore_signals():
232 *
233 * This routine tells the program to completely ignore the events mentioned
234 * in md_heed_signals() above. The event handlers will later be turned on
235 * by a future call to md_heed_signals(), so md_heed_signals() and
236 * md_ignore_signals() need to work together.
237 *
238 * This function should be implemented or the user risks interrupting
239 * critical sections of code, which could cause score file, or saved-game
240 * file, corruption.
241 */
242
243 md_ignore_signals()
244 {
245 signal(SIGQUIT, SIG_IGN);
246 signal(SIGINT, SIG_IGN);
247 signal(SIGHUP, SIG_IGN);
248 }
249
250 /* md_get_file_id():
251 *
252 * This function returns an integer that uniquely identifies the specified
253 * file. It need not check for the file's existence. In UNIX, the inode
254 * number is used.
255 *
256 * This function is used to identify saved-game files.
257 */
258
259 int
260 md_get_file_id(fname)
261 char *fname;
262 {
263 struct stat sbuf;
264
265 if (stat(fname, &sbuf)) {
266 return(-1);
267 }
268 return((int) sbuf.st_ino);
269 }
270
271 /* md_link_count():
272 *
273 * This routine returns the number of hard links to the specified file.
274 *
275 * This function is not strictly necessary. On systems without hard links
276 * this routine can be stubbed by just returning 1.
277 */
278
279 int
280 md_link_count(fname)
281 char *fname;
282 {
283 struct stat sbuf;
284
285 stat(fname, &sbuf);
286 return((int) sbuf.st_nlink);
287 }
288
289 /* md_gct(): (Get Current Time)
290 *
291 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
292 * minute(0-59), and second(0-59). This is used for identifying the time
293 * at which a game is saved.
294 *
295 * This function is not strictly necessary. It can be stubbed by returning
296 * zeros instead of the correct year, month, etc. If your operating
297 * system doesn't provide all of the time units requested here, then you
298 * can provide only those that it does, and return zeros for the others.
299 * If you cannot provide good time values, then users may be able to copy
300 * saved-game files and play them.
301 */
302
303 md_gct(rt_buf)
304 struct rogue_time *rt_buf;
305 {
306 struct tm *t, *localtime();
307 time_t seconds;
308
309 time(&seconds);
310 t = localtime(&seconds);
311
312 rt_buf->year = t->tm_year;
313 rt_buf->month = t->tm_mon + 1;
314 rt_buf->day = t->tm_mday;
315 rt_buf->hour = t->tm_hour;
316 rt_buf->minute = t->tm_min;
317 rt_buf->second = t->tm_sec;
318 }
319
320 /* md_gfmt: (Get File Modification Time)
321 *
322 * This routine returns a file's date of last modification in the same format
323 * as md_gct() above.
324 *
325 * This function is not strictly necessary. It is used to see if saved-game
326 * files have been modified since they were saved. If you have stubbed the
327 * routine md_gct() above by returning constant values, then you may do
328 * exactly the same here.
329 * Or if md_gct() is implemented correctly, but your system does not provide
330 * file modification dates, you may return some date far in the past so
331 * that the program will never know that a saved-game file being modified.
332 * You may also do this if you wish to be able to restore games from
333 * saved-games that have been modified.
334 */
335
336 md_gfmt(fname, rt_buf)
337 char *fname;
338 struct rogue_time *rt_buf;
339 {
340 struct stat sbuf;
341 time_t seconds;
342 struct tm *t;
343
344 stat(fname, &sbuf);
345 seconds = (long) sbuf.st_mtime;
346 t = localtime(&seconds);
347
348 rt_buf->year = t->tm_year;
349 rt_buf->month = t->tm_mon + 1;
350 rt_buf->day = t->tm_mday;
351 rt_buf->hour = t->tm_hour;
352 rt_buf->minute = t->tm_min;
353 rt_buf->second = t->tm_sec;
354 }
355
356 /* md_df: (Delete File)
357 *
358 * This function deletes the specified file, and returns true (1) if the
359 * operation was successful. This is used to delete saved-game files
360 * after restoring games from them.
361 *
362 * Again, this function is not strictly necessary, and can be stubbed
363 * by simply returning 1. In this case, saved-game files will not be
364 * deleted and can be replayed.
365 */
366
367 boolean
368 md_df(fname)
369 char *fname;
370 {
371 if (unlink(fname)) {
372 return(0);
373 }
374 return(1);
375 }
376
377 /* md_gln: (Get login name)
378 *
379 * This routine returns the login name of the user. This string is
380 * used mainly for identifying users in score files.
381 *
382 * A dummy string may be returned if you are unable to implement this
383 * function, but then the score file would only have one name in it.
384 */
385
386 char *
387 md_gln()
388 {
389 struct passwd *p;
390
391 if (!(p = getpwuid(getuid())))
392 return((char *)NULL);
393 return(p->pw_name);
394 }
395
396 /* md_sleep:
397 *
398 * This routine causes the game to pause for the specified number of
399 * seconds.
400 *
401 * This routine is not particularly necessary at all. It is used for
402 * delaying execution, which is useful to this program at some times.
403 */
404
405 md_sleep(nsecs)
406 int nsecs;
407 {
408 (void) sleep(nsecs);
409 }
410
411 /* md_getenv()
412 *
413 * This routine gets certain values from the user's environment. These
414 * values are strings, and each string is identified by a name. The names
415 * of the values needed, and their use, is as follows:
416 *
417 * TERMCAP
418 * The name of the users's termcap file, NOT the termcap entries
419 * themselves. This is used ONLY if the program is compiled with
420 * CURSES defined (-DCURSES). Even in this case, the program need
421 * not find a string for TERMCAP. If it does not, it will use the
422 * default termcap file as returned by md_gdtcf();
423 * TERM
424 * The name of the users's terminal. This is used ONLY if the program
425 * is compiled with CURSES defined (-DCURSES). In this case, the string
426 * value for TERM must be found, or the routines in curses.c cannot
427 * function, and the program will quit.
428 * ROGUEOPTS
429 * A string containing the various game options. This need not be
430 * defined.
431 * HOME
432 * The user's home directory. This is only used when the user specifies
433 * '~' as the first character of a saved-game file. This string need
434 * not be defined.
435 * SHELL
436 * The user's favorite shell. If not found, "/bin/sh" is assumed.
437 *
438 * If your system does not provide a means of searching for these values,
439 * you will have to do it yourself. None of the values above really need
440 * to be defined except TERM when the program is compiled with CURSES
441 * defined. In this case, as a bare minimum, you can check the 'name'
442 * parameter, and if it is "TERM" find the terminal name and return that,
443 * else return zero. If the program is not compiled with CURSES, you can
444 * get by with simply always returning zero. Returning zero indicates
445 * that their is no defined value for the given string.
446 */
447
448 char *
449 md_getenv(name)
450 char *name;
451 {
452 char *value;
453 char *getenv();
454
455 value = getenv(name);
456
457 return(value);
458 }
459
460 /* md_malloc()
461 *
462 * This routine allocates, and returns a pointer to, the specified number
463 * of bytes. This routines absolutely MUST be implemented for your
464 * particular system or the program will not run at all. Return zero
465 * when no more memory can be allocated.
466 */
467
468 char *
469 md_malloc(n)
470 int n;
471 {
472 char *malloc();
473 char *t;
474
475 t = malloc(n);
476 return(t);
477 }
478
479 /* md_gseed() (Get Seed)
480 *
481 * This function returns a seed for the random number generator (RNG). This
482 * seed causes the RNG to begin generating numbers at some point in it's
483 * sequence. Without a random seed, the RNG will generate the same set
484 * of numbers, and every game will start out exactly the same way. A good
485 * number to use is the process id, given by getpid() on most UNIX systems.
486 *
487 * You need to find some single random integer, such as:
488 * process id.
489 * current time (minutes + seconds) returned from md_gct(), if implemented.
490 *
491 * It will not help to return "get_rand()" or "rand()" or the return value of
492 * any pseudo-RNG. If you don't have a random number, you can just return 1,
493 * but this means your games will ALWAYS start the same way, and will play
494 * exactly the same way given the same input.
495 */
496
497 md_gseed()
498 {
499 return(getpid());
500 }
501
502 /* md_exit():
503 *
504 * This function causes the program to discontinue execution and exit.
505 * This function must be implemented or the program will continue to
506 * hang when it should quit.
507 */
508
509 md_exit(status)
510 int status;
511 {
512 exit(status);
513 }
514
515 /* md_lock():
516 *
517 * This function is intended to give the user exclusive access to the score
518 * file. It does so by flock'ing the score file. The full path name of the
519 * score file should be defined for any particular site in rogue.h. The
520 * constants _PATH_SCOREFILE defines this file name.
521 *
522 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
523 * the lock is released.
524 */
525
526 md_lock(l)
527 boolean l;
528 {
529 static int fd;
530 short tries;
531
532 if (l) {
533 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
534 message("cannot lock score file", 0);
535 return;
536 }
537 for (tries = 0; tries < 5; tries++)
538 if (!flock(fd, LOCK_EX|LOCK_NB))
539 return;
540 } else {
541 (void)flock(fd, LOCK_NB);
542 (void)close(fd);
543 }
544 }
545
546 /* md_shell():
547 *
548 * This function spawns a shell for the user to use. When this shell is
549 * terminated, the game continues. Since this program may often be run
550 * setuid to gain access to privileged files, care is taken that the shell
551 * is run with the user's REAL user id, and not the effective user id.
552 * The effective user id is restored after the shell completes.
553 */
554
555 md_shell(shell)
556 char *shell;
557 {
558 long w[2];
559
560 if (!fork()) {
561 int uid;
562
563 uid = getuid();
564 setuid(uid);
565 execl(shell, shell, 0);
566 }
567 wait(w);
568 }
569
570 /* If you have a viable curses/termlib library, then use it and don't bother
571 * implementing the routines below. And don't compile with -DCURSES.
572 */
573
574 #ifdef CURSES
575
576 /* md_cbreak_no_echo_nonl:
577 *
578 * This routine sets up some terminal characteristics. The tty-driver
579 * must be told to:
580 * 1.) Not echo input.
581 * 2.) Transmit input characters immediately upon typing. (cbreak mode)
582 * 3.) Move the cursor down one line, without changing column, and
583 * without generating a carriage-return, when it
584 * sees a line-feed. This is only necessary if line-feed is ever
585 * used in the termcap 'do' (cursor down) entry, in which case,
586 * your system should must have a way of accomplishing this.
587 *
588 * When the parameter 'on' is true, the terminal is set up as specified
589 * above. When this parameter is false, the terminal is restored to the
590 * original state.
591 *
592 * Raw mode should not to be used. Keyboard signals/events/interrupts should
593 * be sent, although they are not strictly necessary. See notes in
594 * md_heed_signals().
595 *
596 * This function must be implemented for rogue to run properly if the
597 * program is compiled with CURSES defined to use the enclosed curses
598 * emulation package. If you are not using this, then this routine is
599 * totally unnecessary.
600 *
601 * Notice that information is saved between calls. This is used to
602 * restore the terminal to an initial saved state.
603 *
604 */
605
606 md_cbreak_no_echo_nonl(on)
607 boolean on;
608 {
609 #ifdef UNIX_BSD4_2
610 static struct sgttyb tty_buf;
611 static int tsave_flags;
612
613 if (on) {
614 ioctl(0, TIOCGETP, &tty_buf);
615 tsave_flags = tty_buf.sg_flags;
616 tty_buf.sg_flags |= CBREAK;
617 tty_buf.sg_flags &= ~(ECHO | CRMOD); /* CRMOD: see note 3 above */
618 ioctl(0, TIOCSETP, &tty_buf);
619 } else {
620 tty_buf.sg_flags = tsave_flags;
621 ioctl(0, TIOCSETP, &tty_buf);
622 }
623 #endif
624 #ifdef UNIX_SYSV
625 struct termio tty_buf;
626 static struct termio tty_save;
627
628 if (on) {
629 ioctl(0, TCGETA, &tty_buf);
630 tty_save = tty_buf;
631 tty_buf.c_lflag &= ~(ICANON | ECHO);
632 tty_buf.c_oflag &= ~ONLCR;
633 tty_buf.c_cc[4] = 1; /* MIN */
634 tty_buf.c_cc[5] = 2; /* TIME */
635 ioctl(0, TCSETAF, &tty_buf);
636 } else {
637 ioctl(0, TCSETAF, &tty_save);
638 }
639 #endif
640 }
641
642 /* md_gdtcf(): (Get Default Termcap File)
643 *
644 * This function is called ONLY when the program is compiled with CURSES
645 * defined. If you use your system's curses/termlib library, this function
646 * won't be called. On most UNIX systems, "/etc/termcap" suffices.
647 *
648 * If their is no such termcap file, then return 0, but in that case, you
649 * must have a TERMCAP file returned from md_getenv("TERMCAP"). The latter
650 * will override the value returned from md_gdtcf(). If the program is
651 * compiled with CURSES defined, and md_gdtcf() returns 0, and
652 * md_getenv("TERMCAP") returns 0, the program will have no terminal
653 * capability information and will quit.
654 */
655
656 char *
657 md_gdtcf()
658 {
659 return("/etc/termcap");
660 }
661
662 /* md_tstp():
663 *
664 * This function puts the game to sleep and returns to the shell. This
665 * only applies to UNIX 4.2 and 4.3. For other systems, the routine should
666 * be provided as a do-nothing routine. md_tstp() will only be referenced
667 * in the code when compiled with CURSES defined.
668 *
669 */
670
671 md_tstp()
672 {
673 #ifdef UNIX_BSD4_2
674 kill(0, SIGTSTP);
675 #endif
676 }
677
678 #endif
679
680 #endif