]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
(prompt,dopipe) [_WIN32]: Enable the pipe capability.
[empserver] / src / client / servcmd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
34
35 #include "misc.h"
36 #include "proto.h"
37 #include "queue.h"
38 #include "ioqueue.h"
39 #include "tags.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <fcntl.h>
46 #if !defined(_WIN32)
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #endif
51
52 char num_teles[64];
53 static char the_prompt[1024];
54 static int mode;
55 static int nbtu;
56 static int nmin;
57 FILE *redir_fp;
58 FILE *pipe_fp;
59 int exec_fd;
60
61 static void prompt(FILE *auxfi);
62 static void doredir(char *p);
63 static void dopipe(char *p);
64 static void doexecute(char *p, FILE *auxfi);
65 static void output(int code, char *buf, FILE *auxfi);
66 static void screen(char *buf);
67
68 void
69 servercmd(struct ioqueue *ioq, FILE *auxfi)
70 {
71     char buf[1024];
72     char *p;
73     int code;
74
75     while (ioq_gets(ioq, buf, sizeof(buf))) {
76         p = buf;
77         while (*p && !isspace(*p))
78             p++;
79         *p++ = 0;
80         if (isalpha(*buf))
81             code = 10 + (*buf - 'a');
82         else
83             code = *buf - '0';
84         switch (code) {
85         case C_PROMPT:
86             if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
87                 fprintf(stderr, "prompt: bad server prompt %s\n", p);
88             }
89             mode = code;
90             sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
91             prompt(auxfi);
92             break;
93         case C_REDIR:
94             doredir(p);
95             break;
96         case C_PIPE:
97             dopipe(p);
98             break;
99         case C_FLUSH:
100             mode = code;
101             sprintf(the_prompt, "%s", p);
102             prompt(auxfi);
103             break;
104         case C_EXECUTE:
105             doexecute(p, auxfi);
106             break;
107         case C_INFORM:
108             if (*p) {
109                 p[strlen(p) - 1] = '\0';
110                 sprintf(num_teles, "(%s) ", p + 1);
111                 if (!redir_fp && !pipe_fp && !exec_fd) {
112                     putchar('\07');
113                     prompt(NULL);
114                 }
115             } else
116                 *num_teles = '\0';
117             break;
118         default:
119             output(code, p, auxfi);
120             break;
121         }
122     }
123 }
124
125 static void
126 prompt(FILE *auxfi)
127 {
128     if (mode == C_PROMPT) {
129         if (redir_fp) {
130             (void)fclose(redir_fp);
131             redir_fp = NULL;
132         } else if (pipe_fp) {
133             (void)pclose(pipe_fp);
134             pipe_fp = NULL;
135         } else if (exec_fd > 0) {
136             close(exec_fd);
137             close(0);
138             exec_fd = -1;
139             open("/dev/tty", O_RDONLY, 0);
140         }
141     }
142     if (mode == C_PROMPT)
143         printf("\n");
144     printf("%s%s", num_teles, the_prompt);
145     (void)fflush(stdout);
146     if (auxfi) {
147         fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
148         (void)fflush(auxfi);
149     }
150 }
151
152 /*
153  * opens redir_fp if successful
154  */
155 static void
156 doredir(char *p)
157 {
158     char *how;
159     char *name;
160     char *tag;
161     int mode;
162     int fd;
163
164     if (redir_fp) {
165         (void)fclose(redir_fp);
166         redir_fp = NULL;
167     }
168     how = p++;
169     if (*p && ((*p == '>') || (*p == '!')))
170         p++;
171     tag = gettag(p);
172     while (*p && isspace(*p))
173         p++;
174     name = p;
175     while (*p && !isspace(*p))
176         p++;
177     *p = 0;
178     if (tag == NULL) {
179         fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
180                 name);
181         return;
182     }
183     mode = O_WRONLY | O_CREAT;
184     if (how[1] == '>')
185         mode |= O_APPEND;
186     else if (how[1] == '!')
187         mode |= O_TRUNC;
188     else
189         mode |= O_EXCL;
190     if (*name == 0) {
191         fprintf(stderr, "Null file name after redirect\n");
192         free(tag);
193         return;
194     }
195     if ((fd = open(name, mode, 0600)) < 0) {
196         fprintf(stderr, "Redirect open failed\n");
197         perror(name);
198     } else {
199         redir_fp = fdopen(fd, "w");
200     }
201     free(tag);
202 }
203
204 /*
205  * opens "pipe_fp" if successful
206  */
207 static void
208 dopipe(char *p)
209 {
210     char *tag;
211
212     if (*p == '|')
213         p++;
214     tag = gettag(p);
215     while (*p && isspace(*p))
216         p++;
217     if (tag == NULL) {
218         fprintf(stderr, "WARNING!  Server attempted to run: %s\n", p);
219         return;
220     }
221     if (*p == 0) {
222         fprintf(stderr, "Null program name after redirect\n");
223         free(tag);
224         return;
225     }
226     if ((pipe_fp = popen(p, "w")) == NULL) {
227         fprintf(stderr, "Pipe open failed\n");
228         perror(p);
229     }
230     free(tag);
231 }
232
233 static void
234 doexecute(char *p, FILE *auxfi)
235 {
236     int fd;
237     char *tag;
238
239     tag = gettag(p);
240     while (*p && isspace(*p))
241         p++;
242     if (tag == NULL) {
243         fprintf(stderr,
244                 "WARNING!  Server attempted unauthorized read of file %s\n",
245                 p);
246         return;
247     }
248     if (p == NULL) {
249         fprintf(stderr, "Null file to execute\n");
250         free(tag);
251         return;
252     }
253 #if !defined(_WIN32)
254     if ((fd = open(p, O_RDONLY, 0)) < 0) {
255 #else
256     if ((fd = open(p, O_RDONLY | O_BINARY, 0)) < 0) {
257 #endif
258         fprintf(stderr, "Can't open execute file\n");
259         perror(p);
260         free(tag);
261         return;
262     }
263     /* copies 4k at a time to the socket */
264     while (termio(fd, sock, auxfi))     /*do copy */
265         ;
266     /* Some platforms don't send the eof (cntl-D) at the end of
267        copying a file.  If emp_client hangs at the end of an
268        execute, include the following line and notify wolfpack
269        of the platform you are using.
270        sendeof(sock);
271      */
272     close(fd);
273     free(tag);
274 }
275
276 static void
277 output(int code, char *buf, FILE *auxfi)
278 {
279     switch (code) {
280     case C_NOECHO:
281         /* not implemented; serve doesn't send it */
282         break;
283     case C_FLUSH:
284         (void)fflush(stdout);
285         if (auxfi)
286             (void)fflush(auxfi);
287         break;
288     case C_ABORT:
289         printf("Aborted\n");
290         if (auxfi)
291             fprintf(auxfi, "Aborted\n");
292         break;
293     case C_CMDERR:
294     case C_BADCMD:
295         printf("Error; ");
296         if (auxfi)
297             fprintf(auxfi, "Error; ");
298         break;
299     case C_EXIT:
300         printf("Exit: ");
301         if (auxfi)
302             fprintf(auxfi, "Exit: ");
303         break;
304     case C_FLASH:
305         printf("\n");
306         break;
307     default:
308         break;
309     }
310     if (auxfi) {
311         fprintf(auxfi, "%s", buf);
312         if (code == C_FLUSH)
313             (void)fflush(auxfi);
314         else
315             (void)putc('\n', auxfi);
316     }
317
318     if (redir_fp)
319         fprintf(redir_fp, "%s\n", buf);
320     else if (pipe_fp)
321         fprintf(pipe_fp, "%s\n", buf);
322     else {
323         screen(buf);
324         if (code == C_FLUSH)
325             (void)fflush(stdout);
326         else
327             (void)putc('\n', stdout);
328     }
329 }
330
331 static void
332 screen(char *buf)
333 {
334     char c;
335
336     while ((c = *buf++)) {
337         if (eight_bit_clean) {
338             if (c == 14)
339                 putso();
340             else if (c == 15)
341                 putse();
342             else
343                 putchar(c);
344         } else if (c & 0x80) {
345             putso();
346             putchar(c & 0x7f);
347             putse();
348         } else
349             putchar(c);
350     }
351 }