2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * pr.c: Output to players
29 * Known contributors to this file:
30 * Dave Pare, 1986, 1989
31 * Steve McClure, 1998-2000
32 * Ron Koenderink, 2005
33 * Markus Armbruster, 2005-2012
37 * Player output is fully buffered. It can block only if the
38 * receiving player is the current player and his last command doesn't
39 * have the C_MOD flag. Output to another player must not block
40 * because that player could be gone when the printing thread wakes
41 * up, and the code isn't prepared for that. Output within C_MOD
42 * command never blocks, so that such commands can print freely
43 * without yielding the processor.
45 * Each line of output starts with an identification character
46 * encoding the output id, followed by space. Ids less than 10 are
47 * encoded as decimal digits, and larger ids as lower case letters,
48 * starting with 'a'. Symbolic names for ids are defined in proto.h.
63 #include "prototypes.h"
68 static void pr_player(struct player *pl, int id, char *buf);
69 static void upr_player(struct player *pl, int id, char *buf);
70 static void outid(struct player *pl, int n);
73 * Print to current player similar to printf().
74 * Use printf-style FORMAT with the optional arguments.
75 * Note: `to print' without further qualifications means sending
85 (void)vsprintf(buf, format, ap);
87 if (player->flags & PF_UTF8)
88 /* normal text needs to be converted to user text */
89 upr_player(player, C_DATA, buf);
91 /* normal text and user text are identical */
92 pr_player(player, C_DATA, buf);
96 * Print UTF-8 text BUF to current player.
103 if (!(player->flags & PF_UTF8)) {
104 p = malloc(strlen(buf) + 1);
105 copy_utf8_to_ascii_no_funny(p, buf);
106 pr_player(player, C_DATA, p);
109 pr_player(player, C_DATA, buf);
113 * Send some text to P with id ID, line-buffered.
114 * Format text to send using printf-style FORMAT and optional
115 * arguments. It is assumed to be already user text. Plain ASCII and
116 * text received from the same player are fine, for anything else the
117 * caller has to deal with output filtering.
118 * If a partial line is buffered, terminate it with a newline first.
121 pr_id(struct player *p, int id, char *format, ...)
127 io_puts(p->iop, "\n");
128 journal_output(p, p->curid, "\n");
131 va_start(ap, format);
132 (void)vsprintf(buf, format, ap);
134 pr_player(p, id, buf);
138 * Send C_FLASH text to PL.
139 * Format text to send using printf-style FORMAT and optional
140 * arguments. It is assumed to be UTF-8.
141 * Initiate an output queue flush, but do not wait for it to complete.
144 pr_flash(struct player *pl, char *format, ...)
146 char buf[4096]; /* UTF-8 */
149 if (pl->state != PS_PLAYING)
151 va_start(ap, format);
152 (void)vsprintf(buf, format, ap);
154 if (!(pl->flags & PF_UTF8))
155 copy_utf8_to_ascii_no_funny(buf, buf);
156 pr_player(pl, C_FLASH, buf);
157 io_output(pl->iop, 0);
161 * Send C_INFORM text to PL.
162 * Format text to send using printf-style FORMAT and optional
163 * arguments. It is assumed to be plain ASCII.
164 * Initiate an output queue flush, but do not wait for it to complete.
167 pr_inform(struct player *pl, char *format, ...)
172 if (pl->state != PS_PLAYING)
174 va_start(ap, format);
175 (void)vsprintf(buf, format, ap);
177 pr_player(pl, C_INFORM, buf);
178 io_output(pl->iop, 0);
182 * Send C_FLASH text to everyone.
183 * Format text to send using printf-style FORMAT and optional
184 * arguments. It is assumed to be plain ASCII.
185 * Prefix text it with a header suitable for broadcast from deity.
186 * Initiate an output queue flush, but do not wait for it to complete.
189 pr_wall(char *format, ...)
193 char buf[4096]; /* UTF-8 */
199 tm = localtime(&now);
200 n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
201 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
203 va_start(ap, format);
204 (void)vsprintf(buf + n, format, ap);
206 for (p = player_next(NULL); p; p = player_next(p)) {
207 if (p->state != PS_PLAYING)
209 pr_player(p, C_FLASH, buf);
210 io_output(p->iop, 0);
215 * Send ID text BUF to PL, line-buffered.
217 * If a partial line with different id is buffered, terminate it with
221 pr_player(struct player *pl, int id, char *buf)
228 journal_output(pl, id, buf);
231 while (*bp != '\0') {
232 if (pl->curid != -1 && pl->curid != id) {
233 io_puts(pl->iop, "\n");
238 p = strchr(bp, '\n');
241 io_write(pl->iop, bp, len);
245 len = io_puts(pl->iop, bp);
251 deadline = (time_t)(pl->may_sleep == PLAYER_SLEEP_FREELY
253 while (io_output_if_queue_long(pl->iop, deadline) > 0)
259 * Send ID text BUF to PL, line-buffered.
260 * This function translates from normal text to user text.
261 * If a partial line with different id is buffered, terminate it with
265 upr_player(struct player *pl, int id, char *buf)
273 journal_output(pl, id, buf);
279 while ((ch = *bp++)) {
280 if (pl->curid != -1 && pl->curid != id) {
281 io_puts(pl->iop, "\n");
290 io_puts(pl->iop, printbuf);
297 io_puts(pl->iop, printbuf);
302 io_write(pl->iop, &ch, 1);
306 io_puts(pl->iop, printbuf);
311 deadline = (time_t)(pl->may_sleep == PLAYER_SLEEP_FREELY
313 while (io_output_if_queue_long(pl->iop, deadline) > 0)
320 * This runs always at the beginning of a line.
323 outid(struct player *pl, int n)
327 if (CANT_HAPPEN(n > C_LAST))
331 buf[0] = 'a' - 10 + n;
336 io_puts(pl->iop, buf);
341 * Send redirection request REDIR to the current player.
342 * REDIR is UTF-8, but non-ASCII characters can occur only if the
343 * player sent them. Therefore, it is also user text.
348 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
352 * Send script execute request FILE to the current player.
353 * FILE is UTF-8, but non-ASCII characters can occur only if the
354 * player sent them. Therefore, it is also user text.
359 pr_id(player, C_EXECUTE, "%s\n", file);
363 * Send a command prompt to the current player.
366 prprompt(int min, int btu)
368 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
372 * Prompt for a line of non-command input.
373 * Send C_FLUSH prompt PROMPT to the current player.
374 * Read a line of input into BUF[SIZE] and convert it to ASCII.
375 * This may block for input, yielding the processor. Flush buffered
376 * output when blocking, to make sure player sees the prompt.
377 * Return number of bytes in BUF[], not counting the terminating 0,
381 prmptrd(char *prompt, char *buf, int size)
385 if (CANT_HAPPEN(!prompt))
388 pr_id(player, C_FLUSH, "%s\n", prompt);
389 if ((r = recvclient(buf, size)) < 0)
391 time(&player->curup);
394 if (player->flags & PF_UTF8)
395 return copy_utf8_to_ascii_no_funny(buf, buf);
396 return copy_ascii_no_funny(buf, buf);
400 * Prompt for a line of non-command, UTF-8 input.
401 * Send C_FLUSH prompt PROMPT to the current player.
402 * Read a line of input into BUF[SIZE], replacing funny characters by
403 * '?'. The result is UTF-8.
404 * This may block for input, yielding the processor. Flush buffered
405 * output when blocking, to make sure player sees the prompt.
406 * Return number of bytes in BUF[], not counting the terminating 0,
410 uprmptrd(char *prompt, char *buf, int size)
414 if (CANT_HAPPEN(!prompt))
417 pr_id(player, C_FLUSH, "%s\n", prompt);
418 if ((r = recvclient(buf, size)) < 0)
420 time(&player->curup);
423 if (player->flags & PF_UTF8)
424 return copy_utf8_no_funny(buf, buf);
425 return copy_ascii_no_funny(buf, buf);
429 * Print the current time in ctime() format.
441 * Print coordinates X, Y.
442 * FORMAT must be a printf-style format string that converts exactly
446 prxy(char *format, coord x, coord y)
450 np = getnatp(player->cnum);
451 pr(format, xrel(np, x), yrel(np, y));
455 * Sound the current player's bell.
460 struct natstr *np = getnatp(player->cnum);
462 if (np->nat_flags & NF_BEEP)
467 * Print complete lines to country CN similar to printf().
468 * Use printf-style FORMAT with the optional arguments. FORMAT must
470 * If CN is zero, don't print anything.
471 * Else, if CN is the current player and we're not in the update,
472 * print just like pr(). Else print into a bulletin.
473 * Because printing like pr() requires normal text, and bulletins
474 * require user text, only plain ASCII is allowed.
477 mpr(int cn, char *format, ...)
482 CANT_HAPPEN(!format[0] || format[strlen(format) - 1] != '\n');
485 va_start(ap, format);
486 (void)vsprintf(buf, format, ap);
488 if (update_running || cn != player->cnum)
489 wu(0, cn, "%s", buf);
491 pr_player(player, C_DATA, buf);
495 * Copy SRC without funny characters to DST.
496 * Drop control characters, except for '\t'.
497 * Replace non-ASCII characters by '?'.
498 * Return length of DST.
499 * DST must have space. If it overlaps SRC, then DST <= SRC must
503 copy_ascii_no_funny(char *dst, char *src)
509 while ((ch = *src++)) {
510 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
511 ; /* ignore funny control */
513 *p++ = '?'; /* replace non-ASCII */
523 * Copy UTF-8 SRC without funny characters to DST.
524 * Drop control characters, except for '\t'.
525 * FIXME Replace malformed UTF-8 sequences by '?'.
526 * Return byte length of DST.
527 * DST must have space. If it overlaps SRC, then DST <= SRC must
531 copy_utf8_no_funny(char *dst, char *src)
537 while ((ch = *src++)) {
538 /* FIXME do the right thing for malformed and overlong sequences */
539 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
540 ; /* ignore funny control */
550 * Copy UTF-8 SRC without funny characters to ASCII DST.
551 * Drop control characters, except for '\t'.
552 * Replace non-ASCII characters by '?'.
553 * Return length of DST.
554 * DST must have space. If it overlaps SRC, then DST <= SRC must
558 copy_utf8_to_ascii_no_funny(char *dst, char *src)
564 while ((ch = *src++)) {
565 /* FIXME do the right thing for malformed and overlong sequences */
566 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
567 ; /* ignore funny control */
568 else if (ch > 0x7f) {
569 *p++ = '?'; /* replace non-ASCII */
570 while ((*src & 0xc0) == 0x80)
581 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
582 * If S doesn't have that many characters, return its length instead.
585 ufindpfx(char *s, int n)
590 if ((s[i++] & 0xc0) == 0xc0)
591 while ((s[i] & 0xc0) == 0x80)