]> git.pond.sub.org Git - empserver/blobdiff - src/client/servcmd.c
client: Add readline support to empire client
[empserver] / src / client / servcmd.c
index 4bd97408ae0d2b302278fc4562e4c6be263f9ba8..a8cce6fd44cc3c86a569b7a64ac63043e8afbd1f 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2015, 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/>.
  *
  *  ---
  *
@@ -31,7 +30,7 @@
  *     Dave Pare, 1989
  *     Steve McClure, 1998
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005-2007
+ *     Markus Armbruster, 2005-2017
  */
 
 #include <config.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include "misc.h"
 #include "proto.h"
 #include "secure.h"
 
-#ifdef _WIN32
-    #define open(filename, flags, ...) \
-        ((flags & O_CREAT) \
-         ? w32_openfd((filename), (flags), ## __VA_ARGS__) \
-         : w32_openhandle((filename), (flags)))
-#endif
+#ifdef HAVE_LIBREADLINE
+#  if defined(HAVE_READLINE_READLINE_H)
+#    include <readline/readline.h>
+#  elif defined(HAVE_READLINE_H)
+#    include <readline.h>
+#  endif /* defined(HAVE_READLINE_H) */
+#endif /* HAVE_LIBREADLINE */
 
 int eight_bit_clean;
 FILE *auxfp;
+int restricted;
 
 static FILE *redir_fp;
 static int redir_is_pipe;
 static int executing;
-static size_t input_to_forget;
 
 static void prompt(int, char *, char *);
 static void doredir(char *p);
 static void dopipe(char *p);
 static int doexecute(char *p);
 
-void
+int
 servercmd(int code, char *arg, int len)
 {
     static int nmin, nbtu, fd;
@@ -76,7 +77,8 @@ servercmd(int code, char *arg, int len)
     switch (code) {
     case C_PROMPT:
        if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
-           fprintf(stderr, "prompt: bad server prompt %s\n", arg);
+           fprintf(stderr, "Warning: server sent malformed prompt %s",
+                   arg);
        }
        snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
                 nmin, nbtu);
@@ -87,10 +89,6 @@ servercmd(int code, char *arg, int len)
                (void)fclose(redir_fp);
            redir_fp = NULL;
        }
-       if (input_to_forget) {
-           forget_input(input_to_forget);
-           input_to_forget = 0;
-       }
        prompt(code, the_prompt, teles);
        executing = 0;
        break;
@@ -100,13 +98,9 @@ servercmd(int code, char *arg, int len)
        break;
     case C_EXECUTE:
        fd = doexecute(arg);
-       if (fd < 0)
-           send_eof++;
-       else {
-           input_fd = fd;
+       if (fd >= 0)
            executing = 1;
-       }
-       break;
+       return fd;
     case C_EXIT:
        printf("Exit: %s", arg);
        if (auxfp)
@@ -119,7 +113,7 @@ servercmd(int code, char *arg, int len)
        break;
     case C_INFORM:
        if (arg[0] != '\n') {
-           snprintf(teles, sizeof(teles), "(%.*s) ", len -1, arg);
+           snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
            if (!redir_fp) {
                putchar('\07');
                prompt(code, the_prompt, teles);
@@ -137,16 +131,26 @@ servercmd(int code, char *arg, int len)
        assert(0);
        break;
     }
+
+    return 0;
 }
 
 static void
 prompt(int code, char *prompt, char *teles)
 {
     char *nl;
+    char pr[1024];
 
     nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
-    printf("%s%s%s", nl, teles, prompt);
+    snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
+#ifdef HAVE_LIBREADLINE
+    rl_set_prompt(pr);
+    printf("%s", nl);
+    rl_forced_update_display();
+#else  /* !HAVE_LIBREADLINE */
+    printf("%s%s", nl, pr);
     fflush(stdout);
+#endif /* !HAVE_LIBREADLINE */
     if (auxfp) {
        fprintf(auxfp, "%s%s%s", nl, teles, prompt);
        fflush(auxfp);
@@ -165,28 +169,48 @@ fname(char *s)
 }
 
 static int
-redir_authorized(char *arg, char *attempt, int expected)
+common_authorized(char *arg, char *attempt)
 {
-    size_t seen = seen_input(arg);
+    if (restricted) {
+       fprintf(stderr, "Can't %s in restricted mode\n", attempt);
+       return 0;
+    }
 
     if (executing) {
        fprintf(stderr, "Can't %s in a batch file\n", attempt);
        return 0;
     }
+    return 1;
+}
 
-    if (!expected) {
-       fprintf(stderr, "WARNING!  Server attempted to %s unexpectedly\n",
-               attempt);
+static int
+redir_authorized(char *arg, char *attempt)
+{
+    if (redir_fp) {
+       fprintf(stderr, "Warning: dropped conflicting %s %s",
+               attempt, arg);
        return 0;
     }
 
-    if (!seen || (input_to_forget && input_to_forget != seen)) {
-       fprintf(stderr, "WARNING!  Server attempted to %s %s\n",
+    if (!seen_input(arg)) {
+       fprintf(stderr, "Warning: server attempted to %s %s",
                attempt, arg);
        return 0;
     }
-    input_to_forget = seen;
-    return 1;
+
+    return common_authorized(arg, attempt);
+}
+
+static int
+exec_authorized(char *arg)
+{
+    if (!seen_exec_input(arg)) {
+       fprintf(stderr,
+               "Warning: server attempted to execute batch file %s", arg);
+       return 0;
+    }
+
+    return common_authorized(arg, "execute batch file");
 }
 
 static void
@@ -195,10 +219,10 @@ doredir(char *p)
     int mode;
     int fd;
 
-    if (!redir_authorized(p, "redirect to file", !redir_fp))
+    if (!redir_authorized(p, "redirect to file"))
        return;
     if (*p++ != '>') {
-       fprintf(stderr, "WARNING!  Weird redirection %s", p);
+       fprintf(stderr, "Warning: dropped weird redirection %s", p);
        return;
     }
 
@@ -230,10 +254,10 @@ doredir(char *p)
 static void
 dopipe(char *p)
 {
-    if (!redir_authorized(p, "pipe to shell command", !redir_fp))
+    if (!redir_authorized(p, "pipe to shell command"))
        return;
     if (*p++ != '|') {
-       fprintf(stderr, "WARNING!  Weird pipe %s", p);
+       fprintf(stderr, "Warning: dropped weird pipe %s", p);
        return;
     }
 
@@ -243,10 +267,14 @@ dopipe(char *p)
        return;
     }
 
+    /* strip newline */
+    p[strlen(p) - 1] = 0;
+
     redir_is_pipe = 1;
+    errno = 0;
     if ((redir_fp = popen(p, "w")) == NULL) {
-       fprintf(stderr, "Can't redirect to pipe %s%s\n",
-               p, strerror(errno));
+       fprintf(stderr, "Can't redirect to pipe %s%s%s\n",
+               p, errno ? ": " : "", errno ? strerror(errno) : "");
     }
 }
 
@@ -255,7 +283,7 @@ doexecute(char *p)
 {
     int fd;
 
-    if (!redir_authorized(p, "execute batch file", 1))
+    if (!exec_authorized(p))
        return -1;
 
     p = fname(p);
@@ -265,7 +293,7 @@ doexecute(char *p)
     }
 
     if ((fd = open(p, O_RDONLY)) < 0) {
-       fprintf(stderr, "Can't open execute file %s: %s\n",
+       fprintf(stderr, "Can't open batch file %s: %s\n",
                p, strerror(errno));
        return -1;
     }