2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * pr.c: Use to do output to a player
30 * Known contributors to this file:
31 * Dave Pare, 1986, 1989
32 * Steve McClure, 1998-2000
35 * The pr routine historically arranged for nonbuffered i/o
36 * because stdio didn't used to automatically flush stdout before
37 * it read something from stdin. Now pr() prepends an "output id"
38 * in front of each line of text, informing the user interface
39 * what sort of item it is seeing; prompt, noecho prompt,
40 * more input data, etc.
56 #include "prototypes.h"
58 static void pr_player(struct player *pl, int id, char *buf);
59 static void upr_player(struct player *pl, int id, char *buf);
60 static void outid(struct player *pl, int n);
63 * Print to current player similar to printf().
64 * Use printf-style FORMAT with the optional arguments.
65 * Note: `to print' without further qualifications means sending
75 (void)vsprintf(buf, format, ap);
77 if (player->flags & PF_UTF8)
78 /* normal text needs to be converted to user text */
79 upr_player(player, C_DATA, buf);
81 /* normal text and user text are identical */
82 pr_player(player, C_DATA, buf);
86 * Print UTF-8 text BUF to current player.
93 if (!(player->flags & PF_UTF8)) {
94 p = malloc(strlen(buf) + 1);
95 copy_utf8_to_ascii_no_funny(p, buf);
96 pr_player(player, C_DATA, p);
99 pr_player(player, C_DATA, buf);
103 * Send some text to P with id ID, line-buffered.
104 * Format text to send using printf-style FORMAT and optional
105 * arguments. It is assumed to be already user text. Plain ASCII and
106 * text received from the same player are fine, for anything else the
107 * caller has to deal with output filtering.
108 * If a partial line with different id is buffered, terminate it with
112 pr_id(struct player *p, int id, char *format, ...)
118 io_puts(p->iop, "\n");
121 va_start(ap, format);
122 (void)vsprintf(buf, format, ap);
124 pr_player(p, id, buf);
128 * Send C_FLASH text to PL.
129 * Format text to send using printf-style FORMAT and optional
130 * arguments. It is assumed to be UTF-8.
133 pr_flash(struct player *pl, char *format, ...)
135 char buf[4096]; /* UTF-8 */
138 if (pl->state != PS_PLAYING)
140 va_start(ap, format);
141 (void)vsprintf(buf, format, ap);
143 if (!(pl->flags & PF_UTF8))
144 copy_utf8_to_ascii_no_funny(buf, buf);
145 pr_player(pl, C_FLASH, buf);
146 io_output(pl->iop, IO_NOWAIT);
150 * Send C_INFORM text to PL.
151 * Format text to send using printf-style FORMAT and optional
152 * arguments. It is assumed to be plain ASCII.
155 pr_inform(struct player *pl, char *format, ...)
160 if (pl->state != PS_PLAYING)
162 va_start(ap, format);
163 (void)vsprintf(buf, format, ap);
165 pr_player(pl, C_INFORM, buf);
166 io_output(pl->iop, IO_NOWAIT);
170 * Send C_FLASH text to everyone.
171 * Format text to send using printf-style FORMAT and optional
172 * arguments. It is assumed to be plain ASCII.
175 pr_wall(char *format, ...)
177 char buf[4096]; /* UTF-8 */
181 va_start(ap, format);
182 (void)vsprintf(buf, format, ap);
184 for (p = player_next(0); p; p = player_next(p)) {
185 if (p->state != PS_PLAYING)
187 pr_player(p, C_FLASH, buf);
188 io_output(p->iop, IO_NOWAIT);
193 * Send ID text BUF to PL, line-buffered.
195 * If a partial line with different id is buffered, terminate it with
199 pr_player(struct player *pl, int id, char *buf)
206 while (*bp != '\0') {
207 if (pl->curid != -1 && pl->curid != id) {
208 io_puts(pl->iop, "\n");
213 p = strchr(bp, '\n');
216 if (pl->command && (pl->command->c_flags & C_MOD))
217 io_write(pl->iop, bp, len, IO_NOWAIT);
219 io_write(pl->iop, bp, len, IO_WAIT);
223 len = io_puts(pl->iop, bp);
230 * Send ID text BUF to PL, line-buffered.
231 * If a partial line with different id is buffered, terminate it with
235 upr_player(struct player *pl, int id, char *buf)
246 while ((ch = *bp++)) {
247 if (pl->curid != -1 && pl->curid != id) {
248 io_puts(pl->iop, "\n");
257 io_puts(pl->iop, printbuf);
264 io_puts(pl->iop, printbuf);
269 if (pl->command && (pl->command->c_flags & C_MOD))
270 io_write(pl->iop, &ch, 1, IO_NOWAIT);
272 io_write(pl->iop, &ch, 1, IO_WAIT);
276 io_puts(pl->iop, printbuf);
283 * This runs always at the beginning of a line.
286 outid(struct player *pl, int n)
290 if (CANT_HAPPEN(n > C_LAST))
294 buf[0] = 'a' - 10 + n;
299 io_puts(pl->iop, buf);
304 * Send redirection request REDIR to the current player.
305 * REDIR is UTF-8, but non-ASCII characters can occur only if the
306 * player sent them. Therefore, it is also user text.
311 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
315 * Send script execute request FILE to the current player.
316 * REDIR is UTF-8, but non-ASCII characters can occur only if the
317 * player sent them. Therefore, it is also user text.
322 pr_id(player, C_EXECUTE, "%s\n", file);
326 * Send a command prompt to the current player.
329 prprompt(int min, int btu)
331 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
335 * Prompt for a line of non-command input.
336 * Send C_FLUSH prompt PROMPT to the current player.
337 * Read a line of input into BUF[SIZE] and convert it to ASCII.
338 * Return number of bytes in BUF[], not counting the terminating 0,
342 prmptrd(char *prompt, char *buf, int size)
347 pr_id(player, C_FLUSH, "%s\n", prompt);
348 if ((r = recvclient(buf, size)) < 0)
350 time(&player->curup);
353 if (player->flags & PF_UTF8)
354 return copy_utf8_to_ascii_no_funny(buf, buf);
355 return copy_ascii_no_funny(buf, buf);
359 * Prompt for a line of non-command, UTF-8 input.
360 * Send C_FLUSH prompt PROMPT to the current player.
361 * Read a line of input into BUF[SIZE], replacing funny characters by
362 * '?'. The result is UTF-8.
363 * Return number of bytes in BUF[], not counting the terminating 0,
367 uprmptrd(char *prompt, char *buf, int size)
372 pr_id(player, C_FLUSH, "%s\n", prompt);
373 if ((r = recvclient(buf, size)) < 0)
375 time(&player->curup);
378 if (player->flags & PF_UTF8)
379 return copy_utf8_no_funny(buf, buf);
380 return copy_ascii_no_funny(buf, buf);
384 * Print the current time in ctime() format.
396 * Print coordinates X, Y for COUNTRY.
397 * FORMAT must be a printf-style format string that converts exactly
401 prxy(char *format, coord x, coord y, natid country)
406 np = getnatp(country);
407 sprintf(buf, format, xrel(np, x), yrel(np, y));
412 * Print to country CN similar to printf().
413 * Use printf-style FORMAT with the optional arguments.
414 * Output is buffered until a newline arrives.
415 * If CN is the current player, print just like pr().
416 * Else print into a bulletin.
417 * Because printing like pr() requires normal text, and bulletins
418 * require user text, only plain ASCII is allowed.
421 PR(int cn, char *format, ...)
423 /* XXX should really do this on a per-nation basis */
424 static char longline[MAXNOC][512];
429 va_start(ap, format);
430 (void)vsprintf(buf, format, ap);
432 newline = strrchr(buf, '\n') ? 1 : 0;
433 strcat(longline[cn], buf);
435 if (update_pending || (cn && cn != player->cnum))
436 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
438 pr_player(player, C_DATA, longline[cn]);
439 longline[cn][0] = '\0';
444 * Print the current time in ctime() format to country CN.
445 * If CN is the current player, print like prdate().
446 * Else print into a bulletin.
458 * Sound the current player's bell.
463 struct natstr *np = getnatp(player->cnum);
465 if (np->nat_flags & NF_BEEP)
470 * Print to country CN similar to printf().
471 * Use printf-style FORMAT with the optional arguments.
472 * If CN is the current player, print just like pr().
473 * 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 va_start(ap, format);
484 (void)vsprintf(buf, format, ap);
487 if (update_pending || cn != player->cnum)
488 typed_wu(0, cn, buf, TEL_BULLETIN);
490 pr_player(player, C_DATA, buf);
495 * Copy SRC without funny characters to DST.
496 * Drop control characters, except for '\t'.
497 * Replace non-ASCII characters by '?'.
498 * Return length of DST.
499 * DST must have space. If it overlaps SRC, then DST <= SRC must
503 copy_ascii_no_funny(char *dst, char *src)
509 while ((ch = *src++)) {
510 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
511 ; /* ignore funny control */
513 *p++ = '?'; /* replace non-ASCII */
523 * Copy UTF-8 SRC without funny characters to DST.
524 * Drop control characters, except for '\t'.
525 * FIXME Replace malformed UTF-8 sequences by '?'.
526 * Return byte length of DST.
527 * DST must have space. If it overlaps SRC, then DST <= SRC must
531 copy_utf8_no_funny(char *dst, char *src)
537 while ((ch = *src++)) {
538 /* FIXME do the right thing for malformed and overlong sequences */
539 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
540 ; /* ignore funny control */
550 * Copy UTF-8 SRC without funny characters to ASCII DST.
551 * Drop control characters, except for '\t'.
552 * Replace non-ASCII characters by '?'.
553 * Return length of DST.
554 * DST must have space. If it overlaps SRC, then DST <= SRC must
558 copy_utf8_to_ascii_no_funny(char *dst, char *src)
564 while ((ch = *src++)) {
565 /* FIXME do the right thing for malformed and overlong sequences */
566 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
567 ; /* ignore funny control */
568 else if (ch > 0x7f) {
569 *p++ = '?'; /* replace non-ASCII */
570 while ((*src++ & 0xc0) == 0x80) ;
580 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
581 * If S doesn't have that many characters, return its length instead.
584 ufindpfx(char *s, int n)
590 if ((s[i++] & 0xc0) == 0xc0)
591 while ((s[i] & 0xc0) == 0x80)