2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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
34 * Markus Armbruster, 2005-2007
50 #define open(filename, flags, ...) \
52 ? w32_openfd((filename), (flags), ## __VA_ARGS__) \
53 : w32_openhandle((filename), (flags)))
59 static FILE *redir_fp;
60 static int redir_is_pipe;
62 static size_t input_to_forget;
64 static void prompt(int, char *, char *);
65 static void doredir(char *p);
66 static void dopipe(char *p);
67 static int doexecute(char *p);
70 servercmd(int code, char *arg, int len)
72 static int nmin, nbtu, fd;
73 static char the_prompt[1024];
74 static char teles[64];
78 if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
79 fprintf(stderr, "prompt: bad server prompt %s\n", arg);
81 snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
85 (void)pclose(redir_fp);
87 (void)fclose(redir_fp);
90 if (input_to_forget) {
91 forget_input(input_to_forget);
94 prompt(code, the_prompt, teles);
98 snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
99 prompt(code, the_prompt, teles);
111 printf("Exit: %s", arg);
113 fprintf(auxfp, "Exit: %s", arg);
118 fprintf(auxfp, "\n%s", arg);
121 if (arg[0] != '\n') {
122 snprintf(teles, sizeof(teles), "(%.*s) ", len -1, arg);
125 prompt(code, the_prompt, teles);
143 prompt(int code, char *prompt, char *teles)
147 nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
148 printf("%s%s%s", nl, teles, prompt);
151 fprintf(auxfp, "%s%s%s", nl, teles, prompt);
161 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
162 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
168 redir_authorized(char *arg, char *attempt, int expected)
170 size_t seen = seen_input(arg);
173 fprintf(stderr, "Can't %s in a batch file\n", attempt);
178 fprintf(stderr, "WARNING! Server attempted to %s unexpectedly\n",
183 if (!seen || (input_to_forget && input_to_forget != seen)) {
184 fprintf(stderr, "WARNING! Server attempted to %s %s\n",
188 input_to_forget = seen;
198 if (!redir_authorized(p, "redirect to file", !redir_fp))
201 fprintf(stderr, "WARNING! Weird redirection %s", p);
205 mode = O_WRONLY | O_CREAT;
209 } else if (*p == '!') {
217 fprintf(stderr, "Redirection lacks a file name\n");
222 fd = open(p, mode, 0666);
223 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
225 fprintf(stderr, "Can't redirect to %s: %s\n",
233 if (!redir_authorized(p, "pipe to shell command", !redir_fp))
236 fprintf(stderr, "WARNING! Weird pipe %s", p);
240 for (; *p && isspace(*p); p++) ;
242 fprintf(stderr, "Redirection lacks a command\n");
247 if ((redir_fp = popen(p, "w")) == NULL) {
248 fprintf(stderr, "Can't redirect to pipe %s: %s\n",
258 if (!redir_authorized(p, "execute batch file", 1))
263 fprintf(stderr, "Need a file to execute\n");
267 if ((fd = open(p, O_RDONLY)) < 0) {
268 fprintf(stderr, "Can't open execute file %s: %s\n",
283 else if (eight_bit_clean) {
290 } else if (c & 0x80) {