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