summaryrefslogtreecommitdiff
path: root/games/hunt/huntd/terminal.c
blob: aaf5ec7ae1e831cf42091957b4c950fe6a2a665b (plain)
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
/*	$OpenBSD: terminal.c,v 1.9 2005/04/21 00:10:23 deraadt Exp $	*/
/*	$NetBSD: terminal.c,v 1.2 1997/10/10 16:34:05 lukem Exp $	*/
/*
 * Copyright (c) 1983-2003, Regents of the University of California.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * + Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 * + 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.
 * + Neither the name of the University of California, San Francisco 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 COPYRIGHT HOLDERS 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 COPYRIGHT 
 * OWNER 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.
 */

#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);
	va_end(ap);
	if (len == -1)
		len = 0;
	if (len >= (int)sizeof(buf))
		len = sizeof(buf) - 1;
	if (y >= 0 && x >= 0)
		cgoto(pp, y, x);
	if (len > 0)
		outstr(pp, buf, len);
}

/*
 * 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(ap);
}

void
logit(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(ap);
}