]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
Update copyright notice.
[empserver] / src / client / expect.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  *  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(int s, int match, s_char *buf)
50 {
51     int size;
52     s_char *p;
53     int n;
54     int code;
55     int newline;
56     s_char *ptr;
57     int cc;
58
59     size = 1024;
60 #ifndef _WIN32
61     (void)alarm(30);
62 #endif
63     ptr = buf;
64     n = recv(s, ptr, size, MSG_PEEK);
65     if (n <= 0) {
66         fprintf(stderr, "Expecting code %d\n", match);
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')) == 0) {
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                 fprintf(stderr, "Expecting %d, got %s\n", match, buf);
96                 return 0;
97             }
98             size -= n;
99             ptr[n] = '\0';
100         } while ((p = strchr(ptr, '\n')) == 0);
101         newline = 1 + p - buf;
102         *p = 0;
103     } else
104         newline = 1 + p - ptr;
105 #ifndef _WIN32
106     cc = read(s, buf, newline);
107 #else
108     cc = recv(s, buf, newline, 0);
109 #endif
110     if (cc < 0) {
111 #ifdef _WIN32
112         errno = WSAGetLastError();
113 #endif
114         perror("expect: read #2");
115         return 0;
116     }
117     if (cc != newline) {
118         fprintf(stderr, "expect: short read #2 (%d not %d)\n",
119                 cc, newline);
120         return 0;
121     }
122     buf[newline] = '\0';
123 #ifndef _WIN32
124     (void)alarm(0);
125 #endif
126     if (!isxdigit(*buf)) {
127         fprintf(stderr, "Expecting %d, got %s\n", match, buf);
128         return 0;
129     }
130     if (isdigit(*buf))
131         code = *buf - '0';
132     else {
133         if (isupper(*buf))
134             *buf = tolower(*buf);
135         code = 10 + *buf - 'a';
136     }
137     if (code == match)
138         return 1;
139     return 0;
140 }
141
142 void
143 sendcmd(int s, int cmd, s_char *arg)
144 {
145     s_char buf[128];
146     int cc;
147     int len;
148
149     (void)sprintf(buf, "%s %s\n", fnlist[cmd].name, arg != 0 ? arg : "");
150     len = strlen(buf);
151 #ifndef _WIN32
152     cc = write(s, buf, len);
153 #else
154     cc = send(s, buf, len, 0);
155 #endif
156     if (cc < 0) {
157 #ifdef _WIN32
158         errno = WSAGetLastError();
159 #endif
160         perror("sendcmd: write");
161     }
162     if (cc != len) {
163         fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
164     }
165 }