]> git.pond.sub.org Git - empserver/blob - src/client/termio.c
Declare tags.c variables in tags.h. Declare stuff with external
[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 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     } else {
102         n = read(fd, p, sizeof(buf) - i);
103     }
104 #endif
105     if (n == 0) {
106         sendeof(sock);
107         return 0;
108     }
109     if (n < 0) {
110         perror("read standard input");
111         return 0;
112     }
113     n += i;
114     ptr = buf;
115     p = buf;
116     q = out;
117     r = out;
118     numarg = 0;
119     tagging = 0;
120     inarg = 0;
121     prespace = 1;
122     quoted = 0;
123     while (p < buf + n && q < out + 4000) {
124         if (*p == '\n') {
125             if (tagging) {
126                 tag = (struct tagstruct *)malloc(sizeof(struct tagstruct));
127                 tag->item = (s_char *)malloc((1 + p - s) * sizeof(s_char));
128                 tag->next = taglist;
129                 taglist = tag;
130                 t = tag->item;
131                 while (s < p)
132                     *t++ = *s++;
133                 *t = 0;
134             }
135             *q++ = *p++;
136             numarg = 0;
137             tagging = 0;
138             inarg = 0;
139             prespace = 1;
140             quoted = 0;
141             ptr = p;
142             r = q;
143         } else if (tagging) {
144             *q++ = *p++;
145         } else if (!quoted && isspace(*p)) {
146             *q++ = *p++;
147             prespace = 1;
148             if (numarg == 1 && exec_com && s > exec + 2) {
149                 tagging = 1;
150                 s = p;
151             }
152         } else if (prespace && *p == '|') {
153             tagging = 1;
154             *q++ = *p++;
155             s = p;
156         } else if (prespace && *p == '>') {
157             tagging = 1;
158             *q++ = *p++;
159             if (*p != '\n' && (*p == '!' || *p == '>'))
160                 *q++ = *p++;
161             s = p;
162         } else {
163             prespace = 0;
164             if (*p == '"') {
165                 quoted = !quoted;
166             } else {
167                 if (!inarg && *p != '?') {
168                     s = exec;
169                     exec_com = 1;
170                     numarg++;
171                 }
172                 inarg = 1;
173                 if (*s && *s++ != *p)
174                     exec_com = 0;
175             }
176             *q++ = *p++;
177         }
178     }
179     p = buf;
180     while (ptr < buf + n)
181         *p++ = *ptr++;
182     *p = 0;
183     ptr = out;
184     n = r - out;
185     if (auxfi) {
186         fwrite(out, n, 1, auxfi);
187     }
188     while (n > 0) {
189 #ifndef _WIN32
190         nbytes = write(sock, ptr, n);
191 #else
192         nbytes = send(sock, ptr, n, 0);
193 #endif
194         if (nbytes <= 0) {
195 #ifdef _WIN32
196             errno = WSAGetLastError();
197 #endif
198             perror("write server socket");
199             return 0;
200         }
201         ptr += nbytes;
202         n -= nbytes;
203     }
204     return 1;
205 }
206
207 int
208 sendeof(int sock)
209 {
210 #ifndef _WIN32
211     if (write(sock, "ctld\n", 5) < 5) {
212 #else
213     if (send(sock, "ctld\n", 5, 0) < 5) {
214 #endif
215         fprintf(stderr, "sendeof: EOF send failed\n");
216         close(sock);
217         return 0;
218     }
219     return 1;
220 }