diff --git a/putty.h b/putty.h index 6f118f7c..f8e296d0 100644 --- a/putty.h +++ b/putty.h @@ -2053,6 +2053,19 @@ NORETURN void cleanup_exit(int); enum config_primary_key { CONFIG_OPTIONS(CONF_ENUM_DEF) N_CONFIG_OPTIONS }; #undef CONF_ENUM_DEF +/* Types that appear in Conf keys and values. CONF_TYPE_NONE is used + * as the subkey type for options that don't have subkeys, and is also + * available as a placeholder value for other kinds of 'no type found' + * error. */ +enum { + CONF_TYPE_NONE, + CONF_TYPE_BOOL, + CONF_TYPE_INT, + CONF_TYPE_STR, + CONF_TYPE_FILENAME, + CONF_TYPE_FONT, +}; + /* Functions handling configuration structures. */ Conf *conf_new(void); /* create an empty configuration */ void conf_free(Conf *conf); diff --git a/utils/conf.c b/utils/conf.c index 2f9093b0..12c73238 100644 --- a/utils/conf.c +++ b/utils/conf.c @@ -10,20 +10,13 @@ #include "tree234.h" #include "putty.h" -/* - * Enumeration of types used in keys and values. - */ -typedef enum { - TYPE_NONE, TYPE_BOOL, TYPE_INT, TYPE_STR, TYPE_FILENAME, TYPE_FONT -} Type; - /* * 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) TYPE_ ## keytype, +#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) TYPE_ ## valtype, +#define CONF_VALUETYPE_DEF(valtype, keytype, keyword) CONF_TYPE_ ## valtype, static int valuetypes[] = { CONFIG_OPTIONS(CONF_VALUETYPE_DEF) }; /* @@ -88,13 +81,13 @@ static int conf_cmp(void *av, void *bv) else if (a->primary > b->primary) return +1; switch (subkeytypes[a->primary]) { - case TYPE_INT: + case CONF_TYPE_INT: if (a->secondary.i < b->secondary.i) return -1; else if (a->secondary.i > b->secondary.i) return +1; return 0; - case TYPE_STR: + case CONF_TYPE_STR: return strcmp(a->secondary.s, b->secondary.s); default: return 0; @@ -111,13 +104,13 @@ static int conf_cmp_constkey(void *av, void *bv) else if (a->primary > b->primary) return +1; switch (subkeytypes[a->primary]) { - case TYPE_INT: + case CONF_TYPE_INT: if (a->secondary.i < b->secondary.i) return -1; else if (a->secondary.i > b->secondary.i) return +1; return 0; - case TYPE_STR: + case CONF_TYPE_STR: return strcmp(a->secondary.s, b->secondary.s); default: return 0; @@ -131,7 +124,7 @@ static int conf_cmp_constkey(void *av, void *bv) */ static void free_key(struct key *key) { - if (subkeytypes[key->primary] == TYPE_STR) + if (subkeytypes[key->primary] == CONF_TYPE_STR) sfree(key->secondary.s); } @@ -143,10 +136,10 @@ static void copy_key(struct key *to, struct key *from) { to->primary = from->primary; switch (subkeytypes[to->primary]) { - case TYPE_INT: + case CONF_TYPE_INT: to->secondary.i = from->secondary.i; break; - case TYPE_STR: + case CONF_TYPE_STR: to->secondary.s = dupstr(from->secondary.s); break; } @@ -159,11 +152,11 @@ static void copy_key(struct key *to, struct key *from) */ static void free_value(struct value *val, int type) { - if (type == TYPE_STR) + if (type == CONF_TYPE_STR) sfree(val->u.stringval); - else if (type == TYPE_FILENAME) + else if (type == CONF_TYPE_FILENAME) filename_free(val->u.fileval); - else if (type == TYPE_FONT) + else if (type == CONF_TYPE_FONT) fontspec_free(val->u.fontval); } @@ -174,19 +167,19 @@ static void free_value(struct value *val, int type) static void copy_value(struct value *to, struct value *from, int type) { switch (type) { - case TYPE_BOOL: + case CONF_TYPE_BOOL: to->u.boolval = from->u.boolval; break; - case TYPE_INT: + case CONF_TYPE_INT: to->u.intval = from->u.intval; break; - case TYPE_STR: + case CONF_TYPE_STR: to->u.stringval = dupstr(from->u.stringval); break; - case TYPE_FILENAME: + case CONF_TYPE_FILENAME: to->u.fileval = filename_copy(from->u.fileval); break; - case TYPE_FONT: + case CONF_TYPE_FONT: to->u.fontval = fontspec_copy(from->u.fontval); break; } @@ -267,8 +260,8 @@ bool conf_get_bool(Conf *conf, int primary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_BOOL); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_BOOL); key.primary = primary; entry = find234(conf->tree, &key, NULL); assert(entry); @@ -280,8 +273,8 @@ int conf_get_int(Conf *conf, int primary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_INT); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_INT); key.primary = primary; entry = find234(conf->tree, &key, NULL); assert(entry); @@ -293,8 +286,8 @@ int conf_get_int_int(Conf *conf, int primary, int secondary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_INT); - assert(valuetypes[primary] == TYPE_INT); + assert(subkeytypes[primary] == CONF_TYPE_INT); + assert(valuetypes[primary] == CONF_TYPE_INT); key.primary = primary; key.secondary.i = secondary; entry = find234(conf->tree, &key, NULL); @@ -307,8 +300,8 @@ char *conf_get_str(Conf *conf, int primary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_STR); key.primary = primary; entry = find234(conf->tree, &key, NULL); assert(entry); @@ -320,8 +313,8 @@ char *conf_get_str_str_opt(Conf *conf, int primary, const char *secondary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_STR); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_STR); + assert(valuetypes[primary] == CONF_TYPE_STR); key.primary = primary; key.secondary.s = (char *)secondary; entry = find234(conf->tree, &key, NULL); @@ -341,8 +334,8 @@ char *conf_get_str_strs(Conf *conf, int primary, struct constkey key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_STR); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_STR); + assert(valuetypes[primary] == CONF_TYPE_STR); key.primary = primary; if (subkeyin) { key.secondary.s = subkeyin; @@ -363,8 +356,8 @@ char *conf_get_str_nthstrkey(Conf *conf, int primary, int n) struct conf_entry *entry; int index; - assert(subkeytypes[primary] == TYPE_STR); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_STR); + assert(valuetypes[primary] == CONF_TYPE_STR); key.primary = primary; key.secondary.s = ""; entry = findrelpos234(conf->tree, &key, conf_cmp_constkey, @@ -382,8 +375,8 @@ Filename *conf_get_filename(Conf *conf, int primary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_FILENAME); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_FILENAME); key.primary = primary; entry = find234(conf->tree, &key, NULL); assert(entry); @@ -395,8 +388,8 @@ FontSpec *conf_get_fontspec(Conf *conf, int primary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_FONT); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_FONT); key.primary = primary; entry = find234(conf->tree, &key, NULL); assert(entry); @@ -407,8 +400,8 @@ void conf_set_bool(Conf *conf, int primary, bool value) { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_BOOL); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_BOOL); entry->key.primary = primary; entry->value.u.boolval = value; conf_insert(conf, entry); @@ -418,8 +411,8 @@ void conf_set_int(Conf *conf, int primary, int value) { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_INT); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_INT); entry->key.primary = primary; entry->value.u.intval = value; conf_insert(conf, entry); @@ -430,8 +423,8 @@ void conf_set_int_int(Conf *conf, int primary, { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_INT); - assert(valuetypes[primary] == TYPE_INT); + assert(subkeytypes[primary] == CONF_TYPE_INT); + assert(valuetypes[primary] == CONF_TYPE_INT); entry->key.primary = primary; entry->key.secondary.i = secondary; entry->value.u.intval = value; @@ -442,8 +435,8 @@ void conf_set_str(Conf *conf, int primary, const char *value) { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_STR); entry->key.primary = primary; entry->value.u.stringval = dupstr(value); conf_insert(conf, entry); @@ -454,8 +447,8 @@ void conf_set_str_str(Conf *conf, int primary, const char *secondary, { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_STR); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_STR); + assert(valuetypes[primary] == CONF_TYPE_STR); entry->key.primary = primary; entry->key.secondary.s = dupstr(secondary); entry->value.u.stringval = dupstr(value); @@ -467,8 +460,8 @@ void conf_del_str_str(Conf *conf, int primary, const char *secondary) struct key key; struct conf_entry *entry; - assert(subkeytypes[primary] == TYPE_STR); - assert(valuetypes[primary] == TYPE_STR); + assert(subkeytypes[primary] == CONF_TYPE_STR); + assert(valuetypes[primary] == CONF_TYPE_STR); key.primary = primary; key.secondary.s = (char *)secondary; entry = find234(conf->tree, &key, NULL); @@ -482,8 +475,8 @@ void conf_set_filename(Conf *conf, int primary, const Filename *value) { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_FILENAME); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_FILENAME); entry->key.primary = primary; entry->value.u.fileval = filename_copy(value); conf_insert(conf, entry); @@ -493,8 +486,8 @@ void conf_set_fontspec(Conf *conf, int primary, const FontSpec *value) { struct conf_entry *entry = snew(struct conf_entry); - assert(subkeytypes[primary] == TYPE_NONE); - assert(valuetypes[primary] == TYPE_FONT); + assert(subkeytypes[primary] == CONF_TYPE_NONE); + assert(valuetypes[primary] == CONF_TYPE_FONT); entry->key.primary = primary; entry->value.u.fontval = fontspec_copy(value); conf_insert(conf, entry); @@ -509,27 +502,27 @@ void conf_serialise(BinarySink *bs, Conf *conf) put_uint32(bs, entry->key.primary); switch (subkeytypes[entry->key.primary]) { - case TYPE_INT: + case CONF_TYPE_INT: put_uint32(bs, entry->key.secondary.i); break; - case TYPE_STR: + case CONF_TYPE_STR: put_asciz(bs, entry->key.secondary.s); break; } switch (valuetypes[entry->key.primary]) { - case TYPE_BOOL: + case CONF_TYPE_BOOL: put_bool(bs, entry->value.u.boolval); break; - case TYPE_INT: + case CONF_TYPE_INT: put_uint32(bs, entry->value.u.intval); break; - case TYPE_STR: + case CONF_TYPE_STR: put_asciz(bs, entry->value.u.stringval); break; - case TYPE_FILENAME: + case CONF_TYPE_FILENAME: filename_serialise(bs, entry->value.u.fileval); break; - case TYPE_FONT: + case CONF_TYPE_FONT: fontspec_serialise(bs, entry->value.u.fontval); break; } @@ -557,28 +550,28 @@ bool conf_deserialise(Conf *conf, BinarySource *src) entry->key.primary = primary; switch (subkeytypes[entry->key.primary]) { - case TYPE_INT: + case CONF_TYPE_INT: entry->key.secondary.i = toint(get_uint32(src)); break; - case TYPE_STR: + case CONF_TYPE_STR: entry->key.secondary.s = dupstr(get_asciz(src)); break; } switch (valuetypes[entry->key.primary]) { - case TYPE_BOOL: + case CONF_TYPE_BOOL: entry->value.u.boolval = get_bool(src); break; - case TYPE_INT: + case CONF_TYPE_INT: entry->value.u.intval = toint(get_uint32(src)); break; - case TYPE_STR: + case CONF_TYPE_STR: entry->value.u.stringval = dupstr(get_asciz(src)); break; - case TYPE_FILENAME: + case CONF_TYPE_FILENAME: entry->value.u.fileval = filename_deserialise(src); break; - case TYPE_FONT: + case CONF_TYPE_FONT: entry->value.u.fontval = fontspec_deserialise(src); break; }