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
|
/* $OpenBSD: terminal.c,v 1.4 1999/02/01 06:53:56 d Exp $ */
/* $NetBSD: terminal.c,v 1.2 1997/10/10 16:34:05 lukem Exp $ */
/*
* Hunt
* Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
* San Francisco, California
*/
#include <stdarg.h>
#include <syslog.h>
#include <err.h>
#include <string.h>
#include "hunt.h"
#include "server.h"
#include "conf.h"
#define TERM_WIDTH 80 /* Assume terminals are 80-char wide */
/*
* cgoto:
* Move the cursor to the given position on the given player's
* terminal.
*/
void
cgoto(pp, y, x)
PLAYER *pp;
int y, x;
{
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
cgoto(pp, y, x);
for (pp = Monitor; pp < End_monitor; pp++)
cgoto(pp, y, x);
return;
}
if (x == pp->p_curx && y == pp->p_cury)
return;
sendcom(pp, MOVE, y, x);
pp->p_cury = y;
pp->p_curx = x;
}
/*
* outch:
* Put out a single character.
*/
void
outch(pp, ch)
PLAYER *pp;
char ch;
{
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
outch(pp, ch);
for (pp = Monitor; pp < End_monitor; pp++)
outch(pp, ch);
return;
}
if (++pp->p_curx >= TERM_WIDTH) {
pp->p_curx = 0;
pp->p_cury++;
}
(void) putc(ch, pp->p_output);
}
/*
* outstr:
* Put out a string of the given length.
*/
void
outstr(pp, str, len)
PLAYER *pp;
char *str;
int len;
{
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
outstr(pp, str, len);
for (pp = Monitor; pp < End_monitor; pp++)
outstr(pp, str, len);
return;
}
pp->p_curx += len;
pp->p_cury += (pp->p_curx / TERM_WIDTH);
pp->p_curx %= TERM_WIDTH;
while (len--)
(void) putc(*str++, pp->p_output);
}
/*
* outat:
* draw a string at a location on the client.
* Cursor doesn't move if the location is invalid
*/
void
outyx(pp, y, x, fmt)
PLAYER *pp;
int y;
int x;
const char *fmt;
{
va_list ap;
char buf[BUFSIZ];
int len;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof buf, fmt, ap);
if (y >= 0 && x >= 0)
cgoto(pp, y, x);
outstr(pp, buf, len);
va_end(ap);
}
/*
* clrscr:
* Clear the screen, and reset the current position on the screen.
*/
void
clrscr(pp)
PLAYER *pp;
{
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
clrscr(pp);
for (pp = Monitor; pp < End_monitor; pp++)
clrscr(pp);
return;
}
sendcom(pp, CLEAR);
pp->p_cury = 0;
pp->p_curx = 0;
}
/*
* ce:
* Clear to the end of the line
*/
void
ce(pp)
PLAYER *pp;
{
sendcom(pp, CLRTOEOL);
}
/*
* sendcom:
* Send a command to the given user
*/
void
sendcom(pp, command)
PLAYER *pp;
int command;
{
va_list ap;
char buf[3];
int len = 0;
va_start(ap, command);
buf[len++] = command;
switch (command & 0377) {
case MOVE:
buf[len++] = va_arg(ap, int);
buf[len++] = va_arg(ap, int);
break;
case ADDCH:
case READY:
case ENDWIN:
buf[len++] = va_arg(ap, int);
break;
}
va_end(ap);
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
fwrite(buf, sizeof buf[0], len, pp->p_output);
for (pp = Monitor; pp < End_monitor; pp++)
fwrite(buf, sizeof buf[0], len, pp->p_output);
return;
} else
fwrite(buf, sizeof buf[0], len, pp->p_output);
}
/*
* sync:
* Flush the output buffer to the player
*/
void
flush(pp)
PLAYER *pp;
{
if (pp == ALL_PLAYERS) {
for (pp = Player; pp < End_player; pp++)
fflush(pp->p_output);
for (pp = Monitor; pp < End_monitor; pp++)
fflush(pp->p_output);
} else
fflush(pp->p_output);
}
void
logx(prio, fmt)
int prio;
const char *fmt;
{
va_list ap;
va_start(ap, fmt);
if (conf_syslog)
vsyslog(prio, fmt, ap);
else if (conf_logerr)
/* if (prio < LOG_NOTICE) */
vwarnx(fmt, ap);
va_end(fmt);
}
void
log(prio, fmt)
int prio;
const char *fmt;
{
va_list ap;
char fmtm[1024];
va_start(ap, fmt);
if (conf_syslog) {
strlcpy(fmtm, fmt, sizeof fmtm);
strlcat(fmtm, ": %m", sizeof fmtm);
vsyslog(prio, fmtm, ap);
} else if (conf_logerr)
/* if (prio < LOG_NOTICE) */
vwarn(fmt, ap);
va_end(fmt);
}
|