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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * termio.c: Various io functions
30 * Known contributors to this file:
48 static int sendeof(int sock);
51 termio(int fd, int sock, FILE *auxfi)
53 static char exec[] = "execute";
54 static char buf[4096];
58 char *p, *q, *r, *s, *t;
60 int prespace, exec_com, inarg, quoted, tagging;
61 struct tagstruct *tag;
64 INPUT_RECORD InpBuffer[2];
72 n = read(fd, p, sizeof(buf) - i);
74 /* The keyboard is sometimes generating both keydown and keyup
75 * events for the same key. Thus, we only want to grab keydown
78 ret = PeekConsoleInput(hStdIn, InpBuffer, 1, &records);
80 fprintf(stderr, "Error peeking the console input (%lu)\n",
86 if (InpBuffer[0].EventType != KEY_EVENT) {
87 ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
89 fprintf(stderr, "Error reading the console input (%lu)\n",
97 if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
98 ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
100 fprintf(stderr, "Error reading the console input (%lu)\n",
108 c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
116 ret = ReadConsole(hStdIn, p, sizeof(buf) - i, &records, NULL);
118 fprintf(stderr, "Error reading the console (%lu)\n",
124 /* Strip off the CRLF to just LF */
126 if (p[records - 2] == 13 && p[records - 1] == 10) {
132 FlushConsoleInputBuffer(hStdIn);
136 } else if (fd == 0) {
141 fgets(p, sizeof(buf) - i, stdin);
144 n = read(fd, p, sizeof(buf) - i);
152 perror("read standard input");
165 while (p < buf + n && q < out + 4000) {
168 tag = malloc(sizeof(struct tagstruct));
169 tag->item = malloc(1 + p - s);
185 } else if (tagging) {
187 } else if (!quoted && isspace(*p)) {
190 if (exec_com && s > exec + 2) {
194 } else if (prespace && *p == '|') {
198 } else if (prespace && *p == '>') {
201 if (*p != '\n' && (*p == '!' || *p == '>'))
209 if (!inarg && *p != '?') {
214 if (exec_com && *s && *s++ != *p)
221 while (ptr < buf + n)
227 fwrite(out, n, 1, auxfi);
231 nbytes = write(sock, ptr, n);
233 nbytes = send(sock, ptr, n, 0);
237 errno = WSAGetLastError();
239 perror("write server socket");
252 if (write(sock, "ctld\n", 5) < 5) {
254 if (send(sock, "ctld\n", 5, 0) < 5) {
256 fprintf(stderr, "sendeof: EOF send failed\n");