-
Notifications
You must be signed in to change notification settings - Fork 4
/
io.c
533 lines (426 loc) · 11.5 KB
/
io.c
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
/* io.c */
/**/
#include "imoria.h"
#include <signal.h>
void convert_time(
unsigned long int_time,
quad_type *bin_time)
{
printf("UNIMPLEMENTED convert_time\n\r");
};
void sleep_(unsigned long int_time)
{
/*{ Sleep for given time -RAK- }*/
/*{ NOTE: Int_time is in seconds }*/
sleep((unsigned int)int_time);
};
void mini_sleep(unsigned long int_time)
{
/*{ Sleep for short time -DMF- }*/
usleep(int_time);
};
void init_priv_switch()
{
/* the hope is that imoria is sgid games or something that can write
to the master and scores files */
games_gid = getegid();
};
void priv_switch(integer switch_val)
{
// { Turns SYSPRV off if 0; on if 1; -RAK- }
// { This is needed if image is installed with SYSPRV because }
// { user could write on system areas. By turning the priv off }
// { system areas are secure }
if (switch_val) {
setegid(games_gid);
} else {
setegid(getgid());
}
};
void signalexit()
{
priv_switch(0);
msg_print("Sorry, caught a core-dump signal.");
py.flags.dead = false;
save_char(true);
exit_game(0);
}
extern void d__quit();
void signalquit()
{
priv_switch(0);
signal(SIGINT,(void *)signalquit);
switch (game_state) {
case GS_GET_COMMAND:
d__quit();
break;
case GS_IGNORE_CTRL_C:
break;
case GS_ALLOW_CTRL_C:
case GS_LOADING:
exit_game();
break;
}
}
void signalsave()
{
priv_switch(0);
py.flags.dead = false;
save_char(true);
py.flags.dead = true;
exit_game();
}
void signalsuspend()
{
if (game_state == GS_HELP) {
signal(SIGTSTP,(void *)signalsuspend);
} else {
priv_switch(0);
echo();
nocbreak();
switch (game_state) {
case GS_GET_COMMAND:
clear_screen();
put_qio();
break;
default:
save_screen();
clear_screen();
put_qio();
break;
}
#if DO_DEBUG
fprintf(debug_file,": suspending...\n");
fflush(debug_file);
#endif
kill(getpid(), SIGTSTP);
signal(SIGTSTP,(void *)signalsuspend);
#if DO_DEBUG
fprintf(debug_file,": ...resuming\n");
fflush(debug_file);
#endif
cbreak();
noecho();
switch (game_state) {
case GS_GET_COMMAND:
clear_screen();
draw_cave();
break;
default:
clear_screen();
put_qio();
restore_screen();
wrefresh(stdscr);
break;
}
}
};
void no_controly()
{
// { Turn off Control-Y -RAK- }
/* ok, this is unix not vms, so it turns off ^C and ^Z */
boolean CATCH_SIGNALS = true;
#ifdef SIGINT
signal(SIGINT,(void *)signalquit);
#endif
#ifdef SIGHUP
signal(SIGHUP,(void *)signalsave);
#endif
if (CATCH_SIGNALS) {
signal(SIGTSTP,(void *)signalsuspend);
/*signal(SIGTSTP,SIG_IGN);*/
signal(SIGQUIT,(void *)signalexit);
signal(SIGILL,(void *)signalexit);
signal(SIGTRAP,(void *)signalexit);
signal(SIGFPE,(void *)signalexit);
signal(SIGSEGV,(void *)signalexit);
#ifdef SIGIOT
signal(SIGIOT,(void *)signalexit);
#endif
#ifdef SIGABRT
signal(SIGABRT,(void *)signalexit);
#endif
#ifdef SIGEMT
signal(SIGEMT,(void *)signalexit);
#endif
#ifdef SIGBUS
signal(SIGBUS,(void *)signalexit);
#endif
#ifdef SIGSYS
signal(SIGSYS,(void *)signalexit);
#endif
}
};
void controly()
{
// { Turn on Control-Y -RAK- }
/* ok, this is unix not vms, so it turns on ^C and ^Z */
};
void exit_game()
{
// { Immediate exit from program }
controly(); // { Turn control-Y back on }
if (curses_is_running) {
put_qio(); // { Dump any remaining buffer }
/* clean up the terminal */
echo();
nocbreak();
endwin();
}
fflush(stdout);
exit(0); // { exit from game }
};
void init_channel()
{
/* XXXX */
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void msg_record(vtype message, boolean save)
{
byteint count;
byteint temp_ctr;
char ic;
string fixed_string;
ENTER("msg_record","i");
if (save) {
record_ctr++;
if (record_ctr > MAX_MESSAGES) {
record_ctr = 1;
}
strcpy(msg_prev[record_ctr], message);
if (strlen(msg_prev[record_ctr]) > 74) {
msg_prev[record_ctr][74] = 0;
}
} else {
/*{pre-declaration of variables}*/
count = 0;
temp_ctr = record_ctr;
do {
count++;
/* XXXX pad, dec, what to do? */
/*prt(pad(msg_prev[temp_ctr],' ',74) + ':' + dec(count,4,3),1,1);*/
sprintf(fixed_string, "%02d> %s",count,msg_prev[temp_ctr]);
//prt(msg_prev[temp_ctr],1,1);
prt(fixed_string,1,1);
temp_ctr--;
if (temp_ctr < 1) {
temp_ctr = MAX_MESSAGES;
}
ic = inkey();
} while (! ( !(ic==13||ic==32||ic==86) || count==MAX_MESSAGES) );
msg_print("End of buffer. ");
/* XXXX another pad, what to do? */
/*msg_print(pad('End of buffer. ',' ',80));*/
}
LEAVE("msg_record","i");
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void inkey_delay(char *getchar,integer delay)
{
/* XXXX check_input consumes the input, so we never actually get data */
put_qio(); /*{ Dump the IO buffer }*/
*getchar = 0;
if(check_input (delay)) {
*getchar = 'a';
}
};
void inkey_flush(char *x)
{
put_qio(); /*{ Dup the IO buffer }*/
if (!(wizard1)) {
flush();
}
*x = inkey();
};
void get_message()
{
};
void set_the_trap()
{
};
void disable_the_trap()
{
};
void clear_rc(integer row,integer col)
{
/* { Clears screen at given row, column }*/
integer i1;
row--; col--;
for (i1=2; i1<=23; i1++) {
used_line[i1] = false;
}
move(row, col);
clrtobot();
// put_buffer(cursor_erp, row, col);
put_qio(); /* dump the clear sequence */
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean msg_print_pass_one(char *str_buff) // : varying[a] of char;
{
boolean return_value = false;
integer old_len = 0;
char ic = 0;
ENTER("msg_print", "m");
if ((msg_flag) && (!msg_terse)) {
old_len = strlen(old_msg) + 1;
put_buffer(" -more-",msg_line,old_len);
do {
ic = inkey();
} while (ic!=10 && ic!=13 && ic!=25 && ic!=26 && ic!=27 && ic!=32);
/* isn't this nicer: until (ord(in_char) in [3,13,25,26,27,32]) ? */
}
if (str_buff && str_buff[0]) {
/* put_buffer(cursor_erl+str_buff,msg_line,msg_line);*/
erase_line(msg_line,msg_line);
put_buffer(str_buff,msg_line,msg_line);
strncpy(old_msg, str_buff, sizeof(vtype));
msg_record(str_buff,true);
if (ic==3||ic==25||ic==26||ic==27) {
return_value = true;
} else {
return_value = false;
}
msg_flag = true;
} else {
msg_flag = false;
}
RETURN("msg_print", "m", 'b',"msg", &return_value);
return return_value;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean msg_print(char *str_buff) // : varying[a] of char;
{
/*{ Outputs message to top line of screen }*/
integer old_len;
char in_char = 0;
obj_set big_set = {3,10,13,25,26,27,32,0};
obj_set small_set = {3,25,26,27,0};
boolean flag;
/* if (str_buff && str_buff[0]) {*/
if ((msg_flag) && (! msg_terse)) {
old_len = strlen(old_msg) + 1;
/*strcat(old_msg, " -more-");*/
/*put_buffer(old_msg,msg_line,msg_line);*/
put_buffer(" -more-",msg_line,old_len);
do {
in_char = inkey();
} while (! (is_in(in_char, big_set)));
}
erase_line(msg_line,msg_line);
put_buffer(str_buff,msg_line,msg_line);
strcpy(old_msg, str_buff);
msg_record(str_buff,true);
msg_flag = true;
/*
} else {
msg_flag = false;
}
*/
if (is_in(in_char,small_set)) {
flag = true;
} else {
flag = false;
}
return flag;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean get_com(char *prompt, char *command)
{
boolean return_value;
integer com_val;
if (strlen(prompt) > 1) {
prt(prompt,1,1);
}
*command = inkey();
com_val = (integer)*command;
return_value = !(com_val==3 || com_val==25 || com_val==27);
erase_line(msg_line,msg_line);
msg_flag = false;
return return_value;
}; /* end get_com */
//////////////////////////////////////////////////////////////////////
void print_str(char *str_buff,int row,int col)
{
row -= panel_row_prt;/*{ Real co-ords convert to screen positions }*/
col -= panel_col_prt;
used_line[row] = true;
put_buffer(str_buff,row,col);
};
//////////////////////////////////////////////////////////////////////
boolean get_yes_no(char *prompt) // : varying[a] of char;
{
/*{ Gets response to a Y/N question }*/
char command;
vtype out_str;
boolean return_value = false;
msg_print(" ");
sprintf(out_str, "%s (Y/N) ", prompt);
get_com(out_str,&command);
switch (command) {
case 'y': case 'Y' : return_value = true; break;
}
return return_value;
};
//////////////////////////////////////////////////////////////////////
integer get_hex_value(integer row,integer col,integer slen)
{
integer return_value = 0;
vtype tmp_str;
if (get_string(tmp_str,row,col,slen)) {
if (strlen(tmp_str) <= 8) {
sscanf(tmp_str, "%lx", &return_value);
}
}
return return_value;
};
void print_hex_value(integer num,integer row,integer col)
{
vtype out_val;
sprintf(out_val, "0x%08lx", num);
prt(out_val, row, col);
};
void pause_game(integer prt_line)
{
pause_line(prt_line);
};
void get_paths()
{
// { Returns the image path for Moria -RAK- }
// { Path is returned in a VARYING[80] of char }
char *datapath = DATA_FILE_PATH;
// fill in the MORIA_ names;
if ( strlen(datapath) > ( sizeof(MORIA_HOU) - 20 ) ) { // "moria_gcustom.mst"
printf("Umm, DATA_FILE_PATH is too long (%d chars).\n\r",
strlen(datapath));
printf("Keep it under %d chars or change the type\n\r",
sizeof(MORIA_HOU) - 20);
printf("of MORIA_HOU and friends in variables.h.\n\r");
printf("Or fix the get_paths() code in io.c. Your choice.\n\r");
exit_game();
}
sprintf(MORIA_HOU, "%s/hours.dat", datapath);
sprintf(MORIA_LCK, "%s/morialock.lock", datapath);
sprintf(MORIA_MON, "%s/monsters.dat", datapath);
sprintf(MORIA_MOR, "%s/moria.dat", datapath);
sprintf(MORIA_DTH, "%s/death.log", datapath);
sprintf(MORIA_MAS, "%s/moriamas.dat", datapath);
sprintf(MORIA_GCST, "%s/moria_gcustom.mst", datapath);
sprintf(MORIA_TOP, "%s/moriatop.dat", datapath);
sprintf(MORIA_TRD, "%s/moriatrd.dat", datapath);
/* sprintf(MORIA_HLP, "%s/moriahlp.hlb", HELP_FILE_PATH); */
sprintf(MORIA_CST, "moria_custom.mst");
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/* END FILE io.c */