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
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 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.
28 * getele.c: Read a telegram from a file or stdin and send it to a country
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
32 * Markus Armbruster, 2005-2009
39 #include "prototypes.h"
42 static int tilde_escape(char *s);
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.
50 getele(char *recipient, char *buf)
54 char buffer[MAXTELSIZE + 2]; /* UTF-8 */
57 pr("Enter telegram for %s\n", recipient);
58 pr("undo last line with ~u, print with ~p, abort with ~q, end with .\n");
62 sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
63 if (uprmptrd(left, buffer, sizeof(buffer) - 2) <= 0)
65 switch (tilde_escape(buffer)) {
70 pr("No more lines to undo\n");
73 for (bp -= 2; bp >= buf && *bp != '\n'; --bp) ;
75 pr("Last line deleted.\n");
78 pr("This is what you have written so far:\n");
82 if (buffer[0] == '.' && buffer[1] == 0)
84 if (buffer[0] == '>') /* forgery attempt? */
85 buffer[0] = '?'; /* foil it */
87 if (CANT_HAPPEN(len > sizeof(buffer) - 2))
88 len = sizeof(buffer) - 2;
91 if (bp + len > buf + MAXTELSIZE)
92 pr("Too long. Try that last line again...\n");
94 memcpy(bp, buffer, len + 1);
102 * If S is a `tilde escape', return its code, else 0.
103 * A tilde escape is '~' followed by the code character.
106 tilde_escape(char *s)
108 return s[0] == '~' && s[2] == 0 ? s[1] : 0;