2 * UCW Library -- Configuration files: sections
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2014 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include <ucw/conf-internal.h>
14 #include <ucw/clists.h>
15 #include <ucw/binsearch.h>
23 cf_add_dirty(struct cf_section *sec, void *ptr)
25 struct cf_context *cc = cf_get_context();
26 dirtsec_grow(&cc->dirty, cc->dirties+1);
27 struct dirty_section *dest = cc->dirty.ptr + cc->dirties;
28 if (cc->dirties && dest[-1].sec == sec && dest[-1].ptr == ptr)
35 #define ASORT_PREFIX(x) dirtsec_##x
36 #define ASORT_KEY_TYPE struct dirty_section
37 #define ASORT_LT(x,y) x.sec < y.sec || x.sec == y.sec && x.ptr < y.ptr
38 #include <ucw/sorter/array-simple.h>
41 sort_dirty(struct cf_context *cc)
45 dirtsec_sort(cc->dirty.ptr, cc->dirties);
46 // and compress the list
47 struct dirty_section *read = cc->dirty.ptr + 1, *write = cc->dirty.ptr + 1, *limit = cc->dirty.ptr + cc->dirties;
48 while (read < limit) {
49 if (read->sec != read[-1].sec || read->ptr != read[-1].ptr) {
56 cc->dirties = write - cc->dirty.ptr;
62 cf_find_subitem(struct cf_section *sec, const char *name)
64 struct cf_item *ci = sec->cfg;
66 if (!strcasecmp(ci->name, name))
72 inspect_section(struct cf_section *sec)
76 for (ci=sec->cfg; ci->cls; ci++)
77 if (ci->cls == CC_SECTION) {
78 inspect_section(ci->u.sec);
79 sec->flags |= ci->u.sec->flags & (SEC_FLAG_DYNAMIC | SEC_FLAG_CANT_COPY);
80 } else if (ci->cls == CC_LIST) {
81 inspect_section(ci->u.sec);
82 sec->flags |= SEC_FLAG_DYNAMIC | SEC_FLAG_CANT_COPY;
83 } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_BITMAP)
84 sec->flags |= SEC_FLAG_DYNAMIC;
85 else if (ci->cls == CC_PARSER) {
86 sec->flags |= SEC_FLAG_CANT_COPY;
88 sec->flags |= SEC_FLAG_DYNAMIC;
91 sec->flags &= ~SEC_FLAG_CANT_COPY;
92 sec->flags |= ci - sec->cfg; // record the number of entries
96 cf_declare_rel_section(const char *name, struct cf_section *sec, void *ptr, uint allow_unknown)
98 struct cf_context *cc = cf_obtain_context();
99 if (!cc->sections.cfg)
101 cc->sections.size = 50;
102 cc->sections.cfg = xmalloc_zero(cc->sections.size * sizeof(struct cf_item));
104 struct cf_item *ci = cf_find_subitem(&cc->sections, name);
106 die("Cannot register section %s twice", name);
107 ci->cls = CC_SECTION;
112 inspect_section(sec);
114 sec->flags |= SEC_FLAG_UNKNOWN;
116 if (ci - cc->sections.cfg >= (int) cc->sections.size)
118 cc->sections.cfg = xrealloc(cc->sections.cfg, 2*cc->sections.size * sizeof(struct cf_item));
119 bzero(cc->sections.cfg + cc->sections.size, cc->sections.size * sizeof(struct cf_item));
120 cc->sections.size *= 2;
125 cf_declare_section(const char *name, struct cf_section *sec, uint allow_unknown)
127 cf_declare_rel_section(name, sec, NULL, allow_unknown);
131 cf_init_section(const char *name, struct cf_section *sec, void *ptr, uint do_bzero)
135 bzero(ptr, sec->size);
137 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
138 if (ci->cls == CC_SECTION)
139 cf_init_section(ci->name, ci->u.sec, ptr + (uintptr_t) ci->ptr, 0);
140 else if (ci->cls == CC_LIST)
141 clist_init(ptr + (uintptr_t) ci->ptr);
142 else if (ci->cls == CC_DYNAMIC) {
143 void **dyn = ptr + (uintptr_t) ci->ptr;
144 if (!*dyn) // replace NULL by an empty array
145 *dyn = GARY_FOREVER_EMPTY;
148 char *msg = sec->init(ptr);
150 die("Cannot initialize section %s: %s", name, msg);
155 commit_section(struct cf_section *sec, void *ptr, uint commit_all)
157 struct cf_context *cc = cf_get_context();
160 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
161 if (ci->cls == CC_SECTION) {
162 if ((err = commit_section(ci->u.sec, ptr + (uintptr_t) ci->ptr, commit_all))) {
163 msg(L_ERROR, "Cannot commit section %s: %s", ci->name, err);
164 return "commit of a subsection failed";
166 } else if (ci->cls == CC_LIST) {
168 CLIST_FOR_EACH(cnode *, n, * (clist*) (ptr + (uintptr_t) ci->ptr))
169 if (idx++, err = commit_section(ci->u.sec, n, commit_all)) {
170 msg(L_ERROR, "Cannot commit node #%d of list %s: %s", idx, ci->name, err);
171 return "commit of a list failed";
175 /* We have to process the whole tree of sections even if just a few changes
176 * have been made, because there are dependencies between commit-hooks and
177 * hence we need to call them in a fixed order. */
178 #define ARY_LT_X(ary,i,x) ary[i].sec < x.sec || ary[i].sec == x.sec && ary[i].ptr < x.ptr
179 struct dirty_section comp = { sec, ptr };
180 uint pos = BIN_SEARCH_FIRST_GE_CMP(cc->dirty.ptr, cc->dirties, comp, ARY_LT_X);
183 || (pos < cc->dirties && cc->dirty.ptr[pos].sec == sec && cc->dirty.ptr[pos].ptr == ptr))
184 return sec->commit(ptr);
190 cf_commit_all(enum cf_commit_mode cm)
192 struct cf_context *cc = cf_get_context();
194 if (cm == CF_NO_COMMIT)
196 if (commit_section(&cc->sections, NULL, cm == CF_COMMIT_ALL))