]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
b30bbf1e3fa329006ad84dd1bc88cef7ce652ad3
[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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <ctype.h>
37 #include <stdio.h>
38 #include <string.h>
39 #ifndef _WIN32
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <unistd.h>
43 #endif
44 #include "misc.h"
45
46 int
47 recvline(int s, char *buf)
48 {
49     int size;
50     char *p;
51     int n;
52     int code;
53     int newline;
54     char *ptr;
55     int cc;
56
57     size = 1024;
58 #ifndef _WIN32
59     (void)alarm(30);
60 #endif
61     ptr = buf;
62     n = recv(s, ptr, size, MSG_PEEK);
63     if (n <= 0) {
64 #ifdef _WIN32
65         errno = WSAGetLastError();
66 #endif
67         perror("recv");
68         return 0;
69     }
70     size -= n;
71     buf[n] = '\0';
72     if ((p = strchr(ptr, '\n')) == NULL) {
73         do {
74 #ifndef _WIN32
75             cc = read(s, ptr, n);
76 #else
77             cc = recv(s, ptr, n, 0);
78 #endif
79             if (cc < 0) {
80 #ifdef _WIN32
81                 errno = WSAGetLastError();
82 #endif
83                 perror("expect: read");
84                 return 0;
85             }
86             if (cc != n) {
87                 fprintf(stderr, "expect: short read (%d not %d)\n", cc, n);
88                 return 0;
89             }
90             ptr += n;
91             if ((n = recv(s, ptr, size, MSG_PEEK)) <= 0) {
92 #ifdef _WIN32
93                 errno = WSAGetLastError();
94 #endif
95                 perror("recv");
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, "Malformed line %s\n", 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     return code;
138 }
139
140 int
141 expect(int s, int match, char *buf)
142 {
143     int code = recvline(s, buf);
144     return code == match;
145 }
146
147 void
148 sendcmd(int s, char *cmd, char *arg)
149 {
150     char buf[128];
151     int cc;
152     int len;
153
154     (void)sprintf(buf, "%s %s\n", cmd, arg != NULL ? arg : "");
155     len = strlen(buf);
156 #ifndef _WIN32
157     cc = write(s, buf, len);
158 #else
159     cc = send(s, buf, len, 0);
160 #endif
161     if (cc < 0) {
162 #ifdef _WIN32
163         errno = WSAGetLastError();
164 #endif
165         perror("sendcmd: write");
166     }
167     if (cc != len) {
168         fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
169     }
170 }