]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
Indented with src/scripts/indent-emp.
[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, "expect: short read (%d not %d)\n", cc, n);
94                 return 0;
95             }
96             ptr += n;
97             if ((n = recv(s, ptr, size, MSG_PEEK)) <= 0) {
98                 fprintf(stderr, "Expecting %d, got %s\n", match, buf);
99                 return 0;
100             }
101             size -= n;
102             ptr[n] = '\0';
103         } while ((p = index(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, "Expecting %d, got %s\n", match, 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     if (code == match)
141         return 1;
142     return 0;
143 }
144
145 void
146 sendcmd(s, cmd, arg)
147 int s;
148 int cmd;
149 s_char *arg;
150 {
151     extern struct fn fnlist[];
152     s_char buf[128];
153     int cc;
154     int len;
155
156     (void)sprintf(buf, "%s %s\n", fnlist[cmd].name, arg != 0 ? arg : "");
157     len = strlen(buf);
158 #ifndef _WIN32
159     cc = write(s, buf, len);
160 #else
161     cc = send(s, buf, len, 0);
162 #endif
163     if (cc < 0) {
164 #ifdef _WIN32
165         errno = WSAGetLastError();
166 #endif
167         perror("sendcmd: write");
168     }
169     if (cc != len) {
170         fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
171     }
172 }