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