2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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
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.
65 #include "prototypes.h"
69 static void pr_player(struct player *pl, int id, char *buf);
70 static void upr_player(struct player *pl, int id, char *buf);
71 static void outid(struct player *pl, int n);
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");
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, IO_NOWAIT);
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, IO_NOWAIT);
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(0); p; p = player_next(p)) {
207 if (p->state != PS_PLAYING)
209 pr_player(p, C_FLASH, buf);
210 io_output(p->iop, IO_NOWAIT);
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 while (*bp != '\0') {
229 if (pl->curid != -1 && pl->curid != id) {
230 io_puts(pl->iop, "\n");
235 p = strchr(bp, '\n');
238 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
240 io_write(pl->iop, bp, len, IO_NOWAIT);
242 io_write(pl->iop, bp, len, IO_WAIT);
246 len = io_puts(pl->iop, bp);
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);
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 * Send redirection request REDIR to the current player.
330 * REDIR is UTF-8, but non-ASCII characters can occur only if the
331 * player sent them. Therefore, it is also user text.
336 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
340 * Send script execute request FILE to the current player.
341 * REDIR is UTF-8, but non-ASCII characters can occur only if the
342 * player sent them. Therefore, it is also user text.
347 pr_id(player, C_EXECUTE, "%s\n", file);
351 * Send a command prompt to the current player.
354 prprompt(int min, int btu)
356 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
360 * Prompt for a line of non-command input.
361 * Send C_FLUSH prompt PROMPT to the current player.
362 * Read a line of input into BUF[SIZE] and convert it to ASCII.
363 * This may block for input, yielding the processor. Flush buffered
364 * output when blocking, to make sure player sees the prompt.
365 * Return number of bytes in BUF[], not counting the terminating 0,
369 prmptrd(char *prompt, char *buf, int size)
374 * Each prompt must consume one line of input. recvclient()
375 * doesn't do that while player->aborted.
377 CANT_HAPPEN(player->aborted);
379 if (CANT_HAPPEN(!prompt))
382 pr_id(player, C_FLUSH, "%s\n", prompt);
383 if ((r = recvclient(buf, size)) < 0)
385 time(&player->curup);
388 if (player->flags & PF_UTF8)
389 return copy_utf8_to_ascii_no_funny(buf, buf);
390 return copy_ascii_no_funny(buf, buf);
394 * Prompt for a line of non-command, UTF-8 input.
395 * Send C_FLUSH prompt PROMPT to the current player.
396 * Read a line of input into BUF[SIZE], replacing funny characters by
397 * '?'. The result is UTF-8.
398 * This may block for input, yielding the processor. Flush buffered
399 * output when blocking, to make sure player sees the prompt.
400 * Return number of bytes in BUF[], not counting the terminating 0,
404 uprmptrd(char *prompt, char *buf, int size)
409 CANT_HAPPEN(player->aborted);
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.
438 * Print coordinates X, Y for COUNTRY.
439 * FORMAT must be a printf-style format string that converts exactly
443 prxy(char *format, coord x, coord y, natid country)
447 np = getnatp(country);
448 pr(format, xrel(np, x), yrel(np, y));
452 * Print to country CN similar to printf().
453 * Use printf-style FORMAT with the optional arguments.
454 * Output is buffered until a newline arrives.
455 * If CN is the current player and we're not in the update, print just
456 * like pr(). Else print into a bulletin.
457 * Because printing like pr() requires normal text, and bulletins
458 * require user text, only plain ASCII is allowed.
461 PR(int cn, char *format, ...)
463 /* XXX should really do this on a per-nation basis */
464 static char longline[MAXNOC][512];
469 va_start(ap, format);
470 (void)vsprintf(buf, format, ap);
472 newline = strrchr(buf, '\n') ? 1 : 0;
473 strcat(longline[cn], buf);
475 if (update_pending || (cn && cn != player->cnum))
476 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
478 pr_player(player, C_DATA, longline[cn]);
479 longline[cn][0] = '\0';
484 * Print the current time in ctime() format to country CN.
485 * If CN is the current player and we're not in the update, print just
486 * like prdate(). Else print into a bulletin.
498 * Sound the current player's bell.
503 struct natstr *np = getnatp(player->cnum);
505 if (np->nat_flags & NF_BEEP)
510 * Print to country CN similar to printf().
511 * Use printf-style FORMAT with the optional arguments.
512 * If CN is the current player and we're not in the update, print just
513 * like pr(). Else print into a bulletin.
514 * Because printing like pr() requires normal text, and bulletins
515 * require user text, only plain ASCII is allowed.
518 mpr(int cn, char *format, ...)
523 va_start(ap, format);
524 (void)vsprintf(buf, format, ap);
527 if (update_pending || cn != player->cnum)
528 typed_wu(0, cn, buf, TEL_BULLETIN);
530 pr_player(player, C_DATA, buf);
535 * Copy SRC without funny characters to DST.
536 * Drop control characters, except for '\t'.
537 * Replace non-ASCII characters by '?'.
538 * Return length of DST.
539 * DST must have space. If it overlaps SRC, then DST <= SRC must
543 copy_ascii_no_funny(char *dst, char *src)
549 while ((ch = *src++)) {
550 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
551 ; /* ignore funny control */
553 *p++ = '?'; /* replace non-ASCII */
563 * Copy UTF-8 SRC without funny characters to DST.
564 * Drop control characters, except for '\t'.
565 * FIXME Replace malformed UTF-8 sequences by '?'.
566 * Return byte length of DST.
567 * DST must have space. If it overlaps SRC, then DST <= SRC must
571 copy_utf8_no_funny(char *dst, char *src)
577 while ((ch = *src++)) {
578 /* FIXME do the right thing for malformed and overlong sequences */
579 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
580 ; /* ignore funny control */
590 * Copy UTF-8 SRC without funny characters to ASCII DST.
591 * Drop control characters, except for '\t'.
592 * Replace non-ASCII characters by '?'.
593 * Return length of DST.
594 * DST must have space. If it overlaps SRC, then DST <= SRC must
598 copy_utf8_to_ascii_no_funny(char *dst, char *src)
604 while ((ch = *src++)) {
605 /* FIXME do the right thing for malformed and overlong sequences */
606 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
607 ; /* ignore funny control */
608 else if (ch > 0x7f) {
609 *p++ = '?'; /* replace non-ASCII */
610 while ((*src++ & 0xc0) == 0x80) ;
620 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
621 * If S doesn't have that many characters, return its length instead.
624 ufindpfx(char *s, int n)
630 if ((s[i++] & 0xc0) == 0xc0)
631 while ((s[i] & 0xc0) == 0x80)