]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getele.c
Update known contributors comments
[empserver] / src / lib / subs / getele.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 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.
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  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2005-2009
33  *
34  */
35
36 #include <config.h>
37
38 #include "player.h"
39 #include "prototypes.h"
40 #include "tel.h"
41
42 static int tilde_escape(char *s);
43
44 /*
45  * Read a telegram for RECIPIENT into BUF, in UTF-8.
46  * BUF must have space for MAXTELSIZE+1 characters.
47  * Return telegram length, or -1 on error.
48  */
49 int
50 getele(char *recipient, char *buf)
51 {
52     char *bp;
53     size_t len;
54     char buffer[MAXTELSIZE + 2]; /* UTF-8 */
55     char left[16];
56
57     pr("Enter telegram for %s\n", recipient);
58     pr("undo last line with ~u, print with ~p, abort with ~q, end with .\n");
59     bp = buf;
60     *bp = 0;
61     for (;;) {
62         sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
63         if (uprmptrd(left, buffer, sizeof(buffer) - 2) <= 0)
64             return -1;
65         switch (tilde_escape(buffer)) {
66         case 'q':
67             return -1;
68         case 'u':
69             if (bp == buf) {
70                 pr("No more lines to undo\n");
71                 continue;
72             }
73             for (bp -= 2; bp >= buf && *bp != '\n'; --bp) ;
74             *++bp = 0;
75             pr("Last line deleted.\n");
76             continue;
77         case 'p':
78             pr("This is what you have written so far:\n");
79             uprnf(buf);
80             continue;
81         }
82         if (buffer[0] == '.' && buffer[1] == 0)
83             break;
84         if (buffer[0] == '>')   /* forgery attempt? */
85             buffer[0] = '?';    /* foil it */
86         len = strlen(buffer);
87         if (CANT_HAPPEN(len > sizeof(buffer) - 2))
88             len = sizeof(buffer) - 2;
89         buffer[len++] = '\n';
90         buffer[len] = 0;
91         if (bp + len > buf + MAXTELSIZE)
92             pr("Too long.  Try that last line again...\n");
93         else {
94             memcpy(bp, buffer, len + 1);
95             bp += len;
96         }
97     }
98     return bp - buf;
99 }
100
101 /*
102  * If S is a `tilde escape', return its code, else 0.
103  * A tilde escape is '~' followed by the code character.
104  */
105 static int
106 tilde_escape(char *s)
107 {
108     return s[0] == '~' && s[2] == 0 ? s[1] : 0;
109 }