-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutil.c
390 lines (351 loc) · 9.22 KB
/
util.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
/*
* Netris -- A free networked version of T*tris
* Copyright (C) 1994,1995,1996 Mark H. Weaver <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: util.c,v 1.29 1999/05/16 06:56:33 mhw Exp $
*/
#include "netris.h"
#include <time.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netdb.h>
#include <errno.h>
static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event);
static EventGenRec alarmGen =
{ &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm };
static EventGenRec *nextGen = &alarmGen;
static int myRandSeed = 1;
static struct timeval baseTimeval;
ExtFunc void InitUtil(void)
{
if (initSeed)
SRandom(initSeed);
else
SRandom(time(0));
signal(SIGINT, CatchInt);
ResetBaseTime();
}
ExtFunc void ResetBaseTime(void)
{
gettimeofday(&baseTimeval, NULL);
}
ExtFunc void AtExit(void (*handler)(void))
{
#ifdef HAS_ON_EXIT
on_exit((void *)handler, NULL);
#else
atexit(handler);
#endif
}
ExtFunc void Usage(void)
{
fprintf(stderr,
"Netris version %s (C) 1994-1996,1999 Mark H. Weaver <[email protected]>\n"
"Usage: netris <options>\n"
" -h Print usage information\n"
" -w Wait for connection\n"
" -c <host> Initiate connection\n"
" -p <port> Set port number (default is %d)\n"
" -k <keys> Remap keys. The argument is a prefix of the string\n"
" containing the keys in order: left, full left, rotate, right, \n"
" full right, drop, down-faster, toggle-spying, pause, faster, "
"redraw, new.\n \"^\" prefixes controls. (default is \"%s\")\n"
" -i <sec> Set the step-down interval, in seconds\n"
" -r <robot> Execute <robot> (a command) as a robot controlling\n"
" the game instead of the keyboard\n"
" -F Use fair robot interface\n"
" -s <seed> Start with given random seed\n"
" -D Drops go into drop mode\n"
" This means that sliding off a cliff after a drop causes\n"
" another drop automatically\n"
" -S Disable inverse/bold/color for slow terminals\n"
" -C Disable color\n"
" -H Show distribution and warranty information\n"
" -R Show rules\n",
version_string, DEFAULT_PORT, DEFAULT_KEYS);
}
ExtFunc void DistInfo(void)
{
fprintf(stderr,
"Netris version %s (C) 1994-1996,1999 Mark H. Weaver <[email protected]>\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n",
version_string);
}
ExtFunc void Rules(void)
{
fprintf(stderr,
"Netris version %s rules\n"
"\n"
"Two player mode\n"
"---------------\n"
"It's just like normal T*tris except that when you clear more than\n"
"one row with a single piece, the other player's board is moved up\n"
"and junk rows are added to the bottom. If you clear 2, 3 or 4\n"
"rows, 1, 2 or 4 junk rows are added to your opponent's board,\n"
"respectively. The junk rows have exactly one empty column.\n"
"For each group of junk rows given, the empty columns will line\n"
"up. This is intentional.\n"
"\n"
"The longest surviving player wins the game.\n"
"\n"
"One player mode\n"
"---------------\n"
"This mode is currently very boring, because there's no scoring\n"
"and it never gets any faster. This will be rectified at some point.\n"
"I'm not very motivated to do it right now because I'm sick of one\n"
"player T*tris. For now, use the \"f\" key (by default) to make the\n"
"game go faster. Speedups cannot be reversed for the remainder of\n"
"the game.\n",
version_string);
}
/*
* My really crappy random number generator follows
* Should be more than sufficient for our purposes though
*/
ExtFunc void SRandom(int seed)
{
initSeed = seed;
myRandSeed = seed % 31751 + 1;
}
ExtFunc int Random(int min, int max1)
{
myRandSeed = (myRandSeed * 31751 + 15437) % 32767;
return myRandSeed % (max1 - min) + min;
}
ExtFunc int MyRead(int fd, void *data, int len)
{
int result, left;
left = len;
while (left > 0) {
result = read(fd, data, left);
if (result > 0) {
data = ((char *)data) + result;
left -= result;
}
else if (errno != EINTR)
return result;
}
return len;
}
ExtFunc int MyWrite(int fd, void *data, int len)
{
int result, left;
left = len;
while (left > 0) {
result = write(fd, data, left);
if (result > 0) {
data = ((char *)data) + result;
left -= result;
}
else if (errno != EINTR)
return result;
}
return len;
}
ExtFunc void NormalizeTime(struct timeval *tv)
{
tv->tv_sec += tv->tv_usec / 1000000;
tv->tv_usec %= 1000000;
if (tv->tv_usec < 0) {
tv->tv_usec += 1000000;
--tv->tv_sec;
}
}
ExtFunc void CatchInt(int sig)
{
exit(0);
}
ExtFunc void CatchAlarm(int sig)
{
alarmGen.ready = 1;
signal(SIGALRM, CatchAlarm);
}
static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event)
{
return E_alarm;
}
ExtFunc long CurTimeval(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec -= baseTimeval.tv_sec;
tv.tv_usec -= baseTimeval.tv_usec;
return GetTimeval(&tv);
}
ExtFunc void SetTimeval(struct timeval *tv, long usec)
{
tv->tv_sec = usec / 1000000;
tv->tv_usec = usec % 1000000;
}
ExtFunc long GetTimeval(struct timeval *tv)
{
return tv->tv_sec * 1000000 + tv->tv_usec;
}
ExtFunc void die(char *msg)
{
perror(msg);
exit(1);
}
static long SetITimer1(long interval, long value)
{
struct itimerval it, old;
SetTimeval(&it.it_interval, interval);
SetTimeval(&it.it_value, value);
if (setitimer(ITIMER_REAL, &it, &old) < 0)
die("setitimer");
signal(SIGALRM, CatchAlarm);
return GetTimeval(&old.it_value);
}
ExtFunc long SetITimer(long interval, long value)
{
long old;
old = SetITimer1(0, 0);
alarmGen.ready = 0;
SetITimer1(interval, value);
return old;
}
ExtFunc void fatal(char *msg)
{
CleanupScreens ();
fprintf(stderr, "%s\n", msg);
exit(1);
}
ExtFunc void BlockSignals(MySigSet *saved, ...)
{
MySigSet set;
va_list args;
int sig;
va_start(args, saved);
#ifdef HAS_SIGPROCMASK
sigemptyset(&set);
#else
set = 0;
#endif
while ((sig = va_arg(args, int))) {
#ifdef HAS_SIGPROCMASK
sigaddset(&set, sig);
#else
sig |= sigmask(sig);
#endif
}
#ifdef HAS_SIGPROCMASK
sigprocmask(SIG_BLOCK, &set, saved);
#else
*saved = sigblock(set);
#endif
va_end(args);
}
ExtFunc void RestoreSignals(MySigSet *saved, MySigSet *set)
{
#ifdef HAS_SIGPROCMASK
sigprocmask(SIG_SETMASK, set, saved);
#else
if (saved)
*saved = sigsetmask(*set);
else
sigsetmask(*set);
#endif
}
ExtFunc void AddEventGen(EventGenRec *gen)
{
assert(gen->next == NULL);
gen->next = nextGen->next;
nextGen->next = gen;
}
ExtFunc void RemoveEventGen(EventGenRec *gen)
{
// assert(gen->next != NULL); /* Be more forgiving, for SIGINTs */
if (gen->next) {
while (nextGen->next != gen)
nextGen = nextGen->next;
nextGen->next = gen->next;
gen->next = NULL;
}
}
ExtFunc MyEventType WaitMyEvent(MyEvent *event, int mask)
{
int i, retry = 0;
fd_set fds[FT_len];
EventGenRec *gen;
int result, anyReady, anySet;
struct timeval tv;
/* XXX In certain circumstances, this routine does polling */
for (;;) {
for (i = 0; i < FT_len; ++i)
FD_ZERO(&fds[i]);
anyReady = anySet = 0;
gen = nextGen;
do {
if (gen->mask & mask) {
if (gen->ready)
anyReady = 1;
if (gen->fd >= 0) {
FD_SET(gen->fd, &fds[gen->fdType]);
anySet = 1;
}
}
gen = gen->next;
} while (gen != nextGen);
if (anySet) {
tv.tv_sec = 0;
tv.tv_usec = (retry && !anyReady) ? 500000 : 0;
result = select(FD_SETSIZE, &fds[FT_read], &fds[FT_write],
&fds[FT_except], anyReady ? &tv : NULL);
}
else {
if (retry && !anyReady)
sleep(1);
result = 0;
}
gen = nextGen;
do {
if ((gen->mask & mask)
&& (gen->ready || (result > 0 && gen->fd >= 0
&& FD_ISSET(gen->fd, &fds[gen->fdType])))) {
gen->ready = 0;
event->type = gen->func(gen, event);
if (event->type != E_none) {
nextGen = gen->next;
return event->type;
}
}
gen = gen->next;
} while (gen != nextGen);
retry = 1;
}
}
/*
* vi: ts=4 ai
* vim: noai si
*/