]> git.pond.sub.org Git - empserver/blobdiff - src/client/termio.c
Update copyright notice.
[empserver] / src / client / termio.c
index 49885031eabcc495e4373ebc60c67d1cfeb9f24e..fa710c37a7c04d3810c5ea07e717811e50356eed 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-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *     Steve McClure, 1998
  */
 
+#include <config.h>
+
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#ifndef _WIN32
-#include <unistd.h>
-#else
-#include <winsock.h>
+#ifdef _WIN32
 #include <io.h>
-#endif /* _WIN32 */
+#else
+#include <unistd.h>
+#endif
 #include "misc.h"
 #include "tags.h"
 
+static int sendeof(int sock);
+
 int
 termio(int fd, int sock, FILE *auxfi)
 {
@@ -59,7 +62,8 @@ termio(int fd, int sock, FILE *auxfi)
 #ifdef _WIN32
     char c;
     INPUT_RECORD InpBuffer[2];
-    int err;
+    int ret;
+    DWORD records;
 #endif
 
     i = strlen(buf);
@@ -71,36 +75,64 @@ termio(int fd, int sock, FILE *auxfi)
  * events for the same key.  Thus, we only want to grab keydown
  * events. */
     if (fd == -1) {
-       err = PeekConsoleInput(hStdIn, InpBuffer, 1, &n);
+       ret = PeekConsoleInput(hStdIn, InpBuffer, 1, &records);
+       if (!ret) {
+           fprintf(stderr, "Error peeking the console input (%lu)\n",
+               GetLastError());
+           return 0;
+       }
+       if (records == 0)
+           return 0;
        if (InpBuffer[0].EventType != KEY_EVENT) {
-           ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
+           ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
+           if (!ret) {
+               fprintf(stderr, "Error reading the console input (%lu)\n",
+                   GetLastError());
+               return 0;
+           }
+           if (records == 0)
+               return 0;
            return 1;
        }
        if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
-           ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
+           ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
+           if (!ret) {
+               fprintf(stderr, "Error reading the console input (%lu)\n",
+                   GetLastError());
+               return 0;
+           }
+           if (records == 0)
+               return 0;
            return 1;
        }
        c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
 
        if (c == 13)
            c = 10;
-       n = 1;
+       records = 1;
        p[0] = c;
        p[1] = '\0';
-       if (c != 10)
-           ReadConsole(hStdIn, p, sizeof(buf) - i, &n, NULL);
-       else
+       if (c != 10) {
+           ret = ReadConsole(hStdIn, p, sizeof(buf) - i, &records, NULL);
+           if (!ret) {
+               fprintf(stderr, "Error reading the console (%lu)\n",
+                   GetLastError());
+               return 0;
+           }
+       } else
            putchar(c);
 /* Strip off the CRLF to just LF */
-       if (n > 1) {
-           if (p[n - 2] == 13 && p[n - 1] == 10) {
-               p[n - 2] = 10;
-               p[n - 1] = 0;
-               n--;
+       if (records > 1) {
+           if (p[records - 2] == 13 && p[records - 1] == 10) {
+               p[records - 2] = 10;
+               p[records - 1] = 0;
+               records--;
            }
        }
        FlushConsoleInputBuffer(hStdIn);
-       if (n == 0) return 1;
+       if (records == 0)
+           return 0;
+       n = records;
     } else if (fd == 0) {
        if (feof(stdin)) {
            sendeof(sock);
@@ -133,8 +165,8 @@ termio(int fd, int sock, FILE *auxfi)
     while (p < buf + n && q < out + 4000) {
        if (*p == '\n') {
            if (tagging) {
-               tag = (struct tagstruct *)malloc(sizeof(struct tagstruct));
-               tag->item = (char *)malloc((1 + p - s) * sizeof(char));
+               tag = malloc(sizeof(struct tagstruct));
+               tag->item = malloc(1 + p - s);
                tag->next = taglist;
                taglist = tag;
                t = tag->item;
@@ -213,7 +245,7 @@ termio(int fd, int sock, FILE *auxfi)
     return 1;
 }
 
-int
+static int
 sendeof(int sock)
 {
 #ifndef _WIN32