2 * conf.c: implementation of the internal storage format used for
\r
3 * the configuration of a PuTTY session.
\r
10 #include "tree234.h"
\r
14 * Enumeration of types used in keys and values.
\r
16 typedef enum { TYPE_NONE, TYPE_INT, TYPE_STR, TYPE_FILENAME, TYPE_FONT } Type;
\r
19 * Arrays which allow us to look up the subkey and value types for a
\r
20 * given primary key id.
\r
22 #define CONF_SUBKEYTYPE_DEF(valtype, keytype, keyword) TYPE_ ## keytype,
\r
23 static int subkeytypes[] = { CONFIG_OPTIONS(CONF_SUBKEYTYPE_DEF) };
\r
24 #define CONF_VALUETYPE_DEF(valtype, keytype, keyword) TYPE_ ## valtype,
\r
25 static int valuetypes[] = { CONFIG_OPTIONS(CONF_VALUETYPE_DEF) };
\r
28 * Configuration keys are primarily integers (big enum of all the
\r
29 * different configurable options); some keys have string-designated
\r
30 * subkeys, such as the list of environment variables (subkeys
\r
31 * defined by the variable names); some have integer-designated
\r
32 * subkeys (wordness, colours, preference lists).
\r
42 /* Variant form of struct key which doesn't contain dynamic data, used
\r
71 * Because 'struct key' is the first element in 'struct conf_entry',
\r
72 * it's safe (guaranteed by the C standard) to cast arbitrarily back
\r
73 * and forth between the two types. Therefore, we only need one
\r
74 * comparison function, which can double as a main sort function for
\r
75 * the tree (comparing two conf_entry structures with each other)
\r
76 * and a search function (looking up an externally supplied key).
\r
78 static int conf_cmp(void *av, void *bv)
\r
80 struct key *a = (struct key *)av;
\r
81 struct key *b = (struct key *)bv;
\r
83 if (a->primary < b->primary)
\r
85 else if (a->primary > b->primary)
\r
87 switch (subkeytypes[a->primary]) {
\r
89 if (a->secondary.i < b->secondary.i)
\r
91 else if (a->secondary.i > b->secondary.i)
\r
95 return strcmp(a->secondary.s, b->secondary.s);
\r
101 static int conf_cmp_constkey(void *av, void *bv)
\r
103 struct key *a = (struct key *)av;
\r
104 struct constkey *b = (struct constkey *)bv;
\r
106 if (a->primary < b->primary)
\r
108 else if (a->primary > b->primary)
\r
110 switch (subkeytypes[a->primary]) {
\r
112 if (a->secondary.i < b->secondary.i)
\r
114 else if (a->secondary.i > b->secondary.i)
\r
118 return strcmp(a->secondary.s, b->secondary.s);
\r
125 * Free any dynamic data items pointed to by a 'struct key'. We
\r
126 * don't free the structure itself, since it's probably part of a
\r
127 * larger allocated block.
\r
129 static void free_key(struct key *key)
\r
131 if (subkeytypes[key->primary] == TYPE_STR)
\r
132 sfree(key->secondary.s);
\r
136 * Copy a 'struct key' into another one, copying its dynamic data
\r
139 static void copy_key(struct key *to, struct key *from)
\r
141 to->primary = from->primary;
\r
142 switch (subkeytypes[to->primary]) {
\r
144 to->secondary.i = from->secondary.i;
\r
147 to->secondary.s = dupstr(from->secondary.s);
\r
153 * Free any dynamic data items pointed to by a 'struct value'. We
\r
154 * don't free the value itself, since it's probably part of a larger
\r
157 static void free_value(struct value *val, int type)
\r
159 if (type == TYPE_STR)
\r
160 sfree(val->u.stringval);
\r
161 else if (type == TYPE_FILENAME)
\r
162 filename_free(val->u.fileval);
\r
163 else if (type == TYPE_FONT)
\r
164 fontspec_free(val->u.fontval);
\r
168 * Copy a 'struct value' into another one, copying its dynamic data
\r
171 static void copy_value(struct value *to, struct value *from, int type)
\r
175 to->u.intval = from->u.intval;
\r
178 to->u.stringval = dupstr(from->u.stringval);
\r
180 case TYPE_FILENAME:
\r
181 to->u.fileval = filename_copy(from->u.fileval);
\r
184 to->u.fontval = fontspec_copy(from->u.fontval);
\r
190 * Free an entire 'struct conf_entry' and its dynamic data.
\r
192 static void free_entry(struct conf_entry *entry)
\r
194 free_key(&entry->key);
\r
195 free_value(&entry->value, valuetypes[entry->key.primary]);
\r
199 Conf *conf_new(void)
\r
201 Conf *conf = snew(struct conf_tag);
\r
203 conf->tree = newtree234(conf_cmp);
\r
208 static void conf_clear(Conf *conf)
\r
210 struct conf_entry *entry;
\r
212 while ((entry = delpos234(conf->tree, 0)) != NULL)
\r
216 void conf_free(Conf *conf)
\r
219 freetree234(conf->tree);
\r
223 static void conf_insert(Conf *conf, struct conf_entry *entry)
\r
225 struct conf_entry *oldentry = add234(conf->tree, entry);
\r
226 if (oldentry && oldentry != entry) {
\r
227 del234(conf->tree, oldentry);
\r
228 free_entry(oldentry);
\r
229 oldentry = add234(conf->tree, entry);
\r
230 assert(oldentry == entry);
\r
234 void conf_copy_into(Conf *newconf, Conf *oldconf)
\r
236 struct conf_entry *entry, *entry2;
\r
239 conf_clear(newconf);
\r
241 for (i = 0; (entry = index234(oldconf->tree, i)) != NULL; i++) {
\r
242 entry2 = snew(struct conf_entry);
\r
243 copy_key(&entry2->key, &entry->key);
\r
244 copy_value(&entry2->value, &entry->value,
\r
245 valuetypes[entry->key.primary]);
\r
246 add234(newconf->tree, entry2);
\r
250 Conf *conf_copy(Conf *oldconf)
\r
252 Conf *newconf = conf_new();
\r
254 conf_copy_into(newconf, oldconf);
\r
259 int conf_get_int(Conf *conf, int primary)
\r
262 struct conf_entry *entry;
\r
264 assert(subkeytypes[primary] == TYPE_NONE);
\r
265 assert(valuetypes[primary] == TYPE_INT);
\r
266 key.primary = primary;
\r
267 entry = find234(conf->tree, &key, NULL);
\r
269 return entry->value.u.intval;
\r
272 int conf_get_int_int(Conf *conf, int primary, int secondary)
\r
275 struct conf_entry *entry;
\r
277 assert(subkeytypes[primary] == TYPE_INT);
\r
278 assert(valuetypes[primary] == TYPE_INT);
\r
279 key.primary = primary;
\r
280 key.secondary.i = secondary;
\r
281 entry = find234(conf->tree, &key, NULL);
\r
283 return entry->value.u.intval;
\r
286 char *conf_get_str(Conf *conf, int primary)
\r
289 struct conf_entry *entry;
\r
291 assert(subkeytypes[primary] == TYPE_NONE);
\r
292 assert(valuetypes[primary] == TYPE_STR);
\r
293 key.primary = primary;
\r
294 entry = find234(conf->tree, &key, NULL);
\r
296 return entry->value.u.stringval;
\r
299 char *conf_get_str_str_opt(Conf *conf, int primary, const char *secondary)
\r
302 struct conf_entry *entry;
\r
304 assert(subkeytypes[primary] == TYPE_STR);
\r
305 assert(valuetypes[primary] == TYPE_STR);
\r
306 key.primary = primary;
\r
307 key.secondary.s = (char *)secondary;
\r
308 entry = find234(conf->tree, &key, NULL);
\r
309 return entry ? entry->value.u.stringval : NULL;
\r
312 char *conf_get_str_str(Conf *conf, int primary, const char *secondary)
\r
314 char *ret = conf_get_str_str_opt(conf, primary, secondary);
\r
319 char *conf_get_str_strs(Conf *conf, int primary,
\r
320 char *subkeyin, char **subkeyout)
\r
322 struct constkey key;
\r
323 struct conf_entry *entry;
\r
325 assert(subkeytypes[primary] == TYPE_STR);
\r
326 assert(valuetypes[primary] == TYPE_STR);
\r
327 key.primary = primary;
\r
329 key.secondary.s = subkeyin;
\r
330 entry = findrel234(conf->tree, &key, NULL, REL234_GT);
\r
332 key.secondary.s = "";
\r
333 entry = findrel234(conf->tree, &key, conf_cmp_constkey, REL234_GE);
\r
335 if (!entry || entry->key.primary != primary)
\r
337 *subkeyout = entry->key.secondary.s;
\r
338 return entry->value.u.stringval;
\r
341 char *conf_get_str_nthstrkey(Conf *conf, int primary, int n)
\r
343 struct constkey key;
\r
344 struct conf_entry *entry;
\r
347 assert(subkeytypes[primary] == TYPE_STR);
\r
348 assert(valuetypes[primary] == TYPE_STR);
\r
349 key.primary = primary;
\r
350 key.secondary.s = "";
\r
351 entry = findrelpos234(conf->tree, &key, conf_cmp_constkey,
\r
352 REL234_GE, &index);
\r
353 if (!entry || entry->key.primary != primary)
\r
355 entry = index234(conf->tree, index + n);
\r
356 if (!entry || entry->key.primary != primary)
\r
358 return entry->key.secondary.s;
\r
361 Filename *conf_get_filename(Conf *conf, int primary)
\r
364 struct conf_entry *entry;
\r
366 assert(subkeytypes[primary] == TYPE_NONE);
\r
367 assert(valuetypes[primary] == TYPE_FILENAME);
\r
368 key.primary = primary;
\r
369 entry = find234(conf->tree, &key, NULL);
\r
371 return entry->value.u.fileval;
\r
374 FontSpec *conf_get_fontspec(Conf *conf, int primary)
\r
377 struct conf_entry *entry;
\r
379 assert(subkeytypes[primary] == TYPE_NONE);
\r
380 assert(valuetypes[primary] == TYPE_FONT);
\r
381 key.primary = primary;
\r
382 entry = find234(conf->tree, &key, NULL);
\r
384 return entry->value.u.fontval;
\r
387 void conf_set_int(Conf *conf, int primary, int value)
\r
389 struct conf_entry *entry = snew(struct conf_entry);
\r
391 assert(subkeytypes[primary] == TYPE_NONE);
\r
392 assert(valuetypes[primary] == TYPE_INT);
\r
393 entry->key.primary = primary;
\r
394 entry->value.u.intval = value;
\r
395 conf_insert(conf, entry);
\r
398 void conf_set_int_int(Conf *conf, int primary, int secondary, int value)
\r
400 struct conf_entry *entry = snew(struct conf_entry);
\r
402 assert(subkeytypes[primary] == TYPE_INT);
\r
403 assert(valuetypes[primary] == TYPE_INT);
\r
404 entry->key.primary = primary;
\r
405 entry->key.secondary.i = secondary;
\r
406 entry->value.u.intval = value;
\r
407 conf_insert(conf, entry);
\r
410 void conf_set_str(Conf *conf, int primary, const char *value)
\r
412 struct conf_entry *entry = snew(struct conf_entry);
\r
414 assert(subkeytypes[primary] == TYPE_NONE);
\r
415 assert(valuetypes[primary] == TYPE_STR);
\r
416 entry->key.primary = primary;
\r
417 entry->value.u.stringval = dupstr(value);
\r
418 conf_insert(conf, entry);
\r
421 void conf_set_str_str(Conf *conf, int primary, const char *secondary,
\r
424 struct conf_entry *entry = snew(struct conf_entry);
\r
426 assert(subkeytypes[primary] == TYPE_STR);
\r
427 assert(valuetypes[primary] == TYPE_STR);
\r
428 entry->key.primary = primary;
\r
429 entry->key.secondary.s = dupstr(secondary);
\r
430 entry->value.u.stringval = dupstr(value);
\r
431 conf_insert(conf, entry);
\r
434 void conf_del_str_str(Conf *conf, int primary, const char *secondary)
\r
437 struct conf_entry *entry;
\r
439 assert(subkeytypes[primary] == TYPE_STR);
\r
440 assert(valuetypes[primary] == TYPE_STR);
\r
441 key.primary = primary;
\r
442 key.secondary.s = (char *)secondary;
\r
443 entry = find234(conf->tree, &key, NULL);
\r
445 del234(conf->tree, entry);
\r
450 void conf_set_filename(Conf *conf, int primary, const Filename *value)
\r
452 struct conf_entry *entry = snew(struct conf_entry);
\r
454 assert(subkeytypes[primary] == TYPE_NONE);
\r
455 assert(valuetypes[primary] == TYPE_FILENAME);
\r
456 entry->key.primary = primary;
\r
457 entry->value.u.fileval = filename_copy(value);
\r
458 conf_insert(conf, entry);
\r
461 void conf_set_fontspec(Conf *conf, int primary, const FontSpec *value)
\r
463 struct conf_entry *entry = snew(struct conf_entry);
\r
465 assert(subkeytypes[primary] == TYPE_NONE);
\r
466 assert(valuetypes[primary] == TYPE_FONT);
\r
467 entry->key.primary = primary;
\r
468 entry->value.u.fontval = fontspec_copy(value);
\r
469 conf_insert(conf, entry);
\r
472 int conf_serialised_size(Conf *conf)
\r
475 struct conf_entry *entry;
\r
478 for (i = 0; (entry = index234(conf->tree, i)) != NULL; i++) {
\r
479 size += 4; /* primary key */
\r
480 switch (subkeytypes[entry->key.primary]) {
\r
485 size += 1 + strlen(entry->key.secondary.s);
\r
488 switch (valuetypes[entry->key.primary]) {
\r
493 size += 1 + strlen(entry->value.u.stringval);
\r
495 case TYPE_FILENAME:
\r
496 size += filename_serialise(entry->value.u.fileval, NULL);
\r
499 size += fontspec_serialise(entry->value.u.fontval, NULL);
\r
504 size += 4; /* terminator value */
\r
509 void conf_serialise(Conf *conf, void *vdata)
\r
511 unsigned char *data = (unsigned char *)vdata;
\r
513 struct conf_entry *entry;
\r
515 for (i = 0; (entry = index234(conf->tree, i)) != NULL; i++) {
\r
516 PUT_32BIT_MSB_FIRST(data, entry->key.primary);
\r
519 switch (subkeytypes[entry->key.primary]) {
\r
521 PUT_32BIT_MSB_FIRST(data, entry->key.secondary.i);
\r
525 len = strlen(entry->key.secondary.s);
\r
526 memcpy(data, entry->key.secondary.s, len);
\r
531 switch (valuetypes[entry->key.primary]) {
\r
533 PUT_32BIT_MSB_FIRST(data, entry->value.u.intval);
\r
537 len = strlen(entry->value.u.stringval);
\r
538 memcpy(data, entry->value.u.stringval, len);
\r
542 case TYPE_FILENAME:
\r
543 data += filename_serialise(entry->value.u.fileval, data);
\r
546 data += fontspec_serialise(entry->value.u.fontval, data);
\r
551 PUT_32BIT_MSB_FIRST(data, 0xFFFFFFFFU);
\r
554 int conf_deserialise(Conf *conf, void *vdata, int maxsize)
\r
556 unsigned char *data = (unsigned char *)vdata;
\r
557 unsigned char *start = data;
\r
558 struct conf_entry *entry;
\r
561 unsigned char *zero;
\r
563 while (maxsize >= 4) {
\r
564 primary = GET_32BIT_MSB_FIRST(data);
\r
565 data += 4, maxsize -= 4;
\r
567 if (primary >= N_CONFIG_OPTIONS)
\r
570 entry = snew(struct conf_entry);
\r
571 entry->key.primary = primary;
\r
573 switch (subkeytypes[entry->key.primary]) {
\r
579 entry->key.secondary.i = toint(GET_32BIT_MSB_FIRST(data));
\r
580 data += 4, maxsize -= 4;
\r
583 zero = memchr(data, 0, maxsize);
\r
588 entry->key.secondary.s = dupstr((char *)data);
\r
589 maxsize -= (zero + 1 - data);
\r
594 switch (valuetypes[entry->key.primary]) {
\r
597 if (subkeytypes[entry->key.primary] == TYPE_STR)
\r
598 sfree(entry->key.secondary.s);
\r
602 entry->value.u.intval = toint(GET_32BIT_MSB_FIRST(data));
\r
603 data += 4, maxsize -= 4;
\r
606 zero = memchr(data, 0, maxsize);
\r
608 if (subkeytypes[entry->key.primary] == TYPE_STR)
\r
609 sfree(entry->key.secondary.s);
\r
613 entry->value.u.stringval = dupstr((char *)data);
\r
614 maxsize -= (zero + 1 - data);
\r
617 case TYPE_FILENAME:
\r
618 entry->value.u.fileval =
\r
619 filename_deserialise(data, maxsize, &used);
\r
620 if (!entry->value.u.fileval) {
\r
621 if (subkeytypes[entry->key.primary] == TYPE_STR)
\r
622 sfree(entry->key.secondary.s);
\r
630 entry->value.u.fontval =
\r
631 fontspec_deserialise(data, maxsize, &used);
\r
632 if (!entry->value.u.fontval) {
\r
633 if (subkeytypes[entry->key.primary] == TYPE_STR)
\r
634 sfree(entry->key.secondary.s);
\r
642 conf_insert(conf, entry);
\r
646 return (int)(data - start);
\r