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 $
44 #define TERM_WIDTH 80 /* Assume terminals are 80-char wide */
48 * Move the cursor to the given position on the given player's
52 cgoto(PLAYER *pp, int y, int x)
55 if (pp == ALL_PLAYERS) {
56 for (pp = Player; pp < End_player; pp++)
58 for (pp = Monitor; pp < End_monitor; pp++)
63 if (x == pp->p_curx && y == pp->p_cury)
66 sendcom(pp, MOVE, y, x);
73 * Put out a single character.
76 outch(PLAYER *pp, char ch)
79 if (pp == ALL_PLAYERS) {
80 for (pp = Player; pp < End_player; pp++)
82 for (pp = Monitor; pp < End_monitor; pp++)
87 if (++pp->p_curx >= TERM_WIDTH) {
91 putc(ch, pp->p_output);
96 * Put out a string of the given length.
99 outstr(PLAYER *pp, const char *str, int len)
101 if (pp == ALL_PLAYERS) {
102 for (pp = Player; pp < End_player; pp++)
103 outstr(pp, str, len);
104 for (pp = Monitor; pp < End_monitor; pp++)
105 outstr(pp, str, len);
110 pp->p_cury += (pp->p_curx / TERM_WIDTH);
111 pp->p_curx %= TERM_WIDTH;
113 putc(*str++, pp->p_output);
118 * draw a string at a location on the client.
119 * Cursor doesn't move if the location is invalid
122 outyx(PLAYER *pp, int y, int x, const char *fmt, ...)
129 len = vsnprintf(buf, sizeof(buf), fmt, ap);
133 if (len >= (int)sizeof(buf))
134 len = sizeof(buf) - 1;
135 if (y >= 0 && x >= 0)
138 outstr(pp, buf, len);
143 * Clear the screen, and reset the current position on the screen.
149 if (pp == ALL_PLAYERS) {
150 for (pp = Player; pp < End_player; pp++)
152 for (pp = Monitor; pp < End_monitor; pp++)
164 * Clear to the end of the line
169 sendcom(pp, CLRTOEOL);
174 * Send a command to the given user
177 sendcom(PLAYER *pp, int command, ...)
183 va_start(ap, command);
184 buf[len++] = command;
185 switch (command & 0377) {
187 buf[len++] = va_arg(ap, int);
188 buf[len++] = va_arg(ap, int);
193 buf[len++] = va_arg(ap, int);
198 if (pp == ALL_PLAYERS) {
199 for (pp = Player; pp < End_player; pp++)
200 fwrite(buf, sizeof buf[0], len, pp->p_output);
201 for (pp = Monitor; pp < End_monitor; pp++)
202 fwrite(buf, sizeof buf[0], len, pp->p_output);
205 fwrite(buf, sizeof buf[0], len, pp->p_output);
210 * Flush the output buffer to the player
215 if (pp == ALL_PLAYERS) {
216 for (pp = Player; pp < End_player; pp++)
217 fflush(pp->p_output);
218 for (pp = Monitor; pp < End_monitor; pp++)
219 fflush(pp->p_output);
221 fflush(pp->p_output);
225 logx(int prio, const char *fmt, ...)
231 vsyslog(prio, fmt, ap);
232 else if (conf_logerr)
233 /* if (prio < LOG_NOTICE) */
239 logit(int prio, const char *fmt, ...)
246 strlcpy(fmtm, fmt, sizeof fmtm);
247 strlcat(fmtm, ": %m", sizeof fmtm);
248 vsyslog(prio, fmtm, ap);
249 } else if (conf_logerr)
250 /* if (prio < LOG_NOTICE) */