machdep.c revision 1.5 1 /* $NetBSD: machdep.c,v 1.5 1995/04/28 23:49:22 mycroft 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.5 1995/04/28 23:49:22 mycroft 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 #endif
109
110 #ifdef UNIX_SYSV
111 #include <time.h>
112 #endif
113
114 #include <signal.h>
115 #include <termios.h>
116 #include "rogue.h"
117 #include "pathnames.h"
118
119 /* md_slurp:
120 *
121 * This routine throws away all keyboard input that has not
122 * yet been read. It is used to get rid of input that the user may have
123 * typed-ahead.
124 *
125 * This function is not necessary, so it may be stubbed. The might cause
126 * message-line output to flash by because the game has continued to read
127 * input without waiting for the user to read the message. Not such a
128 * big deal.
129 */
130
131 md_slurp()
132 {
133 (void)fpurge(stdin);
134 }
135
136 /* md_heed_signals():
137 *
138 * This routine tells the program to call particular routines when
139 * certain interrupts/events occur:
140 *
141 * SIGINT: call onintr() to interrupt fight with monster or long rest.
142 * SIGQUIT: call byebye() to check for game termination.
143 * SIGHUP: call error_save() to save game when terminal hangs up.
144 *
145 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
146 *
147 * This routine is not strictly necessary and can be stubbed. This will
148 * mean that the game cannot be interrupted properly with keyboard
149 * input, this is not usually critical.
150 */
151
152 md_heed_signals()
153 {
154 signal(SIGINT, onintr);
155 signal(SIGQUIT, byebye);
156 signal(SIGHUP, error_save);
157 }
158
159 /* md_ignore_signals():
160 *
161 * This routine tells the program to completely ignore the events mentioned
162 * in md_heed_signals() above. The event handlers will later be turned on
163 * by a future call to md_heed_signals(), so md_heed_signals() and
164 * md_ignore_signals() need to work together.
165 *
166 * This function should be implemented or the user risks interrupting
167 * critical sections of code, which could cause score file, or saved-game
168 * file, corruption.
169 */
170
171 md_ignore_signals()
172 {
173 signal(SIGQUIT, SIG_IGN);
174 signal(SIGINT, SIG_IGN);
175 signal(SIGHUP, SIG_IGN);
176 }
177
178 /* md_get_file_id():
179 *
180 * This function returns an integer that uniquely identifies the specified
181 * file. It need not check for the file's existence. In UNIX, the inode
182 * number is used.
183 *
184 * This function is used to identify saved-game files.
185 */
186
187 int
188 md_get_file_id(fname)
189 char *fname;
190 {
191 struct stat sbuf;
192
193 if (stat(fname, &sbuf)) {
194 return(-1);
195 }
196 return((int) sbuf.st_ino);
197 }
198
199 /* md_link_count():
200 *
201 * This routine returns the number of hard links to the specified file.
202 *
203 * This function is not strictly necessary. On systems without hard links
204 * this routine can be stubbed by just returning 1.
205 */
206
207 int
208 md_link_count(fname)
209 char *fname;
210 {
211 struct stat sbuf;
212
213 stat(fname, &sbuf);
214 return((int) sbuf.st_nlink);
215 }
216
217 /* md_gct(): (Get Current Time)
218 *
219 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
220 * minute(0-59), and second(0-59). This is used for identifying the time
221 * at which a game is saved.
222 *
223 * This function is not strictly necessary. It can be stubbed by returning
224 * zeros instead of the correct year, month, etc. If your operating
225 * system doesn't provide all of the time units requested here, then you
226 * can provide only those that it does, and return zeros for the others.
227 * If you cannot provide good time values, then users may be able to copy
228 * saved-game files and play them.
229 */
230
231 md_gct(rt_buf)
232 struct rogue_time *rt_buf;
233 {
234 struct tm *t, *localtime();
235 time_t seconds;
236
237 time(&seconds);
238 t = localtime(&seconds);
239
240 rt_buf->year = t->tm_year;
241 rt_buf->month = t->tm_mon + 1;
242 rt_buf->day = t->tm_mday;
243 rt_buf->hour = t->tm_hour;
244 rt_buf->minute = t->tm_min;
245 rt_buf->second = t->tm_sec;
246 }
247
248 /* md_gfmt: (Get File Modification Time)
249 *
250 * This routine returns a file's date of last modification in the same format
251 * as md_gct() above.
252 *
253 * This function is not strictly necessary. It is used to see if saved-game
254 * files have been modified since they were saved. If you have stubbed the
255 * routine md_gct() above by returning constant values, then you may do
256 * exactly the same here.
257 * Or if md_gct() is implemented correctly, but your system does not provide
258 * file modification dates, you may return some date far in the past so
259 * that the program will never know that a saved-game file being modified.
260 * You may also do this if you wish to be able to restore games from
261 * saved-games that have been modified.
262 */
263
264 md_gfmt(fname, rt_buf)
265 char *fname;
266 struct rogue_time *rt_buf;
267 {
268 struct stat sbuf;
269 time_t seconds;
270 struct tm *t;
271
272 stat(fname, &sbuf);
273 seconds = (long) sbuf.st_mtime;
274 t = localtime(&seconds);
275
276 rt_buf->year = t->tm_year;
277 rt_buf->month = t->tm_mon + 1;
278 rt_buf->day = t->tm_mday;
279 rt_buf->hour = t->tm_hour;
280 rt_buf->minute = t->tm_min;
281 rt_buf->second = t->tm_sec;
282 }
283
284 /* md_df: (Delete File)
285 *
286 * This function deletes the specified file, and returns true (1) if the
287 * operation was successful. This is used to delete saved-game files
288 * after restoring games from them.
289 *
290 * Again, this function is not strictly necessary, and can be stubbed
291 * by simply returning 1. In this case, saved-game files will not be
292 * deleted and can be replayed.
293 */
294
295 boolean
296 md_df(fname)
297 char *fname;
298 {
299 if (unlink(fname)) {
300 return(0);
301 }
302 return(1);
303 }
304
305 /* md_gln: (Get login name)
306 *
307 * This routine returns the login name of the user. This string is
308 * used mainly for identifying users in score files.
309 *
310 * A dummy string may be returned if you are unable to implement this
311 * function, but then the score file would only have one name in it.
312 */
313
314 char *
315 md_gln()
316 {
317 struct passwd *p;
318
319 if (!(p = getpwuid(getuid())))
320 return((char *)NULL);
321 return(p->pw_name);
322 }
323
324 /* md_sleep:
325 *
326 * This routine causes the game to pause for the specified number of
327 * seconds.
328 *
329 * This routine is not particularly necessary at all. It is used for
330 * delaying execution, which is useful to this program at some times.
331 */
332
333 md_sleep(nsecs)
334 int nsecs;
335 {
336 (void) sleep(nsecs);
337 }
338
339 /* md_getenv()
340 *
341 * This routine gets certain values from the user's environment. These
342 * values are strings, and each string is identified by a name. The names
343 * of the values needed, and their use, is as follows:
344 *
345 * TERMCAP
346 * The name of the users's termcap file, NOT the termcap entries
347 * themselves. This is used ONLY if the program is compiled with
348 * CURSES defined (-DCURSES). Even in this case, the program need
349 * not find a string for TERMCAP. If it does not, it will use the
350 * default termcap file as returned by md_gdtcf();
351 * TERM
352 * The name of the users's terminal. This is used ONLY if the program
353 * is compiled with CURSES defined (-DCURSES). In this case, the string
354 * value for TERM must be found, or the routines in curses.c cannot
355 * function, and the program will quit.
356 * ROGUEOPTS
357 * A string containing the various game options. This need not be
358 * defined.
359 * HOME
360 * The user's home directory. This is only used when the user specifies
361 * '~' as the first character of a saved-game file. This string need
362 * not be defined.
363 * SHELL
364 * The user's favorite shell. If not found, "/bin/sh" is assumed.
365 *
366 * If your system does not provide a means of searching for these values,
367 * you will have to do it yourself. None of the values above really need
368 * to be defined except TERM when the program is compiled with CURSES
369 * defined. In this case, as a bare minimum, you can check the 'name'
370 * parameter, and if it is "TERM" find the terminal name and return that,
371 * else return zero. If the program is not compiled with CURSES, you can
372 * get by with simply always returning zero. Returning zero indicates
373 * that their is no defined value for the given string.
374 */
375
376 char *
377 md_getenv(name)
378 char *name;
379 {
380 char *value;
381 char *getenv();
382
383 value = getenv(name);
384
385 return(value);
386 }
387
388 /* md_malloc()
389 *
390 * This routine allocates, and returns a pointer to, the specified number
391 * of bytes. This routines absolutely MUST be implemented for your
392 * particular system or the program will not run at all. Return zero
393 * when no more memory can be allocated.
394 */
395
396 char *
397 md_malloc(n)
398 int n;
399 {
400 char *malloc();
401 char *t;
402
403 t = malloc(n);
404 return(t);
405 }
406
407 /* md_gseed() (Get Seed)
408 *
409 * This function returns a seed for the random number generator (RNG). This
410 * seed causes the RNG to begin generating numbers at some point in it's
411 * sequence. Without a random seed, the RNG will generate the same set
412 * of numbers, and every game will start out exactly the same way. A good
413 * number to use is the process id, given by getpid() on most UNIX systems.
414 *
415 * You need to find some single random integer, such as:
416 * process id.
417 * current time (minutes + seconds) returned from md_gct(), if implemented.
418 *
419 * It will not help to return "get_rand()" or "rand()" or the return value of
420 * any pseudo-RNG. If you don't have a random number, you can just return 1,
421 * but this means your games will ALWAYS start the same way, and will play
422 * exactly the same way given the same input.
423 */
424
425 md_gseed()
426 {
427 return(getpid());
428 }
429
430 /* md_exit():
431 *
432 * This function causes the program to discontinue execution and exit.
433 * This function must be implemented or the program will continue to
434 * hang when it should quit.
435 */
436
437 md_exit(status)
438 int status;
439 {
440 exit(status);
441 }
442
443 /* md_lock():
444 *
445 * This function is intended to give the user exclusive access to the score
446 * file. It does so by flock'ing the score file. The full path name of the
447 * score file should be defined for any particular site in rogue.h. The
448 * constants _PATH_SCOREFILE defines this file name.
449 *
450 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
451 * the lock is released.
452 */
453
454 md_lock(l)
455 boolean l;
456 {
457 static int fd;
458 short tries;
459
460 if (l) {
461 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
462 message("cannot lock score file", 0);
463 return;
464 }
465 for (tries = 0; tries < 5; tries++)
466 if (!flock(fd, LOCK_EX|LOCK_NB))
467 return;
468 } else {
469 (void)flock(fd, LOCK_NB);
470 (void)close(fd);
471 }
472 }
473
474 /* md_shell():
475 *
476 * This function spawns a shell for the user to use. When this shell is
477 * terminated, the game continues. Since this program may often be run
478 * setuid to gain access to privileged files, care is taken that the shell
479 * is run with the user's REAL user id, and not the effective user id.
480 * The effective user id is restored after the shell completes.
481 */
482
483 md_shell(shell)
484 char *shell;
485 {
486 long w[2];
487
488 if (!fork()) {
489 int uid;
490
491 uid = getuid();
492 setuid(uid);
493 execl(shell, shell, 0);
494 }
495 wait(w);
496 }
497
498 /* If you have a viable curses/termlib library, then use it and don't bother
499 * implementing the routines below. And don't compile with -DCURSES.
500 */
501
502 #ifdef CURSES
503
504 /* md_cbreak_no_echo_nonl:
505 *
506 * This routine sets up some terminal characteristics. The tty-driver
507 * must be told to:
508 * 1.) Not echo input.
509 * 2.) Transmit input characters immediately upon typing. (cbreak mode)
510 * 3.) Move the cursor down one line, without changing column, and
511 * without generating a carriage-return, when it
512 * sees a line-feed. This is only necessary if line-feed is ever
513 * used in the termcap 'do' (cursor down) entry, in which case,
514 * your system should must have a way of accomplishing this.
515 *
516 * When the parameter 'on' is true, the terminal is set up as specified
517 * above. When this parameter is false, the terminal is restored to the
518 * original state.
519 *
520 * Raw mode should not to be used. Keyboard signals/events/interrupts should
521 * be sent, although they are not strictly necessary. See notes in
522 * md_heed_signals().
523 *
524 * This function must be implemented for rogue to run properly if the
525 * program is compiled with CURSES defined to use the enclosed curses
526 * emulation package. If you are not using this, then this routine is
527 * totally unnecessary.
528 *
529 * Notice that information is saved between calls. This is used to
530 * restore the terminal to an initial saved state.
531 *
532 */
533
534 md_cbreak_no_echo_nonl(on)
535 boolean on;
536 {
537 struct termios tty_buf;
538 static struct termios tty_save;
539
540 if (on) {
541 tcgetattr(0, &tty_buf);
542 tty_save = tty_buf;
543 tty_buf.c_lflag &= ~(ICANON | ECHO);
544 tty_buf.c_oflag &= ~ONLCR;
545 tty_buf.c_cc[VMIN] = 1;
546 tty_buf.c_cc[VTIME] = 2;
547 tcsetattr(0, TCSADRAIN, &tty_buf);
548 } else {
549 tcsetattr(0, TCSADRAIN, &tty_save);
550 }
551 }
552
553 /* md_gdtcf(): (Get Default Termcap File)
554 *
555 * This function is called ONLY when the program is compiled with CURSES
556 * defined. If you use your system's curses/termlib library, this function
557 * won't be called. On most UNIX systems, "/etc/termcap" suffices.
558 *
559 * If their is no such termcap file, then return 0, but in that case, you
560 * must have a TERMCAP file returned from md_getenv("TERMCAP"). The latter
561 * will override the value returned from md_gdtcf(). If the program is
562 * compiled with CURSES defined, and md_gdtcf() returns 0, and
563 * md_getenv("TERMCAP") returns 0, the program will have no terminal
564 * capability information and will quit.
565 */
566
567 char *
568 md_gdtcf()
569 {
570 return("/etc/termcap");
571 }
572
573 /* md_tstp():
574 *
575 * This function puts the game to sleep and returns to the shell. This
576 * only applies to UNIX 4.2 and 4.3. For other systems, the routine should
577 * be provided as a do-nothing routine. md_tstp() will only be referenced
578 * in the code when compiled with CURSES defined.
579 *
580 */
581
582 md_tstp()
583 {
584 #ifdef UNIX_BSD4_2
585 kill(0, SIGTSTP);
586 #endif
587 }
588
589 #endif
590
591 #endif
592