1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/testback.c
Simon Tatham b4e1bca2c3 Change vtable defs to use C99 designated initialisers.
This is a sweeping change applied across the whole code base by a spot
of Emacs Lisp. Now, everywhere I declare a vtable filled with function
pointers (and the occasional const data member), all the members of
the vtable structure are initialised by name using the '.fieldname =
value' syntax introduced in C99.

We were already using this syntax for a handful of things in the new
key-generation progress report system, so it's not new to the code
base as a whole.

The advantage is that now, when a vtable only declares a subset of the
available fields, I can initialise the rest to NULL or zero just by
leaving them out. This is most dramatic in a couple of the outlying
vtables in things like psocks (which has a ConnectionLayerVtable
containing only one non-NULL method), but less dramatically, it means
that the new 'flags' field in BackendVtable can be completely left out
of every backend definition except for the SUPDUP one which defines it
to a nonzero value. Similarly, the test_for_upstream method only used
by SSH doesn't have to be mentioned in the rest of the backends;
network Plugs for listening sockets don't have to explicitly null out
'receive' and 'sent', and vice versa for 'accepting', and so on.

While I'm at it, I've normalised the declarations so they don't use
the unnecessarily verbose 'struct' keyword. Also a handful of them
weren't const; now they are.
2020-03-10 21:06:29 +00:00

217 lines
5.7 KiB
C

/*
* Copyright (c) 1999 Simon Tatham
* Copyright (c) 1999 Ben Harris
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* PuTTY test backends */
#include <stdio.h>
#include <stdlib.h>
#include "putty.h"
static const char *null_init(const BackendVtable *, Seat *, Backend **,
LogContext *, Conf *, const char *, int, char **,
bool, bool);
static const char *loop_init(const BackendVtable *, Seat *, Backend **,
LogContext *, Conf *, const char *, int, char **,
bool, bool);
static void null_free(Backend *);
static void loop_free(Backend *);
static void null_reconfig(Backend *, Conf *);
static size_t null_send(Backend *, const char *, size_t);
static size_t loop_send(Backend *, const char *, size_t);
static size_t null_sendbuffer(Backend *);
static void null_size(Backend *, int, int);
static void null_special(Backend *, SessionSpecialCode, int);
static const SessionSpecial *null_get_specials(Backend *);
static int null_connected(Backend *);
static int null_exitcode(Backend *);
static int null_sendok(Backend *);
static int null_ldisc(Backend *, int);
static void null_provide_ldisc(Backend *, Ldisc *);
static void null_unthrottle(Backend *, size_t);
static int null_cfg_info(Backend *);
const BackendVtable null_backend = {
.init = null_init,
.free = null_free,
.reconfig = null_reconfig,
.send = null_send,
.sendbuffer = null_sendbuffer,
.size = null_size,
.special = null_special,
.get_specials = null_get_specials,
.connected = null_connected,
.exitcode = null_exitcode,
.sendok = null_sendok,
.ldisc_option_state = null_ldisc,
.provide_ldisc = null_provide_ldisc,
.unthrottle = null_unthrottle,
.cfg_info = null_cfg_info,
.id = "null",
.displayname = "null",
.protocol = -1,
.default_port = 0,
};
const BackendVtable loop_backend = {
.init = loop_init,
.free = loop_free,
.reconfig = null_reconfig,
.send = loop_send,
.sendbuffer = null_sendbuffer,
.size = null_size,
.special = null_special,
.get_specials = null_get_specials,
.connected = null_connected,
.exitcode = null_exitcode,
.sendok = null_sendok,
.ldisc_option_state = null_ldisc,
.provide_ldisc = null_provide_ldisc,
.unthrottle = null_unthrottle,
.cfg_info = null_cfg_info,
.id = "loop",
.displayname = "loop",
.protocol = -1,
.default_port = 0,
};
struct loop_state {
Seat *seat;
Backend backend;
};
static const char *null_init(const BackendVtable *vt, Seat *seat,
Backend **backend_handle, LogContext *logctx,
Conf *conf, const char *host, int port,
char **realhost, bool nodelay, bool keepalive) {
/* No local authentication phase in this protocol */
seat_set_trust_status(seat, false);
*backend_handle = NULL;
return NULL;
}
static const char *loop_init(const BackendVtable *vt, Seat *seat,
Backend **backend_handle, LogContext *logctx,
Conf *conf, const char *host, int port,
char **realhost, bool nodelay, bool keepalive) {
struct loop_state *st = snew(struct loop_state);
/* No local authentication phase in this protocol */
seat_set_trust_status(seat, false);
st->seat = seat;
*backend_handle = &st->backend;
return NULL;
}
static void null_free(Backend *be)
{
}
static void loop_free(Backend *be)
{
struct loop_state *st = container_of(be, struct loop_state, backend);
sfree(st);
}
static void null_reconfig(Backend *be, Conf *conf) {
}
static size_t null_send(Backend *be, const char *buf, size_t len) {
return 0;
}
static size_t loop_send(Backend *be, const char *buf, size_t len) {
struct loop_state *st = container_of(be, struct loop_state, backend);
return seat_output(st->seat, 0, buf, len);
}
static size_t null_sendbuffer(Backend *be) {
return 0;
}
static void null_size(Backend *be, int width, int height) {
}
static void null_special(Backend *be, SessionSpecialCode code, int arg) {
}
static const SessionSpecial *null_get_specials (Backend *be) {
return NULL;
}
static int null_connected(Backend *be) {
return 0;
}
static int null_exitcode(Backend *be) {
return 0;
}
static int null_sendok(Backend *be) {
return 1;
}
static void null_unthrottle(Backend *be, size_t backlog) {
}
static int null_ldisc(Backend *be, int option) {
return 0;
}
static void null_provide_ldisc (Backend *be, Ldisc *ldisc) {
}
static int null_cfg_info(Backend *be)
{
return 0;
}
/*
* Emacs magic:
* Local Variables:
* c-file-style: "simon"
* End:
*/