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