(getele): Abort telegram when uprmptrd() fails. Previously, failures
that didn't set player->aborted sent the telegram, e.g. EOF. (getele): Fix misleading size of left[]. (getele, tilde_escape): Simplify. tilde_escape() now returns the escape code instead of comparing it to second argument.
This commit is contained in:
parent
df0bbf5bd2
commit
bc8a443264
1 changed files with 34 additions and 30 deletions
|
@ -37,69 +37,73 @@
|
||||||
#include "tel.h"
|
#include "tel.h"
|
||||||
#include "prototypes.h"
|
#include "prototypes.h"
|
||||||
|
|
||||||
static int tilde_escape(s_char *s, s_char c);
|
static int tilde_escape(char *s);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read a telegram for RECIPIENT into BUF.
|
||||||
|
* BUF must have space for MAXTELSIZE+1 characters.
|
||||||
|
* Return telegram length, or -1 on error.
|
||||||
|
* Note: telegrams are message text (see doc/unicode).
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
getele(char *nation, char *buf /* buf is message text */)
|
getele(char *recipient, char *buf)
|
||||||
{
|
{
|
||||||
register char *bp;
|
char *bp;
|
||||||
register int len;
|
size_t len;
|
||||||
char buffer[MAXTELSIZE + 2]; /* buf is message text */
|
char buffer[MAXTELSIZE + 2]; /* message text */
|
||||||
char left[MAXTELSIZE + 2]; /* buf is message text */
|
char left[16];
|
||||||
|
|
||||||
pr("Enter telegram for %s\n", nation);
|
pr("Enter telegram for %s\n", recipient);
|
||||||
pr("undo last line with ~u, print with ~p, abort with ~q, end with ^D or .\n");
|
pr("undo last line with ~u, print with ~p, abort with ~q, end with ^D or .\n");
|
||||||
bp = buf;
|
bp = buf;
|
||||||
while (!player->aborted) {
|
while (!player->aborted) {
|
||||||
sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
|
sprintf(left, "%4d left: ", (int)(buf + MAXTELSIZE - bp));
|
||||||
buffer[0] = 0;
|
if (uprmptrd(left, buffer, sizeof(buffer) - 2) <= 0)
|
||||||
if (uprmptrd(left, buffer, MAXTELSIZE - 2) <= 0)
|
|
||||||
break;
|
|
||||||
if (tilde_escape(buffer, 'q'))
|
|
||||||
return -1;
|
return -1;
|
||||||
if (tilde_escape(buffer, 'u')) {
|
switch(tilde_escape(buffer)) {
|
||||||
|
case 'q':
|
||||||
|
return -1;
|
||||||
|
case 'u':
|
||||||
if (bp == buf) {
|
if (bp == buf) {
|
||||||
pr("No more lines to undo\n");
|
pr("No more lines to undo\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
for (bp -= 2; bp >= buf && *bp != '\n'; --bp) ;
|
||||||
|
*++bp = 0;
|
||||||
pr("Last line deleted.\n");
|
pr("Last line deleted.\n");
|
||||||
for (bp -= 2; bp > buf && *bp != '\n'; --bp) ;
|
|
||||||
if (bp > buf)
|
|
||||||
*(++bp) = 0;
|
|
||||||
else
|
|
||||||
bp = buf;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
case 'p':
|
||||||
if (tilde_escape(buffer, 'p')) {
|
|
||||||
pr("This is what you have written so far:\n");
|
pr("This is what you have written so far:\n");
|
||||||
uprnf(buf);
|
uprnf(buf);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (buffer[0] == '.' && buffer[1] == 0)
|
if (buffer[0] == '.' && buffer[1] == 0)
|
||||||
break;
|
break;
|
||||||
|
if (buffer[0] == '>') /* forgery attempt? */
|
||||||
|
buffer[0] = '?'; /* foil it */
|
||||||
len = strlen(buffer);
|
len = strlen(buffer);
|
||||||
|
if (CANT_HAPPEN(len > sizeof(buffer) - 2))
|
||||||
|
len = sizeof(buffer) - 2;
|
||||||
buffer[len++] = '\n';
|
buffer[len++] = '\n';
|
||||||
buffer[len] = 0;
|
buffer[len] = 0;
|
||||||
if (len + (bp - buf) > MAXTELSIZE)
|
if (bp + len > buf + MAXTELSIZE)
|
||||||
pr("Too long. Try that last line again...\n");
|
pr("Too long. Try that last line again...\n");
|
||||||
else {
|
else {
|
||||||
if (buffer[0] == '>') /* forgery attempt? */
|
memcpy(bp, buffer, len + 1);
|
||||||
buffer[0] = '?'; /* foil it */
|
|
||||||
(void)strcpy(bp, buffer);
|
|
||||||
bp += len;
|
bp += len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (player->aborted)
|
if (player->aborted)
|
||||||
return -1;
|
return -1;
|
||||||
len = bp - buf;
|
return bp - buf;
|
||||||
buf[len] = 0;
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If S is a `tilde escape', return its code, else 0.
|
||||||
|
* A tilde escape is '~' followed by the code character.
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
tilde_escape(s_char *s, s_char c)
|
tilde_escape(char *s)
|
||||||
{
|
{
|
||||||
if (s[0] == '~' && s[1] == c && s[2] == 0)
|
return s[0] == '~' && s[2] == 0 ? s[1] : 0;
|
||||||
return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue