]> git.pond.sub.org Git - empserver/blob - src/client/termio.c
Update copyright notice.
[empserver] / src / client / termio.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 int
48 termio(int fd, int sock, FILE *auxfi)
49 {
50     static char exec[] = "execute";
51     static char buf[4096];
52     char out[4096];
53     int i, n;
54     char *ptr;
55     char *p, *q, *r, *s, *t;
56     int nbytes;
57     int prespace, exec_com, inarg, quoted, tagging;
58     struct tagstruct *tag;
59 #ifdef _WIN32
60     char c;
61     INPUT_RECORD InpBuffer[2];
62     int err;
63 #endif
64
65     i = strlen(buf);
66     p = buf + i;
67 #ifndef _WIN32
68     n = read(fd, p, sizeof(buf) - i);
69 #else
70 /* The keyboard is sometimes generating both keydown and keyup
71  * events for the same key.  Thus, we only want to grab keydown
72  * events. */
73     if (fd == -1) {
74         err = PeekConsoleInput(hStdIn, InpBuffer, 1, &n);
75         if (InpBuffer[0].EventType != KEY_EVENT) {
76             ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
77             return 1;
78         }
79         if (!InpBuffer[0].Event.KeyEvent.bKeyDown) {
80             ReadConsoleInput(hStdIn, InpBuffer, 1, &n);
81             return 1;
82         }
83         c = InpBuffer[0].Event.KeyEvent.uChar.AsciiChar;
84
85         if (c == 13)
86             c = 10;
87         n = 1;
88         p[0] = c;
89         p[1] = '\0';
90         if (c != 10)
91             ReadConsole(hStdIn, p, sizeof(buf) - i, &n, NULL);
92         else
93             putchar(c);
94 /* Strip off the CRLF to just LF */
95         if (n > 1) {
96             if (p[n - 2] == 13 && p[n - 1] == 10) {
97                 p[n - 2] = 10;
98                 p[n - 1] = 0;
99                 n--;
100             }
101         }
102         FlushConsoleInputBuffer(hStdIn);
103         if (n == 0) return 1;
104     } else if (fd == 0) {
105         if (feof(stdin)) {
106             sendeof(sock);
107             return 0;
108         }
109         fgets(p, sizeof(buf) - i, stdin);
110         n = strlen(p);
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     tagging = 0;
129     inarg = 0;
130     prespace = 1;
131     quoted = 0;
132     exec_com = 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 = (char *)malloc((1 + p - s) * sizeof(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             tagging = 0;
147             inarg = 0;
148             prespace = 1;
149             quoted = 0;
150             exec_com = 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 (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                     inarg = 1;
181                 }
182                 if (exec_com && *s && *s++ != *p)
183                     exec_com = 0;
184             }
185             *q++ = *p++;
186         }
187     }
188     p = buf;
189     while (ptr < buf + n)
190         *p++ = *ptr++;
191     *p = 0;
192     ptr = out;
193     n = r - out;
194     if (auxfi) {
195         fwrite(out, n, 1, auxfi);
196     }
197     while (n > 0) {
198 #ifndef _WIN32
199         nbytes = write(sock, ptr, n);
200 #else
201         nbytes = send(sock, ptr, n, 0);
202 #endif
203         if (nbytes <= 0) {
204 #ifdef _WIN32
205             errno = WSAGetLastError();
206 #endif
207             perror("write server socket");
208             return 0;
209         }
210         ptr += nbytes;
211         n -= nbytes;
212     }
213     return 1;
214 }
215
216 int
217 sendeof(int sock)
218 {
219 #ifndef _WIN32
220     if (write(sock, "ctld\n", 5) < 5) {
221 #else
222     if (send(sock, "ctld\n", 5, 0) < 5) {
223 #endif
224         fprintf(stderr, "sendeof: EOF send failed\n");
225         close(sock);
226         return 0;
227     }
228     return 1;
229 }