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
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:
53 static char the_prompt[1024];
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);
69 servercmd(struct ioqueue *ioq, FILE *auxfi)
75 while (ioq_gets(ioq, buf, sizeof(buf))) {
77 while (*p && !isspace(*p))
81 code = 10 + (*buf - 'a');
86 if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
87 fprintf(stderr, "prompt: bad server prompt %s\n", p);
90 sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
101 sprintf(the_prompt, "%s", p);
109 p[strlen(p) - 1] = '\0';
110 sprintf(num_teles, "(%s) ", p + 1);
111 if (!redir_fp && !pipe_fp && !exec_fd) {
119 output(code, p, auxfi);
128 if (mode == C_PROMPT) {
130 (void)fclose(redir_fp);
132 } else if (pipe_fp) {
133 (void)pclose(pipe_fp);
135 } else if (exec_fd > 0) {
139 open("/dev/tty", O_RDONLY, 0);
142 if (mode == C_PROMPT)
144 printf("%s%s", num_teles, the_prompt);
145 (void)fflush(stdout);
147 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
153 * opens redir_fp if successful
165 (void)fclose(redir_fp);
169 if (*p && ((*p == '>') || (*p == '!')))
172 while (*p && isspace(*p))
175 while (*p && !isspace(*p))
179 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
183 mode = O_WRONLY | O_CREAT;
186 else if (how[1] == '!')
191 fprintf(stderr, "Null file name after redirect\n");
195 if ((fd = open(name, mode, 0600)) < 0) {
196 fprintf(stderr, "Redirect open failed\n");
199 redir_fp = fdopen(fd, "w");
205 * opens "pipe_fp" if successful
215 while (*p && isspace(*p))
218 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
222 fprintf(stderr, "Null program name after redirect\n");
226 if ((pipe_fp = popen(p, "w")) == NULL) {
227 fprintf(stderr, "Pipe open failed\n");
234 doexecute(char *p, FILE *auxfi)
240 while (*p && isspace(*p))
244 "WARNING! Server attempted unauthorized read of file %s\n",
249 fprintf(stderr, "Null file to execute\n");
253 if ((fd = open(p, O_RDONLY, 0)) < 0) {
254 fprintf(stderr, "Can't open execute file\n");
259 /* copies 4k at a time to the socket */
260 while (termio(fd, sock, auxfi)) /*do copy */
262 /* Some platforms don't send the eof (cntl-D) at the end of
263 copying a file. If emp_client hangs at the end of an
264 execute, include the following line and notify wolfpack
265 of the platform you are using.
273 output(int code, char *buf, FILE *auxfi)
277 /* not implemented; serve doesn't send it */
280 (void)fflush(stdout);
287 fprintf(auxfi, "Aborted\n");
293 fprintf(auxfi, "Error; ");
298 fprintf(auxfi, "Exit: ");
307 fprintf(auxfi, "%s", buf);
311 (void)putc('\n', auxfi);
315 fprintf(redir_fp, "%s\n", buf);
317 fprintf(pipe_fp, "%s\n", buf);
321 (void)fflush(stdout);
323 (void)putc('\n', stdout);
332 while ((c = *buf++)) {
333 if (eight_bit_clean) {
340 } else if (c & 0x80) {