]> git.pond.sub.org Git - empserver/blobdiff - src/client/play.c
Update copyright notice
[empserver] / src / client / play.c
index 2f75b78326cfb6b27696cf9cb026f0f96801a4b5..b66f7fdc394e6ee1b028559a9df122631caadfdc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -39,6 +39,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #ifdef _WIN32
 #include <process.h>
 #include <sys/socket.h>
@@ -332,10 +333,10 @@ recv_output(int sock)
      * simple state machine.
      * Initial state is SCANNING_ID.
      * In state SCANNING_ID, buffer the character.  If it's a space,
-     * decode the id that has been buffered, and enter state BUFFERING
+     * decode the ID that has been buffered, and enter state BUFFERING
      * or COPYING depending on its value.
      * In state BUFFERING, buffer the character.  If it's newline,
-     * pass id and buffered text to servercmd(), then enter state
+     * pass ID and buffered text to servercmd(), then enter state
      * SCANNING_ID.
      * In state COPYING, pass the character to outch().  If it's
      * newline, enter state SCANNING_ID.
@@ -382,7 +383,7 @@ recv_output(int sock)
                state = BUFFERING;
                break;
            default:
-               /* unknown or unexpected id, treat like C_DATA */
+               /* unknown or unexpected ID, treat like C_DATA */
            case C_DATA:
                state = COPYING;
                break;
@@ -421,6 +422,7 @@ recv_output(int sock)
 }
 
 #ifdef HAVE_LIBREADLINE
+static int use_readline;
 static char *input_from_rl;
 static int has_rl_input;
 
@@ -471,7 +473,7 @@ recv_input(int fd, struct ring *inbuf)
     int res = 1;
 
 #ifdef HAVE_LIBREADLINE
-    if (fd == 0) {
+    if (fd == 0 && use_readline) {
        if (!has_rl_input)
            rl_callback_read_char();
        if (!has_rl_input)
@@ -487,7 +489,7 @@ recv_input(int fd, struct ring *inbuf)
        return -1;
     if (n == 0) {
        /*
-        * Can't put EOF cookie into INBUF here, it may not fit.
+        * Can't put EOF cookie into @inbuf here, it may not fit.
         * Leave it to caller.
         */
        res = 0;
@@ -519,7 +521,7 @@ send_input(int fd, struct ring *inbuf)
     }
 
 #ifdef HAVE_LIBREADLINE
-    if (fd == 0 && has_rl_input && input_from_rl)
+    if (fd == 0 && use_readline && has_rl_input && input_from_rl)
        ring_from_rl(inbuf);
 #endif
 
@@ -542,10 +544,10 @@ int
 play(int sock, char *history_file)
 {
     /*
-     * Player input flows from INPUT_FD through recv_input() into ring
-     * buffer INBUF, which drains into SOCK.  This must not block.
-     * Server output flows from SOCK into recv_output().  Reading SOCK
-     * must not block.
+     * Player input flows from @input_fd through recv_input() into
+     * ring buffer @inbuf, which drains into @sock.  This must not
+     * block.  Server output flows from @sock into recv_output().
+     * Reading @sock must not block.
      */
     struct sigaction sa;
     struct ring inbuf;         /* input buffer, draining to SOCK */
@@ -563,11 +565,15 @@ play(int sock, char *history_file)
     sa.sa_handler = SIG_IGN;
     sigaction(SIGPIPE, &sa, NULL);
 #ifdef HAVE_LIBREADLINE
-    rl_already_prompted = 1;
-    if (history_file)
-       read_history(history_file);
-    rl_bind_key('\t', rl_insert);  /* Disable tab completion */
-    rl_callback_handler_install("", input_handler);
+    if (isatty(0)) {
+       use_readline = 1;
+       rl_already_prompted = 1;
+       rl_readline_name = "Empire";
+       if (history_file)
+           read_history(history_file);
+       rl_bind_key('\t', rl_insert);  /* Disable tab completion */
+       rl_callback_handler_install("", input_handler);
+    }
 #endif /* HAVE_LIBREADLINE */
 
     ring_init(&inbuf);
@@ -670,9 +676,11 @@ play(int sock, char *history_file)
     }
 
 #ifdef HAVE_LIBREADLINE
-    rl_callback_handler_remove();
-    if (history_file)
-       write_history(history_file);
+    if (use_readline) {
+       rl_callback_handler_remove();
+       if (history_file)
+           write_history(history_file);
+    }
 #endif
     return ret;
 }
@@ -684,12 +692,15 @@ prompt(int code, char *prompt, char *teles)
 
     snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
 #ifdef HAVE_LIBREADLINE
-    rl_set_prompt(pr);
-    rl_forced_update_display();
-#else  /* !HAVE_LIBREADLINE */
-    printf("%s", pr);
-    fflush(stdout);
-#endif /* !HAVE_LIBREADLINE */
+    if (use_readline) {
+       rl_set_prompt(pr);
+       rl_forced_update_display();
+    } else
+#endif /* HAVE_LIBREADLINE */
+    {
+       printf("%s", pr);
+       fflush(stdout);
+    }
     if (auxfp) {
        fprintf(auxfp, "%s%s", teles, prompt);
        fflush(auxfp);