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