]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getele.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / subs / getele.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *    
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "tel.h"
40 #include "prototypes.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 ^D or .\n");
59     bp = buf;
60     while (!player->aborted) {
61         sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
62         if (uprmptrd(left, buffer, sizeof(buffer) - 2) <= 0)
63             return -1;
64         switch(tilde_escape(buffer)) {
65         case 'q':
66             return -1;
67         case 'u':
68             if (bp == buf) {
69                 pr("No more lines to undo\n");
70                 continue;
71             }
72             for (bp -= 2; bp >= buf && *bp != '\n'; --bp) ;
73             *++bp = 0;
74             pr("Last line deleted.\n");
75             continue;
76         case 'p':
77             pr("This is what you have written so far:\n");
78             uprnf(buf);
79             continue;
80         }
81         if (buffer[0] == '.' && buffer[1] == 0)
82             break;
83         if (buffer[0] == '>')   /* forgery attempt? */
84             buffer[0] = '?';    /* foil it */
85         len = strlen(buffer);
86         if (CANT_HAPPEN(len > sizeof(buffer) - 2))
87             len = sizeof(buffer) - 2;
88         buffer[len++] = '\n';
89         buffer[len] = 0;
90         if (bp + len > buf + MAXTELSIZE)
91             pr("Too long.  Try that last line again...\n");
92         else {
93             memcpy(bp, buffer, len + 1);
94             bp += len;
95         }
96     }
97     if (player->aborted)
98         return -1;
99     return bp - buf;
100 }
101
102 /*
103  * If S is a `tilde escape', return its code, else 0.
104  * A tilde escape is '~' followed by the code character.
105  */
106 static int
107 tilde_escape(char *s)
108 {
109     return s[0] == '~' && s[2] == 0 ? s[1] : 0;
110 }