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 outid(struct player *pl, int n);
61 * Print to current player similar to printf().
62 * Use printf-style FORMAT with the optional arguments.
63 * Note: `to print' without further qualifications means sending
73 (void)vsprintf(buf, format, ap);
75 if (player->flags & PF_UTF8)
76 /* normal text needs to be converted to user text */
77 upr_player(player, C_DATA, buf);
79 /* normal text and user text are identical */
80 pr_player(player, C_DATA, buf);
84 * Print UTF-8 text BUF to current player.
91 if (!(player->flags & PF_UTF8)) {
92 p = malloc(strlen(buf) + 1);
93 copy_utf8_to_ascii_no_funny(p, buf);
94 pr_player(player, C_DATA, p);
97 pr_player(player, C_DATA, buf);
101 * Send some text to P with id ID, line-buffered.
102 * Format text to send using printf-style FORMAT and optional
103 * arguments. It is assumed to be already user text. Plain ASCII and
104 * text received from the same player are fine, for anything else the
105 * caller has to deal with output filtering.
106 * If a partial line with different id is buffered, terminate it with
110 pr_id(struct player *p, int id, char *format, ...)
116 io_puts(p->iop, "\n");
119 va_start(ap, format);
120 (void)vsprintf(buf, format, ap);
122 pr_player(p, id, buf);
126 * Send C_FLASH text to PL.
127 * Format text to send using printf-style FORMAT and optional
128 * arguments. It is assumed to be UTF-8.
131 pr_flash(struct player *pl, char *format, ...)
133 char buf[4096]; /* UTF-8 */
136 if (pl->state != PS_PLAYING)
138 va_start(ap, format);
139 (void)vsprintf(buf, format, ap);
141 if (!(pl->flags & PF_UTF8))
142 copy_utf8_to_ascii_no_funny(buf, buf);
143 pr_player(pl, C_FLASH, buf);
144 io_output(pl->iop, IO_NOWAIT);
148 * Send C_INFORM text to PL.
149 * Format text to send using printf-style FORMAT and optional
150 * arguments. It is assumed to be plain ASCII.
153 pr_inform(struct player *pl, char *format, ...)
158 if (pl->state != PS_PLAYING)
160 va_start(ap, format);
161 (void)vsprintf(buf, format, ap);
163 pr_player(pl, C_INFORM, buf);
164 io_output(pl->iop, IO_NOWAIT);
168 * Send C_FLASH text to everyone.
169 * Format text to send using printf-style FORMAT and optional
170 * arguments. It is assumed to be plain ASCII.
173 pr_wall(char *format, ...)
175 char buf[4096]; /* UTF-8 */
179 va_start(ap, format);
180 (void)vsprintf(buf, format, ap);
182 for (p = player_next(0); p; p = player_next(p)) {
183 if (p->state != PS_PLAYING)
185 pr_player(p, C_FLASH, buf);
186 io_output(p->iop, IO_NOWAIT);
191 * Send ID text BUF to PL, line-buffered.
193 * If a partial line with different id is buffered, terminate it with
197 pr_player(struct player *pl, int id, char *buf)
204 while (*bp != '\0') {
205 if (pl->curid != -1 && pl->curid != id) {
206 io_puts(pl->iop, "\n");
211 p = strchr(bp, '\n');
214 if (pl->command && (pl->command->c_flags & C_MOD))
215 io_write(pl->iop, bp, len, IO_NOWAIT);
217 io_write(pl->iop, bp, len, IO_WAIT);
221 len = io_puts(pl->iop, bp);
228 * Send ID text BUF to PL, line-buffered.
229 * If a partial line with different id is buffered, terminate it with
233 upr_player(struct player *pl, int id, char *buf)
244 while ((ch = *bp++)) {
245 if (pl->curid != -1 && pl->curid != id) {
246 io_puts(pl->iop, "\n");
255 io_puts(pl->iop, printbuf);
262 io_puts(pl->iop, printbuf);
267 if (pl->command && (pl->command->c_flags & C_MOD))
268 io_write(pl->iop, &ch, 1, IO_NOWAIT);
270 io_write(pl->iop, &ch, 1, IO_WAIT);
274 io_puts(pl->iop, printbuf);
280 * highlighted characters have hex 80 or'ed in
281 * with them to designate their highlightedness
284 pr_hilite(s_char *buf)
290 p = malloc(strlen(buf) + 1);
292 for (bp = p; 0 != (c = *bp); bp++)
301 * This runs always at the beginning of a line.
304 outid(struct player *pl, int n)
308 if (CANT_HAPPEN(n > C_LAST))
312 buf[0] = 'a' - 10 + n;
317 io_puts(pl->iop, buf);
322 * Send redirection request REDIR to the current player.
323 * REDIR is UTF-8, but non-ASCII characters can occur only if the
324 * player sent them. Therefore, it is also user text.
329 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
333 * Send script execute request FILE to the current player.
334 * REDIR is UTF-8, but non-ASCII characters can occur only if the
335 * player sent them. Therefore, it is also user text.
340 pr_id(player, C_EXECUTE, "%s\n", file);
344 * Send a command prompt to the current player.
347 prprompt(int min, int btu)
349 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
353 * Prompt for a line of non-command input.
354 * Send C_FLUSH prompt PROMPT to the current player.
355 * Read a line of input into BUF[SIZE] and convert it to ASCII.
356 * Return number of bytes in BUF[], not counting the terminating 0,
360 prmptrd(char *prompt, char *buf, int size)
365 pr_id(player, C_FLUSH, "%s\n", prompt);
366 if ((r = recvclient(buf, size)) < 0)
368 time(&player->curup);
371 if (player->flags & PF_UTF8)
372 return copy_utf8_to_ascii_no_funny(buf, buf);
373 return copy_ascii_no_funny(buf, buf);
377 * Prompt for a line of non-command, UTF-8 input.
378 * Send C_FLUSH prompt PROMPT to the current player.
379 * Read a line of input into BUF[SIZE], replacing funny characters by
380 * '?'. The result is UTF-8.
381 * Return number of bytes in BUF[], not counting the terminating 0,
385 uprmptrd(char *prompt, char *buf, int size)
390 pr_id(player, C_FLUSH, "%s\n", prompt);
391 if ((r = recvclient(buf, size)) < 0)
393 time(&player->curup);
396 if (player->flags & PF_UTF8)
397 return copy_utf8_no_funny(buf, buf);
398 return copy_ascii_no_funny(buf, buf);
402 * Print the current time in ctime() format.
414 * Print coordinates X, Y for COUNTRY.
415 * FORMAT must be a printf-style format string that converts exactly
419 prxy(char *format, coord x, coord y, natid country)
424 np = getnatp(country);
425 sprintf(buf, format, xrel(np, x), yrel(np, y));
430 * Print to country CN similar to printf().
431 * Use printf-style FORMAT with the optional arguments.
432 * Output is buffered until a newline arrives.
433 * If CN is the current player, print just like pr().
434 * Else print into a bulletin.
435 * Because printing like pr() requires normal text, and bulletins
436 * require user text, only plain ASCII is allowed.
439 PR(int cn, char *format, ...)
441 /* XXX should really do this on a per-nation basis */
442 static char longline[MAXNOC][512];
447 va_start(ap, format);
448 (void)vsprintf(buf, format, ap);
450 newline = strrchr(buf, '\n') ? 1 : 0;
451 strcat(longline[cn], buf);
453 if (update_pending || (cn && cn != player->cnum))
454 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
456 pr_player(player, C_DATA, longline[cn]);
457 longline[cn][0] = '\0';
462 * Print the current time in ctime() format to country CN.
463 * If CN is the current player, print like prdate().
464 * Else print into a bulletin.
476 * Sound the current player's bell.
481 struct natstr *np = getnatp(player->cnum);
483 if (np->nat_flags & NF_BEEP)
488 * Print to country CN similar to printf().
489 * Use printf-style FORMAT with the optional arguments.
490 * If CN is the current player, print just like pr().
491 * Else print into a bulletin.
492 * Because printing like pr() requires normal text, and bulletins
493 * require user text, only plain ASCII is allowed.
496 mpr(int cn, char *format, ...)
501 va_start(ap, format);
502 (void)vsprintf(buf, format, ap);
505 if (update_pending || cn != player->cnum)
506 typed_wu(0, cn, buf, TEL_BULLETIN);
508 pr_player(player, C_DATA, buf);
513 * Copy SRC without funny characters to DST.
514 * Drop control characters, except for '\t'.
515 * Replace non-ASCII characters by '?'.
516 * Return length of DST.
517 * DST must have space. If it overlaps SRC, then DST <= SRC must
521 copy_ascii_no_funny(char *dst, char *src)
527 while ((ch = *src++)) {
528 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
529 ; /* ignore control */
531 *p++ = '?'; /* replace non-ASCII */
541 * Copy UTF-8 SRC without funny characters to DST.
542 * Drop control characters, except for '\t'.
543 * FIXME Replace malformed UTF-8 sequences by '?'.
544 * Return byte length of DST.
545 * DST must have space. If it overlaps SRC, then DST <= SRC must
549 copy_utf8_no_funny(char *dst, char *src)
555 while ((ch = *src++)) {
556 /* FIXME do the right thing for malformed and overlong sequences */
557 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
558 ; /* ignore control */
568 * Copy UTF-8 SRC without funny characters to ASCII DST.
569 * Drop control characters, except for '\t'.
570 * Replace non-ASCII characters by '?'.
571 * Return length of DST.
572 * DST must have space. If it overlaps SRC, then DST <= SRC must
576 copy_utf8_to_ascii_no_funny(char *dst, char *src)
582 while ((ch = *src++)) {
583 /* FIXME do the right thing for malformed and overlong sequences */
584 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
585 ; /* ignore control */
586 else if (ch > 0x7f) {
587 *p++ = '?'; /* replace non-ASCII */
588 while ((*src++ & 0xc0) == 0x80) ;
598 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
599 * If S doesn't have that many characters, return its length instead.
602 ufindpfx(char *s, int n)
608 if ((s[i++] & 0xc0) == 0xc0)
609 while ((s[i] & 0xc0) == 0x80)