The union of the color and the node_number in the struct screen_char.
[elinks.git] / src / config / domain.c
blob897cc5f1ca0f56fd65719f7806a85f7a14b8bfe3
1 /* Domain-specific options infrastructure */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "config/domain.h"
12 #include "config/options.h"
13 #include "protocol/uri.h"
14 #include "session/location.h"
15 #include "session/session.h"
16 #include "util/lists.h"
17 #include "viewer/text/vs.h"
20 INIT_LIST_OF(struct domain_tree, domain_trees);
23 /* Look for the option with the given name in all domain shadow-trees that
24 * match the given domain-name. Return the option from the the shadow tree
25 * that best matches the given domain name. */
26 static struct option *
27 get_domain_option(unsigned char *domain_name, int domain_len,
28 unsigned char *name)
30 struct option *opt, *longest_match_opt = NULL;
31 struct domain_tree *longest_match = NULL;
32 struct domain_tree *domain;
34 assert(domain_name);
35 assert(*domain_name);
37 foreach (domain, domain_trees)
38 if ((!longest_match || domain->len > longest_match->len)
39 && is_in_domain(domain->name, domain_name, domain_len)
40 && (opt = get_opt_rec_real(domain->tree, name))) {
41 longest_match = domain;
42 longest_match_opt = opt;
45 return longest_match_opt;
48 struct option *
49 get_domain_option_from_session(unsigned char *name, struct session *ses)
51 struct uri *uri;
53 assert(ses);
54 assert(name);
56 if (!have_location(ses))
57 return NULL;
59 uri = cur_loc(ses)->vs.uri;
60 if (!uri->host || !uri->hostlen)
61 return NULL;
63 return get_domain_option(uri->host, uri->hostlen, name);
66 /* Return the shadow shadow tree for the given domain name, and
67 * if the domain does not yet have a shadow tree, create it. */
68 struct option *
69 get_domain_tree(unsigned char *domain_name)
71 struct domain_tree *domain;
72 int domain_len;
74 assert(domain_name);
75 assert(*domain_name);
77 foreach (domain, domain_trees)
78 if (!strcasecmp(domain->name, domain_name))
79 return domain->tree;
81 domain_len = strlen(domain_name);
82 /* One byte is reserved for domain in struct domain_tree. */
83 domain = mem_alloc(sizeof(*domain) + domain_len);
84 if (!domain) return NULL;
86 domain->tree = copy_option(config_options, CO_SHALLOW
87 | CO_NO_LISTBOX_ITEM);
88 if (!domain->tree) {
89 mem_free(domain);
90 return NULL;
93 memcpy(domain->name, domain_name, domain_len + 1);
94 domain->len = domain_len;
96 add_to_list(domain_trees, domain);
98 return domain->tree;
101 void
102 done_domain_trees(void)
104 struct domain_tree *domain, *next;
106 foreachsafe (domain, next, domain_trees) {
107 delete_option(domain->tree);
108 domain->tree = NULL;
109 del_from_list(domain);
110 mem_free(domain);