]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
(prompt, servercmd): Move C_PROMPT code from prompt() to the only call
[empserver] / src / client / servcmd.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  *  servercmd.c: Change the state depending on the command from the server.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1998
33  *     Ron Koenderink, 2005
34  *     Markus Armbruster, 2005-2007
35  */
36
37 #include <config.h>
38
39 #include <assert.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include "misc.h"
46 #include "proto.h"
47 #include "secure.h"
48
49 int eight_bit_clean;
50 FILE *auxfp;
51
52 static FILE *redir_fp;
53 static FILE *pipe_fp;
54 static int executing;
55 static size_t input_to_forget;
56
57 static void prompt(int, char *, char *);
58 static void doredir(char *p);
59 static void dopipe(char *p);
60 static int doexecute(char *p);
61
62 void
63 servercmd(int code, char *arg, int len)
64 {
65     static int nmin, nbtu, fd;
66     static char the_prompt[1024];
67     static char teles[64];
68
69     switch (code) {
70     case C_PROMPT:
71         if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
72             fprintf(stderr, "prompt: bad server prompt %s\n", arg);
73         }
74         snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
75                  nmin, nbtu);
76         if (redir_fp) {
77             (void)fclose(redir_fp);
78             redir_fp = NULL;
79         } else if (pipe_fp) {
80             (void)pclose(pipe_fp);
81             pipe_fp = NULL;
82         }
83         if (input_to_forget) {
84             forget_input(input_to_forget);
85             input_to_forget = 0;
86         }
87         prompt(code, the_prompt, teles);
88         executing = 0;
89         break;
90     case C_FLUSH:
91         snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
92         prompt(code, the_prompt, teles);
93         break;
94     case C_EXECUTE:
95         fd = doexecute(arg);
96         if (fd < 0)
97             send_eof++;
98         else {
99             input_fd = fd;
100             executing = 1;
101         }
102         break;
103     case C_EXIT:
104         printf("Exit: %s", arg);
105         if (auxfp)
106             fprintf(auxfp, "Exit: %s", arg);
107         break;
108     case C_FLASH:
109         printf("\n%s", arg);
110         if (auxfp)
111             fprintf(auxfp, "\n%s", arg);
112         break;
113     case C_INFORM:
114         if (*arg) {
115             snprintf(teles, sizeof(teles), "(%.*s )", len -1, arg);
116             if (!redir_fp && !pipe_fp) {
117                 putchar('\07');
118                 prompt(code, the_prompt, teles);
119             }
120         } else
121             teles[0] = 0;
122         break;
123     case C_PIPE:
124         dopipe(arg);
125         break;
126     case C_REDIR:
127         doredir(arg);
128         break;
129     default:
130         assert(0);
131         break;
132     }
133 }
134
135 static void
136 prompt(int code, char *prompt, char *teles)
137 {
138     char *nl;
139
140     nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
141     printf("%s%s%s", nl, teles, prompt);
142     fflush(stdout);
143     if (auxfp) {
144         fprintf(auxfp, "%s%s%s", nl, teles, prompt);
145         fflush(auxfp);
146     }
147 }
148
149 static char *
150 fname(char *s)
151 {
152     char *beg, *end;
153
154     for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
155     for (end = beg; !isspace(*(unsigned char *)end); end++) ;
156     *end = 0;
157     return beg;
158 }
159
160 static int
161 redir_authorized(char *arg, char *attempt)
162 {
163     size_t seen = seen_input(arg);
164
165     if (executing) {
166         fprintf(stderr, "Can't %s in a script\n", attempt);
167         return 0;
168     }
169
170     if (!seen || (input_to_forget && input_to_forget != seen)) {
171         fprintf(stderr, "WARNING!  Server attempted to %s %s\n",
172                 attempt, arg);
173         return 0;
174     }
175     input_to_forget = seen;
176     return 1;
177 }
178
179 /*
180  * opens redir_fp if successful
181  */
182 static void
183 doredir(char *p)
184 {
185     int mode;
186     int fd;
187
188     if (redir_fp) {
189         (void)fclose(redir_fp);
190         redir_fp = NULL;
191     }
192
193     if (!redir_authorized(p, "redirect to file"))
194         return;
195     if (*p++ != '>') {
196         fprintf(stderr, "WARNING!  Weird redirection %s", p);
197         return;
198     }
199
200     mode = O_WRONLY | O_CREAT;
201     if (*p == '>') {
202         mode |= O_APPEND;
203         p++;
204     } else if (*p == '!') {
205         mode |= O_TRUNC;
206         p++;
207     } else
208         mode |= O_EXCL;
209
210     p = fname(p);
211     if (*p == 0) {
212         fprintf(stderr, "Redirection lacks a file name\n");
213         return;
214     }
215
216     fd = open(p, mode, 0666);
217     redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
218     if (!redir_fp) {
219         fprintf(stderr, "Can't redirect to %s: %s\n",
220                 p, strerror(errno));
221     }
222 }
223
224 /*
225  * opens "pipe_fp" if successful
226  */
227 static void
228 dopipe(char *p)
229 {
230     if (!redir_authorized(p, "pipe to shell command"))
231         return;
232     if (*p++ != '|') {
233         fprintf(stderr, "WARNING!  Weird pipe %s", p);
234         return;
235     }
236
237     for (; *p && isspace(*p); p++) ;
238     if (*p == 0) {
239         fprintf(stderr, "Redirection lacks a command\n");
240         return;
241     }
242
243     if ((pipe_fp = popen(p, "w")) == NULL) {
244         fprintf(stderr, "Can't redirect to pipe %s: %s\n",
245                 p, strerror(errno));
246     }
247 }
248
249 static int
250 doexecute(char *p)
251 {
252     int fd;
253
254     if (!redir_authorized(p, "execute script file"))
255         return -1;
256
257     p = fname(p);
258     if (*p == 0) {
259         fprintf(stderr, "Need a file to execute\n");
260         return -1;
261     }
262
263     if ((fd = open(p, O_RDONLY)) < 0) {
264         fprintf(stderr, "Can't open execute file %s: %s\n",
265                 p, strerror(errno));
266         return -1;
267     }
268
269     return fd;
270 }
271
272 void
273 outch(char c)
274 {
275     if (auxfp)
276         putc(c, auxfp);
277     if (redir_fp)
278         putc(c, redir_fp);
279     else if (pipe_fp)
280         putc(c, pipe_fp);
281     else if (eight_bit_clean) {
282         if (c == 14)
283             putso();
284         else if (c == 15)
285             putse();
286         else
287             putchar(c);
288     } else if (c & 0x80) {
289         putso();
290         putchar(c & 0x7f);
291         putse();
292     } else
293         putchar(c);
294 }