]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getele.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / getele.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  getele.c: Read a telegram from a file or stdin and send it to a country
29  * 
30  *  Known contributors to this file:
31  *    
32  */
33
34 #include <ctype.h>
35 #include "misc.h"
36 #include "player.h"
37 #include "tel.h"
38 #include "prototypes.h"
39
40 static int tilde_escape(s_char *s, s_char c);
41
42 int
43 getele(s_char *nation, s_char *buf)
44 {
45         register s_char *bp;
46         register int len;
47         register int c;
48         s_char  buffer[MAXTELSIZE + 2];
49         s_char  left[MAXTELSIZE + 2];
50
51         pr("Enter telegram for %s\n", nation);
52         pr("undo last line with ~u, print with ~p, abort with ~q, end with ^D or .\n");
53         bp = buf;
54         while (!player->aborted) {
55                 sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
56                 buffer[0] = 0;
57                 if (prmptrd(left, buffer, MAXTELSIZE-2) <= 0)
58                         break;
59                 if (tilde_escape(buffer, 'q'))
60                         return -1;
61                 if (tilde_escape(buffer, 'u')) {
62                         if (bp == buf) {
63                                 pr("No more lines to undo\n");
64                                 continue;
65                         }
66                         pr("Last line deleted.\n");
67                         for (bp -= 2; bp > buf && *bp != '\n'; --bp);
68                         if (bp > buf)
69                                 *(++bp) = 0;
70                         else
71                                 bp = buf;
72                         continue;
73                 }
74                 if (tilde_escape(buffer, 'p')) {
75                         pr("This is what you have written so far:\n");
76                         pr("%s", buf);
77                         continue;
78                 }
79                 if (buffer[0] == '.' && ((buffer[1] == 0) ||
80                         (buffer[1] == '\n') || (buffer[1] == '\r')))
81                         break;
82                 len = strlen(buffer);
83                 buffer[len++] = '\n';
84                 buffer[len] = 0;
85                 if (len + (bp - buf) > MAXTELSIZE)
86                         pr("Too long.  Try that last line again...\n");
87                 else {
88                         if (buffer[0] == '>')           /* forgery attempt? */
89                                 buffer[0] = '?';        /* foil it */
90                         (void) strcpy(bp, buffer);
91                         bp += len;
92                 }
93         }
94         if (player->aborted)
95                 return -1;
96         len = bp - buf;
97         buf[len] = 0;
98         /* Get rid of non-ASCII and control characters.  */
99         for (bp = buf; 0 != (c = *bp); bp++) {
100                 if (isascii(c) && (isprint(c) || isspace(c)))
101                         continue;
102                 *bp = '?';
103         }
104         return len;
105 }
106
107 static int
108 tilde_escape(s_char *s, s_char c)
109 {
110         if (s[0] == '~' && s[1] == c && 
111             ((s[2] == 0) || (s[2] == '\n') || (s[2] == '\r')))
112                 return 1;
113         return 0;
114 }
115