]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/pr.c
Fix journalling of output ids
[empserver] / src / lib / subs / pr.c
index 8a4e72800325376f5782b662a1ae379b7a26811d..496bde49ba2901164b051a6435301d12ca34a74b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
- *  pr.c: Use to do output to a player
+ *  pr.c: Output to players
  * 
  *  Known contributors to this file:
  *     Dave Pare, 1986, 1989 
  *     Steve McClure, 1998-2000
+ *     Ron Koenderink, 2005
+ *     Markus Armbruster, 2005-2007
  */
+
 /*
- * The pr routine historically arranged for nonbuffered i/o
- * because stdio didn't used to automatically flush stdout before
- * it read something from stdin.  Now pr() prepends an "output id"
- * in front of each line of text, informing the user interface
- * what sort of item it is seeing; prompt, noecho prompt,
- * more input data, etc.
+ * Player output is fully buffered.  It can block only if the
+ * receiving player is the current player and his last command doesn't
+ * have the C_MOD flag.  Output to another player must not block
+ * because that player could be gone when the printing thread wakes
+ * up, and the code isn't prepared for that.  Output within C_MOD
+ * command never blocks, so that such commands can print freely
+ * without yielding the processor.
+ *
+ * Each line of output starts with an identification character
+ * encoding the output id, followed by space.  Ids less than 10 are
+ * encoded as decimal digits, and larger ids as lower case letters,
+ * starting with 'a'.  Symbolic names for ids are defined in proto.h.
  */
 
-#include <string.h>
-#include <fcntl.h>
-#include <ctype.h>
+#include <config.h>
+
 #include <stdarg.h>
-#include "proto.h"
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
+#include <stdlib.h>
+#include "com.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 "tel.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);
 
 /*
@@ -103,8 +115,7 @@ uprnf(char *buf)
  * 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.
- * If a partial line with different id is buffered, terminate it with
- * a newline first.
+ * If a partial line is buffered, terminate it with a newline first.
  */
 void
 pr_id(struct player *p, int id, char *format, ...)
@@ -114,6 +125,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);
@@ -126,6 +138,7 @@ 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
  * arguments.  It is assumed to be UTF-8.
+ * Initiate an output queue flush, but do not wait for it to complete.
  */
 void
 pr_flash(struct player *pl, char *format, ...)
@@ -148,6 +161,7 @@ pr_flash(struct player *pl, char *format, ...)
  * 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.
  */
 void
 pr_inform(struct player *pl, char *format, ...)
@@ -168,16 +182,26 @@ pr_inform(struct player *pl, char *format, ...)
  * Send C_FLASH text to everyone.
  * 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)) {
        if (p->state != PS_PLAYING)
@@ -193,7 +217,7 @@ pr_wall(char *format, ...)
  * If a partial line with different id is buffered, terminate it with
  * a newline first.
  */
-void
+static void
 pr_player(struct player *pl, int id, char *buf)
 {
     char *p;
@@ -211,7 +235,8 @@ 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))
+           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);
@@ -222,14 +247,16 @@ pr_player(struct player *pl, int id, char *buf)
            bp += len;
        }
     }
+    journal_output(pl, id, buf);
 }
 
 /*
  * 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.
  */
-void
+static void
 upr_player(struct player *pl, int id, char *buf)
 {
     char *bp;
@@ -264,7 +291,8 @@ upr_player(struct player *pl, int id, char *buf)
            }
        }
        if (ch == '\n') {
-           if (pl->command && (pl->command->c_flags & C_MOD))
+           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);
@@ -274,26 +302,7 @@ upr_player(struct player *pl, int id, char *buf)
            io_puts(pl->iop, printbuf);
        }
     }
-}
-
-/*
- * highlighted characters have hex 80 or'ed in
- * with them to designate their highlightedness
- */
-void
-pr_hilite(s_char *buf)
-{
-    register s_char *bp;
-    register s_char c;
-    s_char *p;
-
-    p = malloc(strlen(buf) + 1);
-    strcpy(p, buf);
-    for (bp = p; 0 != (c = *bp); bp++)
-       if (isprint(c))
-           *bp |= 0x80;
-    pr(p);
-    free(p);
+    journal_output(pl, id, buf);
 }
 
 /*
@@ -353,6 +362,8 @@ 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.
+ * 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,
  * or -1 on error.
  */
