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