2 * Copyright (c) 1983-2003, Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University of California, San Francisco nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $OpenBSD: terminal.c,v 1.10 2008/06/20 13:08:44 ragge Exp $
32 * $NetBSD: terminal.c,v 1.2 1997/10/10 16:34:05 lukem Exp $
33 * $DragonFly: src/games/hunt/huntd/terminal.c,v 1.2 2008/09/04 16:12:51 swildner Exp $
45 #define TERM_WIDTH 80 /* Assume terminals are 80-char wide */
49 * Move the cursor to the given position on the given player's
53 cgoto(PLAYER *pp, int y, int x)
56 if (pp == ALL_PLAYERS) {
57 for (pp = Player; pp < End_player; pp++)
59 for (pp = Monitor; pp < End_monitor; pp++)
64 if (x == pp->p_curx && y == pp->p_cury)
67 sendcom(pp, MOVE, y, x);
74 * Put out a single character.
77 outch(PLAYER *pp, char ch)
80 if (pp == ALL_PLAYERS) {
81 for (pp = Player; pp < End_player; pp++)
83 for (pp = Monitor; pp < End_monitor; pp++)
88 if (++pp->p_curx >= TERM_WIDTH) {
92 (void) putc(ch, pp->p_output);
97 * Put out a string of the given length.
100 outstr(PLAYER *pp, const char *str, int len)
102 if (pp == ALL_PLAYERS) {
103 for (pp = Player; pp < End_player; pp++)
104 outstr(pp, str, len);
105 for (pp = Monitor; pp < End_monitor; pp++)
106 outstr(pp, str, len);
111 pp->p_cury += (pp->p_curx / TERM_WIDTH);
112 pp->p_curx %= TERM_WIDTH;
114 (void) putc(*str++, pp->p_output);
119 * draw a string at a location on the client.
120 * Cursor doesn't move if the location is invalid
123 outyx(PLAYER *pp, int y, int x, const char *fmt, ...)
130 len = vsnprintf(buf, sizeof(buf), fmt, ap);
134 if (len >= (int)sizeof(buf))
135 len = sizeof(buf) - 1;
136 if (y >= 0 && x >= 0)
139 outstr(pp, buf, len);
144 * Clear the screen, and reset the current position on the screen.
150 if (pp == ALL_PLAYERS) {
151 for (pp = Player; pp < End_player; pp++)
153 for (pp = Monitor; pp < End_monitor; pp++)
165 * Clear to the end of the line
170 sendcom(pp, CLRTOEOL);
175 * Send a command to the given user
178 sendcom(PLAYER *pp, int command, ...)
184 va_start(ap, command);
185 buf[len++] = command;
186 switch (command & 0377) {
188 buf[len++] = va_arg(ap, int);
189 buf[len++] = va_arg(ap, int);
194 buf[len++] = va_arg(ap, int);
199 if (pp == ALL_PLAYERS) {
200 for (pp = Player; pp < End_player; pp++)
201 fwrite(buf, sizeof buf[0], len, pp->p_output);
202 for (pp = Monitor; pp < End_monitor; pp++)
203 fwrite(buf, sizeof buf[0], len, pp->p_output);
206 fwrite(buf, sizeof buf[0], len, pp->p_output);
211 * Flush the output buffer to the player
216 if (pp == ALL_PLAYERS) {
217 for (pp = Player; pp < End_player; pp++)
218 fflush(pp->p_output);
219 for (pp = Monitor; pp < End_monitor; pp++)
220 fflush(pp->p_output);
222 fflush(pp->p_output);
226 logx(int prio, const char *fmt, ...)
232 vsyslog(prio, fmt, ap);
233 else if (conf_logerr)
234 /* if (prio < LOG_NOTICE) */
240 logit(int prio, const char *fmt, ...)
247 strlcpy(fmtm, fmt, sizeof fmtm);
248 strlcat(fmtm, ": %m", sizeof fmtm);
249 vsyslog(prio, fmtm, ap);
250 } else if (conf_logerr)
251 /* if (prio < LOG_NOTICE) */