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);
71 static void player_output_some(void);
74 * Print to current player similar to printf().
75 * Use printf-style FORMAT with the optional arguments.
76 * Note: `to print' without further qualifications means sending
86 (void)vsprintf(buf, format, ap);
88 if (player->flags & PF_UTF8)
89 /* normal text needs to be converted to user text */
90 upr_player(player, C_DATA, buf);
92 /* normal text and user text are identical */
93 pr_player(player, C_DATA, buf);
97 * Print UTF-8 text BUF to current player.
104 if (!(player->flags & PF_UTF8)) {
105 p = malloc(strlen(buf) + 1);
106 copy_utf8_to_ascii_no_funny(p, buf);
107 pr_player(player, C_DATA, p);
110 pr_player(player, C_DATA, buf);
114 * Send some text to P with id ID, line-buffered.
115 * Format text to send using printf-style FORMAT and optional
116 * arguments. It is assumed to be already user text. Plain ASCII and
117 * text received from the same player are fine, for anything else the
118 * caller has to deal with output filtering.
119 * If a partial line is buffered, terminate it with a newline first.
122 pr_id(struct player *p, int id, char *format, ...)
128 io_puts(p->iop, "\n");
129 journal_output(p, p->curid, "\n");
132 va_start(ap, format);
133 (void)vsprintf(buf, format, ap);
135 pr_player(p, id, buf);
139 * Send C_FLASH text to PL.
140 * Format text to send using printf-style FORMAT and optional
141 * arguments. It is assumed to be UTF-8.
142 * Initiate an output queue flush, but do not wait for it to complete.
145 pr_flash(struct player *pl, char *format, ...)
147 char buf[4096]; /* UTF-8 */
150 if (pl->state != PS_PLAYING)
152 va_start(ap, format);
153 (void)vsprintf(buf, format, ap);
155 if (!(pl->flags & PF_UTF8))
156 copy_utf8_to_ascii_no_funny(buf, buf);
157 pr_player(pl, C_FLASH, buf);
158 io_output(pl->iop, 0);
162 * Send C_INFORM text to PL.
163 * Format text to send using printf-style FORMAT and optional
164 * arguments. It is assumed to be plain ASCII.
165 * Initiate an output queue flush, but do not wait for it to complete.
168 pr_inform(struct player *pl, char *format, ...)
173 if (pl->state != PS_PLAYING)
175 va_start(ap, format);
176 (void)vsprintf(buf, format, ap);
178 pr_player(pl, C_INFORM, buf);
179 io_output(pl->iop, 0);
183 * Send C_FLASH text to everyone.
184 * Format text to send using printf-style FORMAT and optional
185 * arguments. It is assumed to be plain ASCII.
186 * Prefix text it with a header suitable for broadcast from deity.
187 * Initiate an output queue flush, but do not wait for it to complete.
190 pr_wall(char *format, ...)
194 char buf[4096]; /* UTF-8 */
200 tm = localtime(&now);
201 n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
202 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
204 va_start(ap, format);
205 (void)vsprintf(buf + n, format, ap);
207 for (p = player_next(NULL); p; p = player_next(p)) {
208 if (p->state != PS_PLAYING)
210 pr_player(p, C_FLASH, buf);
211 io_output(p->iop, 0);
216 * Send ID text BUF to PL, line-buffered.
218 * If a partial line with different id is buffered, terminate it with
222 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 player_output_some();
255 * Send ID text BUF to PL, line-buffered.
256 * This function translates from normal text to user text.
257 * If a partial line with different id is buffered, terminate it with
261 upr_player(struct player *pl, int id, char *buf)
268 journal_output(pl, id, buf);
274 while ((ch = *bp++)) {
275 if (pl->curid != -1 && pl->curid != id) {
276 io_puts(pl->iop, "\n");
285 io_puts(pl->iop, printbuf);
292 io_puts(pl->iop, printbuf);
297 io_write(pl->iop, &ch, 1);
301 io_puts(pl->iop, printbuf);
306 player_output_some();
311 * This runs always at the beginning of a line.
314 outid(struct player *pl, int n)
318 if (CANT_HAPPEN(n > C_LAST))
322 buf[0] = 'a' - 10 + n;
327 io_puts(pl->iop, buf);
332 player_output_some(void)
336 deadline = (time_t)(player->may_sleep == PLAYER_SLEEP_FREELY ? -1 : 0);
337 while (io_output_if_queue_long(player->iop, deadline) > 0)
342 * Send redirection request REDIR to the current player.
343 * REDIR is UTF-8, but non-ASCII characters can occur only if the
344 * player sent them. Therefore, it is also user text.
349 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
353 * Send script execute request FILE to the current player.
354 * FILE is UTF-8, but non-ASCII characters can occur only if the
355 * player sent them. Therefore, it is also user text.
360 pr_id(player, C_EXECUTE, "%s\n", file);
364 * Send a command prompt to the current player.
367 prprompt(int min, int btu)
369 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
373 * Prompt for a line of non-command input.
374 * Send C_FLUSH prompt PROMPT to the current player.
375 * Read a line of input into BUF[SIZE] and convert it to ASCII.
376 * This may block for input, yielding the processor. Flush buffered
377 * output when blocking, to make sure player sees the prompt.
378 * Return number of bytes in BUF[], not counting the terminating 0,
382 prmptrd(char *prompt, char *buf, int size)
386 if (CANT_HAPPEN(!prompt))
389 pr_id(player, C_FLUSH, "%s\n", prompt);
390 if ((r = recvclient(buf, size)) < 0)
392 time(&player->curup);
395 if (player->flags & PF_UTF8)
396 return copy_utf8_to_ascii_no_funny(buf, buf);
397 return copy_ascii_no_funny(buf, buf);
401 * Prompt for a line of non-command, UTF-8 input.
402 * Send C_FLUSH prompt PROMPT to the current player.
403 * Read a line of input into BUF[SIZE], replacing funny characters by
404 * '?'. The result is UTF-8.
405 * This may block for input, yielding the processor. Flush buffered
406 * output when blocking, to make sure player sees the prompt.
407 * Return number of bytes in BUF[], not counting the terminating 0,
411 uprmptrd(char *prompt, char *buf, int size)
415 if (CANT_HAPPEN(!prompt))
418 pr_id(player, C_FLUSH, "%s\n", prompt);
419 if ((r = recvclient(buf, size)) < 0)
421 time(&player->curup);
424 if (player->flags & PF_UTF8)
425 return copy_utf8_no_funny(buf, buf);
426 return copy_ascii_no_funny(buf, buf);
430 * Print the current time in ctime() format.
442 * Print coordinates X, Y.
443 * FORMAT must be a printf-style format string that converts exactly
447 prxy(char *format, coord x, coord y)
451 np = getnatp(player->cnum);
452 pr(format, xrel(np, x), yrel(np, y));
456 * Sound the current player's bell.
461 struct natstr *np = getnatp(player->cnum);
463 if (np->nat_flags & NF_BEEP)
468 * Print complete lines to country CN similar to printf().
469 * Use printf-style FORMAT with the optional arguments. FORMAT must
471 * If CN is zero, don't print anything.
472 * Else, if CN is the current player and we're not in the update,
473 * print just like pr(). Else print into a bulletin.
474 * Because printing like pr() requires normal text, and bulletins
475 * require user text, only plain ASCII is allowed.
478 mpr(int cn, char *format, ...)
483 CANT_HAPPEN(!format[0] || format[strlen(format) - 1] != '\n');
486 va_start(ap, format);
487 (void)vsprintf(buf, format, ap);
489 if (update_running || cn != player->cnum)
490 wu(0, cn, "%s", buf);
492 pr_player(player, C_DATA, buf);
496 * Copy SRC without funny characters to DST.
497 * Drop control characters, except for '\t'.
498 * Replace non-ASCII characters by '?'.
499 * Return length of DST.
500 * DST must have space. If it overlaps SRC, then DST <= SRC must
504 copy_ascii_no_funny(char *dst, char *src)
510 while ((ch = *src++)) {
511 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
512 ; /* ignore funny control */
514 *p++ = '?'; /* replace non-ASCII */
524 * Copy UTF-8 SRC without funny characters to DST.
525 * Drop control characters, except for '\t'.
526 * FIXME Replace malformed UTF-8 sequences by '?'.
527 * Return byte length of DST.
528 * DST must have space. If it overlaps SRC, then DST <= SRC must
532 copy_utf8_no_funny(char *dst, char *src)
538 while ((ch = *src++)) {
539 /* FIXME do the right thing for malformed and overlong sequences */
540 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
541 ; /* ignore funny control */
551 * Copy UTF-8 SRC without funny characters to ASCII DST.
552 * Drop control characters, except for '\t'.
553 * Replace non-ASCII characters by '?'.
554 * Return length of DST.
555 * DST must have space. If it overlaps SRC, then DST <= SRC must
559 copy_utf8_to_ascii_no_funny(char *dst, char *src)
565 while ((ch = *src++)) {
566 /* FIXME do the right thing for malformed and overlong sequences */
567 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
568 ; /* ignore funny control */
569 else if (ch > 0x7f) {
570 *p++ = '?'; /* replace non-ASCII */
571 while ((*src & 0xc0) == 0x80)
582 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
583 * If S doesn't have that many characters, return its length instead.
586 ufindpfx(char *s, int n)
591 if ((s[i++] & 0xc0) == 0xc0)
592 while ((s[i] & 0xc0) == 0x80)