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
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.
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.
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
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.
28 * servercmd.c: Change the state depending on the command from the server.
30 * Known contributors to this file:
47 extern s_char *gettag();
50 static s_char the_prompt[1024];
77 s_char *ioq_gets(struct ioqueue *, s_char *, int);
82 while (ioq_gets(ioq, buf, sizeof(buf))) {
84 while (*p && !isspace(*p))
88 code = 10 + (*buf - 'a');
93 if (sscanf(p, "%d %d", &nbtu, &nmin) != 2) {
94 fprintf(stderr, "prompt: bad server prompt %s\n", p);
97 sprintf(the_prompt, "[%d:%d] Command : ", nbtu, nmin);
108 sprintf(the_prompt, "%s", p);
116 p[strlen(p) - 1] = '\0';
117 sprintf(num_teles, "(%s) ", p + 1);
118 if (!redir_fp && !pipe_fp && !exec_fd) {
126 output(code, p, auxfi);
136 if (mode == C_PROMPT) {
138 (void)fclose(redir_fp);
140 } else if (pipe_fp) {
142 (void)pclose(pipe_fp);
145 } else if (exec_fd > 0) {
149 open("/dev/tty", O_RDONLY, 0);
152 if (mode == C_PROMPT)
154 printf("%s%s", num_teles, the_prompt);
155 (void)fflush(stdout);
157 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
163 * opens redir_fp if successful
176 (void)fclose(redir_fp);
180 if (*p && ((*p == '>') || (*p == '!')))
183 while (*p && isspace(*p))
186 while (*p && !isspace(*p))
190 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
194 mode = O_WRONLY | O_CREAT;
197 else if (how[1] == '!')
202 fprintf(stderr, "Null file name after redirect\n");
206 if ((fd = open(name, mode, 0600)) < 0) {
207 fprintf(stderr, "Redirect open failed\n");
210 redir_fp = fdopen(fd, "w");
216 * opens "pipe_fp" if successful
222 extern FILE *popen();
228 while (*p && isspace(*p))
231 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
235 fprintf(stderr, "Null program name after redirect\n");
240 if ((pipe_fp = popen(p, "w")) == 0) {
244 fprintf(stderr, "Pipe open failed\n");
260 while (*p && isspace(*p))
264 "WARNING! Server attempted unauthorized read of file %s\n",
269 fprintf(stderr, "Null file to execute\n");
274 if ((fd = open(p, O_RDONLY, 0)) < 0) {
276 if ((fd = open(p, O_RDONLY | O_BINARY, 0)) < 0) {
278 fprintf(stderr, "Can't open execute file\n");
283 /* copies 4k at a time to the socket */
284 while (termio(fd, sock, auxfi)) /*do copy */
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.
297 output(code, buf, auxfi)
307 (void)fflush(stdout);
314 fprintf(auxfi, "Aborted\n");
320 fprintf(auxfi, "Error; ");
325 fprintf(auxfi, "Exit: ");
334 fprintf(auxfi, "%s", buf);
338 (void)putc('\n', auxfi);
342 fprintf(redir_fp, "%s\n", buf);
344 fprintf(pipe_fp, "%s\n", buf);
351 (void)fflush(stdout);
353 (void)putc('\n', stdout);
359 register s_char *buf;
361 register s_char *sop;
364 while ((c = *buf++)) {
366 for (sop = SO; putc(*sop, stdout); sop++) ;
367 (void)putc(c & 0x7f, stdout);
368 for (sop = SE; putc(*sop, stdout); sop++) ;
370 (void)putc(c, stdout);