]> 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-2010, 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-2009
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 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <unistd.h>
45 #include "misc.h"
46 #include "proto.h"
47
48 int
49 recvline(int s, char *buf)
50 {
51     int sz = 1024;
52     char *bp;
53     char ch;
54     ssize_t n;
55
56     bp = buf;
57     for (;;) {
58         n = read(s, &ch, 1);
59         if (n < 0) {
60             if (errno != EINTR) {
61                 perror("read");
62                 exit(1);
63             }
64             continue;
65         }
66         if (n == 0)
67             return -1;
68         if (ch == '\n')
69             break;
70         if (bp < buf + sz - 2)
71             *bp++ = ch;
72         /* else silently truncate */
73     }
74
75     *bp++ = ch;
76     *bp = 0;
77     return parseid(buf);
78 }
79
80 int
81 parseid(char *line)
82 {
83     char *end;
84     long id;
85
86     id = strtol(line, &end, 36);
87     if (end == line || *end != ' ') {
88         fprintf(stderr, "Malformed id in line %s", line);
89         id = -1;
90     }
91     if (id > C_LAST)
92         id = -1;
93     return id;
94 }
95
96 int
97 expect(int s, int match, char *buf)
98 {
99     return recvline(s, buf) == match;
100 }
101
102 void
103 sendcmd(int s, char *cmd, char *arg)
104 {
105     char buf[128];
106     char *p;
107     ssize_t n;
108     int len;
109
110     len = snprintf(buf, sizeof(buf), "%s %s\n",
111                    cmd, arg != NULL ? arg : "");
112     if (len >= (int)sizeof(buf)) {
113         fprintf(stderr, "%s too long\n", cmd);
114         exit(1);
115     }
116     p = buf;
117     while (len > 0) {
118         n = write(s, buf, len);
119         if (n < 0) {
120             if (errno != EINTR) {
121                 perror("sendcmd: write");
122                 exit(1);
123             }
124             n = 0;
125         }
126         p += n;
127         len -= n;
128     }
129 }