1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Move default_protocol and default_port into settings.c.

These global variables are only ever used by load_settings, which uses
them to vary the default protocol and port number in the absence of
any specification elsewhere. So there's no real need for them to be
universally accessible via the awkward GLOBAL mechanism: they can be
statics inside settings.c, with accessor functions that can set them.

That was the last GLOBAL in putty.h, so I've removed the definition of
the macro GLOBAL itself as well. There are still some GLOBALs in the
Windows subdirectory, though.
This commit is contained in:
Simon Tatham 2020-02-02 10:00:42 +00:00
parent e9c3f1ca6d
commit 46fc31c062
9 changed files with 53 additions and 66 deletions

View File

@ -152,6 +152,18 @@ bool cmdline_seat_verbose(Seat *seat) { return cmdline_verbose(); }
bool cmdline_lp_verbose(LogPolicy *lp) { return cmdline_verbose(); }
bool cmdline_loaded_session(void) { return loaded_session; }
static void set_protocol(Conf *conf, int protocol)
{
settings_set_default_protocol(protocol);
conf_set_int(conf, CONF_protocol, protocol);
}
static void set_port(Conf *conf, int port)
{
settings_set_default_port(port);
conf_set_int(conf, CONF_port, port);
}
int cmdline_process_param(const char *p, char *value,
int need_save, Conf *conf)
{
@ -269,9 +281,7 @@ int cmdline_process_param(const char *p, char *value,
backend_vt_from_name(prefix);
if (vt) {
default_protocol = vt->protocol;
conf_set_int(conf, CONF_protocol,
default_protocol);
set_protocol(conf, vt->protocol);
port_override = vt->default_port;
} else {
cmdline_error("unrecognised protocol prefix '%s'",
@ -399,46 +409,38 @@ int cmdline_process_param(const char *p, char *value,
RETURN(1);
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
SAVEABLE(0);
default_protocol = PROT_SSH;
default_port = 22;
conf_set_int(conf, CONF_protocol, default_protocol);
conf_set_int(conf, CONF_port, default_port);
set_protocol(conf, PROT_SSH);
set_port(conf, 22);
return 1;
}
if (!strcmp(p, "-telnet")) {
RETURN(1);
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
SAVEABLE(0);
default_protocol = PROT_TELNET;
default_port = 23;
conf_set_int(conf, CONF_protocol, default_protocol);
conf_set_int(conf, CONF_port, default_port);
set_protocol(conf, PROT_TELNET);
set_port(conf, 23);
return 1;
}
if (!strcmp(p, "-rlogin")) {
RETURN(1);
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
SAVEABLE(0);
default_protocol = PROT_RLOGIN;
default_port = 513;
conf_set_int(conf, CONF_protocol, default_protocol);
conf_set_int(conf, CONF_port, default_port);
set_protocol(conf, PROT_RLOGIN);
set_port(conf, 513);
return 1;
}
if (!strcmp(p, "-raw")) {
RETURN(1);
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
SAVEABLE(0);
default_protocol = PROT_RAW;
conf_set_int(conf, CONF_protocol, default_protocol);
set_protocol(conf, PROT_RAW);
}
if (!strcmp(p, "-serial")) {
RETURN(1);
/* Serial is not NONNETWORK in an odd sense of the word */
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
SAVEABLE(0);
default_protocol = PROT_SERIAL;
conf_set_int(conf, CONF_protocol, default_protocol);
set_protocol(conf, PROT_SERIAL);
/* The host parameter will already be loaded into CONF_host,
* so copy it across */
conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));

2
pscp.c
View File

@ -2229,7 +2229,7 @@ int psftp_main(int argc, char *argv[])
int i;
bool sanitise_stderr = true;
default_protocol = PROT_TELNET;
settings_set_default_protocol(PROT_TELNET);
sk_init();

23
putty.h
View File

@ -4,19 +4,6 @@
#include <stddef.h> /* for wchar_t */
#include <limits.h> /* for INT_MAX */
/*
* Global variables. Most modules declare these `extern', but
* window.c will do `#define PUTTY_DO_GLOBALS' before including this
* module, and so will get them properly defined.
*/
#ifndef GLOBAL
#ifdef PUTTY_DO_GLOBALS
#define GLOBAL
#else
#define GLOBAL extern
#endif
#endif
#include "defs.h"
#include "puttyps.h"
#include "network.h"
@ -578,14 +565,6 @@ extern const int be_default_protocol;
*/
extern const char *const appname;
/*
* Likewise, these two variables are set up when the application
* initialises, and inform all default-settings accesses after
* that.
*/
GLOBAL int default_protocol;
GLOBAL int default_port;
/*
* Mechanism for getting text strings such as usernames and passwords
* from the front-end.
@ -1562,6 +1541,8 @@ void load_open_settings(settings_r *sesskey, Conf *conf);
void get_sesslist(struct sesslist *, bool allocate);
bool do_defaults(const char *, Conf *);
void registry_cleanup(void);
void settings_set_default_protocol(int);
void settings_set_default_port(int);
/*
* Functions used by settings.c to provide platform-specific

View File

@ -69,6 +69,10 @@ const char *const ttymodes[] = {
"CS7", "CS8", "PARENB", "PARODD", NULL
};
static int default_protocol, default_port;
void settings_set_default_protocol(int newval) { default_protocol = newval; }
void settings_set_default_port(int newval) { default_port = newval; }
/*
* Convenience functions to access the backends[] array
* (which is only present in tools that manage settings).

View File

@ -598,8 +598,8 @@ int main(int argc, char **argv)
* Initialise port and protocol to sensible defaults. (These
* will be overridden by more or less anything.)
*/
default_protocol = PROT_SSH;
default_port = 22;
settings_set_default_protocol(PROT_SSH);
settings_set_default_port(22);
bufchain_init(&stdout_data);
bufchain_init(&stderr_data);
@ -615,8 +615,8 @@ int main(int argc, char **argv)
*/
conf = conf_new();
do_defaults(NULL, conf);
default_protocol = conf_get_int(conf, CONF_protocol);
default_port = conf_get_int(conf, CONF_port);
settings_set_default_protocol(conf_get_int(conf, CONF_protocol));
settings_set_default_port(conf_get_int(conf, CONF_port));
errors = false;
{
/*
@ -626,10 +626,10 @@ int main(int argc, char **argv)
if (p) {
const struct BackendVtable *vt = backend_vt_from_name(p);
if (vt) {
default_protocol = vt->protocol;
default_port = vt->default_port;
conf_set_int(conf, CONF_protocol, default_protocol);
conf_set_int(conf, CONF_port, default_port);
settings_set_default_protocol(vt->protocol);
settings_set_default_port(vt->default_port);
conf_set_int(conf, CONF_protocol, vt->protocol);
conf_set_int(conf, CONF_port, vt->default_port);
}
}
}

View File

@ -48,7 +48,7 @@ char *make_default_wintitle(char *hostname)
void setup(bool single)
{
default_protocol = -1;
settings_set_default_protocol(-1);
if (single)
pty_pre_init();

View File

@ -85,13 +85,13 @@ const unsigned cmdline_tooltype =
void setup(bool single)
{
sk_init();
default_protocol = be_default_protocol;
settings_set_default_protocol(be_default_protocol);
/* Find the appropriate default port. */
{
const struct BackendVtable *vt =
backend_vt_from_proto(default_protocol);
default_port = 0; /* illegal */
backend_vt_from_proto(be_default_protocol);
settings_set_default_port(0); /* illegal */
if (vt)
default_port = vt->default_port;
settings_set_default_port(vt->default_port);
}
}

View File

@ -532,14 +532,14 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
char *p;
bool special_launchable_argument = false;
default_protocol = be_default_protocol;
settings_set_default_protocol(be_default_protocol);
/* Find the appropriate default port. */
{
const struct BackendVtable *vt =
backend_vt_from_proto(default_protocol);
default_port = 0; /* illegal */
backend_vt_from_proto(be_default_protocol);
settings_set_default_port(0); /* illegal */
if (vt)
default_port = vt->default_port;
settings_set_default_port(vt->default_port);
}
conf_set_int(conf, CONF_logtype, LGTYP_NONE);

View File

@ -252,16 +252,16 @@ int main(int argc, char **argv)
* Initialise port and protocol to sensible defaults. (These
* will be overridden by more or less anything.)
*/
default_protocol = PROT_SSH;
default_port = 22;
settings_set_default_protocol(PROT_SSH);
settings_set_default_port(22);
/*
* Process the command line.
*/
conf = conf_new();
do_defaults(NULL, conf);
default_protocol = conf_get_int(conf, CONF_protocol);
default_port = conf_get_int(conf, CONF_port);
settings_set_default_protocol(conf_get_int(conf, CONF_protocol));
settings_set_default_port(conf_get_int(conf, CONF_port));
errors = false;
{
/*
@ -271,10 +271,10 @@ int main(int argc, char **argv)
if (p) {
const struct BackendVtable *vt = backend_vt_from_name(p);
if (vt) {
default_protocol = vt->protocol;
default_port = vt->default_port;
conf_set_int(conf, CONF_protocol, default_protocol);
conf_set_int(conf, CONF_port, default_port);
settings_set_default_protocol(vt->protocol);
settings_set_default_port(vt->default_port);
conf_set_int(conf, CONF_protocol, vt->protocol);
conf_set_int(conf, CONF_port, vt->default_port);
}
}
}