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
34 * Markus Armbruster, 2005-2007
52 static FILE *redir_fp;
54 static size_t input_to_forget;
56 static void prompt(int, char *, char *);
57 static void doredir(char *p);
58 static void dopipe(char *p);
59 static void doexecute(char *p);
62 servercmd(int code, char *arg, int len)
64 static int nmin, nbtu;
65 static char the_prompt[1024];
66 static char teles[64];
70 if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
71 fprintf(stderr, "prompt: bad server prompt %s\n", arg);
73 snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
75 prompt(code, the_prompt, teles);
78 snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
79 prompt(code, the_prompt, teles);
85 printf("Exit: %s", arg);
87 fprintf(auxfp, "Exit: %s", arg);
92 fprintf(auxfp, "\n%s", arg);
96 snprintf(teles, sizeof(teles), "(%.*s )", len -1, arg);
97 if (!redir_fp && !pipe_fp) {
99 prompt(code, the_prompt, teles);
117 prompt(int code, char *prompt, char *teles)
121 if (code == C_PROMPT) {
123 (void)fclose(redir_fp);
125 } else if (pipe_fp) {
126 (void)pclose(pipe_fp);
129 if (input_to_forget) {
130 forget_input(input_to_forget);
135 nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
136 printf("%s%s%s", nl, teles, prompt);
139 fprintf(auxfp, "%s%s%s", nl, teles, prompt);
149 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
150 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
156 redir_authorized(char *arg, char *attempt)
158 size_t seen = seen_input(arg);
160 if (!seen || (input_to_forget && input_to_forget != seen)) {
161 fprintf(stderr, "WARNING! Server attempted to %s %s\n",
165 input_to_forget = seen;
170 * opens redir_fp if successful
179 (void)fclose(redir_fp);
183 if (!redir_authorized(p, "redirect to file"))
186 fprintf(stderr, "WARNING! Weird redirection %s", p);
190 mode = O_WRONLY | O_CREAT;
194 } else if (*p == '!') {
202 fprintf(stderr, "Redirection lacks a file name\n");
206 fd = open(p, mode, 0666);
207 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
209 fprintf(stderr, "Can't redirect to %s: %s\n",
215 * opens "pipe_fp" if successful
220 if (!redir_authorized(p, "pipe to command"))
223 fprintf(stderr, "WARNING! Weird pipe %s", p);
227 for (; *p && isspace(*p); p++) ;
229 fprintf(stderr, "Redirection lacks a command\n");
233 if ((pipe_fp = popen(p, "w")) == NULL) {
234 fprintf(stderr, "Can't redirect to pipe %s: %s\n",
242 input_fd = -1; /* make sure play() terminates exec */
244 if (!redir_authorized(p, "read file"))
249 fprintf(stderr, "Need a file to execute\n");
253 if ((input_fd = open(p, O_RDONLY)) < 0) {
254 fprintf(stderr, "Can't open execute file %s: %s\n",
269 else if (eight_bit_clean) {
276 } else if (c & 0x80) {