]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/pr.c
Update copyright notice
[empserver] / src / lib / subs / pr.c
index 15c58b85405fef69d097b97cd89b152bbe8999ad..db8943acc438562462378ac771676a1157ca1d3f 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  pr.c: Output to players
- * 
+ *
  *  Known contributors to this file:
- *     Dave Pare, 1986, 1989 
+ *     Dave Pare, 1986, 1989
  *     Steve McClure, 1998-2000
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2012
  */
 
 /*
 
 #include <config.h>
 
-#include <string.h>
-#include <fcntl.h>
-#include <ctype.h>
 #include <stdarg.h>
-#include "proto.h"
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
+#include <stdlib.h>
 #include "empio.h"
 #include "file.h"
-#include "com.h"
-#include "tel.h"
-#include "server.h"
+#include "journal.h"
+#include "misc.h"
+#include "nat.h"
+#include "player.h"
+#include "proto.h"
 #include "prototypes.h"
+#include "server.h"
+#include "xy.h"
 
 static void pr_player(struct player *pl, int id, char *buf);
 static void upr_player(struct player *pl, int id, char *buf);
 static void outid(struct player *pl, int n);
+static void player_output_some(void);
 
 /*
  * Print to current player similar to printf().
- * Use printf-style FORMAT with the optional arguments.
+ * Use printf-style @format with the optional arguments.
  * Note: `to print' without further qualifications means sending
  * C_DATA text.
  */
@@ -90,11 +88,11 @@ pr(char *format, ...)
        upr_player(player, C_DATA, buf);
     else
        /* normal text and user text are identical */
-        pr_player(player, C_DATA, buf);
+       pr_player(player, C_DATA, buf);
 }
 
 /*
- * Print UTF-8 text BUF to current player.
+ * Print UTF-8 text @buf to current player.
  */
 void
 uprnf(char *buf)
@@ -111,8 +109,8 @@ uprnf(char *buf)
 }
 
 /*
- * Send some text to P with id ID, line-buffered.
- * Format text to send using printf-style FORMAT and optional
+ * Send some text to @p with id @id, line-buffered.
+ * Format text to send using printf-style @format and optional
  * arguments.  It is assumed to be already user text.  Plain ASCII and
  * text received from the same player are fine, for anything else the
  * caller has to deal with output filtering.
@@ -126,6 +124,7 @@ pr_id(struct player *p, int id, char *format, ...)
 
     if (p->curid >= 0) {
        io_puts(p->iop, "\n");
+       journal_output(p, p->curid, "\n");
        p->curid = -1;
     }
     va_start(ap, format);
@@ -135,8 +134,8 @@ pr_id(struct player *p, int id, char *format, ...)
 }
 
 /*
- * Send C_FLASH text to PL.
- * Format text to send using printf-style FORMAT and optional
+ * Send C_FLASH text to @pl.
+ * Format text to send using printf-style @format and optional
  * arguments.  It is assumed to be UTF-8.
  * Initiate an output queue flush, but do not wait for it to complete.
  */
@@ -154,12 +153,12 @@ pr_flash(struct player *pl, char *format, ...)
     if (!(pl->flags & PF_UTF8))
        copy_utf8_to_ascii_no_funny(buf, buf);
     pr_player(pl, C_FLASH, buf);
-    io_output(pl->iop, IO_NOWAIT);
+    io_output(pl->iop, 0);
 }
 
 /*
- * Send C_INFORM text to PL.
- * Format text to send using printf-style FORMAT and optional
+ * Send C_INFORM text to @pl.
+ * Format text to send using printf-style @format and optional
  * arguments.  It is assumed to be plain ASCII.
  * Initiate an output queue flush, but do not wait for it to complete.
  */
@@ -175,36 +174,45 @@ pr_inform(struct player *pl, char *format, ...)
     (void)vsprintf(buf, format, ap);
     va_end(ap);
     pr_player(pl, C_INFORM, buf);
