]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getele.c
Update copyright notice
[empserver] / src / lib / subs / getele.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  getele.c: Read a telegram from a file or stdin and send it to a country
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  *     Markus Armbruster, 2005-2009
32  *
33  */
34
35 #include <config.h>
36
37 #include "prototypes.h"
38 #include "tel.h"
39
40 static int tilde_escape(char *s);
41
42 /*
43  * Read a telegram for RECIPIENT into BUF, in UTF-8.
44  * BUF must have space for MAXTELSIZE+1 characters.
45  * Return telegram length, or -1 on error.
46  */
47 int
48 getele(char *recipient, char *buf)
49 {
50     char *bp;
51     size_t len;
52     char buffer[MAXTELSIZE + 2]; /* UTF-8 */
53     char left[16];
54
55     pr("Enter telegram for %s\n", recipient);
56     pr("undo last line with ~u, print with ~p, abort with ~q, end with .\n");
57     bp = buf;
58     *bp = 0;
59     for (;;) {
60         sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
61         if (uprmptrd(left, buffer, sizeof(buffer) - 2) <= 0)
62             return -1;
63         switch (tilde_escape(buffer)) {
64         case 'q':
65             return -1;
66         case 'u':
67             if (bp == buf) {
68                 pr("No more lines to undo\n");
69                 continue;
70             }
71             for (bp -= 2; bp >= buf && *bp != '\n'; --bp) ;
72             *++bp = 0;
73             pr("Last line deleted.\n");
74             continue;
75         case 'p':
76             pr("This is what you have written so far:\n");
77             uprnf(buf);
78             continue;
79         }
80         if (buffer[0] == '.' && buffer[1] == 0)
81             break;
82         if (buffer[0] == '>')   /* forgery attempt? */
83             buffer[0] = '?';    /* foil it */
84         len = strlen(buffer);
85         if (CANT_HAPPEN(len > sizeof(buffer) - 2))
86             len = sizeof(buffer) - 2;
87         buffer[len++] = '\n';
88         buffer[len] = 0;
89         if (bp + len > buf + MAXTELSIZE)
90             pr("Too long.  Try that last line again...\n");
91         else {
92             memcpy(bp, buffer, len + 1);
93             bp += len;
94         }
95     }
96     return bp - buf;
97 }
98
99 /*
100  * If S is a `tilde escape', return its code, else 0.
101  * A tilde escape is '~' followed by the code character.
102  */
103 static int
104 tilde_escape(char *s)
105 {
106     return s[0] == '~' && s[2] == 0 ? s[1] : 0;
107 }