2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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:
53 static char the_prompt[1024];
61 static void prompt(FILE *auxfi);
62 static void doredir(char *p);
63 static void dopipe(char *p);
64 static void doexecute(char *p, FILE *auxfi);
65 static void output(int code, char *buf, FILE *auxfi, int eol);
66 static void screen(char *buf);
69 servercmd(struct ioqueue *ioq, FILE *auxfi)
76 while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
80 code = 10 + (*buf - 'a');
83 while (*p && !isspace(*p))
89 * C_REDIR, C_PIPE, and C_EXECUTE will not
90 * work with filename longer than one buffer
94 if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
95 fprintf(stderr, "prompt: bad server prompt %s\n", p);
98 sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
103 p[strlen(p) - 1] = '\0';
108 p[strlen(p) - 1] = '\0';
114 p[strlen(p) - 1] = '\0';
115 sprintf(the_prompt, "%s", p);
120 p[strlen(p) - 1] = '\0';
125 p[strlen(p) - 1] = '\0';
127 p[strlen(p) - 1] = '\0';
128 sprintf(num_teles, "(%s) ", p + 1);
129 if (!redir_fp && !pipe_fp && !exec_fd) {
137 output(code, p, auxfi, eol);
148 if (mode == C_PROMPT) {
150 (void)fclose(redir_fp);
152 } else if (pipe_fp) {
153 (void)pclose(pipe_fp);
155 } else if (exec_fd > 0) {
159 open("/dev/tty", O_RDONLY, 0);
162 if (mode == C_PROMPT)
164 printf("%s%s", num_teles, the_prompt);
165 (void)fflush(stdout);
167 fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
173 * opens redir_fp if successful
185 (void)fclose(redir_fp);
189 if (*p && ((*p == '>') || (*p == '!')))
192 while (*p && isspace(*p))
195 while (*p && !isspace(*p))
199 fprintf(stderr, "WARNING! Server redirected output to file %s\n",
203 mode = O_WRONLY | O_CREAT;
206 else if (how[1] == '!')
211 fprintf(stderr, "Null file name after redirect\n");
215 if ((fd = open(name, mode, 0600)) < 0) {
216 fprintf(stderr, "Redirect open failed\n");
219 redir_fp = fdopen(fd, "w");
225 * opens "pipe_fp" if successful
235 while (*p && isspace(*p))
238 fprintf(stderr, "WARNING! Server attempted to run: %s\n", p);
242 fprintf(stderr, "Null program name after redirect\n");
246 if ((pipe_fp = popen(p, "w")) == NULL) {
247 fprintf(stderr, "Pipe open failed\n");
254 doexecute(char *p, FILE *auxfi)
260 while (*p && isspace(*p))
264 "WARNING! Server attempted unauthorized read of file %s\n",
269 fprintf(stderr, "Null file to execute\n");
273 if ((fd = open(p, O_RDONLY, 0)) < 0) {
274 fprintf(stderr, "Can't open execute file\n");
279 /* copies 4k at a time to the socket */
280 while (termio(fd, sock, auxfi)) /*do copy */
282 /* Some platforms don't send the eof (cntl-D) at the end of
283 copying a file. If emp_client hangs at the end of an
284 execute, include the following line and notify wolfpack
285 of the platform you are using.
293 output(int code, char *buf, FILE *auxfi, int eol)
297 /* not implemented; serve doesn't send it */
302 fprintf(auxfi, "Aborted\n");
308 fprintf(auxfi, "Error; ");
313 fprintf(auxfi, "Exit: ");
322 fprintf(auxfi, "%s", buf);
326 fprintf(redir_fp, "%s", buf);
328 fprintf(pipe_fp, "%s", buf);
339 while ((c = *buf++)) {
340 if (eight_bit_clean) {
347 } else if (c & 0x80) {