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
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 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.
28 * servercmd.c: Change the state depending on the command from the server.
30 * Known contributors to this file:
33 * Ron Koenderink, 2005
54 static char num_teles[64];
55 static char the_prompt[1024];
59 static FILE *redir_fp;
63 static void prompt(FILE *auxfi);
64 static void doredir(char *p);
65 static void dopipe(char *p);
66 static void doexecute(char *p, FILE *auxfi);
67 static void output(int code, char *buf, FILE *auxfi, int eol);
68 static void screen(char *buf);
71 servercmd(struct ioqueue *ioq, FILE *auxfi)
78 while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
82 code = 10 + (*buf - 'a');
85 while (*p && !isspace(*p))
91 * C_REDIR, C_PIPE, and C_EXECUTE will not
92 * work with filename longer than one buffer
96 if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
97 fprintf(stderr, "prompt: bad server prompt %s\n", p);
100 sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
105 p[strlen(p) - 1] = '\0';
110 p[strlen(p) - 1] = '\0';
116 p[strlen(p) - 1] = '\0';
117 sprintf(the_prompt, "%s", p);
122 p[strlen(p) - 1] = '\0';
127 p[strlen(p) - 1] = '\0';
129 p[strlen(p) - 1] = '\0';
130 sprintf(num_teles, "(%s) ", p + 1);
131 if (!redir_fp && !pipe_fp && !exec_fd) {
139 output(code, p, auxfi, eol);
150 if (mode == C_PROMPT) {
152 (void)fclose(redir_fp);
154 } else if (pipe_fp) {
155 (void)pclose(pipe_fp);
157 } else if (exec_fd > 0) {
161 open("/dev/tty", O_RDONLY, 0);
164 if (mode == C_PROMPT)
166 printf("%s%s", num_teles, the_prompt);
167 (void)fflush(stdout);
169 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
175 * opens redir_fp if successful
187 (void)fclose(redir_fp);
191 if (*p && ((*p == '>') || (*p == '!')))
194 while (*p && isspace(*p))
197 while (*p && !isspace(*p))
201 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
205 mode = O_WRONLY | O_CREAT;
208 else if (how[1] == '!')
213 fprintf(stderr, "Null file name after redirect\n");
217 if ((fd = open(name, mode, 0600)) < 0) {
218 fprintf(stderr, "Redirect open failed\n");
221 redir_fp = fdopen(fd, "w");
227 * opens "pipe_fp" if successful
237 while (*p && isspace(*p))
240 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
244 fprintf(stderr, "Null program name after redirect\n");
248 if ((pipe_fp = popen(p, "w")) == NULL) {
249 fprintf(stderr, "Pipe open failed\n");
256 doexecute(char *p, FILE *auxfi)
262 while (*p && isspace(*p))
266 "WARNING! Server attempted unauthorized read of file %s\n",
271 fprintf(stderr, "Null file to execute\n");
275 if ((fd = open(p, O_RDONLY, 0)) < 0) {
276 fprintf(stderr, "Can't open execute file\n");
281 /* copies 4k at a time to the socket */
282 while (termio(fd, sock, auxfi)) /*do copy */
285 * Some platforms don't send the eof (cntl-D) at the end of
286 * copying a file. If emp_client hangs at the end of an
287 * execute, include the following line and notify wolfpack
288 * of the platform you are using.
296 output(int code, char *buf, FILE *auxfi, int eol)
300 /* not implemented; serve doesn't send it */
305 fprintf(auxfi, "Aborted\n");
311 fprintf(auxfi, "Error; ");
316 fprintf(auxfi, "Exit: ");
325 fprintf(auxfi, "%s", buf);
329 fprintf(redir_fp, "%s", buf);
331 fprintf(pipe_fp, "%s", buf);
342 while ((c = *buf++)) {
343 if (eight_bit_clean) {
350 } else if (c & 0x80) {