]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
Refactor redirection data structure, no functional change:
[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 int redir_is_pipe;
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             if (redir_is_pipe)
78                 (void)pclose(redir_fp);
79             else
80                 (void)fclose(redir_fp);
81             redir_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) {
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 static void
180 doredir(char *p)
181 {
182     int mode;
183     int fd;
184
185     if (redir_fp) {
186         (void)fclose(redir_fp);
187         redir_fp = NULL;
188     }
189
190     if (!redir_authorized(p, "redirect to file"))
191         return;
192     if (*p++ != '>') {
193         fprintf(stderr, "WARNING!  Weird redirection %s", p);
194         return;
195     }
196
197     mode = O_WRONLY | O_CREAT;
198     if (*p == '>') {
199         mode |= O_APPEND;
200         p++;
201     } else if (*p == '!') {
202         mode |= O_TRUNC;
203         p++;
204     } else
205         mode |= O_EXCL;
206
207     p = fname(p);
208     if (*p == 0) {
209         fprintf(stderr, "Redirection lacks a file name\n");
210         return;
211     }
212
213     redir_is_pipe = 0;
214     fd = open(p, mode, 0666);
215     redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
216     if (!redir_fp) {
217         fprintf(stderr, "Can't redirect to %s: %s\n",
218                 p, strerror(errno));
219     }
220 }
221
222 static void
223 dopipe(char *p)
224 {
225     if (!redir_authorized(p, "pipe to shell command"))
226         return;
227     if (*p++ != '|') {
228         fprintf(stderr, "WARNING!  Weird pipe %s", p);
229         return;
230     }
231
232     for (; *p && isspace(*p); p++) ;
233     if (*p == 0) {
234         fprintf(stderr, "Redirection lacks a command\n");
235         return;
236     }
237
238     redir_is_pipe = 1;
239     if ((redir_fp = popen(p, "w")) == NULL) {
240         fprintf(stderr, "Can't redirect to pipe %s: %s\n",
241                 p, strerror(errno));
242     }
243 }
244
245 static int
246 doexecute(char *p)
247 {
248     int fd;
249
250     if (!redir_authorized(p, "execute script file"))
251         return -1;
252
253     p = fname(p);
254     if (*p == 0) {
255         fprintf(stderr, "Need a file to execute\n");
256         return -1;
257     }
258
259     if ((fd = open(p, O_RDONLY)) < 0) {
260         fprintf(stderr, "Can't open execute file %s: %s\n",
261                 p, strerror(errno));
262         return -1;
263     }
264
265     return fd;
266 }
267
268 void
269 outch(char c)
270 {
271     if (auxfp)
272         putc(c, auxfp);
273     if (redir_fp)
274         putc(c, redir_fp);
275     else if (eight_bit_clean) {
276         if (c == 14)
277             putso();
278         else if (c == 15)
279             putse();
280         else
281             putchar(c);
282     } else if (c & 0x80) {
283         putso();
284         putchar(c & 0x7f);
285         putse();
286     } else
287         putchar(c);
288 }