mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 09:27:59 +00:00
3214563d8e
My normal habit these days, in new code, is to treat int and bool as _almost_ completely separate types. I'm still willing to use C's implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine, no need to spell it out as blob.len != 0), but generally, if a variable is going to be conceptually a boolean, I like to declare it bool and assign to it using 'true' or 'false' rather than 0 or 1. PuTTY is an exception, because it predates the C99 bool, and I've stuck to its existing coding style even when adding new code to it. But it's been annoying me more and more, so now that I've decided C99 bool is an acceptable thing to require from our toolchain in the first place, here's a quite thorough trawl through the source doing 'boolification'. Many variables and function parameters are now typed as bool rather than int; many assignments of 0 or 1 to those variables are now spelled 'true' or 'false'. I managed this thorough conversion with the help of a custom clang plugin that I wrote to trawl the AST and apply heuristics to point out where things might want changing. So I've even managed to do a decent job on parts of the code I haven't looked at in years! To make the plugin's work easier, I pushed platform front ends generally in the direction of using standard 'bool' in preference to platform-specific boolean types like Windows BOOL or GTK's gboolean; I've left the platform booleans in places they _have_ to be for the platform APIs to work right, but variables only used by my own code have been converted wherever I found them. In a few places there are int values that look very like booleans in _most_ of the places they're used, but have a rarely-used third value, or a distinction between different nonzero values that most users don't care about. In these cases, I've _removed_ uses of 'true' and 'false' for the return values, to emphasise that there's something more subtle going on than a simple boolean answer: - the 'multisel' field in dialog.h's list box structure, for which the GTK front end in particular recognises a difference between 1 and 2 but nearly everything else treats as boolean - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you something about the specific location of the urgent pointer, but most clients only care about 0 vs 'something nonzero' - the return value of wc_match, where -1 indicates a syntax error in the wildcard. - the return values from SSH-1 RSA-key loading functions, which use -1 for 'wrong passphrase' and 0 for all other failures (so any caller which already knows it's not loading an _encrypted private_ key can treat them as boolean) - term->esc_query, and the 'query' parameter in toggle_mode in terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h, but can also hold -1 for some other intervening character that we don't support. In a few places there's an integer that I haven't turned into a bool even though it really _can_ only take values 0 or 1 (and, as above, tried to make the call sites consistent in not calling those values true and false), on the grounds that I thought it would make it more confusing to imply that the 0 value was in some sense 'negative' or bad and the 1 positive or good: - the return value of plug_accepting uses the POSIXish convention of 0=success and nonzero=error; I think if I made it bool then I'd also want to reverse its sense, and that's a job for a separate piece of work. - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1 represent the default and alternate screens. There's no obvious reason why one of those should be considered 'true' or 'positive' or 'success' - they're just indices - so I've left it as int. ssh_scp_recv had particularly confusing semantics for its previous int return value: its call sites used '<= 0' to check for error, but it never actually returned a negative number, just 0 or 1. Now the function and its call sites agree that it's a bool. In a couple of places I've renamed variables called 'ret', because I don't like that name any more - it's unclear whether it means the return value (in preparation) for the _containing_ function or the return value received from a subroutine call, and occasionally I've accidentally used the same variable for both and introduced a bug. So where one of those got in my way, I've renamed it to 'toret' or 'retd' (the latter short for 'returned') in line with my usual modern practice, but I haven't done a thorough job of finding all of them. Finally, one amusing side effect of doing this is that I've had to separate quite a few chained assignments. It used to be perfectly fine to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a the 'true' defined by stdbool.h, that idiom provokes a warning from gcc: 'suggest parentheses around assignment used as truth value'!
350 lines
10 KiB
C
350 lines
10 KiB
C
/*
|
|
* ldisc.c: PuTTY line discipline. Sits between the input coming
|
|
* from keypresses in the window, and the output channel leading to
|
|
* the back end. Implements echo and/or local line editing,
|
|
* depending on what's currently configured.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <assert.h>
|
|
|
|
#include "putty.h"
|
|
#include "terminal.h"
|
|
#include "ldisc.h"
|
|
|
|
#define ECHOING (ldisc->localecho == FORCE_ON || \
|
|
(ldisc->localecho == AUTO && \
|
|
(backend_ldisc_option_state(ldisc->backend, LD_ECHO) || \
|
|
term_ldisc(ldisc->term, LD_ECHO))))
|
|
#define EDITING (ldisc->localedit == FORCE_ON || \
|
|
(ldisc->localedit == AUTO && \
|
|
(backend_ldisc_option_state(ldisc->backend, LD_EDIT) || \
|
|
term_ldisc(ldisc->term, LD_EDIT))))
|
|
|
|
static void c_write(Ldisc *ldisc, const void *buf, int len)
|
|
{
|
|
seat_stdout(ldisc->seat, buf, len);
|
|
}
|
|
|
|
static int plen(Ldisc *ldisc, unsigned char c)
|
|
{
|
|
if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
|
|
return 1;
|
|
else if (c < 128)
|
|
return 2; /* ^x for some x */
|
|
else if (in_utf(ldisc->term) && c >= 0xC0)
|
|
return 1; /* UTF-8 introducer character
|
|
* (FIXME: combining / wide chars) */
|
|
else if (in_utf(ldisc->term) && c >= 0x80 && c < 0xC0)
|
|
return 0; /* UTF-8 followup character */
|
|
else
|
|
return 4; /* <XY> hex representation */
|
|
}
|
|
|
|
static void pwrite(Ldisc *ldisc, unsigned char c)
|
|
{
|
|
if ((c >= 32 && c <= 126) ||
|
|
(!in_utf(ldisc->term) && c >= 0xA0) ||
|
|
(in_utf(ldisc->term) && c >= 0x80)) {
|
|
c_write(ldisc, &c, 1);
|
|
} else if (c < 128) {
|
|
char cc[2];
|
|
cc[1] = (c == 127 ? '?' : c + 0x40);
|
|
cc[0] = '^';
|
|
c_write(ldisc, cc, 2);
|
|
} else {
|
|
char cc[5];
|
|
sprintf(cc, "<%02X>", c);
|
|
c_write(ldisc, cc, 4);
|
|
}
|
|
}
|
|
|
|
static bool char_start(Ldisc *ldisc, unsigned char c)
|
|
{
|
|
if (in_utf(ldisc->term))
|
|
return (c < 0x80 || c >= 0xC0);
|
|
else
|
|
return true;
|
|
}
|
|
|
|
static void bsb(Ldisc *ldisc, int n)
|
|
{
|
|
while (n--)
|
|
c_write(ldisc, "\010 \010", 3);
|
|
}
|
|
|
|
#define CTRL(x) (x^'@')
|
|
#define KCTRL(x) ((x^'@') | 0x100)
|
|
|
|
Ldisc *ldisc_create(Conf *conf, Terminal *term, Backend *backend, Seat *seat)
|
|
{
|
|
Ldisc *ldisc = snew(Ldisc);
|
|
|
|
ldisc->buf = NULL;
|
|
ldisc->buflen = 0;
|
|
ldisc->bufsiz = 0;
|
|
ldisc->quotenext = false;
|
|
|
|
ldisc->backend = backend;
|
|
ldisc->term = term;
|
|
ldisc->seat = seat;
|
|
|
|
ldisc_configure(ldisc, conf);
|
|
|
|
/* Link ourselves into the backend and the terminal */
|
|
if (term)
|
|
term->ldisc = ldisc;
|
|
if (backend)
|
|
backend_provide_ldisc(backend, ldisc);
|
|
|
|
return ldisc;
|
|
}
|
|
|
|
void ldisc_configure(Ldisc *ldisc, Conf *conf)
|
|
{
|
|
ldisc->telnet_keyboard = conf_get_bool(conf, CONF_telnet_keyboard);
|
|
ldisc->telnet_newline = conf_get_bool(conf, CONF_telnet_newline);
|
|
ldisc->protocol = conf_get_int(conf, CONF_protocol);
|
|
ldisc->localecho = conf_get_int(conf, CONF_localecho);
|
|
ldisc->localedit = conf_get_int(conf, CONF_localedit);
|
|
}
|
|
|
|
void ldisc_free(Ldisc *ldisc)
|
|
{
|
|
if (ldisc->term)
|
|
ldisc->term->ldisc = NULL;
|
|
if (ldisc->backend)
|
|
backend_provide_ldisc(ldisc->backend, NULL);
|
|
if (ldisc->buf)
|
|
sfree(ldisc->buf);
|
|
sfree(ldisc);
|
|
}
|
|
|
|
void ldisc_echoedit_update(Ldisc *ldisc)
|
|
{
|
|
seat_echoedit_update(ldisc->seat, ECHOING, EDITING);
|
|
}
|
|
|
|
void ldisc_send(Ldisc *ldisc, const void *vbuf, int len, bool interactive)
|
|
{
|
|
const char *buf = (const char *)vbuf;
|
|
int keyflag = 0;
|
|
|
|
assert(ldisc->term);
|
|
assert(len);
|
|
|
|
if (interactive) {
|
|
/*
|
|
* Interrupt a paste from the clipboard, if one was in
|
|
* progress when the user pressed a key. This is easier than
|
|
* buffering the current piece of data and saving it until the
|
|
* terminal has finished pasting, and has the potential side
|
|
* benefit of permitting a user to cancel an accidental huge
|
|
* paste.
|
|
*/
|
|
term_nopaste(ldisc->term);
|
|
}
|
|
|
|
/*
|
|
* Less than zero means null terminated special string.
|
|
*/
|
|
if (len < 0) {
|
|
len = strlen(buf);
|
|
keyflag = KCTRL('@');
|
|
}
|
|
/*
|
|
* Either perform local editing, or just send characters.
|
|
*/
|
|
if (EDITING) {
|
|
while (len--) {
|
|
int c;
|
|
c = (unsigned char)(*buf++) + keyflag;
|
|
if (!interactive && c == '\r')
|
|
c += KCTRL('@');
|
|
switch (ldisc->quotenext ? ' ' : c) {
|
|
/*
|
|
* ^h/^?: delete, and output BSBs, to return to
|
|
* last character boundary (in UTF-8 mode this may
|
|
* be more than one byte)
|
|
* ^w: delete, and output BSBs, to return to last
|
|
* space/nonspace boundary
|
|
* ^u: delete, and output BSBs, to return to BOL
|
|
* ^c: Do a ^u then send a telnet IP
|
|
* ^z: Do a ^u then send a telnet SUSP
|
|
* ^\: Do a ^u then send a telnet ABORT
|
|
* ^r: echo "^R\n" and redraw line
|
|
* ^v: quote next char
|
|
* ^d: if at BOL, end of file and close connection,
|
|
* else send line and reset to BOL
|
|
* ^m: send line-plus-\r\n and reset to BOL
|
|
*/
|
|
case KCTRL('H'):
|
|
case KCTRL('?'): /* backspace/delete */
|
|
if (ldisc->buflen > 0) {
|
|
do {
|
|
if (ECHOING)
|
|
bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
|
|
ldisc->buflen--;
|
|
} while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
|
|
}
|
|
break;
|
|
case CTRL('W'): /* delete word */
|
|
while (ldisc->buflen > 0) {
|
|
if (ECHOING)
|
|
bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
|
|
ldisc->buflen--;
|
|
if (ldisc->buflen > 0 &&
|
|
isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
|
|
!isspace((unsigned char)ldisc->buf[ldisc->buflen]))
|
|
break;
|
|
}
|
|
break;
|
|
case CTRL('U'): /* delete line */
|
|
case CTRL('C'): /* Send IP */
|
|
case CTRL('\\'): /* Quit */
|
|
case CTRL('Z'): /* Suspend */
|
|
while (ldisc->buflen > 0) {
|
|
if (ECHOING)
|
|
bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
|
|
ldisc->buflen--;
|
|
}
|
|
backend_special(ldisc->backend, SS_EL, 0);
|
|
/*
|
|
* We don't send IP, SUSP or ABORT if the user has
|
|
* configured telnet specials off! This breaks
|
|
* talkers otherwise.
|
|
*/
|
|
if (!ldisc->telnet_keyboard)
|
|
goto default_case;
|
|
if (c == CTRL('C'))
|
|
backend_special(ldisc->backend, SS_IP, 0);
|
|
if (c == CTRL('Z'))
|
|
backend_special(ldisc->backend, SS_SUSP, 0);
|
|
if (c == CTRL('\\'))
|
|
backend_special(ldisc->backend, SS_ABORT, 0);
|
|
break;
|
|
case CTRL('R'): /* redraw line */
|
|
if (ECHOING) {
|
|
int i;
|
|
c_write(ldisc, "^R\r\n", 4);
|
|
for (i = 0; i < ldisc->buflen; i++)
|
|
pwrite(ldisc, ldisc->buf[i]);
|
|
}
|
|
break;
|
|
case CTRL('V'): /* quote next char */
|
|
ldisc->quotenext = true;
|
|
break;
|
|
case CTRL('D'): /* logout or send */
|
|
if (ldisc->buflen == 0) {
|
|
backend_special(ldisc->backend, SS_EOF, 0);
|
|
} else {
|
|
backend_send(ldisc->backend, ldisc->buf, ldisc->buflen);
|
|
ldisc->buflen = 0;
|
|
}
|
|
break;
|
|
/*
|
|
* This particularly hideous bit of code from RDB
|
|
* allows ordinary ^M^J to do the same thing as
|
|
* magic-^M when in Raw protocol. The line `case
|
|
* KCTRL('M'):' is _inside_ the if block. Thus:
|
|
*
|
|
* - receiving regular ^M goes straight to the
|
|
* default clause and inserts as a literal ^M.
|
|
* - receiving regular ^J _not_ directly after a
|
|
* literal ^M (or not in Raw protocol) fails the
|
|
* if condition, leaps to the bottom of the if,
|
|
* and falls through into the default clause
|
|
* again.
|
|
* - receiving regular ^J just after a literal ^M
|
|
* in Raw protocol passes the if condition,
|
|
* deletes the literal ^M, and falls through
|
|
* into the magic-^M code
|
|
* - receiving a magic-^M empties the line buffer,
|
|
* signals end-of-line in one of the various
|
|
* entertaining ways, and _doesn't_ fall out of
|
|
* the bottom of the if and through to the
|
|
* default clause because of the break.
|
|
*/
|
|
case CTRL('J'):
|
|
if (ldisc->protocol == PROT_RAW &&
|
|
ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
|
|
if (ECHOING)
|
|
bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
|
|
ldisc->buflen--;
|
|
/* FALLTHROUGH */
|
|
case KCTRL('M'): /* send with newline */
|
|
if (ldisc->buflen > 0)
|
|
backend_send(ldisc->backend,
|
|
ldisc->buf, ldisc->buflen);
|
|
if (ldisc->protocol == PROT_RAW)
|
|
backend_send(ldisc->backend, "\r\n", 2);
|
|
else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
|
|
backend_special(ldisc->backend, SS_EOL, 0);
|
|
else
|
|
backend_send(ldisc->backend, "\r", 1);
|
|
if (ECHOING)
|
|
c_write(ldisc, "\r\n", 2);
|
|
ldisc->buflen = 0;
|
|
break;
|
|
}
|
|
/* FALLTHROUGH */
|
|
default: /* get to this label from ^V handler */
|
|
default_case:
|
|
if (ldisc->buflen >= ldisc->bufsiz) {
|
|
ldisc->bufsiz = ldisc->buflen + 256;
|
|
ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
|
|
}
|
|
ldisc->buf[ldisc->buflen++] = c;
|
|
if (ECHOING)
|
|
pwrite(ldisc, (unsigned char) c);
|
|
ldisc->quotenext = false;
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (ldisc->buflen != 0) {
|
|
backend_send(ldisc->backend, ldisc->buf, ldisc->buflen);
|
|
while (ldisc->buflen > 0) {
|
|
bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
|
|
ldisc->buflen--;
|
|
}
|
|
}
|
|
if (len > 0) {
|
|
if (ECHOING)
|
|
c_write(ldisc, buf, len);
|
|
if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) {
|
|
switch (buf[0]) {
|
|
case CTRL('M'):
|
|
if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
|
|
backend_special(ldisc->backend, SS_EOL, 0);
|
|
else
|
|
backend_send(ldisc->backend, "\r", 1);
|
|
break;
|
|
case CTRL('?'):
|
|
case CTRL('H'):
|
|
if (ldisc->telnet_keyboard) {
|
|
backend_special(ldisc->backend, SS_EC, 0);
|
|
break;
|
|
}
|
|
case CTRL('C'):
|
|
if (ldisc->telnet_keyboard) {
|
|
backend_special(ldisc->backend, SS_IP, 0);
|
|
break;
|
|
}
|
|
case CTRL('Z'):
|
|
if (ldisc->telnet_keyboard) {
|
|
backend_special(ldisc->backend, SS_SUSP, 0);
|
|
break;
|
|
}
|
|
|
|
default:
|
|
backend_send(ldisc->backend, buf, len);
|
|
break;
|
|
}
|
|
} else
|
|
backend_send(ldisc->backend, buf, len);
|
|
}
|
|
}
|
|
}
|