2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * pr.c: Output to players
30 * Known contributors to this file:
31 * Dave Pare, 1986, 1989
32 * Steve McClure, 1998-2000
33 * Ron Koenderink, 2005
34 * Markus Armbruster, 2005-2007
38 * Player output is fully buffered. It can block only if the
39 * receiving player is the current player and his last command doesn't
40 * have the C_MOD flag. Output to another player must not block
41 * because that player could be gone when the printing thread wakes
42 * up, and the code isn't prepared for that. Output within C_MOD
43 * command never blocks, so that such commands can print freely
44 * without yielding the processor.
46 * Each line of output starts with an identification character
47 * encoding the output id, followed by space. Ids less than 10 are
48 * encoded as decimal digits, and larger ids as lower case letters,
49 * starting with 'a'. Symbolic names for ids are defined in proto.h.
64 #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");
130 va_start(ap, format);
131 (void)vsprintf(buf, format, ap);
133 pr_player(p, id, buf);
137 * Send C_FLASH text to PL.
138 * Format text to send using printf-style FORMAT and optional
139 * arguments. It is assumed to be UTF-8.
140 * Initiate an output queue flush, but do not wait for it to complete.
143 pr_flash(struct player *pl, char *format, ...)
145 char buf[4096]; /* UTF-8 */
148 if (pl->state != PS_PLAYING)
150 va_start(ap, format);
151 (void)vsprintf(buf, format, ap);
153 if (!(pl->flags & PF_UTF8))
154 copy_utf8_to_ascii_no_funny(buf, buf);
155 pr_player(pl, C_FLASH, buf);
156 io_output(pl->iop, IO_NOWAIT);
160 * Send C_INFORM text to PL.
161 * Format text to send using printf-style FORMAT and optional
162 * arguments. It is assumed to be plain ASCII.
163 * Initiate an output queue flush, but do not wait for it to complete.
166 pr_inform(struct player *pl, char *format, ...)
171 if (pl->state != PS_PLAYING)
173 va_start(ap, format);
174 (void)vsprintf(buf, format, ap);
176 pr_player(pl, C_INFORM, buf);
177 io_output(pl->iop, IO_NOWAIT);
181 * Send C_FLASH text to everyone.
182 * Format text to send using printf-style FORMAT and optional
183 * arguments. It is assumed to be plain ASCII.
184 * Prefix text it with a header suitable for broadcast from deity.
185 * Initiate an output queue flush, but do not wait for it to complete.
188 pr_wall(char *format, ...)
192 char buf[4096]; /* UTF-8 */
198 tm = localtime(&now);
199 n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
200 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
202 va_start(ap, format);
203 (void)vsprintf(buf + n, format, ap);
205 for (p = player_next(0); p; p = player_next(p)) {
206 if (p->state != PS_PLAYING)
208 pr_player(p, C_FLASH, buf);
209 io_output(p->iop, IO_NOWAIT);
214 * Send ID text BUF to PL, line-buffered.
216 * If a partial line with different id is buffered, terminate it with
220 pr_player(struct player *pl, int id, char *buf)
227 while (*bp != '\0') {
228 if (pl->curid != -1 && pl->curid != id) {
229 io_puts(pl->iop, "\n");
234 p = strchr(bp, '\n');
237 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
239 io_write(pl->iop, bp, len, IO_NOWAIT);
241 io_write(pl->iop, bp, len, IO_WAIT);
245 len = io_puts(pl->iop, bp);
249 journal_output(pl, id, buf);
253 * Send ID text BUF to PL, line-buffered.
254 * This function translates from normal text to user text.
255 * If a partial line with different id is buffered, terminate it with
259 upr_player(struct player *pl, int id, char *buf)
270 while ((ch = *bp++)) {
271 if (pl->curid != -1 && pl->curid != id) {
272 io_puts(pl->iop, "\n");
281 io_puts(pl->iop, printbuf);
288 io_puts(pl->iop, printbuf);
293 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
295 io_write(pl->iop, &ch, 1, IO_NOWAIT);
297 io_write(pl->iop, &ch, 1, IO_WAIT);
301 io_puts(pl->iop, printbuf);
304 journal_output(pl, id, buf);
309 * This runs always at the beginning of a line.
312 outid(struct player *pl, int n)
316 if (CANT_HAPPEN(n > C_LAST))
320 buf[0] = 'a' - 10 + n;
325 io_puts(pl->iop, buf);
330 * Send redirection request REDIR to the current player.
331 * REDIR is UTF-8, but non-ASCII characters can occur only if the
332 * player sent them. Therefore, it is also user text.
337 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
341 * Send script execute request FILE 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, C_EXECUTE, "%s\n", file);
352 * Send a command prompt to the current player.
355 prprompt(int min, int btu)
357 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
361 * Prompt for a line of non-command input.
362 * Send C_FLUSH prompt PROMPT to the current player.
363 * Read a line of input into BUF[SIZE] and convert it to ASCII.
364 * This may block for input, yielding the processor. Flush buffered
365 * output when blocking, to make sure player sees the prompt.
366 * Return number of bytes in BUF[], not counting the terminating 0,
370 prmptrd(char *prompt, char *buf, int size)
374 if (CANT_HAPPEN(!prompt))
377 pr_id(player, C_FLUSH, "%s\n", prompt);
378 if ((r = recvclient(buf, size)) < 0)
380 time(&player->curup);
383 if (player->flags & PF_UTF8)
384 return copy_utf8_to_ascii_no_funny(buf, buf);
385 return copy_ascii_no_funny(buf, buf);
389 * Prompt for a line of non-command, UTF-8 input.
390 * Send C_FLUSH prompt PROMPT to the current player.
391 * Read a line of input into BUF[SIZE], replacing funny characters by
392 * '?'. The result is UTF-8.
393 * This may block for input, yielding the processor. Flush buffered
394 * output when blocking, to make sure player sees the prompt.
395 * Return number of bytes in BUF[], not counting the terminating 0,
399 uprmptrd(char *prompt, char *buf, int size)
403 if (CANT_HAPPEN(!prompt))
406 pr_id(player, C_FLUSH, "%s\n", prompt);
407 if ((r = recvclient(buf, size)) < 0)
409 time(&player->curup);
412 if (player->flags & PF_UTF8)
413 return copy_utf8_no_funny(buf, buf);
414 return copy_ascii_no_funny(buf, buf);
418 * Print the current time in ctime() format.
430 * Print coordinates X, Y for COUNTRY.
431 * FORMAT must be a printf-style format string that converts exactly
435 prxy(char *format, coord x, coord y, natid country)
439 np = getnatp(country);
440 pr(format, xrel(np, x), yrel(np, y));
444 * Print to country CN similar to printf().
445 * Use printf-style FORMAT with the optional arguments.
446 * Output is buffered until a newline arrives.
447 * If CN is the current player and we're not in the update, print just
448 * like pr(). Else print into a bulletin.
449 * Because printing like pr() requires normal text, and bulletins
450 * require user text, only plain ASCII is allowed.
453 PR(int cn, char *format, ...)
455 /* XXX should really do this on a per-nation basis */
456 static char longline[MAXNOC][512];
461 va_start(ap, format);
462 (void)vsprintf(buf, format, ap);
464 newline = strrchr(buf, '\n') ? 1 : 0;
465 strcat(longline[cn], buf);
467 if (update_running || (cn && cn != player->cnum))
468 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
470 pr_player(player, C_DATA, longline[cn]);
471 longline[cn][0] = '\0';
476 * Print the current time in ctime() format to country CN.
477 * If CN is the current player and we're not in the update, print just
478 * like prdate(). Else print into a bulletin.
490 * Sound the current player's bell.
495 struct natstr *np = getnatp(player->cnum);
497 if (np->nat_flags & NF_BEEP)
502 * Print to country CN similar to printf().
503 * Use printf-style FORMAT with the optional arguments.
504 * If CN is zero, don't print anything.
505 * Else, if CN is the current player and we're not in the update,
506 * print just like pr(). Else print into a bulletin.
507 * Because printing like pr() requires normal text, and bulletins
508 * require user text, only plain ASCII is allowed.
511 mpr(int cn, char *format, ...)
518 va_start(ap, format);
519 (void)vsprintf(buf, format, ap);
521 if (update_running || cn != player->cnum)
522 typed_wu(0, cn, buf, TEL_BULLETIN);
524 pr_player(player, C_DATA, buf);
528 * Copy SRC without funny characters to DST.
529 * Drop control characters, except for '\t'.
530 * Replace non-ASCII characters by '?'.
531 * Return length of DST.
532 * DST must have space. If it overlaps SRC, then DST <= SRC must
536 copy_ascii_no_funny(char *dst, char *src)
542 while ((ch = *src++)) {
543 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
544 ; /* ignore funny control */
546 *p++ = '?'; /* replace non-ASCII */
556 * Copy UTF-8 SRC without funny characters to DST.
557 * Drop control characters, except for '\t'.
558 * FIXME Replace malformed UTF-8 sequences by '?'.
559 * Return byte length of DST.
560 * DST must have space. If it overlaps SRC, then DST <= SRC must
564 copy_utf8_no_funny(char *dst, char *src)
570 while ((ch = *src++)) {
571 /* FIXME do the right thing for malformed and overlong sequences */
572 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
573 ; /* ignore funny control */
583 * Copy UTF-8 SRC without funny characters to ASCII DST.
584 * Drop control characters, except for '\t'.
585 * Replace non-ASCII characters by '?'.
586 * Return length of DST.
587 * DST must have space. If it overlaps SRC, then DST <= SRC must
591 copy_utf8_to_ascii_no_funny(char *dst, char *src)
597 while ((ch = *src++)) {
598 /* FIXME do the right thing for malformed and overlong sequences */
599 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
600 ; /* ignore funny control */
601 else if (ch > 0x7f) {
602 *p++ = '?'; /* replace non-ASCII */
603 while ((*src++ & 0xc0) == 0x80) ;
613 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
614 * If S doesn't have that many characters, return its length instead.
617 ufindpfx(char *s, int n)
623 if ((s[i++] & 0xc0) == 0xc0)
624 while ((s[i] & 0xc0) == 0x80)