]> git.pond.sub.org Git - empserver/blob - src/client/expect.c
7a19564f5c84c21ae6cee0e111048c6bb37bc9e8
[empserver] / src / client / expect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  expect.c: Read from the socket, expecting to see a particular code.
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1998
31  *     Markus Armbruster, 2007-2013
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <unistd.h>
44 #include "misc.h"
45 #include "proto.h"
46
47 int
48 recvline(int s, char *buf)
49 {
50     int sz = 1024;
51     char *bp;
52     char ch;
53     ssize_t n;
54
55     bp = buf;
56     for (;;) {
57         n = read(s, &ch, 1);
58         if (n < 0) {
59             if (errno != EINTR) {
60                 perror("read");
61                 exit(1);
62             }
63             continue;
64         }
65         if (n == 0)
66             return -1;
67         if (ch == '\n')
68             break;
69         if (bp < buf + sz - 2)
70             *bp++ = ch;
71         /* else silently truncate */
72     }
73
74     *bp++ = ch;
75     *bp = 0;
76     return parseid(buf);
77 }
78
79 int
80 parseid(char *line)
81 {
82     char *end;
83     long id;
84
85     id = strtol(line, &end, 36);
86     if (end == line || *end != ' ') {
87         fprintf(stderr, "Malformed id in line %s", line);
88         id = -1;
89     }
90     if (id > C_LAST)
91         id = -1;
92     return id;
93 }
94
95 int
96 expect(int s, int match, char *buf)
97 {
98     return recvline(s, buf) == match;
99 }
100
101 void
102 sendcmd(int s, char *cmd, char *arg)
103 {
104     char buf[128];
105     char *p;
106     ssize_t n;
107     int len;
108
109     len = snprintf(buf, sizeof(buf), "%s%s%s\n",
110                    cmd, arg ? " " : "", arg ? arg : "");
111     if (len >= (int)sizeof(buf)) {
112         fprintf(stderr, "%s too long\n", cmd);
113         exit(1);
114     }
115     p = buf;
116     while (len > 0) {
117         n = write(s, buf, len);
118         if (n < 0) {
119             if (errno != EINTR) {
120                 perror("sendcmd: write");
121                 exit(1);
122             }
123             n = 0;
124         }
125         p += n;
126         len -= n;
127     }
128 }