2 * Copyright © 2020 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
32 #include <libxml/parser.h>
34 #include "xkbcommon/xkbregistry.h"
36 #include "util-list.h"
40 typedef void (*destroy_func_t)(struct rxkb_object *object);
43 * All our objects are refcounted and are linked to iterate through them.
44 * Abstract those bits away into a shared parent class so we can generate
45 * most of the functions through macros.
48 struct rxkb_object *parent;
51 destroy_func_t destroy;
54 struct rxkb_iso639_code {
55 struct rxkb_object base;
59 struct rxkb_iso3166_code {
60 struct rxkb_object base;
71 struct rxkb_object base;
72 enum context_state context_state;
74 bool load_extra_rules_files;
76 struct list models; /* list of struct rxkb_models */
77 struct list layouts; /* list of struct rxkb_layouts */
78 struct list option_groups; /* list of struct rxkb_option_group */
80 darray(char *) includes;
83 ATTR_PRINTF(3, 0) void (*log_fn)(struct rxkb_context *ctx,
84 enum rxkb_log_level level,
85 const char *fmt, va_list args);
86 enum rxkb_log_level log_level;
92 struct rxkb_object base;
97 enum rxkb_popularity popularity;
101 struct rxkb_object base;
107 enum rxkb_popularity popularity;
109 struct list iso639s; /* list of struct rxkb_iso639_code */
110 struct list iso3166s; /* list of struct rxkb_iso3166_code */
113 struct rxkb_option_group {
114 struct rxkb_object base;
117 struct list options; /* list of struct rxkb_options */
120 enum rxkb_popularity popularity;
124 struct rxkb_object base;
129 enum rxkb_popularity popularity;
133 parse(struct rxkb_context *ctx, const char *path,
134 enum rxkb_popularity popularity);
138 rxkb_log(struct rxkb_context *ctx, enum rxkb_log_level level,
139 const char *fmt, ...)
143 if (ctx->log_level < level)
147 ctx->log_fn(ctx, level, fmt, args);
152 * The format is not part of the argument list in order to avoid the
153 * "ISO C99 requires rest arguments to be used" warning when only the
154 * format is supplied without arguments. Not supplying it would still
155 * result in an error, though.
157 #define log_dbg(ctx, ...) \
158 rxkb_log((ctx), RXKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
159 #define log_info(ctx, ...) \
160 rxkb_log((ctx), RXKB_LOG_LEVEL_INFO, __VA_ARGS__)
161 #define log_warn(ctx, ...) \
162 rxkb_log((ctx), RXKB_LOG_LEVEL_WARNING, __VA_ARGS__)
163 #define log_err(ctx, ...) \
164 rxkb_log((ctx), RXKB_LOG_LEVEL_ERROR, __VA_ARGS__)
165 #define log_wsgo(ctx, ...) \
166 rxkb_log((ctx), RXKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
169 #define DECLARE_REF_UNREF_FOR_TYPE(type_) \
170 XKB_EXPORT struct type_ * type_##_ref(struct type_ *object) { \
171 rxkb_object_ref(&object->base); \
174 XKB_EXPORT struct type_ * type_##_unref(struct type_ *object) { \
175 if (!object) return NULL; \
176 return rxkb_object_unref(&object->base); \
179 #define DECLARE_CREATE_FOR_TYPE(type_) \
180 static inline struct type_ * type_##_create(struct rxkb_object *parent) { \
181 struct type_ *t = calloc(1, sizeof *t); \
183 rxkb_object_init(&t->base, parent, (destroy_func_t)type_##_destroy); \
187 #define DECLARE_TYPED_GETTER_FOR_TYPE(type_, field_, rtype_) \
188 XKB_EXPORT rtype_ type_##_get_##field_(struct type_ *object) { \
189 return object->field_; \
192 #define DECLARE_GETTER_FOR_TYPE(type_, field_) \
193 DECLARE_TYPED_GETTER_FOR_TYPE(type_, field_, const char*)
195 #define DECLARE_FIRST_NEXT_FOR_TYPE(type_, parent_type_, parent_field_) \
196 XKB_EXPORT struct type_ * type_##_first(struct parent_type_ *parent) { \
197 struct type_ *o = NULL; \
198 if (!list_empty(&parent->parent_field_)) \
199 o = list_first_entry(&parent->parent_field_, o, base.link); \
202 XKB_EXPORT struct type_ * \
203 type_##_next(struct type_ *o) \
205 struct parent_type_ *parent; \
206 struct type_ *next; \
207 parent = container_of(o->base.parent, struct parent_type_, base); \
208 next = list_first_entry(&o->base.link, o, base.link); \
209 if (list_is_last(&parent->parent_field_, &o->base.link)) \
215 rxkb_object_init(struct rxkb_object *object, struct rxkb_object *parent, destroy_func_t destroy)
217 object->refcount = 1;
218 object->destroy = destroy;
219 object->parent = parent;
220 list_init(&object->link);
224 rxkb_object_destroy(struct rxkb_object *object)
227 object->destroy(object);
228 list_remove(&object->link);
233 rxkb_object_ref(struct rxkb_object *object)
235 assert(object->refcount >= 1);
241 rxkb_object_unref(struct rxkb_object *object)
243 assert(object->refcount >= 1);
244 if (--object->refcount == 0)
245 rxkb_object_destroy(object);
250 rxkb_iso639_code_destroy(struct rxkb_iso639_code *code)
255 XKB_EXPORT struct rxkb_iso639_code *
256 rxkb_layout_get_iso639_first(struct rxkb_layout *layout)
258 struct rxkb_iso639_code *code = NULL;
260 if (!list_empty(&layout->iso639s))
261 code = list_first_entry(&layout->iso639s, code, base.link);
266 XKB_EXPORT struct rxkb_iso639_code *
267 rxkb_iso639_code_next(struct rxkb_iso639_code *code)
269 struct rxkb_iso639_code *next = NULL;
270 struct rxkb_layout *layout;
272 layout = container_of(code->base.parent, struct rxkb_layout, base);
274 if (list_is_last(&layout->iso639s, &code->base.link))
277 next = list_first_entry(&code->base.link, code, base.link);
282 DECLARE_REF_UNREF_FOR_TYPE(rxkb_iso639_code);
283 DECLARE_CREATE_FOR_TYPE(rxkb_iso639_code);
284 DECLARE_GETTER_FOR_TYPE(rxkb_iso639_code, code);
287 rxkb_iso3166_code_destroy(struct rxkb_iso3166_code *code)
292 XKB_EXPORT struct rxkb_iso3166_code *
293 rxkb_layout_get_iso3166_first(struct rxkb_layout *layout)
295 struct rxkb_iso3166_code *code = NULL;
297 if (!list_empty(&layout->iso3166s))
298 code = list_first_entry(&layout->iso3166s, code, base.link);
303 XKB_EXPORT struct rxkb_iso3166_code *
304 rxkb_iso3166_code_next(struct rxkb_iso3166_code *code)
306 struct rxkb_iso3166_code *next = NULL;
307 struct rxkb_layout *layout;
309 layout = container_of(code->base.parent, struct rxkb_layout, base);
311 if (list_is_last(&layout->iso3166s, &code->base.link))
314 next = list_first_entry(&code->base.link, code, base.link);
319 DECLARE_REF_UNREF_FOR_TYPE(rxkb_iso3166_code);
320 DECLARE_CREATE_FOR_TYPE(rxkb_iso3166_code);
321 DECLARE_GETTER_FOR_TYPE(rxkb_iso3166_code, code);
324 rxkb_option_destroy(struct rxkb_option *o)
328 free(o->description);
331 DECLARE_REF_UNREF_FOR_TYPE(rxkb_option);
332 DECLARE_CREATE_FOR_TYPE(rxkb_option);
333 DECLARE_GETTER_FOR_TYPE(rxkb_option, name);
334 DECLARE_GETTER_FOR_TYPE(rxkb_option, brief);
335 DECLARE_GETTER_FOR_TYPE(rxkb_option, description);
336 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_option, popularity, enum rxkb_popularity);
337 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_option, rxkb_option_group, options);
340 rxkb_layout_destroy(struct rxkb_layout *l)
342 struct rxkb_iso639_code *iso639, *tmp_639;
343 struct rxkb_iso3166_code *iso3166, *tmp_3166;
347 free(l->description);
350 list_for_each_safe(iso639, tmp_639, &l->iso639s, base.link) {
351 rxkb_iso639_code_unref(iso639);
353 list_for_each_safe(iso3166, tmp_3166, &l->iso3166s, base.link) {
354 rxkb_iso3166_code_unref(iso3166);
358 DECLARE_REF_UNREF_FOR_TYPE(rxkb_layout);
359 DECLARE_CREATE_FOR_TYPE(rxkb_layout);
360 DECLARE_GETTER_FOR_TYPE(rxkb_layout, name);
361 DECLARE_GETTER_FOR_TYPE(rxkb_layout, brief);
362 DECLARE_GETTER_FOR_TYPE(rxkb_layout, description);
363 DECLARE_GETTER_FOR_TYPE(rxkb_layout, variant);
364 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_layout, popularity, enum rxkb_popularity);
365 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_layout, rxkb_context, layouts);
368 rxkb_model_destroy(struct rxkb_model *m)
372 free(m->description);
375 DECLARE_REF_UNREF_FOR_TYPE(rxkb_model);
376 DECLARE_CREATE_FOR_TYPE(rxkb_model);
377 DECLARE_GETTER_FOR_TYPE(rxkb_model, name);
378 DECLARE_GETTER_FOR_TYPE(rxkb_model, vendor);
379 DECLARE_GETTER_FOR_TYPE(rxkb_model, description);
380 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_model, popularity, enum rxkb_popularity);
381 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_model, rxkb_context, models);
384 rxkb_option_group_destroy(struct rxkb_option_group *og)
386 struct rxkb_option *o, *otmp;
389 free(og->description);
391 list_for_each_safe(o, otmp, &og->options, base.link) {
392 rxkb_option_unref(o);
397 rxkb_option_group_allows_multiple(struct rxkb_option_group *g)
399 return g->allow_multiple;
402 DECLARE_REF_UNREF_FOR_TYPE(rxkb_option_group);
403 DECLARE_CREATE_FOR_TYPE(rxkb_option_group);
404 DECLARE_GETTER_FOR_TYPE(rxkb_option_group, name);
405 DECLARE_GETTER_FOR_TYPE(rxkb_option_group, description);
406 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_option_group, popularity, enum rxkb_popularity);
407 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_option_group, rxkb_context, option_groups);
410 rxkb_context_destroy(struct rxkb_context *ctx)
412 struct rxkb_model *m, *mtmp;
413 struct rxkb_layout *l, *ltmp;
414 struct rxkb_option_group *og, *ogtmp;
417 list_for_each_safe(m, mtmp, &ctx->models, base.link)
419 assert(list_empty(&ctx->models));
421 list_for_each_safe(l, ltmp, &ctx->layouts, base.link)
422 rxkb_layout_unref(l);
423 assert(list_empty(&ctx->layouts));
425 list_for_each_safe(og, ogtmp, &ctx->option_groups, base.link)
426 rxkb_option_group_unref(og);
427 assert(list_empty(&ctx->option_groups));
429 darray_foreach(path, ctx->includes)
431 darray_free(ctx->includes);
433 assert(darray_empty(ctx->includes));
436 DECLARE_REF_UNREF_FOR_TYPE(rxkb_context);
437 DECLARE_CREATE_FOR_TYPE(rxkb_context);
438 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_context, log_level, enum rxkb_log_level);
441 rxkb_context_set_log_level(struct rxkb_context *ctx,
442 enum rxkb_log_level level)
444 ctx->log_level = level;
448 log_level_to_prefix(enum rxkb_log_level level)
451 case RXKB_LOG_LEVEL_DEBUG:
452 return "xkbregistry: DEBUG: ";
453 case RXKB_LOG_LEVEL_INFO:
454 return "xkbregistry: INFO: ";
455 case RXKB_LOG_LEVEL_WARNING:
456 return "xkbregistry: WARNING: ";
457 case RXKB_LOG_LEVEL_ERROR:
458 return "xkbregistry: ERROR: ";
459 case RXKB_LOG_LEVEL_CRITICAL:
460 return "xkbregistry: CRITICAL: ";
466 ATTR_PRINTF(3, 0) static void
467 default_log_fn(struct rxkb_context *ctx, enum rxkb_log_level level,
468 const char *fmt, va_list args)
470 const char *prefix = log_level_to_prefix(level);
473 fprintf(stderr, "%s", prefix);
474 vfprintf(stderr, fmt, args);
477 static enum rxkb_log_level
478 log_level(const char *level) {
480 enum rxkb_log_level lvl;
483 lvl = strtol(level, &endptr, 10);
484 if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
486 if (istreq_prefix("crit", level))
487 return RXKB_LOG_LEVEL_CRITICAL;
488 if (istreq_prefix("err", level))
489 return RXKB_LOG_LEVEL_ERROR;
490 if (istreq_prefix("warn", level))
491 return RXKB_LOG_LEVEL_WARNING;
492 if (istreq_prefix("info", level))
493 return RXKB_LOG_LEVEL_INFO;
494 if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
495 return RXKB_LOG_LEVEL_DEBUG;
497 return RXKB_LOG_LEVEL_ERROR;
500 XKB_EXPORT struct rxkb_context *
501 rxkb_context_new(enum rxkb_context_flags flags)
503 struct rxkb_context *ctx = rxkb_context_create(NULL);
509 ctx->context_state = CONTEXT_NEW;
510 ctx->load_extra_rules_files = flags & RXKB_CONTEXT_LOAD_EXOTIC_RULES;
511 ctx->log_fn = default_log_fn;
512 ctx->log_level = RXKB_LOG_LEVEL_ERROR;
514 /* Environment overwrites defaults. */
515 env = secure_getenv("RXKB_LOG_LEVEL");
517 rxkb_context_set_log_level(ctx, log_level(env));
519 list_init(&ctx->models);
520 list_init(&ctx->layouts);
521 list_init(&ctx->option_groups);
523 if (!(flags & RXKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
524 !rxkb_context_include_path_append_default(ctx)) {
525 rxkb_context_unref(ctx);
533 rxkb_context_set_log_fn(struct rxkb_context *ctx,
534 void (*log_fn)(struct rxkb_context *ctx,
535 enum rxkb_log_level level,
536 const char *fmt, va_list args))
538 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
542 rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path)
544 struct stat stat_buf;
547 char rules[PATH_MAX];
549 if (ctx->context_state != CONTEXT_NEW) {
550 log_err(ctx, "include paths can only be appended to a new context\n");
558 err = stat(path, &stat_buf);
561 if (!S_ISDIR(stat_buf.st_mode))
564 if (!check_eaccess(path, R_OK | X_OK))
567 /* Pre-filter for the 99.9% case - if we can't assemble the default ruleset
568 * path, complain here instead of during parsing later. The niche cases
569 * where this is the wrong behaviour aren't worth worrying about.
571 if (!snprintf_safe(rules, sizeof(rules), "%s/rules/%s.xml",
572 path, DEFAULT_XKB_RULES))
575 darray_append(ctx->includes, tmp);
585 rxkb_context_include_path_append_default(struct rxkb_context *ctx)
587 const char *home, *xdg, *root, *extra;
591 if (ctx->context_state != CONTEXT_NEW) {
592 log_err(ctx, "include paths can only be appended to a new context\n");
596 home = secure_getenv("HOME");
598 xdg = secure_getenv("XDG_CONFIG_HOME");
600 user_path = asprintf_safe("%s/xkb", xdg);
602 ret |= rxkb_context_include_path_append(ctx, user_path);
605 } else if (home != NULL) {
606 /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
607 user_path = asprintf_safe("%s/.config/xkb", home);
609 ret |= rxkb_context_include_path_append(ctx, user_path);
615 user_path = asprintf_safe("%s/.xkb", home);
617 ret |= rxkb_context_include_path_append(ctx, user_path);
622 extra = secure_getenv("XKB_CONFIG_EXTRA_PATH");
624 ret |= rxkb_context_include_path_append(ctx, extra);
626 ret |= rxkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_EXTRA_PATH);
628 root = secure_getenv("XKB_CONFIG_ROOT");
630 ret |= rxkb_context_include_path_append(ctx, root);
632 ret |= rxkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
638 rxkb_context_parse_default_ruleset(struct rxkb_context *ctx)
640 return rxkb_context_parse(ctx, DEFAULT_XKB_RULES);
644 rxkb_context_parse(struct rxkb_context *ctx, const char *ruleset)
647 bool success = false;
649 if (ctx->context_state != CONTEXT_NEW) {
650 log_err(ctx, "parse must only be called on a new context\n");
654 darray_foreach_reverse(path, ctx->includes) {
655 char rules[PATH_MAX];
657 if (snprintf_safe(rules, sizeof(rules), "%s/rules/%s.xml",
659 log_dbg(ctx, "Parsing %s\n", rules);
660 if (parse(ctx, rules, RXKB_POPULARITY_STANDARD))
664 if (ctx->load_extra_rules_files &&
665 snprintf_safe(rules, sizeof(rules), "%s/rules/%s.extras.xml",
667 log_dbg(ctx, "Parsing %s\n", rules);
668 if (parse(ctx, rules, RXKB_POPULARITY_EXOTIC))
673 ctx->context_state = success ? CONTEXT_PARSED : CONTEXT_FAILED;
680 rxkb_context_set_user_data(struct rxkb_context *ctx, void *userdata)
682 ctx->userdata = userdata;
686 rxkb_context_get_user_data(struct rxkb_context *ctx)
688 return ctx->userdata;
692 is_node(xmlNode *node, const char *name)
694 return node->type == XML_ELEMENT_NODE &&
695 xmlStrEqual(node->name, (const xmlChar*)name);
698 /* return a copy of the text content from the first text node of this node */
700 extract_text(xmlNode *node)
704 for (n = node->children; n; n = n->next) {
705 if (n->type == XML_TEXT_NODE)
706 return (char *)xmlStrdup(n->content);
712 parse_config_item(struct rxkb_context *ctx,
719 xmlNode *node = NULL;
722 for (ci = parent->children; ci; ci = ci->next) {
723 if (is_node(ci, "configItem")) {
729 for (node = ci->children; node; node = node->next) {
730 if (is_node(node, "name"))
731 *name = extract_text(node);
732 else if (is_node(node, "description"))
733 *description = extract_text(node);
734 else if (is_node(node, "shortDescription"))
735 *brief = extract_text(node);
736 else if (is_node(node, "vendor"))
737 *vendor = extract_text(node);
738 /* Note: the DTD allows for vendor + brief but models only use
739 * vendor and everything else only uses shortDescription */
742 if (!*name || !strlen(*name)) {
743 log_err(ctx, "xml:%d: missing required element 'name'\n",
752 return true; /* only one configItem allowed in the dtd */
760 parse_model(struct rxkb_context *ctx, xmlNode *model,
761 enum rxkb_popularity popularity)
763 char *name, *description, *brief, *vendor;
765 if (parse_config_item(ctx, model, &name, &description, &brief, &vendor)) {
766 struct rxkb_model *m;
768 list_for_each(m, &ctx->models, base.link) {
769 if (streq(m->name, name)) {
779 m = rxkb_model_create(&ctx->base);
781 m->description = description;
783 m->popularity = popularity;
784 list_append(&ctx->models, &m->base.link);
789 parse_model_list(struct rxkb_context *ctx, xmlNode *model_list,
790 enum rxkb_popularity popularity)
792 xmlNode *node = NULL;
794 for (node = model_list->children; node; node = node->next) {
795 if (is_node(node, "model"))
796 parse_model(ctx, node, popularity);
801 parse_language_list(xmlNode *language_list, struct rxkb_layout *layout)
803 xmlNode *node = NULL;
804 struct rxkb_iso639_code *code;
806 for (node = language_list->children; node; node = node->next) {
807 if (is_node(node, "iso639Id")) {
808 char *str = extract_text(node);
809 struct rxkb_object *parent;
811 parent = &layout->base;
812 code = rxkb_iso639_code_create(parent);
814 list_append(&layout->iso639s, &code->base.link);
820 parse_country_list(xmlNode *country_list, struct rxkb_layout *layout)
822 xmlNode *node = NULL;
823 struct rxkb_iso3166_code *code;
825 for (node = country_list->children; node; node = node->next) {
826 if (is_node(node, "iso3166Id")) {
827 char *str = extract_text(node);
828 struct rxkb_object *parent;
830 parent = &layout->base;
831 code = rxkb_iso3166_code_create(parent);
833 list_append(&layout->iso3166s, &code->base.link);
839 parse_variant(struct rxkb_context *ctx, struct rxkb_layout *l,
840 xmlNode *variant, enum rxkb_popularity popularity)
843 char *name, *description, *brief, *vendor;
845 if (parse_config_item(ctx, variant, &name, &description, &brief, &vendor)) {
846 struct rxkb_layout *v;
849 list_for_each(v, &ctx->layouts, base.link) {
850 if (streq(v->name, name) && streq(v->name, l->name)) {
857 v = rxkb_layout_create(&ctx->base);
858 list_init(&v->iso639s);
859 list_init(&v->iso3166s);
860 v->name = strdup(l->name);
862 v->description = description;
864 v->popularity = popularity;
865 list_append(&ctx->layouts, &v->base.link);
867 for (ci = variant->children; ci; ci = ci->next) {
870 if (!is_node(ci, "configItem"))
873 for (node = ci->children; node; node = node->next) {
874 if (is_node(node, "languageList"))
875 parse_language_list(node, v);
876 if (is_node(node, "countryList"))
877 parse_country_list(node, v);
890 parse_variant_list(struct rxkb_context *ctx, struct rxkb_layout *l,
891 xmlNode *variant_list, enum rxkb_popularity popularity)
893 xmlNode *node = NULL;
895 for (node = variant_list->children; node; node = node->next) {
896 if (is_node(node, "variant"))
897 parse_variant(ctx, l, node, popularity);
902 parse_layout(struct rxkb_context *ctx, xmlNode *layout,
903 enum rxkb_popularity popularity)
905 char *name, *description, *brief, *vendor;
906 struct rxkb_layout *l;
907 xmlNode *node = NULL;
910 if (!parse_config_item(ctx, layout, &name, &description, &brief, &vendor))
913 list_for_each(l, &ctx->layouts, base.link) {
914 if (streq(l->name, name) && l->variant == NULL) {
921 l = rxkb_layout_create(&ctx->base);
922 list_init(&l->iso639s);
923 list_init(&l->iso3166s);
926 l->description = description;
928 l->popularity = popularity;
929 list_append(&ctx->layouts, &l->base.link);
937 for (node = layout->children; node; node = node->next) {
938 if (is_node(node, "variantList")) {
939 parse_variant_list(ctx, l, node, popularity);
941 if (!exists && is_node(node, "configItem")) {
943 for (ll = node->children; ll; ll = ll->next) {
944 if (is_node(ll, "languageList"))
945 parse_language_list(ll, l);
946 if (is_node(ll, "countryList"))
947 parse_country_list(ll, l);
954 parse_layout_list(struct rxkb_context *ctx, xmlNode *layout_list,
955 enum rxkb_popularity popularity)
957 xmlNode *node = NULL;
959 for (node = layout_list->children; node; node = node->next) {
960 if (is_node(node, "layout"))
961 parse_layout(ctx, node, popularity);
966 parse_option(struct rxkb_context *ctx, struct rxkb_option_group *group,
967 xmlNode *option, enum rxkb_popularity popularity)
969 char *name, *description, *brief, *vendor;
971 if (parse_config_item(ctx, option, &name, &description, &brief, &vendor)) {
972 struct rxkb_option *o;
974 list_for_each(o, &group->options, base.link) {
975 if (streq(o->name, name)) {
984 o = rxkb_option_create(&group->base);
986 o->description = description;
987 o->popularity = popularity;
988 list_append(&group->options, &o->base.link);
993 parse_group(struct rxkb_context *ctx, xmlNode *group,
994 enum rxkb_popularity popularity)
996 char *name, *description, *brief, *vendor;
997 struct rxkb_option_group *g;
998 xmlNode *node = NULL;
1000 bool exists = false;
1002 if (!parse_config_item(ctx, group, &name, &description, &brief, &vendor))
1005 list_for_each(g, &ctx->option_groups, base.link) {
1006 if (streq(g->name, name)) {
1013 g = rxkb_option_group_create(&ctx->base);
1015 g->description = description;
1016 g->popularity = popularity;
1018 multiple = xmlGetProp(group, (const xmlChar*)"allowMultipleSelection");
1019 if (multiple && xmlStrEqual(multiple, (const xmlChar*)"true"))
1020 g->allow_multiple = true;
1023 list_init(&g->options);
1024 list_append(&ctx->option_groups, &g->base.link);
1032 for (node = group->children; node; node = node->next) {
1033 if (is_node(node, "option"))
1034 parse_option(ctx, g, node, popularity);
1039 parse_option_list(struct rxkb_context *ctx, xmlNode *option_list,
1040 enum rxkb_popularity popularity)
1042 xmlNode *node = NULL;
1044 for (node = option_list->children; node; node = node->next) {
1045 if (is_node(node, "group"))
1046 parse_group(ctx, node, popularity);
1051 parse_rules_xml(struct rxkb_context *ctx, xmlNode *root,
1052 enum rxkb_popularity popularity)
1054 xmlNode *node = NULL;
1056 for (node = root->children; node; node = node->next) {
1057 if (is_node(node, "modelList"))
1058 parse_model_list(ctx, node, popularity);
1059 else if (is_node(node, "layoutList"))
1060 parse_layout_list(ctx, node, popularity);
1061 else if (is_node(node, "optionList"))
1062 parse_option_list(ctx, node, popularity);
1068 xml_error_func(void *ctx, const char *msg, ...)
1070 static char buf[PATH_MAX];
1071 static int slen = 0;
1075 /* libxml2 prints IO errors from bad includes paths by
1076 * calling the error function once per word. So we get to
1077 * re-assemble the message here and print it when we get
1078 * the line break. My enthusiasm about this is indescribable.
1080 va_start(args, msg);
1081 rc = vsnprintf(&buf[slen], sizeof(buf) - slen, msg, args);
1084 /* This shouldn't really happen */
1086 log_err(ctx, "+++ out of cheese error. redo from start +++\n");
1088 memset(buf, 0, sizeof(buf));
1093 if (slen >= (int)sizeof(buf)) {
1094 /* truncated, let's flush this */
1095 buf[sizeof(buf) - 1] = '\n';
1099 /* We're assuming here that the last character is \n. */
1100 if (buf[slen - 1] == '\n') {
1101 log_err(ctx, "%s", buf);
1102 memset(buf, 0, sizeof(buf));
1108 validate(struct rxkb_context *ctx, xmlDoc *doc)
1110 bool success = false;
1111 xmlValidCtxt *dtdvalid = NULL;
1113 xmlParserInputBufferPtr buf = NULL;
1114 /* This is a modified version of the xkeyboard-config xkb.dtd. That one
1115 * requires modelList, layoutList and optionList, we
1116 * allow for any of those to be missing.
1118 const char dtdstr[] =
1119 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1120 "<!ELEMENT xkbConfigRegistry (modelList?, layoutList?, optionList?)>\n"
1121 "<!ATTLIST xkbConfigRegistry version CDATA \"1.1\">\n"
1122 "<!ELEMENT modelList (model*)>\n"
1123 "<!ELEMENT model (configItem)>\n"
1124 "<!ELEMENT layoutList (layout*)>\n"
1125 "<!ELEMENT layout (configItem, variantList?)>\n"
1126 "<!ELEMENT optionList (group*)>\n"
1127 "<!ELEMENT variantList (variant*)>\n"
1128 "<!ELEMENT variant (configItem)>\n"
1129 "<!ELEMENT group (configItem, option*)>\n"
1130 "<!ATTLIST group allowMultipleSelection (true|false) \"false\">\n"
1131 "<!ELEMENT option (configItem)>\n"
1132 "<!ELEMENT configItem (name, shortDescription?, description?, vendor?, countryList?, languageList?, hwList?)>\n"
1133 "<!ATTLIST configItem popularity (standard|exotic) \"standard\">\n"
1134 "<!ELEMENT name (#PCDATA)>\n"
1135 "<!ELEMENT shortDescription (#PCDATA)>\n"
1136 "<!ELEMENT description (#PCDATA)>\n"
1137 "<!ELEMENT vendor (#PCDATA)>\n"
1138 "<!ELEMENT countryList (iso3166Id+)>\n"
1139 "<!ELEMENT iso3166Id (#PCDATA)>\n"
1140 "<!ELEMENT languageList (iso639Id+)>\n"
1141 "<!ELEMENT iso639Id (#PCDATA)>\n"
1142 "<!ELEMENT hwList (hwId+)>\n"
1143 "<!ELEMENT hwId (#PCDATA)>\n";
1145 /* Note: do not use xmlParserInputBufferCreateStatic, it generates random
1146 * DTD validity errors for unknown reasons */
1147 buf = xmlParserInputBufferCreateMem(dtdstr, sizeof(dtdstr),
1148 XML_CHAR_ENCODING_UTF8);
1152 dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_UTF8);
1154 log_err(ctx, "Failed to load DTD\n");
1158 dtdvalid = xmlNewValidCtxt();
1159 if (xmlValidateDtd(dtdvalid, doc, dtd))
1165 xmlFreeValidCtxt(dtdvalid);
1171 parse(struct rxkb_context *ctx, const char *path,
1172 enum rxkb_popularity popularity)
1174 bool success = false;
1176 xmlNode *root = NULL;
1178 if (!check_eaccess(path, R_OK))
1183 xmlSetGenericErrorFunc(ctx, xml_error_func);
1185 doc = xmlParseFile(path);
1189 if (!validate(ctx, doc)) {
1190 log_err(ctx, "XML error: failed to validate document at %s\n", path);
1194 root = xmlDocGetRootElement(doc);
1195 parse_rules_xml(ctx, root, popularity);