2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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.
61 #include "prototypes.h"
65 static void pr_player(struct player *pl, int id, char *buf);
66 static void upr_player(struct player *pl, int id, char *buf);
67 static void outid(struct player *pl, int n);
68 static void player_output_some(void);
71 * Print to current player similar to printf().
72 * Use printf-style @format with the optional arguments.
73 * Note: `to print' without further qualifications means sending
83 (void)vsprintf(buf, format, ap);
85 if (player->flags & PF_UTF8)
86 /* normal text needs to be converted to user text */
87 upr_player(player, C_DATA, buf);
89 /* normal text and user text are identical */
90 pr_player(player, C_DATA, buf);
94 * Print UTF-8 text @buf to current player.
101 if (!(player->flags & PF_UTF8)) {
102 p = malloc(strlen(buf) + 1);
103 copy_utf8_to_ascii_no_funny(p, buf);
104 pr_player(player, C_DATA, p);
107 pr_player(player, C_DATA, buf);
111 * Send some text to @p with ID @id, line-buffered.
112 * Format text to send using printf-style @format and optional
113 * arguments. It is assumed to be already user text. Plain ASCII and
114 * text received from the same player are fine, for anything else the
115 * caller has to deal with output filtering.
116 * If a partial line is buffered, terminate it with a newline first.
119 pr_id(struct player *p, int id, char *format, ...)
125 io_puts(p->iop, "\n");
126 journal_output(p, p->curid, "\n");
129 va_start(ap, format);
130 (void)vsprintf(buf, format, ap);
132 pr_player(p, id, buf);
136 * Send C_FLASH text to @pl.
137 * Format text to send using printf-style @format and optional
138 * arguments. It is assumed to be UTF-8.
139 * Initiate an output queue flush, but do not wait for it to complete.
142 pr_flash(struct player *pl, char *format, ...)
144 char buf[4096]; /* UTF-8 */
147 if (pl->state != PS_PLAYING)
149 va_start(ap, format);
150 (void)vsprintf(buf, format, ap);
152 if (!(pl->flags & PF_UTF8))
153 copy_utf8_to_ascii_no_funny(buf, buf);
154 pr_player(pl, C_FLASH, buf);
155 io_output(pl->iop, 0);
159 * Send C_INFORM text to @pl.
160 * Format text to send using printf-style @format and optional
161 * arguments. It is assumed to be plain ASCII.
162 * Initiate an output queue flush, but do not wait for it to complete.
165 pr_inform(struct player *pl, char *format, ...)
170 if (pl->state != PS_PLAYING)
172 va_start(ap, format);
173 (void)vsprintf(buf, format, ap);
175 pr_player(pl, C_INFORM, buf);
176 io_output(pl->iop, 0);
180 * Send C_FLASH text to everyone.
181 * Format text to send using printf-style @format and optional
182 * arguments. It is assumed to be plain ASCII.
183 * Prefix text it with a header suitable for broadcast from deity.
184 * Initiate an output queue flush, but do not wait for it to complete.
187 pr_wall(char *format, ...)
191 char buf[4096]; /* UTF-8 */
197 tm = localtime(&now);
198 n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
199 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
201 va_start(ap, format);
202 (void)vsprintf(buf + n, format, ap);
204 for (p = player_next(NULL); p; p = player_next(p)) {
205 if (p->state != PS_PLAYING)
207 pr_player(p, C_FLASH, buf);
208 io_output(p->iop, 0);
213 * Send @id text @buf to @pl, line-buffered.
215 * If a partial line with different ID is buffered, terminate it with
219 pr_player(struct player *pl, int id, char *buf)
225 journal_output(pl, id, buf);
228 while (*bp != '\0') {
229 if (pl->curid != -1 && pl->curid != id) {
230 io_puts(pl->iop, "\n");
235 p = strchr(bp, '\n');
238 io_write(pl->iop, bp, len);
242 len = io_puts(pl->iop, bp);
248 player_output_some();
252 * Send @id text @buf to @pl, line-buffered.
253 * This function translates from normal text to user text.
254 * If a partial line with different ID is buffered, terminate it with
258 upr_player(struct player *pl, int id, char *buf)
265 journal_output(pl, id, buf);
271 while ((ch = *bp++)) {
272 if (pl->curid != -1 && pl->curid != id) {
273 io_puts(pl->iop, "\n");
282 io_puts(pl->iop, printbuf);
289 io_puts(pl->iop, printbuf);
294 io_write(pl->iop, &ch, 1);
298 io_puts(pl->iop, printbuf);
303 player_output_some();
308 * This runs always at the beginning of a line.
311 outid(struct player *pl, int n)
315 if (CANT_HAPPEN(n > C_LAST))
319 buf[0] = 'a' - 10 + n;
324 io_puts(pl->iop, buf);
329 player_output_some(void)
331 time_t deadline = player_io_deadline(player, 1);
333 while (io_output_if_queue_long(player->iop, deadline) > 0)
338 * Send redirection request @redir to the current player.
339 * @redir is UTF-8, but non-ASCII characters can occur only if the
340 * player sent them. Therefore, it is also user text.
345 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
349 * Send script execute request @file to the current player.
350 * @file is UTF-8, but non-ASCII characters can occur only if the
351 * player sent them. Therefore, it is also user text.
356 pr_id(player, C_EXECUTE, "%s\n", file);
360 * Send a command prompt to the current player.
363 prprompt(int min, int btu)
365 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
369 * Prompt for a line of non-command input.
370 * Send C_FLUSH prompt @prompt to the current player.
371 * Read a line of input into @buf[@size] and convert it to ASCII.
372 * This may block for input, yielding the processor. Flush buffered
373 * output when blocking, to make sure player sees the prompt.
374 * Return number of bytes in @buf[], not counting the terminating 0,
378 prmptrd(char *prompt, char *buf, int size)
382 if (CANT_HAPPEN(!prompt))
385 pr_id(player, C_FLUSH, "%s\n", prompt);
386 if ((r = recvclient(buf, size)) < 0)
388 time(&player->curup);
391 if (player->flags & PF_UTF8)
392 return copy_utf8_to_ascii_no_funny(buf, buf);
393 return copy_ascii_no_funny(buf, buf);
397 * Prompt for a line of non-command, UTF-8 input.
398 * Send C_FLUSH prompt @prompt to the current player.
399 * Read a line of input into @buf[@size], replacing funny characters by
400 * '?'. The result is UTF-8.
401 * This may block for input, yielding the processor. Flush buffered
402 * output when blocking, to make sure player sees the prompt.
403 * Return number of bytes in @buf[], not counting the terminating 0,
407 uprmptrd(char *prompt, char *buf, int size)
411 if (CANT_HAPPEN(!prompt))
414 pr_id(player, C_FLUSH, "%s\n", prompt);
415 if ((r = recvclient(buf, size)) < 0)
417 time(&player->curup);
420 if (player->flags & PF_UTF8)
421 return copy_utf8_no_funny(buf, buf);
422 return copy_ascii_no_funny(buf, buf);
426 * Print the current time in ctime() format.
434 pr("%s", ctime(&now));
438 * Print coordinates @x,@y.
439 * @format must be a printf-style format string that converts exactly
443 prxy(char *format, coord x, coord y)
447 np = getnatp(player->cnum);
448 pr(format, xrel(np, x), yrel(np, y));
452 * Sound the current player's bell.
457 struct natstr *np = getnatp(player->cnum);
459 if (np->nat_flags & NF_BEEP)
464 * Print complete lines to country @cn similar to printf().
465 * Use printf-style @format with the optional arguments. @format must
467 * If @cn is zero, don't print anything.
468 * Else, if @cn is the current player and we're not in the update,
469 * print just like pr(). Else print into a bulletin.
470 * Because printing like pr() requires normal text, and bulletins
471 * require user text, only plain ASCII is allowed.
474 mpr(int cn, char *format, ...)
479 CANT_HAPPEN(!format[0] || format[strlen(format) - 1] != '\n');
482 va_start(ap, format);
483 (void)vsprintf(buf, format, ap);
485 if (update_running || cn != player->cnum)
486 wu(0, cn, "%s", buf);
488 pr_player(player, C_DATA, buf);
492 * Copy @src without funny characters to @dst.
493 * Drop control characters, except for '\t'.
494 * Replace non-ASCII characters by '?'.
495 * Return length of @dst.
496 * @dst must have space. If it overlaps @src, then @dst <= @src must
500 copy_ascii_no_funny(char *dst, char *src)
506 while ((ch = *src++)) {
507 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
508 ; /* ignore funny control */
510 *p++ = '?'; /* replace non-ASCII */
520 * Copy UTF-8 @src without funny characters to @dst.
521 * Drop control characters, except for '\t'.
522 * FIXME Replace malformed UTF-8 sequences by '?'.
523 * Return byte length of @dst.
524 * @dst must have space. If it overlaps @src, then @dst <= @src must
528 copy_utf8_no_funny(char *dst, char *src)
534 while ((ch = *src++)) {
535 /* FIXME do the right thing for malformed and overlong sequences */
536 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
537 ; /* ignore funny control */
547 * Copy UTF-8 @src without funny characters to ASCII @dst.
548 * Drop control characters, except for '\t'.
549 * Replace non-ASCII characters by '?'.
550 * Return length of @dst.
551 * @dst must have space. If it overlaps @src, then @dst <= @src must
555 copy_utf8_to_ascii_no_funny(char *dst, char *src)
561 while ((ch = *src++)) {
562 /* FIXME do the right thing for malformed and overlong sequences */
563 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
564 ; /* ignore funny control */
565 else if (ch > 0x7f) {
566 *p++ = '?'; /* replace non-ASCII */
567 while ((*src & 0xc0) == 0x80)
578 * Return byte-index of the @n-th UTF-8 character in UTF-8 string @s.
579 * If @s doesn't have that many characters, return its length instead.
582 ufindpfx(char *s, int n)
587 if ((s[i++] & 0xc0) == 0xc0)
588 while ((s[i] & 0xc0) == 0x80)