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