-    io_output(pl->iop, IO_NOWAIT);
+    io_output(pl->iop, 0);
 }
 
 /*
  * Send C_FLASH text to everyone.
- * Format text to send using printf-style FORMAT and optional
+ * Format text to send using printf-style @format and optional
  * arguments.  It is assumed to be plain ASCII.
+ * Prefix text it with a header suitable for broadcast from deity.
  * Initiate an output queue flush, but do not wait for it to complete.
  */
 void
 pr_wall(char *format, ...)
 {
+    time_t now;
+    struct tm *tm;
     char buf[4096];            /* UTF-8 */
+    int n;
     struct player *p;
     va_list ap;
 
+    time(&now);
+    tm = localtime(&now);
+    n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
+               getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
+
     va_start(ap, format);
-    (void)vsprintf(buf, format, ap);
+    (void)vsprintf(buf + n, format, ap);
     va_end(ap);
-    for (p = player_next(0); p; p = player_next(p)) {
+    for (p = player_next(NULL); p; p = player_next(p)) {
        if (p->state != PS_PLAYING)
            continue;
        pr_player(p, C_FLASH, buf);
-       io_output(p->iop, IO_NOWAIT);
+       io_output(p->iop, 0);
     }
 }
 
 /*
- * Send ID text BUF to PL, line-buffered.
- * BUF is user text.
+ * Send @id text @buf to @pl, line-buffered.
+ * @buf is user text.
  * If a partial line with different id is buffered, terminate it with
  * a newline first.
  */
@@ -215,6 +223,8 @@ pr_player(struct player *pl, int id, char *buf)
     char *bp;
     int len;
 
+    journal_output(pl, id, buf);
+
     bp = buf;
     while (*bp != '\0') {
        if (pl->curid != -1 && pl->curid != id) {
@@ -226,11 +236,7 @@ pr_player(struct player *pl, int id, char *buf)
        p = strchr(bp, '\n');
        if (p != NULL) {
            len = (p - bp) + 1;
-           if ((pl->command && (pl->command->c_flags & C_MOD)) ||
-               (player != pl))
-               io_write(pl->iop, bp, len, IO_NOWAIT);
-           else
-               io_write(pl->iop, bp, len, IO_WAIT);
+           io_write(pl->iop, bp, len);
            bp += len;
            pl->curid = -1;
        } else {
@@ -238,10 +244,13 @@ pr_player(struct player *pl, int id, char *buf)
            bp += len;
        }
     }
+
+    if (player == pl)
+       player_output_some();
 }
 
 /*
- * Send ID text BUF to PL, line-buffered.
+ * Send @id text @buf to @pl, line-buffered.
  * This function translates from normal text to user text.
  * If a partial line with different id is buffered, terminate it with
  * a newline first.
@@ -254,6 +263,8 @@ upr_player(struct player *pl, int id, char *buf)
     char printbuf[2];
     char ch;
 
+    journal_output(pl, id, buf);
+
     printbuf[0] = '\0';
     printbuf[1] = '\0';
 
@@ -281,21 +292,20 @@ upr_player(struct player *pl, int id, char *buf)
            }
        }
        if (ch == '\n') {
-           if ((pl->command && (pl->command->c_flags & C_MOD)) ||
-               (player != pl))
-               io_write(pl->iop, &ch, 1, IO_NOWAIT);
-           else
-               io_write(pl->iop, &ch, 1, IO_WAIT);
+           io_write(pl->iop, &ch, 1);
            pl->curid = -1;
        } else {
            printbuf[0] = ch;
            io_puts(pl->iop, printbuf);
        }
     }
+
+    if (player == pl)
+       player_output_some();
 }
 
 /*
- * Send id N to PL.
+ * Send id @n to @pl.
  * This runs always at the beginning of a line.
  */
 static void
@@ -316,9 +326,18 @@ outid(struct player *pl, int n)
     pl->curid = n;
 }
 
+static void
+player_output_some(void)
+{
+    time_t deadline = player_io_deadline(player, 1);
+
+    while (io_output_if_queue_long(player->iop, deadline) > 0)
+       ;
+}
+
 /*
- * Send redirection request REDIR to the current player.
- * REDIR is UTF-8, but non-ASCII characters can occur only if the
+ * Send redirection request @redir to the current player.
+ * @redir is UTF-8, but non-ASCII characters can occur only if the
  * player sent them.  Therefore, it is also user text.
  */
 void
@@ -328,8 +347,8 @@ prredir(char *redir)
 }
 
 /*
- * Send script execute request FILE to the current player.
- * REDIR is UTF-8, but non-ASCII characters can occur only if the
+ * Send script execute request @file to the current player.
+ * @file is UTF-8, but non-ASCII characters can occur only if the
  * player sent them.  Therefore, it is also user text.
  */
 void
