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
55 static char num_teles[64];
56 static char the_prompt[1024];
60 static FILE *redir_fp;
63 static void prompt(FILE *auxfi);
64 static void doredir(char *p);
65 static void dopipe(char *p);
66 static void doexecute(char *p, FILE *auxfi);
67 static void output(int code, char *buf, FILE *auxfi);
68 static void screen(char *buf);
71 servercmd(struct ioqueue *ioq, FILE *auxfi)
78 while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
82 code = 10 + (*buf - 'a');
85 while (*p && !isspace(*p))
91 * C_REDIR, C_PIPE, and C_EXECUTE will not
92 * work with filename longer than one buffer
96 if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
97 fprintf(stderr, "prompt: bad server prompt %s\n", p);
100 sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
105 p[strlen(p) - 1] = '\0';
110 p[strlen(p) - 1] = '\0';
116 p[strlen(p) - 1] = '\0';
117 sprintf(the_prompt, "%s", p);
122 p[strlen(p) - 1] = '\0';
127 p[strlen(p) - 1] = '\0';
129 p[strlen(p) - 1] = '\0';
130 sprintf(num_teles, "(%s) ", p + 1);
131 if (!redir_fp && !pipe_fp) {
139 output(code, p, auxfi);
150 if (mode == C_PROMPT) {
152 (void)fclose(redir_fp);
154 } else if (pipe_fp) {
155 (void)pclose(pipe_fp);
159 if (mode == C_PROMPT)
161 printf("%s%s", num_teles, the_prompt);
162 (void)fflush(stdout);
164 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
174 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
175 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
181 * opens redir_fp if successful
191 (void)fclose(redir_fp);
196 fprintf(stderr, "WARNING! Weird redirection %s", p);
200 mode = O_WRONLY | O_CREAT;
204 } else if (*p == '!') {
213 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
220 fprintf(stderr, "Redirection lacks a file name\n");
223 fd = open(p, mode, 0600);
224 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
226 fprintf(stderr, "Can't redirect to %s: %s\n",
232 * opens "pipe_fp" if successful
240 fprintf(stderr, "WARNING! Weird pipe %s", p);
246 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
251 for (; *p && isspace(*p); p++) ;
253 fprintf(stderr, "Redirection lacks a command\n");
256 if ((pipe_fp = popen(p, "w")) == NULL) {
257 fprintf(stderr, "Can't redirect to pipe %s: %s\n",
263 doexecute(char *p, FILE *auxfi)
271 "WARNING! Server attempted unauthorized read of file %s\n",
279 fprintf(stderr, "Need a file to execute\n");
282 if ((fd = open(p, O_RDONLY, 0)) < 0) {
283 fprintf(stderr, "Can't open execute file %s: %s\n",
287 /* copies 4k at a time to the socket */
288 while (termio(fd, sock, auxfi)) /*do copy */
291 * Some platforms don't send the eof (cntl-D) at the end of
292 * copying a file. If emp_client hangs at the end of an
293 * execute, include the following line and notify wolfpack
294 * of the platform you are using.
301 output(int code, char *buf, FILE *auxfi)
305 /* not implemented; server doesn't send it */
310 fprintf(auxfi, "Aborted\n");
316 fprintf(auxfi, "Error; ");
321 fprintf(auxfi, "Exit: ");
330 fprintf(auxfi, "%s", buf);
334 fprintf(redir_fp, "%s", buf);
336 fprintf(pipe_fp, "%s", buf);
347 while ((c = *buf++)) {
348 if (eight_bit_clean) {
355 } else if (c & 0x80) {