2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * servercmd.c: Change the state depending on the command from the server.
29 * Known contributors to this file:
32 * Ron Koenderink, 2005
33 * Markus Armbruster, 2005-2010
52 static FILE *redir_fp;
53 static int redir_is_pipe;
55 static size_t input_to_forget;
57 static void prompt(int, char *, char *);
58 static void doredir(char *p);
59 static void dopipe(char *p);
60 static int doexecute(char *p);
63 servercmd(int code, char *arg, int len)
65 static int nmin, nbtu, fd;
66 static char the_prompt[1024];
67 static char teles[64];
71 if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
72 fprintf(stderr, "prompt: bad server prompt %s\n", arg);
74 snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
78 (void)pclose(redir_fp);
80 (void)fclose(redir_fp);
83 if (input_to_forget) {
84 forget_input(input_to_forget);
87 prompt(code, the_prompt, teles);
91 snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
92 prompt(code, the_prompt, teles);
104 printf("Exit: %s", arg);
106 fprintf(auxfp, "Exit: %s", arg);
111 fprintf(auxfp, "\n%s", arg);
114 if (arg[0] != '\n') {
115 snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
118 prompt(code, the_prompt, teles);
136 prompt(int code, char *prompt, char *teles)
140 nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
141 printf("%s%s%s", nl, teles, prompt);
144 fprintf(auxfp, "%s%s%s", nl, teles, prompt);
154 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
155 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
161 redir_authorized(char *arg, char *attempt, int expected)
163 size_t seen = seen_input(arg);
166 fprintf(stderr, "Can't %s in restricted mode\n", attempt);
171 fprintf(stderr, "Can't %s in a batch file\n", attempt);
176 fprintf(stderr, "WARNING! Server attempted to %s unexpectedly\n",
181 if (!seen || (input_to_forget && input_to_forget != seen)) {
182 fprintf(stderr, "WARNING! Server attempted to %s %s\n",
186 input_to_forget = seen;
196 if (!redir_authorized(p, "redirect to file", !redir_fp))
199 fprintf(stderr, "WARNING! Weird redirection %s", p);
203 mode = O_WRONLY | O_CREAT;
207 } else if (*p == '!') {
215 fprintf(stderr, "Redirection lacks a file name\n");
220 fd = open(p, mode, 0666);
221 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
223 fprintf(stderr, "Can't redirect to %s: %s\n",
231 if (!redir_authorized(p, "pipe to shell command", !redir_fp))
234 fprintf(stderr, "WARNING! Weird pipe %s", p);
238 for (; *p && isspace(*p); p++) ;
240 fprintf(stderr, "Redirection lacks a command\n");
245 if ((redir_fp = popen(p, "w")) == NULL) {
246 fprintf(stderr, "Can't redirect to pipe %s: %s\n",
256 if (!redir_authorized(p, "execute batch file", 1))
261 fprintf(stderr, "Need a file to execute\n");
265 if ((fd = open(p, O_RDONLY)) < 0) {
266 fprintf(stderr, "Can't open execute file %s: %s\n",
281 else if (eight_bit_clean) {
288 } else if (c & 0x80) {