src/client/expect.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  expect.c: Read from the socket, expecting to see a particular code.
00029  * 
00030  *  Known contributors to this file:
00031  *      Steve McClure, 1998
00032  *      Markus Armbruster, 2007
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include <ctype.h>
00038 #include <errno.h>
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #ifndef _WIN32
00043 #include <sys/types.h>
00044 #include <sys/socket.h>
00045 #include <unistd.h>
00046 #endif
00047 #include "misc.h"
00048 #include "proto.h"
00049 
00050 #ifdef _WIN32
00051 #define read(sock, buffer, buf_size) \
00052         w32_recv((sock), (buffer), (buf_size), 0)
00053 #define write(sock, buffer, buf_size) \
00054         w32_send((sock), (buffer), (buf_size), 0)
00055 #endif
00056 
00057 int
00058 recvline(int s, char *buf)
00059 {
00060     int sz = 1024;
00061     char *bp;
00062     char ch;
00063     ssize_t n;
00064 
00065     bp = buf;
00066     for (;;) {
00067         n = read(s, &ch, 1);
00068         if (n < 0) {
00069             if (errno != EINTR) {
00070                 perror("read");
00071                 exit(1);
00072             }
00073             continue;
00074         }
00075         if (n == 0)
00076             return -1;
00077         if (ch == '\n')
00078             break;
00079         if (bp < buf + sz - 2)
00080             *bp++ = ch;
00081         /* else silently truncate */
00082     }
00083 
00084     *bp++ = ch;
00085     *bp = 0;
00086     return parseid(buf);
00087 }
00088 
00089 int
00090 parseid(char *line)
00091 {
00092     char *end;
00093     long id;
00094 
00095     id = strtol(line, &end, 36);
00096     if (end == line || *end != ' ') {
00097         fprintf(stderr, "Malformed id in line %s", line);
00098         id = -1;
00099     }
00100     if (id > C_LAST)
00101         id = -1;
00102     return id;
00103 }
00104 
00105 int
00106 expect(int s, int match, char *buf)
00107 {
00108     return recvline(s, buf) == match;
00109 }
00110 
00111 void
00112 sendcmd(int s, char *cmd, char *arg)
00113 {
00114     char buf[128];
00115     char *p;
00116     ssize_t n;
00117     int len;
00118 
00119     len = snprintf(buf, sizeof(buf), "%s %s\n",
00120                    cmd, arg != NULL ? arg : "");
00121     if (len >= (int)sizeof(buf)) {
00122         fprintf(stderr, "%s too long\n", cmd);
00123         exit(1);
00124     }
00125     p = buf;
00126     while (len > 0) {
00127         n = write(s, buf, len);
00128         if (n < 0) {
00129             if (errno != EINTR) {
00130                 perror("sendcmd: write");
00131                 exit(1);
00132             }
00133             n = 0;
00134         }
00135         p += n;
00136         len -= n;
00137     }
00138 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2