src/client/misc.h File Reference

#include <stdio.h>

Include dependency graph for misc.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define MAX(a, b)   ((a) >= (b) ? (a) : (b))
#define MIN(a, b)   ((a) < (b) ? (a) : (b))

Functions

void getsose (void)
void putso (void)
void putse (void)
int recvline (int s, char *buf)
int parseid (char *)
int expect (int s, int match, char *buf)
int tcp_connect (char *, char *)
int login (int s, char *uname, char *cname, char *cpass, int kill_proc, int)
int play (int)
void sendcmd (int s, char *cmd, char *arg)
void servercmd (int, char *, int)
void outch (char)

Variables

char empirehost []
char empireport []
int eight_bit_clean
int input_fd
int send_eof
FILE * auxfp
char * SO
char * SE


Define Documentation

#define MAX ( a,
 )     ((a) >= (b) ? (a) : (b))

Definition at line 42 of file misc.h.

#define MIN ( a,
 )     ((a) < (b) ? (a) : (b))

Definition at line 43 of file misc.h.


Function Documentation

int expect ( int  s,
int  match,
char *  buf 
)

Definition at line 106 of file expect.c.

References recvline().

Referenced by login().

00107 {
00108     return recvline(s, buf) == match;
00109 }

Here is the call graph for this function:

void getsose ( void   ) 

Definition at line 49 of file termlib.c.

References fileno(), NULL, rmso, and smso.

00050 {
00051     int err;
00052 
00053     if (!isatty(fileno(stdout)))
00054         return;
00055 
00056     if (setupterm(NULL, fileno(stdout), &err) != OK) {
00057         fprintf(stderr,
00058                 "Can't setup terminal, check environment variable TERM\n");
00059         return;
00060     }
00061 
00062     smso = tigetstr("smso");
00063     rmso = tigetstr("rmso");
00064 }

Here is the call graph for this function:

int login ( int  s,
char *  uname,
char *  cname,
char *  cpass,
int  kill_proc,
int   
)

Definition at line 48 of file login.c.

References C_CMDOK, C_DATA, C_EXIT, C_INIT, CLIENTPROTO, expect(), getpass(), NULL, recvline(), and sendcmd().

Referenced by main().

00050 {
00051     char tmp[128];
00052     char buf[1024];
00053     char *ptr;
00054     char *p;
00055     int len, code;
00056 
00057     if (!expect(s, C_INIT, buf))
00058         return 0;
00059     sendcmd(s, "user", uname);
00060     if (!expect(s, C_CMDOK, buf)) {
00061         fprintf(stderr, "Server rejects your user name\n");
00062         return 0;
00063     }
00064     if (utf8) {
00065         sendcmd(s, "options", "utf-8");
00066         for (;;) {
00067             code = recvline(s, buf);
00068             if (code == C_CMDOK)
00069                 break;
00070             if (code != C_DATA) {
00071                 fprintf(stderr, "Server doesn't support UTF-8\n");
00072                 return 0;
00073             }
00074         }
00075     }
00076     if (cname == NULL) {
00077         (void)printf("Country name? ");
00078         fflush(stdout);
00079         cname = fgets(tmp, sizeof(tmp), stdin);
00080         if (cname == NULL || *cname == 0)
00081             return 0;
00082     }
00083     len = strlen(cname);
00084     if (cname[len-1] == '\n')
00085         cname[len-1] = 0;
00086     sendcmd(s, "coun", cname);
00087     if (!expect(s, C_CMDOK, buf)) {
00088         (void)fprintf(stderr, "No such country\n");
00089         return 0;
00090     }
00091     if (cpass == NULL) {
00092         cpass = getpass("Your name? ");
00093         if (cpass == NULL || *cpass == 0)
00094             return 0;
00095     }
00096     (void)printf("\n");
00097     sendcmd(s, "pass", cpass);
00098     memset(cpass, 0, strlen(cpass));    /* for core dumps */
00099     if (!expect(s, C_CMDOK, buf)) {
00100         (void)fprintf(stderr, "Bad password\n");
00101         return 0;
00102     }
00103     if (kill_proc) {
00104         sendcmd(s, "kill", NULL);
00105         (void)printf("\n\t-=O=-\n");
00106         (void)expect(s, C_EXIT, buf);
00107         fprintf(stderr, "%s\n", buf);
00108         return 0;
00109     }
00110     sendcmd(s, "play", NULL);
00111     (void)printf("\n\t-=O=-\n");
00112     if (!expect(s, C_INIT, buf)) {
00113         fprintf(stderr, "%s\n", buf);
00114         return 0;
00115     }
00116     for (ptr = buf; !isspace(*ptr); ptr++) ;
00117     ptr++;
00118     p = strchr(ptr, '\n');
00119     if (p != NULL)
00120         *p = 0;
00121     if (atoi(ptr) != CLIENTPROTO) {
00122         printf("Empire client out of date; get new version!\n");
00123         printf("   this version: %d, current version: %d\n",
00124                CLIENTPROTO, atoi(ptr));
00125     }
00126     fflush(stdout);
00127     return 1;
00128 }