@@ -349,11 +368,11 @@ prprompt(int min, int btu)
 
 /*
  * Prompt for a line of non-command input.
- * Send C_FLUSH prompt PROMPT to the current player.
- * Read a line of input into BUF[SIZE] and convert it to ASCII.
+ * Send C_FLUSH prompt @prompt to the current player.
+ * Read a line of input into @buf[@size] and convert it to ASCII.
  * This may block for input, yielding the processor.  Flush buffered
  * output when blocking, to make sure player sees the prompt.
- * Return number of bytes in BUF[], not counting the terminating 0,
+ * Return number of bytes in @buf[], not counting the terminating 0,
  * or -1 on error.
  */
 int
@@ -361,11 +380,8 @@ prmptrd(char *prompt, char *buf, int size)
 {
     int r;
 
-    /*
-     * Each prompt must consume one line of input.  recvclient()
-     * doesn't do that while player->aborted.
-     */
-    CANT_HAPPEN(player->aborted);
+    if (CANT_HAPPEN(!prompt))
+       prompt = "? ";
 
     pr_id(player, C_FLUSH, "%s\n", prompt);
     if ((r = recvclient(buf, size)) < 0)
@@ -380,12 +396,12 @@ prmptrd(char *prompt, char *buf, int size)
 
 /*
  * Prompt for a line of non-command, UTF-8 input.
- * Send C_FLUSH prompt PROMPT to the current player.
- * Read a line of input into BUF[SIZE], replacing funny characters by
+ * Send C_FLUSH prompt @prompt to the current player.
+ * Read a line of input into @buf[@size], replacing funny characters by
  * '?'.  The result is UTF-8.
  * This may block for input, yielding the processor.  Flush buffered
  * output when blocking, to make sure player sees the prompt.
- * Return number of bytes in BUF[], not counting the terminating 0,
+ * Return number of bytes in @buf[], not counting the terminating 0,
  * or -1 on error.
  */
 int
@@ -393,8 +409,8 @@ uprmptrd(char *prompt, char *buf, int size)
 {
     int r;
 
-    /* See prmptrd() */
-    CANT_HAPPEN(player->aborted);
+    if (CANT_HAPPEN(!prompt))
+       prompt = "? ";
 
     pr_id(player, C_FLUSH, "%s\n", prompt);
     if ((r = recvclient(buf, size)) < 0)
@@ -416,69 +432,23 @@ prdate(void)
     time_t now;
 
     (void)time(&now);
-    pr(ctime(&now));
+    pr("%s", ctime(&now));
 }
 
 /*
- * Print coordinates X, Y for COUNTRY.
- * FORMAT must be a printf-style format string that converts exactly
+ * Print coordinates @x,@y.
+ * @format must be a printf-style format string that converts exactly
  * two int values.
  */
 void
-prxy(char *format, coord x, coord y, natid country)
+prxy(char *format, coord x, coord y)
 {
     struct natstr *np;
 
-    np = getnatp(country);
+    np = getnatp(player->cnum);
     pr(format, xrel(np, x), yrel(np, y));
 }
 
-/*
- * Print to country CN similar to printf().
- * Use printf-style FORMAT with the optional arguments.
- * Output is buffered until a newline arrives.
- * If CN is the current player and we're not in the update, print just
- * like pr().  Else print into a bulletin.
- * Because printing like pr() requires normal text, and bulletins
- * require user text, only plain ASCII is allowed.
- */
-void
-PR(int cn, char *format, ...)
-{
-    /* XXX should really do this on a per-nation basis */
-    static char longline[MAXNOC][512];
-    int newline;
-    va_list ap;
-    char buf[1024];
-
-    va_start(ap, format);
-    (void)vsprintf(buf, format, ap);
-    va_end(ap);
-    newline = strrchr(buf, '\n') ? 1 : 0;
-    strcat(longline[cn], buf);
-    if (newline) {
-       if (update_pending || (cn && cn != player->cnum))
-           typed_wu(0, cn, longline[cn], TEL_BULLETIN);
-       else
-           pr_player(player, C_DATA, longline[cn]);
-       longline[cn][0] = '\0';
-    }
-}
-
-/*
- * Print the current time in ctime() format to country CN.
- * If CN is the current player and we're not in the update, print just
- * like prdate().  Else print into a bulletin.
- */
-void
-PRdate(natid cn)
-{
-    time_t now;
-
-    (void)time(&now);
-    PR(cn, ctime(&now));
-}
-
 /*
  * Sound the current player's bell.
  */
@@ -492,10 +462,12 @@ pr_beep(void)
 }
 
 /*
- * Print to country CN similar to printf().
- * Use printf-style FORMAT with the optional arguments.
- * If CN is the current player and we're not in the update, print just
- * like pr().  Else print into a bulletin.
+ * Print complete lines to country @cn similar to printf().
+ * Use printf-style @format with the optional arguments.  @format must
+ * end with '\n'.
+ * If @cn is zero, don't print anything.
+ * Else, if @cn is the current player and we're not in the update,
+ * print just like pr().  Else print into a bulletin.
  * Because printing like pr() requires normal text, and bulletins
  * require user text, only plain ASCII is allowed.
  */
