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