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, int eol);
66 static void screen(char *buf);
69 servercmd(struct ioqueue *ioq, FILE *auxfi)
76 while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
80 code = 10 + (*buf - 'a');
83 while (*p && !isspace(*p))
89 if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
90 fprintf(stderr, "prompt: bad server prompt %s\n", p);
93 sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
104 sprintf(the_prompt, "%s", p);
112 p[strlen(p) - 1] = '\0';
113 sprintf(num_teles, "(%s) ", p + 1);
114 if (!redir_fp && !pipe_fp && !exec_fd) {
122 output(code, p, auxfi, eol);
133 if (mode == C_PROMPT) {
135 (void)fclose(redir_fp);
137 } else if (pipe_fp) {
138 (void)pclose(pipe_fp);
140 } else if (exec_fd > 0) {
144 open("/dev/tty", O_RDONLY, 0);
147 if (mode == C_PROMPT)
149 printf("%s%s", num_teles, the_prompt);
150 (void)fflush(stdout);
152 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
158 * opens redir_fp if successful
170 (void)fclose(redir_fp);
174 if (*p && ((*p == '>') || (*p == '!')))
177 while (*p && isspace(*p))
180 while (*p && !isspace(*p))
184 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
188 mode = O_WRONLY | O_CREAT;
191 else if (how[1] == '!')
196 fprintf(stderr, "Null file name after redirect\n");
200 if ((fd = open(name, mode, 0600)) < 0) {
201 fprintf(stderr, "Redirect open failed\n");
204 redir_fp = fdopen(fd, "w");
210 * opens "pipe_fp" if successful
220 while (*p && isspace(*p))
223 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
227 fprintf(stderr, "Null program name after redirect\n");
231 if ((pipe_fp = popen(p, "w")) == NULL) {
232 fprintf(stderr, "Pipe open failed\n");
239 doexecute(char *p, FILE *auxfi)
245 while (*p && isspace(*p))
249 "WARNING! Server attempted unauthorized read of file %s\n",
254 fprintf(stderr, "Null file to execute\n");
258 if ((fd = open(p, O_RDONLY, 0)) < 0) {
259 fprintf(stderr, "Can't open execute file\n");
264 /* copies 4k at a time to the socket */
265 while (termio(fd, sock, auxfi)) /*do copy */
267 /* Some platforms don't send the eof (cntl-D) at the end of
268 copying a file. If emp_client hangs at the end of an
269 execute, include the following line and notify wolfpack
270 of the platform you are using.
278 output(int code, char *buf, FILE *auxfi, int eol)
282 /* not implemented; serve doesn't send it */
285 (void)fflush(stdout);
292 fprintf(auxfi, "Aborted\n");
298 fprintf(auxfi, "Error; ");
303 fprintf(auxfi, "Exit: ");
312 fprintf(auxfi, "%s", buf);
316 (void)putc('\n', auxfi);
320 fprintf(redir_fp, "%s%s", buf, eol ? "\n" : "");
322 fprintf(pipe_fp, eol ? "%s\n": "%s", buf);
326 (void)fflush(stdout);
328 (void)putc('\n', stdout);
337 while ((c = *buf++)) {
338 if (eight_bit_clean) {
345 } else if (c & 0x80) {