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
51 static FILE *redir_fp;
54 static void prompt(int, char *, char *);
55 static void doredir(char *p);
56 static void dopipe(char *p);
57 static void doexecute(char *p);
60 servercmd(int code, char *arg, int len)
62 static int nmin, nbtu;
63 static char the_prompt[1024];
64 static char teles[64];
68 if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
69 fprintf(stderr, "prompt: bad server prompt %s\n", arg);
71 snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
73 prompt(code, the_prompt, teles);
76 snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
77 prompt(code, the_prompt, teles);
83 printf("Exit: %s", arg);
85 fprintf(auxfp, "Exit: %s", arg);
90 fprintf(auxfp, "\n%s", arg);
94 snprintf(teles, sizeof(teles), "(%.*s )", len -1, arg);
95 if (!redir_fp && !pipe_fp) {
97 prompt(code, the_prompt, teles);
115 prompt(int code, char *prompt, char *teles)
119 if (code == C_PROMPT) {
121 (void)fclose(redir_fp);
123 } else if (pipe_fp) {
124 (void)pclose(pipe_fp);
128 nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
129 printf("%s%s%s", nl, teles, prompt);
132 fprintf(auxfp, "%s%s%s", nl, teles, prompt);
142 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
143 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
149 redir_authorized(char *arg, char *attempt)
151 if (!seen_input(arg)) {
152 fprintf(stderr, "WARNING! Server attempted to %s %s\n",
160 * opens redir_fp if successful
169 (void)fclose(redir_fp);
173 if (!redir_authorized(p, "redirect to file"))
176 fprintf(stderr, "WARNING! Weird redirection %s", p);
180 mode = O_WRONLY | O_CREAT;
184 } else if (*p == '!') {
192 fprintf(stderr, "Redirection lacks a file name\n");
196 fd = open(p, mode, 0666);
197 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
199 fprintf(stderr, "Can't redirect to %s: %s\n",
205 * opens "pipe_fp" if successful
210 if (!redir_authorized(p, "pipe to command"))
213 fprintf(stderr, "WARNING! Weird pipe %s", p);
217 for (; *p && isspace(*p); p++) ;
219 fprintf(stderr, "Redirection lacks a command\n");
223 if ((pipe_fp = popen(p, "w")) == NULL) {
224 fprintf(stderr, "Can't redirect to pipe %s: %s\n",
232 input_fd = -1; /* make sure play() terminates exec */
234 if (!redir_authorized(p, "read file"))
239 fprintf(stderr, "Need a file to execute\n");
243 if ((input_fd = open(p, O_RDONLY)) < 0) {
244 fprintf(stderr, "Can't open execute file %s: %s\n",
259 else if (eight_bit_clean) {
266 } else if (c & 0x80) {