@@ -360,7 +371,9 @@ int
 prmptrd(char *prompt, char *buf, int size)
 {
     int r;
-    char *cp;
+
+    if (CANT_HAPPEN(!prompt))
+       prompt = "? ";
 
     pr_id(player, C_FLUSH, "%s\n", prompt);
     if ((r = recvclient(buf, size)) < 0)
@@ -378,6 +391,8 @@ prmptrd(char *prompt, char *buf, int size)
  * 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,
  * or -1 on error.
  */
@@ -385,7 +400,9 @@ int
 uprmptrd(char *prompt, char *buf, int size)
 {
     int r;
-    char *cp;
+
+    if (CANT_HAPPEN(!prompt))
+       prompt = "? ";
 
     pr_id(player, C_FLUSH, "%s\n", prompt);
     if ((r = recvclient(buf, size)) < 0)
@@ -418,20 +435,18 @@ prdate(void)
 void
 prxy(char *format, coord x, coord y, natid country)
 {
-    char buf[255];
     struct natstr *np;
 
     np = getnatp(country);
-    sprintf(buf, format, xrel(np, x), yrel(np, y));
-    pr(buf);
+    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, print just like pr().
- * Else print into a bulletin.
+ * 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.
  */
@@ -450,7 +465,7 @@ PR(int cn, char *format, ...)
     newline = strrchr(buf, '\n') ? 1 : 0;
     strcat(longline[cn], buf);
     if (newline) {
-       if (update_pending || (cn && cn != player->cnum))
+       if (update_running || (cn && cn != player->cnum))
            typed_wu(0, cn, longline[cn], TEL_BULLETIN);
        else
            pr_player(player, C_DATA, longline[cn]);
@@ -460,8 +475,8 @@ PR(int cn, char *format, ...)
 
 /*
  * Print the current time in ctime() format to country CN.
- * If CN is the current player, print like prdate().
- * Else print into a bulletin.
+ * 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)
@@ -487,8 +502,9 @@ pr_beep(void)
 /*
  * Print to country CN similar to printf().
  * Use printf-style FORMAT with the optional arguments.
- * If CN is the current player, print just like pr().
- * Else print into a bulletin.
+ * 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.
  */
@@ -498,15 +514,15 @@ mpr(int cn, char *format, ...)
     char buf[4096];
     va_list ap;
 
+    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)
+       typed_wu(0, cn, buf, TEL_BULLETIN);
+    else
+       pr_player(player, C_DATA, buf);
 }
 
 /*
@@ -525,8 +541,8 @@ copy_ascii_no_funny(char *dst, char *src)
 
     p = dst;
     while ((ch = *src++)) {
-       if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
-           ;                   /* ignore control */
+       if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
+           ;                   /* ignore funny control */
        else if (ch > 0x7f)
            *p++ = '?'; /* replace non-ASCII */
        else
@@ -554,8 +570,8 @@ copy_utf8_no_funny(char *dst, char *src)
     p = dst;
     while ((ch = *src++)) {
        /* FIXME do the right thing for malformed and overlong sequences */
-       if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
-           ;                   /* ignore control */
+       if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
+           ;                   /* ignore funny control */
        else
            *p++ = ch;
     }
@@ -581,8 +597,8 @@ copy_utf8_to_ascii_no_funny(char *dst, char *src)
     p = dst;
     while ((ch = *src++)) {
        /* FIXME do the right thing for malformed and overlong sequences */
-       if ((ch < 0x20 && ch != '\t') || ch == 0x7f)
-           ;                   /* ignore control */
+       if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
+           ;                   /* ignore funny control */
        else if (ch > 0x7f) {
            *p++ = '?';         /* replace non-ASCII */
            while ((*src++ & 0xc0) == 0x80) ;