Here is the call graph for this function:

void outch ( char   ) 

Definition at line 277 of file servcmd.c.

References auxfp, eight_bit_clean, putse, putso, and redir_fp.

Referenced by recv_output().

00278 {
00279     if (auxfp)
00280         putc(c, auxfp);
00281     if (redir_fp)
00282         putc(c, redir_fp);
00283     else if (eight_bit_clean) {
00284         if (c == 14)
00285             putso();
00286         else if (c == 15)
00287             putse();
00288         else
00289             putchar(c);
00290     } else if (c & 0x80) {
00291         putso();
00292         putchar(c & 0x7f);
00293         putse();
00294     } else
00295         putchar(c);
00296 }

int parseid ( char *   ) 

Definition at line 90 of file expect.c.

References C_LAST, and id.

Referenced by recv_output(), and recvline().

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 }

int play ( int   ) 

Definition at line 444 of file play.c.

00445 {
00446     /*
00447      * Player input flows from INPUT_FD through recv_input() into ring
00448      * buffer INBUF, which drains into SOCK.  This must not block.
00449      * Server output flows from SOCK into recv_output().  Reading SOCK
00450      * must not block.
00451      */
00452     struct sigaction sa;
00453     struct ring inbuf;          /* input buffer, draining to SOCK */
00454     int eof_fd0;                /* read fd 0 hit EOF? */
00455     fd_set rdfd, wrfd;
00456     int n;
00457 
00458     sa.sa_flags = 0;
00459     sigemptyset(&sa.sa_mask);
00460     sa.sa_handler = intr;
00461     sigaction(SIGINT, &sa, NULL);
00462     sa.sa_handler = SIG_IGN;
00463     sigaction(SIGPIPE, &sa, NULL);
00464 
00465     ring_init(&inbuf);
00466     eof_fd0 = send_eof = send_intr = 0;
00467     input_fd = 0;
00468     sysdep_stdin_init();
00469 
00470     for (;;) {
00471         FD_ZERO(&rdfd);
00472         FD_ZERO(&wrfd);
00473 
00474         /*
00475          * Want to read player input only when we don't need to send
00476          * cookies, and we haven't hit EOF on fd 0, and INBUF can
00477          * accept some.
00478          */
00479         if (!send_intr && !send_eof && !eof_fd0 && ring_space(&inbuf))
00480             FD_SET(input_fd, &rdfd);
00481         /* Want to send player input only when we have something */
00482         if (send_intr || send_eof || ring_len(&inbuf))
00483             FD_SET(sock, &wrfd);
00484         /* Always want to read server output */
00485         FD_SET(sock, &rdfd);
00486 
00487         n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
00488         if (n < 0) {
00489             if (errno != EINTR) {
00490                 perror("select");
00491                 return -1;
00492             }
00493         }
00494 
00495         if (send_eof
00496             && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
00497             send_eof--;
00498         if (send_intr
00499             && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0)
00500             send_intr = 0;
00501 
00502         if (n < 0)
00503             continue;
00504 
00505         /* read player input */
00506         if (FD_ISSET(input_fd, &rdfd)) {
00507             n = recv_input(input_fd, &inbuf);
00508             if (n < 0) {
00509                 perror("read stdin"); /* FIXME stdin misleading, could be execing */
00510                 n = 0;
00511             }
00512             if (n == 0) {
00513                 /* EOF on input */
00514                 send_eof++;
00515                 if (input_fd) {
00516                     /* execute done, switch back to fd 0 */
00517                     close(input_fd);
00518                     input_fd = 0;
00519                 } else {
00520                     /* stop reading input, drain socket ring buffers */
00521                     eof_fd0 = 1;
00522                     sa.sa_handler = SIG_DFL;
00523                     sigaction(SIGINT, &sa, NULL);
00524                 }
00525             }
00526         }
00527 
00528         /* send it to the server */
00529         if (FD_ISSET(sock, &wrfd)) {
00530             n = ring_to_file(&inbuf, sock);
00531             if (n < 0) {
00532                 perror("write socket");
00533                 return -1;
00534             }
00535         }
00536 
00537         /* read server output and print it */
00538         if (FD_ISSET(sock, &rdfd)) {
00539             n = recv_output(sock);
00540             if (n < 0) {
00541                 perror("read socket");
00542                 return -1;
00543             }
00544             if (n == 0)
00545                 return 0;
00546         }
00547     }
00548 }

void putse ( void   ) 

Definition at line 74 of file termlib.c.

References rmso.

00075 {
00076     if (rmso)
00077         putp(rmso);
00078 }

void putso ( void   ) 

Definition at line 67 of file termlib.c.

References smso.

00068 {
00069     if (smso)
00070         putp(smso);
00071 }

int recvline ( int  s,
char *  buf 
)

Definition at line 58 of file expect.c.

References parseid(), and read.

Referenced by expect(), and login().

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 }

