]> git.pond.sub.org Git - empserver/blob - src/client/termio.c
58932cc0605e63acf01bef374f59ce67bc1fcd6c
[empserver] / src / client / termio.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  termio.c: Various io functions
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include <config.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #ifndef _WIN32
41 #include <unistd.h>
42 #else
43 #include <winsock.h>
44 #include <io.h>
45 #endif /* _WIN32 */
46 #include "misc.h"
47 #include "tags.h"
48
49 int
50 termio(int fd, int sock, FILE *auxfi)
51 {
52     static char exec[] = "execute";
53     static char buf[4096];
54     char out[4096];
55     int i, n;
56     char *ptr;
57     char *p, *q, *r, *s, *t;
58     int nbytes;
59     int prespace, exec_com, inarg, quoted, tagging;
60     struct tagstruct *tag;
61 #ifdef _WIN32
62     char c;
63     INPUT_RECORD InpBuffer[2];
64     int ret;
65     DWORD records;
66 #endif
67
68     i = strlen(buf);
69     p = buf + i;
70 #ifndef _WIN32
71     n = read(fd, p, sizeof(buf) - i);
72 #else
73 /* The keyboard is sometimes generating both keydown and keyup
74  * events for the same key.  Thus, we only want to grab keydown
75  * events. */
76     if (fd == -1) {
77         ret = PeekConsoleInput(hStdIn, InpBuffer, 1, &records);
78         if (!ret) {
79             fprintf(stderr, "Error peeking the console input (%lu)\n",
80                 GetLastError());
81             return 0;
82         }
83         if (records == 0)
84             return 0;
85         if (InpBuffer[0].EventType != KEY_EVENT) {
86             ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
87             if (!ret) {
88                 fprintf(stderr, "Error reading the console input (%lu)\n",
89                     GetLastError());
90                 return 0;
91             }
92             if (records == 0)
93                 return 0;
94             return 1;
95         }
96         if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
97             ret = ReadConsoleInput(hStdIn, InpBuffer, 1, &records);
98             if (!ret) {
99                 fprintf(stderr, "Error reading the console input (%lu)\n",
100                     GetLastError());
101                 return 0;
102             }
103             if (records == 0)
104                 return 0;
105             return 1;
106         }
107         c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
108
109         if (c == 13)
110             c = 10;
111         n = 1;
112         p[0] = c;
113         p[1] = '\0';
114         if (c != 10) {
115             ret = ReadConsole(hStdIn, p, sizeof(buf) - i, &records, NULL);
116             if (!ret) {
117                 fprintf(stderr, "Error reading the console (%lu)\n",
118                     GetLastError());
119                 return 0;
120             }
121         } else
122             putchar(c);
123 /* Strip off the CRLF to just LF */
124         if (n > 1) {
125             if (p[n - 2] == 13 && p[n - 1] == 10) {
126                 p[n - 2] = 10;
127                 p[n - 1] = 0;
128                 n--;
129             }
130         }
131         FlushConsoleInputBuffer(hStdIn);
132         if (records == 0)
133             return 0;
134         n = records;
135     } else if (fd == 0) {
136         if (feof(stdin)) {
137             sendeof(sock);
138             return 0;
139         }
140         fgets(p, sizeof(buf) - i, stdin);
141         n = strlen(p);
142     } else {
143         n = read(fd, p, sizeof(buf) - i);
144     }
145 #endif
146     if (n == 0) {
147         sendeof(sock);
148         return 0;
149     }
150     if (n < 0) {
151         perror("read standard input");
152         return 0;
153     }
154     n += i;
155     ptr = buf;
156     p = buf;
157     q = out;
158     r = out;
159     tagging = 0;
160     inarg = 0;
161     prespace = 1;
162     quoted = 0;
163     exec_com = 0;
164     while (p < buf + n && q < out + 4000) {
165         if (*p == '\n') {
166             if (tagging) {
167                 tag = malloc(sizeof(struct tagstruct));
168                 tag->item = malloc((1 + p - s) * sizeof(char));
169                 tag->next = taglist;
170                 taglist = tag;
171                 t = tag->item;
172                 while (s < p)
173                     *t++ = *s++;
174                 *t = 0;
175             }
176             *q++ = *p++;
177             tagging = 0;
178             inarg = 0;
179             prespace = 1;
180             quoted = 0;
181             exec_com = 0;
182             ptr = p;
183             r = q;
184         } else if (tagging) {
185             *q++ = *p++;
186         } else if (!quoted && isspace(*p)) {
187             *q++ = *p++;
188             prespace = 1;
189             if (exec_com && s > exec + 2) {
190                 tagging = 1;
191                 s = p;
192             }
193         } else if (prespace && *p == '|') {
194             tagging = 1;
195             *q++ = *p++;
196             s = p;
197         } else if (prespace && *p == '>') {
198             tagging = 1;
199             *q++ = *p++;
200             if (*p != '\n' && (*p == '!' || *p == '>'))
201                 *q++ = *p++;
202             s = p;
203         } else {
204             prespace = 0;
205             if (*p == '"') {
206                 quoted = !quoted;
207             } else {
208                 if (!inarg && *p != '?') {
209                     s = exec;
210                     exec_com = 1;
211                     inarg = 1;
212                 }
213                 if (exec_com && *s && *s++ != *p)
214                     exec_com = 0;
215             }
216             *q++ = *p++;
217         }
218     }
219     p = buf;
220     while (ptr < buf + n)
221         *p++ = *ptr++;
222     *p = 0;
223     ptr = out;
224     n = r - out;
225     if (auxfi) {
226         fwrite(out, n, 1, auxfi);
227     }
228     while (n > 0) {
229 #ifndef _WIN32
230         nbytes = write(sock, ptr, n);
231 #else
232         nbytes = send(sock, ptr, n, 0);
233 #endif
234         if (nbytes <= 0) {
235 #ifdef _WIN32
236             errno = WSAGetLastError();
237 #endif
238             perror("write server socket");
239             return 0;
240         }
241         ptr += nbytes;
242         n -= nbytes;
243     }
244     return 1;
245 }
246
247 int
248 sendeof(int sock)
249 {
250 #ifndef _WIN32
251     if (write(sock, "ctld\n", 5) < 5) {
252 #else
253     if (send(sock, "ctld\n", 5, 0) < 5) {
254 #endif
255         fprintf(stderr, "sendeof: EOF send failed\n");
256         close(sock);
257         return 0;
258     }
259     return 1;
260 }