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) {
134 (void)pclose(pipe_fp);
137 } else if (exec_fd > 0) {
141 open("/dev/tty", O_RDONLY, 0);
144 if (mode == C_PROMPT)
146 printf("%s%s", num_teles, the_prompt);
147 (void)fflush(stdout);
149 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
155 * opens redir_fp if successful
167 (void)fclose(redir_fp);
171 if (*p && ((*p == '>') || (*p == '!')))
174 while (*p && isspace(*p))
177 while (*p && !isspace(*p))
181 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
185 mode = O_WRONLY | O_CREAT;
188 else if (how[1] == '!')
193 fprintf(stderr, "Null file name after redirect\n");
197 if ((fd = open(name, mode, 0600)) < 0) {
198 fprintf(stderr, "Redirect open failed\n");
201 redir_fp = fdopen(fd, "w");
207 * opens "pipe_fp" if successful
217 while (*p && isspace(*p))
220 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
224 fprintf(stderr, "Null program name after redirect\n");
229 if ((pipe_fp = popen(p, "w")) == NULL) {
233 fprintf(stderr, "Pipe open failed\n");
240 doexecute(char *p, FILE *auxfi)
246 while (*p && isspace(*p))
250 "WARNING! Server attempted unauthorized read of file %s\n",
255 fprintf(stderr, "Null file to execute\n");
260 if ((fd = open(p, O_RDONLY, 0)) < 0) {
262 if ((fd = open(p, O_RDONLY | O_BINARY, 0)) < 0) {
264 fprintf(stderr, "Can't open execute file\n");
269 /* copies 4k at a time to the socket */
270 while (termio(fd, sock, auxfi)) /*do copy */
272 /* Some platforms don't send the eof (cntl-D) at the end of
273 copying a file. If emp_client hangs at the end of an
274 execute, include the following line and notify wolfpack
275 of the platform you are using.
283 output(int code, char *buf, FILE *auxfi)
287 /* not implemented; serve doesn't send it */
290 (void)fflush(stdout);
297 fprintf(auxfi, "Aborted\n");
303 fprintf(auxfi, "Error; ");
308 fprintf(auxfi, "Exit: ");
317 fprintf(auxfi, "%s", buf);
321 (void)putc('\n', auxfi);
325 fprintf(redir_fp, "%s\n", buf);
327 fprintf(pipe_fp, "%s\n", buf);
331 (void)fflush(stdout);
333 (void)putc('\n', stdout);
342 while ((c = *buf++)) {
343 if (eight_bit_clean) {
353 } else if (c & 0x80) {