]> git.pond.sub.org Git - empserver/blob - src/client/termio.c
Fix previous rev.
[empserver] / src / client / termio.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #if defined(aix) || defined(hpux) || defined(sgi)
39 #include <sys/termio.h>
40 #elif defined(linux)
41 #include <unistd.h>
42 #include <termio.h>
43 #else
44 #ifndef _WIN32
45 #include <unistd.h>
46 #include <sgtty.h>
47 #else
48 #include <winsock.h>
49 #endif /* _WIN32 */
50 #endif
51 #include "misc.h"
52 #include "tags.h"
53
54 extern struct tagstruct *taglist;
55 extern s_char buf[4096];
56 extern s_char exec[];
57
58 #ifdef _WIN32
59 extern HANDLE hStdIn;
60 #endif
61
62 int sendeof();
63
64 int
65 termio(fd, sock, auxfi)
66 int fd;
67 int sock;
68 FILE *auxfi;
69 {
70     s_char out[4096];
71     int i, n;
72     s_char *ptr;
73     s_char *p, *q, *r, *s, *t;
74     int nbytes;
75     int numarg, prespace, exec_com, inarg, quoted, tagging;
76     struct tagstruct *tag;
77 #ifdef _WIN32
78     char c;
79     INPUT_RECORD InpBuffer[2];
80     int err;
81 #endif
82
83     i = strlen(buf);
84     p = buf + i;
85 #ifndef _WIN32
86     n = read(fd, p, sizeof(buf) - i);
87 #else
88 /* The keyboard is sometimes generating both keydown and keyup
89  * events for the same key.  Thus, we only want to grab keydown
90  * events. */
91     if (fd == -1) {
92         err = PeekConsoleInput(hStdIn, InpBuffer, 1, &n);
93         if (InpBuffer[0].EventType != KEY_EVENT) {
94             ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
95             return 1;
96         }
97         if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
98             ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
99             return 1;
100         }
101         c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
102
103         if (c == 13)
104             c = 10;
105         n = 1;
106         p[0] = c;
107         p[1] = '\0';
108         if (c != 10)
109             ReadConsole(hStdIn, &p[0], sizeof(buf) - i, &n, NULL);
110         else
111             putchar(c);
112 /* Strip off the CRLF to just LF */
113         if (n > 1) {
114             if (p[n - 2] == 13 && p[n - 1] == 10) {
115                 p[n - 2] = 10;
116                 p[n - 1] = 0;
117                 n--;
118             }
119         }
120         FlushConsoleInputBuffer(hStdIn);
121     } else {
122         n = read(fd, p, sizeof(buf) - i);
123     }
124 #endif
125     if (n == 0) {
126         sendeof(sock);
127         return 0;
128     }
129     if (n < 0) {
130         perror("read standard input");
131         return 0;
132     }
133     n += i;
134     ptr = buf;
135     p = buf;
136     q = out;
137     r = out;
138     numarg = 0;
139     tagging = 0;
140     inarg = 0;
141     prespace = 1;
142     quoted = 0;
143     while (p < buf + n && q < out + 4000) {
144         if (*p == '\n') {
145             if (tagging) {
146                 tag = (struct tagstruct *)malloc(sizeof(struct tagstruct));
147                 tag->item = (s_char *)malloc((1 + p - s) * sizeof(s_char));
148                 tag->next = taglist;
149                 taglist = tag;
150                 t = tag->item;
151                 while (s < p)
152                     *t++ = *s++;
153                 *t = 0;
154             }
155             *q++ = *p++;
156             numarg = 0;
157             tagging = 0;
158             inarg = 0;
159             prespace = 1;
160             quoted = 0;
161             ptr = p;
162             r = q;
163         } else if (tagging) {
164             *q++ = *p++;
165         } else if (!quoted && isspace(*p)) {
166             *q++ = *p++;
167             prespace = 1;
168             if (numarg == 1 && exec_com && s > exec + 2) {
169                 tagging = 1;
170                 s = p;
171             }
172         } else if (prespace && *p == '|') {
173             tagging = 1;
174             *q++ = *p++;
175             s = p;
176         } else if (prespace && *p == '>') {
177             tagging = 1;
178             *q++ = *p++;
179             if (*p != '\n' && (*p == '!' || *p == '>'))
180                 *q++ = *p++;
181             s = p;
182         } else {
183             prespace = 0;
184             if (*p == '"') {
185                 quoted = !quoted;
186             } else {
187                 if (!inarg && *p != '?') {
188                     s = exec;
189                     exec_com = 1;
190                     numarg++;
191                 }
192                 inarg = 1;
193                 if (*s && *s++ != *p)
194                     exec_com = 0;
195             }
196             *q++ = *p++;
197         }
198     }
199     p = buf;
200     while (ptr < buf + n)
201         *p++ = *ptr++;
202     *p = 0;
203     ptr = out;
204     n = r - out;
205     if (auxfi) {
206         fwrite(out, n, 1, auxfi);
207     }
208     while (n > 0) {
209 #ifndef _WIN32
210         nbytes = write(sock, ptr, n);
211 #else
212         nbytes = send(sock, ptr, n, 0);
213 #endif
214         if (nbytes <= 0) {
215 #ifdef _WIN32
216             errno = WSAGetLastError();
217 #endif
218             perror("write server socket");
219             return 0;
220         }
221         ptr += nbytes;
222         n -= nbytes;
223     }
224     return 1;
225 }
226
227 int
228 sendeof(sock)
229 int sock;
230 {
231 #ifndef _WIN32
232     if (write(sock, "ctld\n", 5) < 5) {
233 #else
234     if (send(sock, "ctld\n", 5, 0) < 5) {
235 #endif
236         fprintf(stderr, "sendeof: EOF send failed\n");
237         close(sock);
238         return 0;
239     }
240     return 1;
241 }
242
243 int echomode = 1;
244
245 #if defined(hpux) || defined(aix) || defined (sgi) || defined(linux)
246 void
247 _noecho(fd)
248 int fd;
249 {
250     struct termio io;
251
252     echomode = 0;
253     (void)ioctl(fd, TCGETA, &io);
254     io.c_line |= ECHO;
255     (void)ioctl(fd, TCSETA, &io);
256 }
257
258 void
259 _echo(fd)
260 int fd;
261 {
262     struct termio io;
263
264     if (echomode)
265         return;
266     (void)ioctl(fd, TCGETA, &io);
267     io.c_line &= ~ECHO;
268     (void)ioctl(fd, TCSETA, &io);
269     echomode++;
270 }
271
272 #else
273 #ifndef _WIN32
274
275 void
276 _noecho(fd)
277 int fd;
278 {
279     struct sgttyb sgbuf;
280
281     echomode = 0;
282     (void)ioctl(fd, TIOCGETP, &sgbuf);
283     sgbuf.sg_flags &= ~ECHO;
284     (void)ioctl(fd, TIOCSETP, &sgbuf);
285 }
286
287 void
288 _echo(fd)
289 int fd;
290 {
291     struct sgttyb sgbuf;
292
293     if (echomode)
294         return;
295     (void)ioctl(fd, TIOCGETP, &sgbuf);
296     sgbuf.sg_flags |= ECHO;
297     (void)ioctl(0, TIOCSETP, &sgbuf);
298     echomode++;
299 }
300 #else
301 void
302 _noecho(fd)
303 int fd;
304 {
305     echomode = 0;
306 }
307
308 void
309 _echo(fd)
310 int fd;
311 {
312     echomode++;
313 }
314
315 #endif /* _WIN32 */
316 #endif