2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * termio.c: Various io functions
30 * Known contributors to this file:
39 #include <sys/types.h>
50 termio(int fd, int sock, FILE *auxfi)
52 static char exec[] = "execute";
53 static char buf[4096];
57 char *p, *q, *r, *s, *t;
59 int prespace, exec_com, inarg, quoted, tagging;
60 struct tagstruct *tag;
63 INPUT_RECORD InpBuffer[2];
71 n = read(fd, p, sizeof(buf) - i);
73 /* The keyboard is sometimes generating both keydown and keyup
74 * events for the same key. Thus, we only want to grab keydown
77 ret = PeekConsoleInput(hStdIn, InpBuffer, 1, &records);
79 fprintf(stderr, "Error peeking the console input (%lu)\n",
85 if (InpBuffer[0].EventType != KEY_EVENT) {
86 ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
88 fprintf(stderr, "Error reading the console input (%lu)\n",
96 if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
97 ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
99 fprintf(stderr, "Error reading the console input (%lu)\n",
107 c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
115 ret = ReadConsole(hStdIn, p, sizeof(buf) - i, &records, NULL);
117 fprintf(stderr, "Error reading the console (%lu)\n",
123 /* Strip off the CRLF to just LF */
125 if (p[n - 2] == 13 && p[n - 1] == 10) {
131 FlushConsoleInputBuffer(hStdIn);
135 } else if (fd == 0) {
140 fgets(p, sizeof(buf) - i, stdin);
143 n = read(fd, p, sizeof(buf) - i);
151 perror("read standard input");
164 while (p < buf + n && q < out + 4000) {
167 tag = malloc(sizeof(struct tagstruct));
168 tag->item = malloc((1 + p - s) * sizeof(char));
184 } else if (tagging) {
186 } else if (!quoted && isspace(*p)) {
189 if (exec_com && s > exec + 2) {
193 } else if (prespace && *p == '|') {
197 } else if (prespace && *p == '>') {
200 if (*p != '\n' && (*p == '!' || *p == '>'))
208 if (!inarg && *p != '?') {
213 if (exec_com && *s && *s++ != *p)
220 while (ptr < buf + n)
226 fwrite(out, n, 1, auxfi);
230 nbytes = write(sock, ptr, n);
232 nbytes = send(sock, ptr, n, 0);
236 errno = WSAGetLastError();
238 perror("write server socket");
251 if (write(sock, "ctld\n", 5) < 5) {
253 if (send(sock, "ctld\n", 5, 0) < 5) {
255 fprintf(stderr, "sendeof: EOF send failed\n");