1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

Replace conf.c's type arrays with a new conf_key_info[].

This array is planned to be exposed more widely, and used for more
purposes than just checking the types of Conf options. In this commit
it just takes over from the two previous smaller arrays, and adds no
extra data.
This commit is contained in:
Simon Tatham
2023-09-22 12:54:15 +01:00
parent 72bbca9ddd
commit 860bd0d5b7
6 changed files with 802 additions and 317 deletions

View File

@ -14,6 +14,7 @@ add_sources_from_current_dir(utils
chomp.c
cmdline_get_passwd_input_state_new.c
conf.c
conf_data.c
conf_dest.c
conf_launchable.c
ctrlparse.c

View File

@ -10,15 +10,6 @@
#include "tree234.h"
#include "putty.h"
/*
* Arrays which allow us to look up the subkey and value types for a
* given primary key id.
*/
#define CONF_SUBKEYTYPE_DEF(valtype, keytype, keyword) CONF_TYPE_ ## keytype,
static int subkeytypes[] = { CONFIG_OPTIONS(CONF_SUBKEYTYPE_DEF) };
#define CONF_VALUETYPE_DEF(valtype, keytype, keyword) CONF_TYPE_ ## valtype,
static int valuetypes[] = { CONFIG_OPTIONS(CONF_VALUETYPE_DEF) };
/*
* Configuration keys are primarily integers (big enum of all the
* different configurable options); some keys have string-designated
@ -80,7 +71,7 @@ static int conf_cmp(void *av, void *bv)
return -1;
else if (a->primary > b->primary)
return +1;
switch (subkeytypes[a->primary]) {
switch (conf_key_info[a->primary].subkey_type) {
case CONF_TYPE_INT:
if (a->secondary.i < b->secondary.i)
return -1;
@ -103,7 +94,7 @@ static int conf_cmp_constkey(void *av, void *bv)
return -1;
else if (a->primary > b->primary)
return +1;
switch (subkeytypes[a->primary]) {
switch (conf_key_info[a->primary].subkey_type) {
case CONF_TYPE_INT:
if (a->secondary.i < b->secondary.i)
return -1;
@ -124,7 +115,7 @@ static int conf_cmp_constkey(void *av, void *bv)
*/
static void free_key(struct key *key)
{
if (subkeytypes[key->primary] == CONF_TYPE_STR)
if (conf_key_info[key->primary].subkey_type == CONF_TYPE_STR)
sfree(key->secondary.s);
}
@ -135,7 +126,7 @@ static void free_key(struct key *key)
static void copy_key(struct key *to, struct key *from)
{
to->primary = from->primary;
switch (subkeytypes[to->primary]) {
switch (conf_key_info[to->primary].subkey_type) {
case CONF_TYPE_INT:
to->secondary.i = from->secondary.i;
break;
@ -191,7 +182,7 @@ static void copy_value(struct value *to, struct value *from, int type)
static void free_entry(struct conf_entry *entry)
{
free_key(&entry->key);
free_value(&entry->value, valuetypes[entry->key.primary]);
free_value(&entry->value, conf_key_info[entry->key.primary].value_type);
sfree(entry);
}
@ -241,7 +232,7 @@ void conf_copy_into(Conf *newconf, Conf *oldconf)
entry2 = snew(struct conf_entry);
copy_key(&entry2->key, &entry->key);
copy_value(&entry2->value, &entry->value,
valuetypes[entry->key.primary]);
conf_key_info[entry->key.primary].value_type);
add234(newconf->tree, entry2);
}
}
@ -260,8 +251,8 @@ bool conf_get_bool(Conf *conf, int primary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_BOOL);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_BOOL);
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
@ -273,8 +264,8 @@ int conf_get_int(Conf *conf, int primary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_INT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_INT);
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
@ -286,8 +277,8 @@ int conf_get_int_int(Conf *conf, int primary, int secondary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_INT);
assert(valuetypes[primary] == CONF_TYPE_INT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_INT);
assert(conf_key_info[primary].value_type == CONF_TYPE_INT);
key.primary = primary;
key.secondary.i = secondary;
entry = find234(conf->tree, &key, NULL);
@ -300,8 +291,8 @@ char *conf_get_str(Conf *conf, int primary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
@ -313,8 +304,8 @@ char *conf_get_str_str_opt(Conf *conf, int primary, const char *secondary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_STR);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_STR);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
key.primary = primary;
key.secondary.s = (char *)secondary;
entry = find234(conf->tree, &key, NULL);
@ -334,8 +325,8 @@ char *conf_get_str_strs(Conf *conf, int primary,
struct constkey key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_STR);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_STR);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
key.primary = primary;
if (subkeyin) {
key.secondary.s = subkeyin;
@ -356,8 +347,8 @@ char *conf_get_str_nthstrkey(Conf *conf, int primary, int n)
struct conf_entry *entry;
int index;
assert(subkeytypes[primary] == CONF_TYPE_STR);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_STR);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
key.primary = primary;
key.secondary.s = "";
entry = findrelpos234(conf->tree, &key, conf_cmp_constkey,
@ -375,8 +366,8 @@ Filename *conf_get_filename(Conf *conf, int primary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_FILENAME);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_FILENAME);
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
@ -388,8 +379,8 @@ FontSpec *conf_get_fontspec(Conf *conf, int primary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_FONT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_FONT);
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
@ -400,8 +391,8 @@ void conf_set_bool(Conf *conf, int primary, bool value)
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_BOOL);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_BOOL);
entry->key.primary = primary;
entry->value.u.boolval = value;
conf_insert(conf, entry);
@ -411,8 +402,8 @@ void conf_set_int(Conf *conf, int primary, int value)
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_INT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_INT);
entry->key.primary = primary;
entry->value.u.intval = value;
conf_insert(conf, entry);
@ -423,8 +414,8 @@ void conf_set_int_int(Conf *conf, int primary,
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_INT);
assert(valuetypes[primary] == CONF_TYPE_INT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_INT);
assert(conf_key_info[primary].value_type == CONF_TYPE_INT);
entry->key.primary = primary;
entry->key.secondary.i = secondary;
entry->value.u.intval = value;
@ -435,8 +426,8 @@ void conf_set_str(Conf *conf, int primary, const char *value)
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
entry->key.primary = primary;
entry->value.u.stringval = dupstr(value);
conf_insert(conf, entry);
@ -447,8 +438,8 @@ void conf_set_str_str(Conf *conf, int primary, const char *secondary,
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_STR);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_STR);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
entry->key.primary = primary;
entry->key.secondary.s = dupstr(secondary);
entry->value.u.stringval = dupstr(value);
@ -460,8 +451,8 @@ void conf_del_str_str(Conf *conf, int primary, const char *secondary)
struct key key;
struct conf_entry *entry;
assert(subkeytypes[primary] == CONF_TYPE_STR);
assert(valuetypes[primary] == CONF_TYPE_STR);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_STR);
assert(conf_key_info[primary].value_type == CONF_TYPE_STR);
key.primary = primary;
key.secondary.s = (char *)secondary;
entry = find234(conf->tree, &key, NULL);
@ -475,8 +466,8 @@ void conf_set_filename(Conf *conf, int primary, const Filename *value)
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_FILENAME);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_FILENAME);
entry->key.primary = primary;
entry->value.u.fileval = filename_copy(value);
conf_insert(conf, entry);
@ -486,8 +477,8 @@ void conf_set_fontspec(Conf *conf, int primary, const FontSpec *value)
{
struct conf_entry *entry = snew(struct conf_entry);
assert(subkeytypes[primary] == CONF_TYPE_NONE);
assert(valuetypes[primary] == CONF_TYPE_FONT);
assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
assert(conf_key_info[primary].value_type == CONF_TYPE_FONT);
entry->key.primary = primary;
entry->value.u.fontval = fontspec_copy(value);
conf_insert(conf, entry);
@ -501,7 +492,7 @@ void conf_serialise(BinarySink *bs, Conf *conf)
for (i = 0; (entry = index234(conf->tree, i)) != NULL; i++) {
put_uint32(bs, entry->key.primary);
switch (subkeytypes[entry->key.primary]) {
switch (conf_key_info[entry->key.primary].subkey_type) {
case CONF_TYPE_INT:
put_uint32(bs, entry->key.secondary.i);
break;
@ -509,7 +500,7 @@ void conf_serialise(BinarySink *bs, Conf *conf)
put_asciz(bs, entry->key.secondary.s);
break;
}
switch (valuetypes[entry->key.primary]) {
switch (conf_key_info[entry->key.primary].value_type) {
case CONF_TYPE_BOOL:
put_bool(bs, entry->value.u.boolval);
break;
@ -549,7 +540,7 @@ bool conf_deserialise(Conf *conf, BinarySource *src)
entry = snew(struct conf_entry);
entry->key.primary = primary;
switch (subkeytypes[entry->key.primary]) {
switch (conf_key_info[entry->key.primary].subkey_type) {
case CONF_TYPE_INT:
entry->key.secondary.i = toint(get_uint32(src));
break;
@ -558,7 +549,7 @@ bool conf_deserialise(Conf *conf, BinarySource *src)
break;
}
switch (valuetypes[entry->key.primary]) {
switch (conf_key_info[entry->key.primary].value_type) {
case CONF_TYPE_BOOL:
entry->value.u.boolval = get_bool(src);
break;

9
utils/conf_data.c Normal file
View File

@ -0,0 +1,9 @@
#include "putty.h"
#define CONF_OPTION(id, ...) { __VA_ARGS__ },
#define VALUE_TYPE(x) .value_type = CONF_TYPE_ ## x
#define SUBKEY_TYPE(x) .subkey_type = CONF_TYPE_ ## x
const ConfKeyInfo conf_key_info[] = {
#include "conf.h"
};