]> git.pond.sub.org Git - empserver/blob - src/lib/subs/pr.c
5cf6833d7e11d76b54b74f5dc2ff5af9a09c9f02
[empserver] / src / lib / subs / pr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  pr.c: Output to players
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986, 1989
31  *     Steve McClure, 1998-2000
32  *     Ron Koenderink, 2005
33  *     Markus Armbruster, 2005-2011
34  */
35
36 /*
37  * Player output is fully buffered.  It can block only if the
38  * receiving player is the current player and his last command doesn't
39  * have the C_MOD flag.  Output to another player must not block
40  * because that player could be gone when the printing thread wakes
41  * up, and the code isn't prepared for that.  Output within C_MOD
42  * command never blocks, so that such commands can print freely
43  * without yielding the processor.
44  *
45  * Each line of output starts with an identification character
46  * encoding the output id, followed by space.  Ids less than 10 are
47  * encoded as decimal digits, and larger ids as lower case letters,
48  * starting with 'a'.  Symbolic names for ids are defined in proto.h.
49  */
50
51 #include <config.h>
52
53 #include <stdarg.h>
54 #include <stdlib.h>
55 #include "com.h"
56 #include "empio.h"
57 #include "file.h"
58 #include "journal.h"
59 #include "misc.h"
60 #include "nat.h"
61 #include "player.h"
62 #include "proto.h"
63 #include "prototypes.h"
64 #include "server.h"
65 #include "tel.h"
66 #include "xy.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         journal_output(p, p->curid, "\n");
129         p->curid = -1;
130     }
131     va_start(ap, format);
132     (void)vsprintf(buf, format, ap);
133     va_end(ap);
134     pr_player(p, id, buf);
135 }
136
137 /*
138  * Send C_FLASH text to PL.
139  * Format text to send using printf-style FORMAT and optional
140  * arguments.  It is assumed to be UTF-8.
141  * Initiate an output queue flush, but do not wait for it to complete.
142  */
143 void
144 pr_flash(struct player *pl, char *format, ...)
145 {
146     char buf[4096];             /* UTF-8 */
147     va_list ap;
148
149     if (pl->state != PS_PLAYING)
150         return;
151     va_start(ap, format);
152     (void)vsprintf(buf, format, ap);
153     va_end(ap);
154     if (!(pl->flags & PF_UTF8))
155         copy_utf8_to_ascii_no_funny(buf, buf);
156     pr_player(pl, C_FLASH, buf);
157     io_output(pl->iop, 0);
158 }
159
160 /*
161  * Send C_INFORM text to PL.
162  * Format text to send using printf-style FORMAT and optional
163  * arguments.  It is assumed to be plain ASCII.
164  * Initiate an output queue flush, but do not wait for it to complete.
165  */
166 void
167 pr_inform(struct player *pl, char *format, ...)
168 {
169     char buf[4096];
170     va_list ap;
171
172     if (pl->state != PS_PLAYING)
173         return;
174     va_start(ap, format);
175     (void)vsprintf(buf, format, ap);
176     va_end(ap);
177     pr_player(pl, C_INFORM, buf);
178     io_output(pl->iop, 0);
179 }
180
181 /*
182  * Send C_FLASH text to everyone.
183  * Format text to send using printf-style FORMAT and optional
184  * arguments.  It is assumed to be plain ASCII.
185  * Prefix text it with a header suitable for broadcast from deity.
186  * Initiate an output queue flush, but do not wait for it to complete.
187  */
188 void
189 pr_wall(char *format, ...)
190 {
191     time_t now;
192     struct tm *tm;
193     char buf[4096];             /* UTF-8 */
194     int n;
195     struct player *p;
196     va_list ap;
197
198     time(&now);
199     tm = localtime(&now);
200     n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
201                 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
202
203     va_start(ap, format);
204     (void)vsprintf(buf + n, format, ap);
205     va_end(ap);
206     for (p = player_next(NULL); p; p = player_next(p)) {
207         if (p->state != PS_PLAYING)
208             continue;
209         pr_player(p, C_FLASH, buf);
210         io_output(p->iop, 0);
211     }
212 }
213
214 /*
215  * Send ID text BUF to PL, line-buffered.
216  * BUF is user text.
217  * If a partial line with different id is buffered, terminate it with
218  * a newline first.
219  */
220 static void
221 pr_player(struct player *pl, int id, char *buf)
222 {
223     char *p;
224     char *bp;
225     int len;
226
227     journal_output(pl, id, buf);
228
229     bp = buf;
230     while (*bp != '\0') {
231         if (pl->curid != -1 && pl->curid != id) {
232             io_puts(pl->iop, "\n");
233             pl->curid = -1;
234         }
235         if (pl->curid == -1)
236             outid(pl, id);
237         p = strchr(bp, '\n');
238         if (p != NULL) {
239             len = (p - bp) + 1;
240             io_write(pl->iop, bp, len);
241             bp += len;
242             pl->curid = -1;
243         } else {
244             len = io_puts(pl->iop, bp);
245             bp += len;
246         }
247     }
248
249     if (player == pl) {
250         while (io_output_if_queue_long(pl->iop,
251                         pl->may_sleep == PLAYER_SLEEP_FREELY) > 0)
252             ;
253     }
254 }
255
256 /*
257  * Send ID text BUF to PL, line-buffered.
258  * This function translates from normal text to user text.
259  * If a partial line with different id is buffered, terminate it with
260  * a newline first.
261  */
262 static void
263 upr_player(struct player *pl, int id, char *buf)
264 {
265     char *bp;
266     int standout = 0;
267     char printbuf[2];
268     char ch;
269
270     journal_output(pl, id, buf);
271
272     printbuf[0] = '\0';
273     printbuf[1] = '\0';
274
275     bp = buf;
276     while ((ch = *bp++)) {
277         if (pl->curid != -1 && pl->curid != id) {
278             io_puts(pl->iop, "\n");
279             pl->curid = -1;
280         }
281         if (pl->curid == -1)
282             outid(pl, id);
283
284         if (ch & 0x80) {
285             if (standout == 0) {
286                 printbuf[0] = 0x0e;
287                 io_puts(pl->iop, printbuf);
288                 standout = 1;
289             }
290             ch &= 0x7f;
291         } else {
292             if (standout == 1) {
293                 printbuf[0] = 0x0f;
294                 io_puts(pl->iop, printbuf);
295                 standout = 0;
296             }
297         }
298         if (ch == '\n') {
299             io_write(pl->iop, &ch, 1);
300             pl->curid = -1;
301         } else {
302             printbuf[0] = ch;
303             io_puts(pl->iop, printbuf);
304         }
305     }
306
307     if (player == pl) {
308         while (io_output_if_queue_long(pl->iop,
309                         pl->may_sleep == PLAYER_SLEEP_FREELY) > 0)
310             ;
311     }
312 }
313
314 /*
315  * Send id N to PL.
316  * This runs always at the beginning of a line.
317  */
318 static void
319 outid(struct player *pl, int n)
320 {
321     char buf[3];
322
323     if (CANT_HAPPEN(n > C_LAST))
324         n = C_DATA;
325
326     if (n >= 10)
327         buf[0] = 'a' - 10 + n;
328     else
329         buf[0] = '0' + n;
330     buf[1] = ' ';
331     buf[2] = '\0';
332     io_puts(pl->iop, buf);
333     pl->curid = n;
334 }
335
336 /*
337  * Send redirection request REDIR to the current player.
338  * REDIR is UTF-8, but non-ASCII characters can occur only if the
339  * player sent them.  Therefore, it is also user text.
340  */
341 void
342 prredir(char *redir)
343 {
344     pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
345 }
346
347 /*
348  * Send script execute request FILE to the current player.
349  * REDIR is UTF-8, but non-ASCII characters can occur only if the
350  * player sent them.  Therefore, it is also user text.
351  */
352 void
353 prexec(char *file)
354 {
355     pr_id(player, C_EXECUTE, "%s\n", file);
356 }
357
358 /*
359  * Send a command prompt to the current player.
360  */
361 void
362 prprompt(int min, int btu)
363 {
364     pr_id(player, C_PROMPT, "%d %d\n", min, btu);
365 }
366
367 /*
368  * Prompt for a line of non-command input.
369  * Send C_FLUSH prompt PROMPT to the current player.
370  * Read a line of input into BUF[SIZE] and convert it to ASCII.
371  * This may block for input, yielding the processor.  Flush buffered
372  * output when blocking, to make sure player sees the prompt.
373  * Return number of bytes in BUF[], not counting the terminating 0,
374  * or -1 on error.
375  */
376 int
377 prmptrd(char *prompt, char *buf, int size)
378 {
379     int r;
380
381     if (CANT_HAPPEN(!prompt))
382         prompt = "? ";
383
384     pr_id(player, C_FLUSH, "%s\n", prompt);
385     if ((r = recvclient(buf, size)) < 0)
386         return r;
387     time(&player->curup);
388     if (*buf == 0)
389         return 1;
390     if (player->flags & PF_UTF8)
391         return copy_utf8_to_ascii_no_funny(buf, buf);
392     return copy_ascii_no_funny(buf, buf);
393 }
394
395 /*
396  * Prompt for a line of non-command, UTF-8 input.
397  * Send C_FLUSH prompt PROMPT to the current player.
398  * Read a line of input into BUF[SIZE], replacing funny characters by
399  * '?'.  The result is UTF-8.
400  * This may block for input, yielding the processor.  Flush buffered
401  * output when blocking, to make sure player sees the prompt.
402  * Return number of bytes in BUF[], not counting the terminating 0,
403  * or -1 on error.
404  */
405 int
406 uprmptrd(char *prompt, char *buf, int size)
407 {
408     int r;
409
410     if (CANT_HAPPEN(!prompt))
411         prompt = "? ";
412
413     pr_id(player, C_FLUSH, "%s\n", prompt);
414     if ((r = recvclient(buf, size)) < 0)
415         return r;
416     time(&player->curup);
417     if (*buf == 0)
418         return 1;
419     if (player->flags & PF_UTF8)
420         return copy_utf8_no_funny(buf, buf);
421     return copy_ascii_no_funny(buf, buf);
422 }
423
424 /*
425  * Print the current time in ctime() format.
426  */
427 void
428 prdate(void)
429 {
430     time_t now;
431
432     (void)time(&now);
433     pr(ctime(&now));
434 }
435
436 /*
437  * Print coordinates X, Y.
438  * FORMAT must be a printf-style format string that converts exactly
439  * two int values.
440  */
441 void
442 prxy(char *format, coord x, coord y)
443 {
444     struct natstr *np;
445
446     np = getnatp(player->cnum);
447     pr(format, xrel(np, x), yrel(np, y));
448 }
449
450 /*
451  * Sound the current player's bell.
452  */
453 void
454 pr_beep(void)
455 {
456     struct natstr *np = getnatp(player->cnum);
457
458     if (np->nat_flags & NF_BEEP)
459         pr("\07");
460 }
461
462 /*
463  * Print complete lines to country CN similar to printf().
464  * Use printf-style FORMAT with the optional arguments.  FORMAT must
465  * end with '\n'.
466  * If CN is zero, don't print anything.
467  * Else, if CN is the current player and we're not in the update,
468  * print just like pr().  Else print into a bulletin.
469  * Because printing like pr() requires normal text, and bulletins
470  * require user text, only plain ASCII is allowed.
471  */
472 void
473 mpr(int cn, char *format, ...)
474 {
475     char buf[4096];
476     va_list ap;
477
478     CANT_HAPPEN(!format[0] || format[strlen(format) - 1] != '\n');
479     if (!cn)
480         return;
481     va_start(ap, format);
482     (void)vsprintf(buf, format, ap);
483     va_end(ap);
484     if (update_running || cn != player->cnum)
485         typed_wu(0, cn, buf, TEL_BULLETIN);
486     else
487         pr_player(player, C_DATA, buf);
488 }
489
490 /*
491  * Copy SRC without funny characters to DST.
492  * Drop control characters, except for '\t'.
493  * Replace non-ASCII characters by '?'.
494  * Return length of DST.
495  * DST must have space.  If it overlaps SRC, then DST <= SRC must
496  * hold.
497  */
498 size_t
499 copy_ascii_no_funny(char *dst, char *src)
500 {
501     char *p;
502     unsigned char ch;
503
504     p = dst;
505     while ((ch = *src++)) {
506         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
507             ;                   /* ignore funny control */
508         else if (ch > 0x7f)
509             *p++ = '?';         /* replace non-ASCII */
510         else
511             *p++ = ch;
512     }
513     *p = 0;
514
515     return p - dst;
516 }
517
518 /*
519  * Copy UTF-8 SRC without funny characters to DST.
520  * Drop control characters, except for '\t'.
521  * FIXME Replace malformed UTF-8 sequences by '?'.
522  * Return byte length of DST.
523  * DST must have space.  If it overlaps SRC, then DST <= SRC must
524  * hold.
525  */
526 size_t
527 copy_utf8_no_funny(char *dst, char *src)
528 {
529     char *p;
530     unsigned char ch;
531
532     p = dst;
533     while ((ch = *src++)) {
534         /* FIXME do the right thing for malformed and overlong sequences */
535         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
536             ;                   /* ignore funny control */
537         else
538             *p++ = ch;
539     }
540     *p = 0;
541
542     return p - dst;
543 }
544
545 /*
546  * Copy UTF-8 SRC without funny characters to ASCII DST.
547  * Drop control characters, except for '\t'.
548  * Replace non-ASCII characters by '?'.
549  * Return length of DST.
550  * DST must have space.  If it overlaps SRC, then DST <= SRC must
551  * hold.
552  */
553 size_t
554 copy_utf8_to_ascii_no_funny(char *dst, char *src)
555 {
556     char *p;
557     unsigned char ch;
558
559     p = dst;
560     while ((ch = *src++)) {
561         /* FIXME do the right thing for malformed and overlong sequences */
562         if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
563             ;                   /* ignore funny control */
564         else if (ch > 0x7f) {
565             *p++ = '?';         /* replace non-ASCII */
566             while ((*src++ & 0xc0) == 0x80) ;
567         } else
568             *p++ = ch;
569     }
570     *p = 0;
571
572     return p - dst;
573 }
574
575 /*
576  * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
577  * If S doesn't have that many characters, return its length instead.
578  */
579 int
580 ufindpfx(char *s, int n)
581 {
582     int i = 0;
583
584     while (n && s[i]) {
585         if ((s[i++] & 0xc0) == 0xc0)
586             while ((s[i] & 0xc0) == 0x80)
587                 i++;
588         --n;
589     }
590     return i;
591 }