Here is the call graph for this function:

void sendcmd ( int  s,
char *  cmd,
char *  arg 
)

Definition at line 112 of file expect.c.

References NULL, snprintf, and write.

Referenced by login().

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 }

void servercmd ( int  ,
char *  ,
int   
)

Definition at line 70 of file servcmd.c.

References auxfp, C_EXECUTE, C_EXIT, C_FLASH, C_FLUSH, C_INFORM, C_PIPE, C_PROMPT, C_REDIR, doexecute(), dopipe(), doredir(), executing, forget_input(), input_fd, input_to_forget, nmin, NULL, pclose, prompt(), redir_fp, redir_is_pipe, send_eof, and snprintf.

Referenced by recv_output().

00071 {
00072     static int nmin, nbtu, fd;
00073     static char the_prompt[1024];
00074     static char teles[64];
00075 
00076     switch (code) {
00077     case C_PROMPT:
00078         if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
00079             fprintf(stderr, "prompt: bad server prompt %s\n", arg);
00080         }
00081         snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
00082                  nmin, nbtu);
00083         if (redir_fp) {
00084             if (redir_is_pipe)
00085                 (void)pclose(redir_fp);
00086             else
00087                 (void)fclose(redir_fp);
00088             redir_fp = NULL;
00089         }
00090         if (input_to_forget) {
00091             forget_input(input_to_forget);
00092             input_to_forget = 0;
00093         }
00094         prompt(code, the_prompt, teles);
00095         executing = 0;
00096         break;
00097     case C_FLUSH:
00098         snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
00099         prompt(code, the_prompt, teles);
00100         break;
00101     case C_EXECUTE:
00102         fd = doexecute(arg);
00103         if (fd < 0)
00104             send_eof++;
00105         else {
00106             input_fd = fd;
00107             executing = 1;
00108         }
00109         break;
00110     case C_EXIT:
00111         printf("Exit: %s", arg);
00112         if (auxfp)
00113             fprintf(auxfp, "Exit: %s", arg);
00114         break;
00115     case C_FLASH:
00116         printf("\n%s", arg);
00117         if (auxfp)
00118             fprintf(auxfp, "\n%s", arg);
00119         break;
00120     case C_INFORM:
00121         if (arg[0] != '\n') {
00122             snprintf(teles, sizeof(teles), "(%.*s) ", len -1, arg);
00123             if (!redir_fp) {
00124                 putchar('\07');
00125                 prompt(code, the_prompt, teles);
00126             }
00127         } else
00128             teles[0] = 0;
00129         break;
00130     case C_PIPE:
00131         dopipe(arg);
00132         break;
00133     case C_REDIR:
00134         doredir(arg);
00135         break;
00136     default:
00137         assert(0);
00138         break;
00139     }
00140 }

Here is the call graph for this function:

int tcp_connect ( char *  ,
char *   
)

Definition at line 156 of file host.c.

References hostaddr(), hostconnect(), and hostport().

Referenced by main().

00157 {
00158     struct sockaddr_in sin;
00159     int sock;
00160 
00161     if (!hostport(serv, &sin)) {
00162         fprintf(stderr, "Can't resolve Empire port %s\n", serv);
00163         exit(1);
00164     }
00165     if (!hostaddr(host, &sin)) {
00166         fprintf(stderr, "Can't resolve Empire host %s\n", host);
00167         exit(1);
00168     }
00169     if ((sock = hostconnect(&sin)) < 0) {
00170         fprintf(stderr, "Can't connect to %s:%s: %s\n",
00171                 serv, host, strerror(errno));
00172         exit(1);
00173     }
00174     return sock;
00175 }

Here is the call graph for this function:


Variable Documentation

FILE* auxfp

Definition at line 57 of file servcmd.c.

Referenced by main(), outch(), prompt(), recv_input(), and servercmd().

int eight_bit_clean

Definition at line 56 of file servcmd.c.

Referenced by main(), and outch().

char empirehost[]

Definition at line 35 of file ipglob.c.

Referenced by main().

char empireport[]

Definition at line 36 of file ipglob.c.

Referenced by main().

int input_fd

Definition at line 294 of file play.c.

Referenced by servercmd().

char* SE

int send_eof

Definition at line 295 of file play.c.

Referenced by servercmd().

char* SO


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