]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
19afc601bca7a73c4086c2c3172875ec369f4389
[empserver] / src / client / expect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  expect.c: Read from the socket, expecting to see a particular code.
29  * 
30  *  Known contributors to this file:
31  *      Steve McClure, 1998
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <sys/types.h>
42 #ifndef _WIN32
43 #include <sys/socket.h>
44 #include <unistd.h>
45 #else
46 #include <winsock.h>
47 #endif
48
49 int
50 recvline(int s, char *buf)
51 {
52     int size;
53     char *p;
54     int n;
55     int code;
56     int newline;
57     char *ptr;
58     int cc;
59
60     size = 1024;
61 #ifndef _WIN32
62     (void)alarm(30);
63 #endif
64     ptr = buf;
65     n = recv(s, ptr, size, MSG_PEEK);
66     if (n <= 0) {
67 #ifdef _WIN32
68         errno = WSAGetLastError();
69 #endif
70         perror("recv");
71         return 0;
72     }
73     size -= n;
74     buf[n] = '\0';
75     if ((p = strchr(ptr, '\n')) == NULL) {
76         do {
77 #ifndef _WIN32
78             cc = read(s, ptr, n);
79 #else
80             cc = recv(s, ptr, n, 0);
81 #endif
82             if (cc < 0) {
83 #ifdef _WIN32
84                 errno = WSAGetLastError();
85 #endif
86                 perror("expect: read");
87                 return 0;
88             }
89             if (cc != n) {
90                 fprintf(stderr, "expect: short read (%d not %d)\n", cc, n);
91                 return 0;
92             }
93             ptr += n;
94             if ((n = recv(s, ptr, size, MSG_PEEK)) <= 0) {
95 #ifdef _WIN32
96                 errno = WSAGetLastError();
97 #endif
98                 perror("recv");
99                 return 0;
100             }
101             size -= n;
102             ptr[n] = '\0';
103         } while ((p = strchr(ptr, '\n')) == 0);
104         newline = 1 + p - buf;
105         *p = 0;
106     } else
107         newline = 1 + p - ptr;
108 #ifndef _WIN32
109     cc = read(s, buf, newline);
110 #else
111     cc = recv(s, buf, newline, 0);
112 #endif
113     if (cc < 0) {
114 #ifdef _WIN32
115         errno = WSAGetLastError();
116 #endif
117         perror("expect: read #2");
118         return 0;
119     }
120     if (cc != newline) {
121         fprintf(stderr, "expect: short read #2 (%d not %d)\n",
122                 cc, newline);
123         return 0;
124     }
125     buf[newline] = '\0';
126 #ifndef _WIN32
127     (void)alarm(0);
128 #endif
129     if (!isxdigit(*buf)) {
130         fprintf(stderr, "Malformed line %s\n", buf);
131         return 0;
132     }
133     if (isdigit(*buf))
134         code = *buf - '0';
135     else {
136         if (isupper(*buf))
137             *buf = tolower(*buf);
138         code = 10 + *buf - 'a';
139     }
140     return code;
141 }
142
143 int
144 expect(int s, int match, char *buf)
145 {
146     int code = recvline(s, buf);
147     return code == match;
148 }
149
150 void
151 sendcmd(int s, char *cmd, char *arg)
152 {
153     char buf[128];
154     int cc;
155     int len;
156
157     (void)sprintf(buf, "%s %s\n", cmd, arg != NULL ? arg : "");
158     len = strlen(buf);
159 #ifndef _WIN32
160     cc = write(s, buf, len);
161 #else
162     cc = send(s, buf, len, 0);
163 #endif
164     if (cc < 0) {
165 #ifdef _WIN32
166         errno = WSAGetLastError();
167 #endif
168         perror("sendcmd: write");
169     }
170     if (cc != len) {
171         fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
172     }
173 }