File: tos.c

package info (click to toggle)
nethack 3.4.3-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,512 kB
  • ctags: 16,957
  • sloc: ansic: 196,792; cpp: 7,083; sh: 6,785; yacc: 2,005; lex: 377; makefile: 120; awk: 89; sed: 11
file content (372 lines) | stat: -rw-r--r-- 7,464 bytes parent folder | download | duplicates (24)
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
/*	SCCS Id: @(#)tos.c	3.1	90/14/08
/* NetHack may be freely redistributed.  See license for details. */

/*
 *  TOS system functions.
 */

#define NEED_VARARGS
#include "hack.h"

#ifdef TTY_GRAPHICS
# include "tcap.h"
#else
/* To avoid error for tos.c; will be removed later */
static char *nh_HE="\033q";
#endif

#ifdef TOS

# include <osbind.h>
# ifndef WORD
#  define WORD short		/* 16 bits -- redefine if necessary */
# endif

#include <ctype.h>

static char NDECL(DOSgetch);
static char NDECL(BIOSgetch);
static void NDECL(init_aline);
char *_a_line;			/* for Line A variables */
# ifdef TEXTCOLOR
boolean colors_changed = FALSE;
# endif

int
tgetch()
{
	char ch;

	/* BIOSgetch can use the numeric key pad on IBM compatibles. */
	if (iflags.BIOS)
		ch = BIOSgetch();
	else
		ch = DOSgetch();
	return ((ch == '\r') ? '\n' : ch);
}

/*
 *  Keyboard translation tables.
 */
#define KEYPADLO	0x61
#define KEYPADHI	0x71

#define PADKEYS 	(KEYPADHI - KEYPADLO + 1)
#define iskeypad(x)	(KEYPADLO <= (x) && (x) <= KEYPADHI)

/*
 * Keypad keys are translated to the normal values below.
 * When iflags.BIOS is active, shifted keypad keys are translated to the
 *    shift values below.
 */
static const struct pad {
	char normal, shift, cntrl;
} keypad[PADKEYS] = {
			{C('['), 'Q', C('[')},		/* UNDO */
			{'?', '/', '?'},		/* HELP */
			{'(', 'a', '('},		/* ( */
			{')', 'w', ')'},		/* ) */
			{'/', '/', '/'},		/* / */
			{C('p'), '$', C('p')},		/* * */
			{'y', 'Y', C('y')},		/* 7 */
			{'k', 'K', C('k')},		/* 8 */
			{'u', 'U', C('u')},		/* 9 */
			{'h', 'H', C('h')},		/* 4 */
			{'.', '.', '.'},
			{'l', 'L', C('l')},		/* 6 */
			{'b', 'B', C('b')},		/* 1 */
			{'j', 'J', C('j')},		/* 2 */
			{'n', 'N', C('n')},		/* 3 */
			{'i', 'I', C('i')},		/* Ins */
			{'.', ':', ':'}			/* Del */
}, numpad[PADKEYS] = {
			{C('['), 'Q', C('[')}	,	/* UNDO */
			{'?', '/', '?'},		/* HELP */
			{'(', 'a', '('},		/* ( */
			{')', 'w', ')'},		/* ) */
			{'/', '/', '/'},		/* / */
			{C('p'), '$', C('p')},		/* * */
			{'7', M('7'), '7'},		/* 7 */
			{'8', M('8'), '8'},		/* 8 */
			{'9', M('9'), '9'},		/* 9 */
			{'4', M('4'), '4'},		/* 4 */
			{'.', '.', '.'},		/* 5 */
			{'6', M('6'), '6'},		/* 6 */
			{'1', M('1'), '1'},		/* 1 */
			{'2', M('2'), '2'},		/* 2 */
			{'3', M('3'), '3'},		/* 3 */
			{'i', 'I', C('i')},		/* Ins */
			{'.', ':', ':'}			/* Del */
};

/*
 * Unlike Ctrl-letter, the Alt-letter keystrokes have no specific ASCII
 * meaning unless assigned one by a keyboard conversion table, so the
 * keyboard BIOS normally does not return a character code when Alt-letter
 * is pressed.	So, to interpret unassigned Alt-letters, we must use a
 * scan code table to translate the scan code into a letter, then set the
 * "meta" bit for it.  -3.
 */
#define SCANLO		0x10

static const char scanmap[] = { 	/* ... */
	'q','w','e','r','t','y','u','i','o','p','[',']', '\n',
	0, 'a','s','d','f','g','h','j','k','l',';','\'', '`',
	0, '\\', 'z','x','c','v','b','N','m',',','.','?' 	/* ... */
};

#define inmap(x)	(SCANLO <= (x) && (x) < SCANLO + SIZE(scanmap))

/*
 * BIOSgetch gets keys directly with a BIOS call.
 */
#define SHIFT		(0x1 | 0x2)
#define CTRL		0x4
#define ALT		0x8