@@ -505,23 +477,24 @@ mpr(int cn, char *format, ...)
     char buf[4096];
     va_list ap;
 
+    CANT_HAPPEN(!format[0] || format[strlen(format) - 1] != '\n');
+    if (!cn)
+       return;
     va_start(ap, format);
     (void)vsprintf(buf, format, ap);
     va_end(ap);
-    if (cn) {
-       if (update_pending || cn != player->cnum)
-           typed_wu(0, cn, buf, TEL_BULLETIN);
-       else
-           pr_player(player, C_DATA, buf);
-    }
+    if (update_running || cn != player->cnum)
+       wu(0, cn, "%s", buf);
+    else
+       pr_player(player, C_DATA, buf);
 }
 
 /*
- * Copy SRC without funny characters to DST.
+ * Copy @src without funny characters to @dst.
  * Drop control characters, except for '\t'.
  * Replace non-ASCII characters by '?'.
- * Return length of DST.
- * DST must have space.  If it overlaps SRC, then DST <= SRC must
+ * Return length of @dst.
+ * @dst must have space.  If it overlaps @src, then @dst <= @src must
  * hold.
  */
 size_t
@@ -535,7 +508,7 @@ copy_ascii_no_funny(char *dst, char *src)
        if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
            ;                   /* ignore funny control */
        else if (ch > 0x7f)
-           *p++ = '?'; /* replace non-ASCII */
+           *p++ = '?';         /* replace non-ASCII */
        else
            *p++ = ch;
     }
@@ -545,11 +518,11 @@ copy_ascii_no_funny(char *dst, char *src)
 }
 
 /*
- * Copy UTF-8 SRC without funny characters to DST.
+ * Copy UTF-8 @src without funny characters to @dst.
  * Drop control characters, except for '\t'.
  * FIXME Replace malformed UTF-8 sequences by '?'.
- * Return byte length of DST.
- * DST must have space.  If it overlaps SRC, then DST <= SRC must
+ * Return byte length of @dst.
+ * @dst must have space.  If it overlaps @src, then @dst <= @src must
  * hold.
  */
 size_t
@@ -572,11 +545,11 @@ copy_utf8_no_funny(char *dst, char *src)
 }
 
 /*
- * Copy UTF-8 SRC without funny characters to ASCII DST.
+ * Copy UTF-8 @src without funny characters to ASCII @dst.
  * Drop control characters, except for '\t'.
  * Replace non-ASCII characters by '?'.
- * Return length of DST.
- * DST must have space.  If it overlaps SRC, then DST <= SRC must
+ * Return length of @dst.
+ * @dst must have space.  If it overlaps @src, then @dst <= @src must
  * hold.
  */
 size_t
@@ -592,7 +565,8 @@ copy_utf8_to_ascii_no_funny(char *dst, char *src)
            ;                   /* ignore funny control */
        else if (ch > 0x7f) {
            *p++ = '?';         /* replace non-ASCII */
-           while ((*src++ & 0xc0) == 0x80) ;
+           while ((*src & 0xc0) == 0x80)
+               src++;
        } else
            *p++ = ch;
     }
@@ -602,20 +576,19 @@ copy_utf8_to_ascii_no_funny(char *dst, char *src)
 }
 
 /*
- * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
- * If S doesn't have that many characters, return its length instead.
+ * Return byte-index of the @n-th UTF-8 character in UTF-8 string @s.
+ * If @s doesn't have that many characters, return its length instead.
  */
 int
 ufindpfx(char *s, int n)
 {
     int i = 0;
 
-    while (n && s[i])
-    {
+    while (n && s[i]) {
        if ((s[i++] & 0xc0) == 0xc0)
-            while ((s[i] & 0xc0) == 0x80)
+           while ((s[i] & 0xc0) == 0x80)
                i++;
-        --n;
+       --n;
     }
     return i;
 }