]> git.pond.sub.org Git - empserver/blobdiff - src/client/secure.c
Merge branch 'readline'
[empserver] / src / client / secure.c
index 401eaefd74261f94eaed5d3eb15a55f51132f187..14bb6d1fe27ef93eef90a16b8371177debffb51b 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-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *  secure.c: Check redir etc. to protect against tampering deity
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2007-2015
+ *     Markus Armbruster, 2007-2017
  */
 
 #include <config.h>
 
 #include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
 #include <string.h>
 #include "ringbuf.h"
 #include "secure.h"
 static struct ring recent_input;
 
 /*
- * Remember line of input @inp for a while.
- * It must end with a newline.
+ * Remember input @inp for a while.
  */
 void
-save_input(char *inp)
+save_input(char inp)
 {
-    size_t len = strlen(inp);
     int eol;
 
-    assert(len && inp[len - 1] == '\n');
-
-    while (ring_putm(&recent_input, inp, len) < 0) {
-       eol = ring_search(&recent_input, "\n");
+    while (ring_putc(&recent_input, inp) < 0) {
+       eol = ring_search(&recent_input, "\n", 0);
        assert(eol >= 0);
        ring_discard(&recent_input, eol + 1);
     }
@@ -68,5 +66,42 @@ seen_input(char *tail)
     size_t len = strlen(tail);
 
     assert(len && tail[len - 1] == '\n');
-    return ring_search(&recent_input, tail) >= 0;
+    return ring_search(&recent_input, tail, 0) >= 0;
+}
+
+/*
+ * Can you still remember input that looks like an execute @arg?
+ * @arg must end with a newline.
+ */
+int
+seen_exec_input(char *arg)
+{
+    size_t len = strlen(arg);
+    int n, i, j, ch;
+    unsigned char buf[RING_SIZE + 1];
+
+    assert(len && arg[len - 1] == '\n');
+
+    n = 1;
+    for (;;) {
+       /* find next line ending with arg */
+       n = ring_search(&recent_input, arg, n + 1);
+       if (n <= 0)
+           return 0;
+
+       /* extract command (same or previous line) */
+       i = n - 1;
+       if (ring_peek(&recent_input, i) == '\n')
+           i--;
+       j = sizeof(buf);
+       buf[--j] = 0;
+       for (; i >= 0 && (ch = ring_peek(&recent_input, i)) != '\n'; i--)
+           buf[--j] = ch;
+
+       /* compare command */
+       for (; isspace(buf[j]); j++) ;
+       for (i = j; buf[i] && !isspace(buf[i]); i++) ;
+       if (i - j >= 2 && !strncmp("execute", (char *)buf + j, i - j))
+           return 1;
+    }
 }