]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
(recvline): Cope with multiple-digit ids. Server doesn't send such
[empserver] / src / client / expect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *      Markus Armbruster, 2007
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #ifndef _WIN32
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <unistd.h>
46 #endif
47 #include "misc.h"
48 #include "proto.h"
49
50 #ifdef _WIN32
51 #define read(sock, buffer, buf_size) \
52         w32_recv((sock), (buffer), (buf_size), 0)
53 #define write(sock, buffer, buf_size) \
54         w32_send((sock), (buffer), (buf_size), 0)
55 #endif
56
57 int
58 recvline(int s, char *buf)
59 {
60     int sz = 1024;
61     char *bp, *end;
62     char ch;
63     ssize_t n;
64     long id;
65
66     bp = buf;
67     for (;;) {
68         n = read(s, &ch, 1);
69         if (n < 0) {
70             if (errno != EINTR) {
71                 perror("read");
72                 exit(1);
73             }
74             continue;
75         }
76         if (n == 0)
77             return -1;
78         if (ch == '\n')
79             break;
80         if (bp < buf + sz - 2)
81             *bp++ = ch;
82         /* else silently truncate */
83     }
84
85     *bp++ = ch;
86     *bp = 0;
87
88     id = strtol(buf, &end, 16);
89     if (end == buf || *end != ' ') {
90         fprintf(stderr, "Malformed id in line %s", buf);
91         id = -1;
92     }
93     if (id > C_LAST)
94         id = -1;
95     return id;
96 }
97
98 int
99 expect(int s, int match, char *buf)
100 {
101     int code = recvline(s, buf);
102     return code == match;
103 }
104
105 void
106 sendcmd(int s, char *cmd, char *arg)
107 {
108     char buf[128];
109     char *p;
110     ssize_t n;
111     int len;
112
113     len = snprintf(buf, sizeof(buf), "%s %s\n",
114                    cmd, arg != NULL ? arg : "");
115     if (len >= (int)sizeof(buf)) {
116         fprintf(stderr, "%s too long\n", cmd);
117         exit(1);
118     }
119     p = buf;
120     while (len > 0) {
121         n = write(s, buf, len);
122         if (n < 0) {
123             if (errno != EINTR) {
124                 perror("sendcmd: write");
125                 exit(1);
126             }
127             n = 0;
128         }
129         p += n;
130         len -= n;
131     }
132 }