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