static char
BIOSgetch()
{
	unsigned char scan, shift, ch;
	const struct pad *kpad;

	long  x;

	/* Get scan code.
	 */
	x = Crawcin();
	ch = x & 0x0ff;
	scan = (x & 0x00ff0000L) >> 16;
	/* Get shift status.
	 */
	shift = Kbshift(-1);

	/* Translate keypad keys */
	if (iskeypad(scan)) {
		kpad = iflags.num_pad ? numpad : keypad;
		if (shift & SHIFT)
			ch = kpad[scan - KEYPADLO].shift;
		else if (shift & CTRL)
			ch = kpad[scan - KEYPADLO].cntrl;
		else
			ch = kpad[scan - KEYPADLO].normal;
	}
	/* Translate unassigned Alt-letters */
	if ((shift & ALT) && !ch) {
		if (inmap(scan))
			ch = scanmap[scan - SCANLO];
		return (isprint(ch) ? M(ch) : ch);
	}
	return ch;
}

static char
DOSgetch()
{
	return (Crawcin() & 0x007f);
}


long
freediskspace(path)
char *path;
{
	int drive = 0;
	struct {
		long freal; /*free allocation units*/
		long total; /*total number of allocation units*/
		long bps;   /*bytes per sector*/
		long pspal; /*physical sectors per allocation unit*/
	} freespace;
	if (path[0] && path[1] == ':')
		drive = (toupper(path[0]) - 'A') + 1;
	if (Dfree(&freespace,drive)<0) return -1;
	return freespace.freal*freespace.bps*freespace.pspal;
}

/*
 * Functions to get filenames using wildcards
 */
int
findfirst(path)
char *path;
{
	return (Fsfirst(path, 0) == 0);
}

int
findnext()
{
	return (Fsnext() == 0);
}

char *
foundfile_buffer()
{
	return (char *)Fgetdta() + 30;
}

long
filesize(file)
char *file;
{
	if (findfirst(file))
		return  (* (long *) ((char *)Fgetdta() + 26));
	else
		return -1L;
}

/*
 * Chdrive() changes the default drive.
 */
void
chdrive(str)
char *str;
{
	char *ptr;
	char drive;

	if ((ptr = index(str, ':')) != (char *)0) {
		drive = toupper(*(ptr - 1));
		(void)Dsetdrv(drive - 'A');
	}
	return;
}


void
get_scr_size()
{
# ifdef MINT
#  include <ioctl.h>
	struct winsize win;
	char *tmp;

	if((tmp=nh_getenv("LINES")))
		LI = atoi(tmp);
	else if((tmp=nh_getenv("ROWS")))
		LI = atoi(tmp);
	if(tmp && (tmp=nh_getenv("COLUMNS")))
		CO = atoi(tmp);
	else {
	    ioctl(0,TIOCGWINSZ, &win);
	    LI = win.ws_row;
	    CO = win.ws_col;
	}
# else
	init_aline();
	LI = (*((WORD  *)(_a_line + -42L))) + 1;
	CO = (*((WORD  *)(_a_line + -44L))) + 1;
# endif
}

# define BIGBUF  8192

int
_copyfile(from, to)
char *from, *to;
{
	int fromfd, tofd, r;
	char *buf;

	if ((fromfd = open(from, O_RDONLY|O_BINARY, 0)) < 0)
		return -1;
	if ((tofd = open(to, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, FCMASK)) < 0)
		return -1;
	buf = (char *)alloc((size_t)BIGBUF);
	while ( (r = read(fromfd, buf, BIGBUF)) > 0)
		write(tofd, buf, r);
	close(fromfd);
	close(tofd);
	free(buf);
	return 0;	/* successful */
}

int kbhit()
{
	return Cconis();
}

static void
init_aline()
{
# ifdef __GNUC__
/* line A calls nuke registers d0-d2,a0-a2; not all compilers regard these
   as scratch registers, though, so we save them
 */
	asm(" moveml d0-d2/a0-a2, sp@-");
	asm(" .word 0xa000; movel d0, __a_line");
	asm(" moveml sp@+, d0-d2/a0-a2");
# else
	asm(" movem.l d0-d2/a0-a2, -(sp)");
	asm(" .dc.w 0xa000");	/* tweak as necessary for your compiler */
	asm(" move.l d0, __a_line");
	asm(" movem.l (sp)+, d0-d2/a0-a2");
# endif
}

# ifdef TEXTCOLOR
/* used in termcap.c to decide how to set up the hilites */
unsigned long tos_numcolors = 2;

void
set_colors()
{
	static char colorHE[] = "\033q\033b0";

	if (!iflags.BIOS)
		return;
	init_aline();
	tos_numcolors = 1 << (((unsigned char *) _a_line)[1]);
	if (tos_numcolors <= 2) {			/* mono */
		iflags.use_color = FALSE;
		return;
	} else {
		colors_changed = TRUE;
		nh_HE = colorHE;
	}
}

void
restore_colors()
{
	static char plainHE[] = "\033q";

	if (colors_changed)
		nh_HE = plainHE;
	colors_changed = FALSE;
}
# endif /* TEXTCOLOR */

# ifdef SUSPEND

#include	<signal.h>

#   ifdef MINT
extern int __mint;
#   endif

int
dosuspend() {
#   ifdef MINT
	extern int kill();
	if (__mint == 0) {
#   endif
		pline("Sorry, it seems we have no SIGTSTP here.  Try ! or S.");
#   ifdef MINT
	}
	else if(signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
		suspend_nhwindows((char *)0);
		(void) signal(SIGTSTP, SIG_DFL);
		(void) kill(0, SIGTSTP);
		get_scr_size();
		resume_nhwindows();
	} else {
		pline("I don't think your shell has job control.");
	}
#   endif /* MINT */
	return(0);
}
# endif /* SUSPEND */

#endif /* TOS */