]> git.pond.sub.org Git - empserver/blob - src/lib/subs/pr.c
New journal event output
[empserver] / src / lib / subs / pr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  pr.c: Output to players
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986, 1989
32  *     Steve McClure, 1998-2000
33  *     Ron Koenderink, 2005
34  *     Markus Armbruster, 2005-2009
35  */
36
37 /*
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.
45  *
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.
50  */
51
52 #include <config.h>
53
54 #include <stdarg.h>
55 #include <stdlib.h>
56 #include "com.h"
57 #include "empio.h"
58 #include "file.h"
59 #include "journal.h"
60 #include "misc.h"
61 #include "nat.h"
62 #include "player.h"
63 #include "proto.h"
64 #include "prototypes.h"
65 #include "server.h"
66 #include "tel.h"
67
68 static void pr_player(struct player *pl, int id, char *buf);
69 static void upr_player(struct player *pl, int id, char *buf);
70 static void outid(struct player *pl, int n);
71
72 /*
73  * Print to current player similar to printf().
74  * Use printf-style FORMAT with the optional arguments.
75  * Note: `to print' without further qualifications means sending
76  * C_DATA text.
77  */
78 void
79 pr(char *format, ...)
80 {
81     char buf[4096];
82     va_list ap;
83
84     va_start(ap, format);
85     (void)vsprintf(buf, format, ap);
86     va_end(ap);
87     if (player->flags & PF_UTF8)
88         /* normal text needs to be converted to user text */
89         upr_player(player, C_DATA, buf);
90     else
91         /* normal text and user text are identical */
92         pr_player(player, C_DATA, buf);
93 }
94
95 /*
96  * Print UTF-8 text BUF to current player.
97  */
98 void
99 uprnf(char *buf)
100 {
101     char *p;
102
103     if (!(player->flags & PF_UTF8)) {
104         p = malloc(strlen(buf) + 1);
105         copy_utf8_to_ascii_no_funny(p, buf);
106         pr_player(player, C_DATA, p);
107         free(p);
108     } else
109         pr_player(player, C_DATA, buf);
110 }
111
112 /*
113  * Send some text to P with id ID, line-buffered.
114  * Format text to send using printf-style FORMAT and optional
115  * arguments.  It is assumed to be already user text.  Plain ASCII and
116  * text received from the same player are fine, for anything else the
117  * caller has to deal with output filtering.
118  * If a partial line is buffered, terminate it with a newline first.
119  */
120 void
121 pr_id(struct player *p, int id, char *format, ...)
122 {
123     char buf[4096];
124     va_list ap;
125
126     if (p->curid >= 0) {
127         io_puts(p->iop, "\n");
128         p->curid = -1;
129     }
130     va_start(ap, format);
131     (void)vsprintf(buf, format, ap);
132     va_end(ap);
133     pr_player(p, id, buf);
134 }
135
136 /*
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.
141  */
142 void
143 pr_flash(struct player *pl, char *format, ...)
144 {
145     char buf[4096];             /* UTF-8 */
146     va_list ap;
147
148     if (pl->state != PS_PLAYING)
149         return;
150     va_start(ap, format);
151     (void)vsprintf(buf, format, ap);
152     va_end(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, 0);
157 }
158
159 /*
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.
164  */
165 void
166 pr_inform(struct player *pl, char *format, ...)
167 {
168     char buf[4096];
169     va_list ap;
170
171     if (pl->state != PS_PLAYING)
172         return;
173     va_start(ap, format);
174     (void)vsprintf(buf, format, ap);
175     va_end(ap);
176     pr_player(pl, C_INFORM, buf);
177     io_output(pl->iop, 0);
178 }
179
180 /*
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  * Prefix text it with a header suitable for broadcast from deity.
185  * Initiate an output queue flush, but do not wait for it to complete.
186  */
187 void
188 pr_wall(char *format, ...)
189 {
190     time_t now;
191     struct tm *tm;
192     char buf[4096];             /* UTF-8 */
193     int n;
194     struct player *p;
195     va_list ap;
196
197     time(&now);
198     tm = localtime(&now);
199     n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
200                 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
201
202     va_start(ap, format);
203     (void)vsprintf(buf + n, format, ap);
204     va_end(ap);
205     for (p = player_next(NULL); p; p = player_next(p)) {
206         if (p->state != PS_PLAYING)
207             continue;
208         pr_player(p, C_FLASH, buf);
209         io_output(p->iop, 0);
210     }
211 }
212
213 /*
214  * Send ID text BUF to PL, line-buffered.
215  * BUF is user text.
216  * If a partial line with different id is buffered, terminate it with
217  * a newline first.
218  */
219 static void
220 pr_player(struct player *pl, int id, char *buf)
221 {
222     char *p;
223     char *bp;
224     int len;
225
226     bp = buf;
227     while (*bp != '\0') {
228         if (pl->curid != -1 && pl->curid != id) {
229             io_puts(pl->iop, "\n");
230             pl->curid = -1;
231         }
232         if (pl->curid == -1)
233             outid(pl, id);
234         p = strchr(bp, '\n');
235         if (p != NULL) {
236             len = (p - bp) + 1;
237             io_write(pl->iop, bp, len);
238             bp += len;
239             pl->curid = -1;
240         } else {
241             len = io_puts(pl->iop, bp);
242             bp += len;
243         }
244     }
245
246     journal_output(pl, id, buf);
247
248     if (player == pl) {
249         while (io_output_if_queue_long(pl->iop,
250                         pl->may_sleep == PLAYER_SLEEP_FREELY) > 0)
251             ;
252     }
253 }
254
255 /*
256  * Send ID text BUF to PL, line-buffered.
257  * This function translates from normal text to user text.
258  * If a partial line with different id is buffered, terminate it with
259  * a newline first.
260  */
261 static void
262 upr_player(struct player *pl, int id, char *buf)
263 {
264     char *bp;
265     int standout = 0;
266     char printbuf[2];
267     char ch;
268
269     printbuf[0] = '\0';
270     printbuf[1] = '\0';
271
272     bp = buf;
273     while ((ch = *bp++)) {
274         if (pl->curid != -1 && pl->curid != id) {
275             io_puts(pl->iop, "\n");
276             pl->curid = -1;
277         }
278         if (pl->curid == -1)
279             outid(pl, id);
280
281         if (ch & 0x80) {
282             if (standout == 0) {
283                 printbuf[0] = 0x0e;
284                 io_puts(pl->iop, printbuf);
285                 standout = 1;
286             }
287             ch &= 0x7f;
288         } else {
289             if (standout == 1) {
290                 printbuf[0] = 0x0f;
291                 io_puts(pl->iop, printbuf);
292                 standout = 0;
293             }
294         }
295         if (ch == '\n') {
296             io_write(pl->iop, &ch, 1);
297             pl->curid = -1;
298         } else {
299             printbuf[0] = ch;
300             io_puts(pl->iop, printbuf);
301         }
302     }
303
304     journal_output(pl, id, buf);
305
306     if (player == pl) {
307         while (io_output_if_queue_long(pl->iop,
308                         pl->may_sleep == PLAYER_SLEEP_FREELY) > 0)
309             ;
310     }
311 }
312
313 /*
314  * Send id N to PL.
315  * This runs always at the beginning of a line.
316  */
317 static void
318 outid(struct player *pl, int n)
319 {
320     char buf[3];
321
322     if (CANT_HAPPEN(n > C_LAST))
323         n = C_DATA;
324
325     if (n >= 10)
326         buf[0] = 'a' - 10 + n;
327     else
328         buf[0] = '0' + n;
329     buf[1] = ' ';
330     buf[2] = '\0';
331     io_puts(pl->iop, buf);
332     pl->curid = n;
333 }
334
335 /*
336  * Send redirection request REDIR to the current player.
337  * REDIR is UTF-8, but non-ASCII characters can occur only if the
338  * player sent them.  Therefore, it is also user text.
339  */
340 void
341 prredir(char *redir)
342 {
343     pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
344 }
345
346 /*
347  * Send script execute request FILE to the current player.
348  * REDIR is UTF-8, but non-ASCII characters can occur only if the
349  * player sent them.  Therefore, it is also user text.
350  */
351 void
352 prexec(char *file)
353 {
354     pr_id(player, C_EXECUTE, "%s\n", file);
355 }
356
357 /*
358  * Send a command prompt to the current player.
359  */
360 void
361 prprompt(int min, int btu)
362 {
363     pr_id(player, C_PROMPT, "%d %d\n", min, btu);
364 }
365
366 /*
367  * Prompt for a line of non-command input.
368  * Send C_FLUSH prompt PROMPT to the current player.
369  * Read a line of input into BUF[SIZE] and convert it to ASCII.
370  * This may block for input, yielding the processor.  Flush buffered
371  * output when blocking, to make sure player sees the prompt.
372  * Return number of bytes in BUF[], not counting the terminating 0,
373  * or -1 on error.
374  */
375 int
376 prmptrd(char *prompt, char *buf, int size)
377 {
378     int r;
379
380     if (CANT_HAPPEN(!prompt))
381         prompt = "? ";
382
383     pr_id(player, C_FLUSH, "%s\n", prompt);
384     if ((r = recvclient(buf, size)) < 0)
385         return r;
386     time(&player->curup);
387     if (*buf == 0)
388         return 1;
389     if (player->flags & PF_UTF8)
390         return copy_utf8_to_ascii_no_funny(buf, buf);
391     return copy_ascii_no_funny(buf, buf);
392 }
393
394 /*
395  * Prompt for a line of non-command, UTF-8 input.
396  * Send C_FLUSH prompt PROMPT to the current player.
397  * Read a line of input into BUF[SIZE], replacing funny characters by
398  * '?'.  The result is UTF-8.
399  * This may block for input, yielding the processor.  Flush buffered
400  * output when blocking, to make sure player sees the prompt.
401  * Return number of bytes in BUF[], not counting the terminating 0,
402  * or -1 on error.
403  */
404 int
405 uprmptrd(char *prompt, char *buf, int size)
406 {
407     int r;
408
409     if (CANT_HAPPEN(!prompt))
410         prompt = "? ";
411
412     pr_id(player, C_FLUSH, "%s\n", prompt);
413     if ((r = recvclient(buf, size)) < 0)
414         return r;
415     time(&player->curup);
416     if (*buf == 0)
417         return 1;
418     if (player->flags & PF_UTF8)
419         return copy_utf8_no_funny(buf, buf);
420     return copy_ascii_no_funny(buf, buf);
421 }
422
423 /*
424  * Print the current time in ctime() format.
425  */
426 void
427 prdate(void)
428 {
429     time_t now;
430
431     (void)time(&now);
432     pr(ctime(&now));
433 }
434
435 /*
436  * Print coordinates X, Y for COUNTRY.
437  * FORMAT must be a printf-style format string that converts exactly
438  * two int values.
439  */
440 void
441 prxy(char *format, coord x, coord y, natid country)
442 {
443     struct natstr *np;
444
445     np = getnatp(country);
446     pr(format, xrel(np, x), yrel(np, y));
447 }
448
449 /*
450  * Print to country CN similar to printf().
451  * Use printf-style FORMAT with the optional arguments.
452  * Output is buffered until a newline arrives.
453  * If CN is the current player and we're not in the update, print just
454  * like pr().  Else print into a bulletin.
455  * Because printing like pr() requires normal text, and bulletins
456  * require user text, only plain ASCII is allowed.
457  */
458 void
459 PR(int cn, char *format, ...)
460 {
461     /* XXX should really do this on a per-nation basis */
462     static char longline[MAXNOC][512];
463     int newline;
464     va_list ap;
465     char buf[1024];
466
467     va_start(ap, format);
468     (void)vsprintf(buf, format, ap);
469     va_end(ap);
470     newline = strrchr(buf, '\n') ? 1 : 0;
471     strcat(longline[cn], buf);
472     if (newline) {
473         if (update_running || (cn && cn != player->cnum))
474             typed_wu(0, cn, longline[cn], TEL_BULLETIN);
475         else
476             pr_player(player, C_DATA, longline[cn]);
477         longline[cn][0] = '\0';
478     }
479 }
480
481 /*
482  * Print the current time in ctime() format to country CN.
483  * If CN is the current player and we're not in the update, print just
484  * like prdate().  Else print into a bulletin.
485  */
486 void
487 PRdate(natid cn)
488 {
489     time_t now;
490
491     (void)time(&now);
492     PR(cn, ctime(&now));
493 }
494
495 /*
496  * Sound the current player's bell.
497  */
498 void
499 pr_beep(void)
500 {
501     struct natstr *np = getnatp(player->cnum);
502
503     if (np->nat_flags & NF_BEEP)
504         pr("\07");
505 }
506
507 /*
508  * Print to country CN similar to printf().
509  * Use printf-style FORMAT with the optional arguments.
510  * If CN is zero, don't print anything.
511  * Else, if CN is the current player and we're not in the update,
512  * print just like pr().  Else print into a bulletin.
513  * Because printing like pr() requires normal text, and bulletins
514  * require user text, only plain ASCII is allowed.
515  */
516 void
517 mpr(int cn, char *format, ...)
518 {
519     char buf[4096];
520     va_list ap;
521
522     if (!cn)
523         return;
524     va_start(ap, format);
525     (void)vsprintf(buf, format, ap);
526     va_end(ap);
527     if (update_running || cn != player->cnum)
528         typed_wu(0, cn, buf, TEL_BULLETIN);
529     else
530         pr_player(player, C_DATA, buf);
531 }
532
533 /*
534  * Copy SRC without funny characters to DST.
535  * Drop control characters, except for '\t'.
536  * Replace non-ASCII characters by '?'.
537  * Return length of DST.
538  * DST must have space.  If it overlaps SRC, then DST <= SRC must
539  * hold.
540  */
541 size_t
542 copy_ascii_no_funny(char *dst, char *src)
543 {
544     char *p;
545     unsigned char ch;
546
547     p = dst;
548     while ((ch = *src++)) {
549         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
550             ;                   /* ignore funny control */
551         else if (ch > 0x7f)
552             *p++ = '?'; /* replace non-ASCII */
553         else
554             *p++ = ch;
555     }
556     *p = 0;
557
558     return p - dst;
559 }
560
561 /*
562  * Copy UTF-8 SRC without funny characters to DST.
563  * Drop control characters, except for '\t'.
564  * FIXME Replace malformed UTF-8 sequences by '?'.
565  * Return byte length of DST.
566  * DST must have space.  If it overlaps SRC, then DST <= SRC must
567  * hold.
568  */
569 size_t
570 copy_utf8_no_funny(char *dst, char *src)
571 {
572     char *p;
573     unsigned char ch;
574
575     p = dst;
576     while ((ch = *src++)) {
577         /* FIXME do the right thing for malformed and overlong sequences */
578         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
579             ;                   /* ignore funny control */
580         else
581             *p++ = ch;
582     }
583     *p = 0;
584
585     return p - dst;
586 }
587
588 /*
589  * Copy UTF-8 SRC without funny characters to ASCII DST.
590  * Drop control characters, except for '\t'.
591  * Replace non-ASCII characters by '?'.
592  * Return length of DST.
593  * DST must have space.  If it overlaps SRC, then DST <= SRC must
594  * hold.
595  */
596 size_t
597 copy_utf8_to_ascii_no_funny(char *dst, char *src)
598 {
599     char *p;
600     unsigned char ch;
601
602     p = dst;
603     while ((ch = *src++)) {
604         /* FIXME do the right thing for malformed and overlong sequences */
605         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
606             ;                   /* ignore funny control */
607         else if (ch > 0x7f) {
608             *p++ = '?';         /* replace non-ASCII */
609             while ((*src++ & 0xc0) == 0x80) ;
610         } else
611             *p++ = ch;
612     }
613     *p = 0;
614
615     return p - dst;
616 }
617
618 /*
619  * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
620  * If S doesn't have that many characters, return its length instead.
621  */
622 int
623 ufindpfx(char *s, int n)
624 {
625     int i = 0;
626
627     while (n && s[i])
628     {
629         if ((s[i++] & 0xc0) == 0xc0)
630             while ((s[i] & 0xc0) == 0x80)
631                 i++;
632         --n;
633     }
634     return i;
635 }