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);
68 (void)vsprintf(buf, format, ap);
70 if (player->flags & PF_UTF8)
71 upr_player(player, C_DATA, buf);
73 pr_player(player, C_DATA, buf);
77 * Print UTF-8 text BUF to current player.
84 if (!(player->flags & PF_UTF8)) {
85 p = malloc(strlen(buf) + 1);
86 copy_utf8_to_ascii_no_funny(p, buf);
87 pr_player(player, C_DATA, p);
90 pr_player(player, C_DATA, buf);
95 pr_id(struct player *p, int id, s_char *format, ...)
101 io_puts(p->iop, "\n");
104 va_start(ap, format);
105 (void)vsprintf(buf, format, ap);
107 pr_player(p, id, buf);
111 pr_flash(struct player *pl, char *format
112 /* format is message text */, ...)
114 char buf[4096]; /* buf is message text */
117 if (pl->state != PS_PLAYING)
119 va_start(ap, format);
120 (void)vsprintf(buf, format, ap);
122 if (!(pl->flags & PF_UTF8))
123 copy_utf8_to_ascii_no_funny(buf, buf);
124 pr_player(pl, C_FLASH, buf);
125 io_output(pl->iop, IO_NOWAIT);
129 pr_inform(struct player *pl, s_char *format, ...)
134 if (pl->state != PS_PLAYING)
136 va_start(ap, format);
137 (void)vsprintf(buf, format, ap);
139 pr_player(pl, C_INFORM, buf);
140 io_output(pl->iop, IO_NOWAIT);
144 pr_wall(s_char *format, ...)
150 va_start(ap, format);
151 (void)vsprintf(buf, format, ap);
153 for (p = player_next(0); p; p = player_next(p)) {
154 if (p->state != PS_PLAYING)
156 pr_player(p, C_FLASH, buf);
157 io_output(p->iop, IO_NOWAIT);
162 pr_player(struct player *pl, int id, s_char *buf)
169 while (*bp != '\0') {
170 if (pl->curid != -1 && pl->curid != id) {
171 io_puts(pl->iop, "\n");
176 p = strchr(bp, '\n');
179 if (pl->command && (pl->command->c_flags & C_MOD))
180 io_write(pl->iop, bp, len, IO_NOWAIT);
182 io_write(pl->iop, bp, len, IO_WAIT);
186 len = io_puts(pl->iop, bp);
193 upr_player(struct player *pl, int id, char *buf
194 /* buf is message text */)
196 register char *bp; /* bp is message text */
197 register int standout = 0;
198 char printbuf[2]; /* bp is message text */
205 while ((ch = *bp++)) {
206 if (pl->curid != -1 && pl->curid != id) {
207 io_puts(pl->iop, "\n");
216 io_puts(pl->iop, printbuf);
223 io_puts(pl->iop, printbuf);
228 if (pl->command && (pl->command->c_flags & C_MOD))
229 io_write(pl->iop, &ch, 1, IO_NOWAIT);
231 io_write(pl->iop, &ch, 1, IO_WAIT);
235 io_puts(pl->iop, printbuf);
241 * highlighted characters have hex 80 or'ed in
242 * with them to designate their highlightedness
245 pr_hilite(s_char *buf)
251 p = malloc(strlen(buf) + 1);
253 for (bp = p; 0 != (c = *bp); bp++)
261 * output hex code + space
264 outid(struct player *pl, int n)
270 logerror("outid: %d not valid code\n", n);
280 io_puts(pl->iop, buf);
285 prredir(s_char *redir)
287 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
293 pr_id(player, C_EXECUTE, "%s\n", file);
297 prprompt(int min, int btu)
299 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
303 prmptrd(char *prompt, char *str, int size)
308 pr_id(player, C_FLUSH, "%s\n", prompt);
309 if ((r = recvclient(str, size)) < 0)
311 time(&player->curup);
314 if (player->flags & PF_UTF8)
315 return copy_utf8_to_ascii_no_funny(str, str);
316 return copy_ascii_no_funny(str, str);
320 uprmptrd(char *prompt, char *str /* str is message text */, int size)
323 char *cp; /* cp is message text */
325 pr_id(player, C_FLUSH, "%s\n", prompt);
326 if ((r = recvclient(str, size)) < 0)
328 time(&player->curup);
331 if (player->flags & PF_UTF8)
332 return copy_utf8_no_funny(str, str);
333 return copy_ascii_no_funny(str, str);
346 * print x,y formatting as country
349 prxy(s_char *format, coord x, coord y, natid country)
354 np = getnatp(country);
355 sprintf(buf, format, xrel(np, x), yrel(np, y));
361 PR(int cn, s_char *format, ...)
363 /* XXX should really do this on a per-nation basis */
364 static s_char longline[MAXNOC][512];
369 va_start(ap, format);
370 (void)vsprintf(buf, format, ap);
372 newline = strrchr(buf, '\n') ? 1 : 0;
373 strcat(longline[cn], buf);
375 if (update_pending || (cn && cn != player->cnum))
376 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
378 pr_player(player, C_DATA, longline[cn]);
379 longline[cn][0] = '\0';
395 struct natstr *np = getnatp(player->cnum);
397 if (np->nat_flags & NF_BEEP)
402 mpr(int cn, s_char *format, ...)
407 va_start(ap, format);
408 (void)vsprintf(buf, format, ap);
411 if (update_pending || cn != player->cnum)
412 typed_wu(0, cn, buf, TEL_BULLETIN);
414 pr_player(player, C_DATA, buf);
419 * Copy SRC without funny characters to DST.
420 * Drop control characters, except for '\t'.
421 * Replace non-ASCII characters by '?'.
422 * Return length of DST.
423 * DST must have space. If it overlaps SRC, then DST <= SRC must
427 copy_ascii_no_funny(char *dst, char *src)
433 while ((ch = *src++)) {
434 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
435 ; /* ignore control */
437 *p++ = '?'; /* replace non-ASCII */
447 * Copy UTF-8 SRC without funny characters to DST.
448 * Drop control characters, except for '\t'.
449 * FIXME Replace malformed UTF-8 sequences by '?'.
450 * Return byte length of DST.
451 * DST must have space. If it overlaps SRC, then DST <= SRC must
455 copy_utf8_no_funny(char *dst, char *src)
461 while ((ch = *src++)) {
462 /* FIXME do the right thing for malformed and overlong sequences */
463 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
464 ; /* ignore control */
474 * Copy UTF-8 SRC without funny characters to ASCII DST.
475 * Drop control characters, except for '\t'.
476 * Replace non-ASCII characters by '?'.
477 * Return length of DST.
478 * DST must have space. If it overlaps SRC, then DST <= SRC must
482 copy_utf8_to_ascii_no_funny(char *dst, char *src)
488 while ((ch = *src++)) {
489 /* FIXME do the right thing for malformed and overlong sequences */
490 if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
491 ; /* ignore control */
492 else if (ch > 0x7f) {
493 *p++ = '?'; /* replace non-ASCII */
494 while ((*src++ & 0xc0) == 0x80) ;
504 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
505 * If S doesn't have that many characters, return its length instead.
508 ufindpfx(char *s, int n)
514 if ((s[i++] & 0xc0) == 0xc0)
515 while ((s[i] & 0xc0) == 0x80)