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