2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
36 * Player output is fully buffered. It can block only if the
37 * receiving player is the current player and his last command doesn't
38 * have the C_MOD flag. Output to another player must not block
39 * because that player could be gone when the printing thread wakes
40 * up, and the code isn't prepared for that. Output within C_MOD
41 * command never blocks, so that such commands can print freely
42 * without yielding the processor.
44 * Each line of output starts with an identification character
45 * encoding the output id, followed by space. Ids less than 10 are
46 * encoded as decimal digits, and larger ids as lower case letters,
47 * starting with 'a'. Symbolic names for ids are defined in proto.h.
65 #include "prototypes.h"
67 static void pr_player(struct player *pl, int id, char *buf);
68 static void upr_player(struct player *pl, int id, char *buf);
69 static void outid(struct player *pl, int n);
72 * Print to current player similar to printf().
73 * Use printf-style FORMAT with the optional arguments.
74 * Note: `to print' without further qualifications means sending
84 (void)vsprintf(buf, format, ap);
86 if (player->flags & PF_UTF8)
87 /* normal text needs to be converted to user text */
88 upr_player(player, C_DATA, buf);
90 /* normal text and user text are identical */
91 pr_player(player, C_DATA, buf);
95 * Print UTF-8 text BUF to current player.
102 if (!(player->flags & PF_UTF8)) {
103 p = malloc(strlen(buf) + 1);
104 copy_utf8_to_ascii_no_funny(p, buf);
105 pr_player(player, C_DATA, p);
108 pr_player(player, C_DATA, buf);
112 * Send some text to P with id ID, line-buffered.
113 * Format text to send using printf-style FORMAT and optional
114 * arguments. It is assumed to be already user text. Plain ASCII and
115 * text received from the same player are fine, for anything else the
116 * caller has to deal with output filtering.
117 * If a partial line with different id is buffered, terminate it with
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 * Initiate an output queue flush, but do not wait for it to complete.
187 pr_wall(char *format, ...)
189 char buf[4096]; /* UTF-8 */
193 va_start(ap, format);
194 (void)vsprintf(buf, format, ap);
196 for (p = player_next(0); p; p = player_next(p)) {
197 if (p->state != PS_PLAYING)
199 pr_player(p, C_FLASH, buf);
200 io_output(p->iop, IO_NOWAIT);
205 * Send ID text BUF to PL, line-buffered.
207 * If a partial line with different id is buffered, terminate it with
211 pr_player(struct player *pl, int id, char *buf)
218 while (*bp != '\0') {
219 if (pl->curid != -1 && pl->curid != id) {
220 io_puts(pl->iop, "\n");
225 p = strchr(bp, '\n');
228 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
230 io_write(pl->iop, bp, len, IO_NOWAIT);
232 io_write(pl->iop, bp, len, IO_WAIT);
236 len = io_puts(pl->iop, bp);
243 * Send ID text BUF to PL, line-buffered.
244 * This function translates from normal text to user text.
245 * If a partial line with different id is buffered, terminate it with
249 upr_player(struct player *pl, int id, char *buf)
260 while ((ch = *bp++)) {
261 if (pl->curid != -1 && pl->curid != id) {
262 io_puts(pl->iop, "\n");
271 io_puts(pl->iop, printbuf);
278 io_puts(pl->iop, printbuf);
283 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
285 io_write(pl->iop, &ch, 1, IO_NOWAIT);
287 io_write(pl->iop, &ch, 1, IO_WAIT);
291 io_puts(pl->iop, printbuf);
298 * This runs always at the beginning of a line.
301 outid(struct player *pl, int n)
305 if (CANT_HAPPEN(n > C_LAST))
309 buf[0] = 'a' - 10 + n;
314 io_puts(pl->iop, buf);
319 * Send redirection request REDIR to the current player.
320 * REDIR is UTF-8, but non-ASCII characters can occur only if the
321 * player sent them. Therefore, it is also user text.
326 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
330 * Send script execute request FILE 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, C_EXECUTE, "%s\n", file);
341 * Send a command prompt to the current player.
344 prprompt(int min, int btu)
346 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
350 * Prompt for a line of non-command input.
351 * Send C_FLUSH prompt PROMPT to the current player.
352 * Read a line of input into BUF[SIZE] and convert it to ASCII.
353 * This may block for input, yielding the processor. Flush buffered
354 * output when blocking, to make sure player sees the prompt.
355 * Return number of bytes in BUF[], not counting the terminating 0,
359 prmptrd(char *prompt, char *buf, int size)
364 * Each prompt must consume one line of input. recvclient()
365 * doesn't do that while player->aborted.
367 CANT_HAPPEN(player->aborted);
369 pr_id(player, C_FLUSH, "%s\n", prompt);
370 if ((r = recvclient(buf, size)) < 0)
372 time(&player->curup);
375 if (player->flags & PF_UTF8)
376 return copy_utf8_to_ascii_no_funny(buf, buf);
377 return copy_ascii_no_funny(buf, buf);
381 * Prompt for a line of non-command, UTF-8 input.
382 * Send C_FLUSH prompt PROMPT to the current player.
383 * Read a line of input into BUF[SIZE], replacing funny characters by
384 * '?'. The result is UTF-8.
385 * This may block for input, yielding the processor. Flush buffered
386 * output when blocking, to make sure player sees the prompt.
387 * Return number of bytes in BUF[], not counting the terminating 0,
391 uprmptrd(char *prompt, char *buf, int size)
396 CANT_HAPPEN(player->aborted);
398 pr_id(player, C_FLUSH, "%s\n", prompt);
399 if ((r = recvclient(buf, size)) < 0)
401 time(&player->curup);
404 if (player->flags & PF_UTF8)
405 return copy_utf8_no_funny(buf, buf);
406 return copy_ascii_no_funny(buf, buf);
410 * Print the current time in ctime() format.
422 * Print coordinates X, Y for COUNTRY.
423 * FORMAT must be a printf-style format string that converts exactly
427 prxy(char *format, coord x, coord y, natid country)
432 np = getnatp(country);
433 sprintf(buf, format, xrel(np, x), yrel(np, y));
438 * Print to country CN similar to printf().
439 * Use printf-style FORMAT with the optional arguments.
440 * Output is buffered until a newline arrives.
441 * If CN is the current player and we're not in the update, print just
442 * like pr(). Else print into a bulletin.
443 * Because printing like pr() requires normal text, and bulletins
444 * require user text, only plain ASCII is allowed.
447 PR(int cn, char *format, ...)
449 /* XXX should really do this on a per-nation basis */
450 static char longline[MAXNOC][512];
455 va_start(ap, format);
456 (void)vsprintf(buf, format, ap);
458 newline = strrchr(buf, '\n') ? 1 : 0;
459 strcat(longline[cn], buf);
461 if (update_pending || (cn && cn != player->cnum))
462 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
464 pr_player(player, C_DATA, longline[cn]);
465 longline[cn][0] = '\0';
470 * Print the current time in ctime() format to country CN.
471 * If CN is the current player and we're not in the update, print just
472 * like prdate(). Else print into a bulletin.
484 * Sound the current player's bell.
489 struct natstr *np = getnatp(player->cnum);
491 if (np->nat_flags & NF_BEEP)
496 * Print to country CN similar to printf().
497 * Use printf-style FORMAT with the optional arguments.
498 * If CN is the current player and we're not in the update, print just
499 * like pr(). Else print into a bulletin.
500 * Because printing like pr() requires normal text, and bulletins
501 * require user text, only plain ASCII is allowed.
504 mpr(int cn, char *format, ...)
509 va_start(ap, format);
510 (void)vsprintf(buf, format, ap);
513 if (update_pending || cn != player->cnum)
514 typed_wu(0, cn, buf, TEL_BULLETIN);
516 pr_player(player, C_DATA, buf);
521 * Copy SRC without funny characters to DST.
522 * Drop control characters, except for '\t'.
523 * Replace non-ASCII characters by '?'.
524 * Return length of DST.
525 * DST must have space. If it overlaps SRC, then DST <= SRC must
529 copy_ascii_no_funny(char *dst, char *src)
535 while ((ch = *src++)) {
536 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
537 ; /* ignore funny control */
539 *p++ = '?'; /* replace non-ASCII */
549 * Copy UTF-8 SRC without funny characters to DST.
550 * Drop control characters, except for '\t'.
551 * FIXME Replace malformed UTF-8 sequences by '?'.
552 * Return byte length of DST.
553 * DST must have space. If it overlaps SRC, then DST <= SRC must
557 copy_utf8_no_funny(char *dst, char *src)
563 while ((ch = *src++)) {
564 /* FIXME do the right thing for malformed and overlong sequences */
565 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
566 ; /* ignore funny control */
576 * Copy UTF-8 SRC without funny characters to ASCII DST.
577 * Drop control characters, except for '\t'.
578 * Replace non-ASCII characters by '?'.
579 * Return length of DST.
580 * DST must have space. If it overlaps SRC, then DST <= SRC must
584 copy_utf8_to_ascii_no_funny(char *dst, char *src)
590 while ((ch = *src++)) {
591 /* FIXME do the right thing for malformed and overlong sequences */
592 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
593 ; /* ignore funny control */
594 else if (ch > 0x7f) {
595 *p++ = '?'; /* replace non-ASCII */
596 while ((*src++ & 0xc0) == 0x80) ;
606 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
607 * If S doesn't have that many characters, return its length instead.
610 ufindpfx(char *s, int n)
616 if ((s[i++] & 0xc0) == 0xc0)
617 while ((s[i] & 0xc0) == 0x80)