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