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);
137 rxkb_log(struct rxkb_context *ctx, enum rxkb_log_level level,
138 const char *fmt, ...)
142 if (ctx->log_level < level)
146 ctx->log_fn(ctx, level, fmt, args);
151 * The format is not part of the argument list in order to avoid the
152 * "ISO C99 requires rest arguments to be used" warning when only the
153 * format is supplied without arguments. Not supplying it would still
154 * result in an error, though.
156 #define log_dbg(ctx, ...) \
157 rxkb_log((ctx), RXKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
158 #define log_info(ctx, ...) \
159 rxkb_log((ctx), RXKB_LOG_LEVEL_INFO, __VA_ARGS__)
160 #define log_warn(ctx, ...) \
161 rxkb_log((ctx), RXKB_LOG_LEVEL_WARNING, __VA_ARGS__)
162 #define log_err(ctx, ...) \
163 rxkb_log((ctx), RXKB_LOG_LEVEL_ERROR, __VA_ARGS__)
164 #define log_wsgo(ctx, ...) \
165 rxkb_log((ctx), RXKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
168 #define DECLARE_REF_UNREF_FOR_TYPE(type_) \
169 XKB_EXPORT struct type_ * type_##_ref(struct type_ *object) { \
170 rxkb_object_ref(&object->base); \
173 XKB_EXPORT struct type_ * type_##_unref(struct type_ *object) { \
174 if (!object) return NULL; \
175 return rxkb_object_unref(&object->base); \
178 #define DECLARE_CREATE_FOR_TYPE(type_) \
179 static inline struct type_ * type_##_create(struct rxkb_object *parent) { \
180 struct type_ *t = calloc(1, sizeof *t); \
182 rxkb_object_init(&t->base, parent, (destroy_func_t)type_##_destroy); \
186 #define DECLARE_TYPED_GETTER_FOR_TYPE(type_, field_, rtype_) \
187 XKB_EXPORT rtype_ type_##_get_##field_(struct type_ *object) { \
188 return object->field_; \
191 #define DECLARE_GETTER_FOR_TYPE(type_, field_) \
192 DECLARE_TYPED_GETTER_FOR_TYPE(type_, field_, const char*)
194 #define DECLARE_FIRST_NEXT_FOR_TYPE(type_, parent_type_, parent_field_) \
195 XKB_EXPORT struct type_ * type_##_first(struct parent_type_ *parent) { \
196 struct type_ *o = NULL; \
197 if (!list_empty(&parent->parent_field_)) \
198 o = list_first_entry(&parent->parent_field_, o, base.link); \
201 XKB_EXPORT struct type_ * \
202 type_##_next(struct type_ *o) \
204 struct parent_type_ *parent; \
205 struct type_ *next; \
206 parent = container_of(o->base.parent, struct parent_type_, base); \
207 next = list_first_entry(&o->base.link, o, base.link); \
208 if (list_is_last(&parent->parent_field_, &o->base.link)) \
214 rxkb_object_init(struct rxkb_object *object, struct rxkb_object *parent, destroy_func_t destroy)
216 object->refcount = 1;
217 object->destroy = destroy;
218 object->parent = parent;
219 list_init(&object->link);
223 rxkb_object_destroy(struct rxkb_object *object)
226 object->destroy(object);
227 list_remove(&object->link);
232 rxkb_object_ref(struct rxkb_object *object)
234 assert(object->refcount >= 1);
240 rxkb_object_unref(struct rxkb_object *object)
242 assert(object->refcount >= 1);
243 if (--object->refcount == 0)
244 rxkb_object_destroy(object);
249 rxkb_iso639_code_destroy(struct rxkb_iso639_code *code)
254 XKB_EXPORT struct rxkb_iso639_code *
255 rxkb_layout_get_iso639_first(struct rxkb_layout *layout)
257 struct rxkb_iso639_code *code = NULL;
259 if (!list_empty(&layout->iso639s))
260 code = list_first_entry(&layout->iso639s, code, base.link);
265 XKB_EXPORT struct rxkb_iso639_code *
266 rxkb_iso639_code_next(struct rxkb_iso639_code *code)
268 struct rxkb_iso639_code *next = NULL;
269 struct rxkb_layout *layout;
271 layout = container_of(code->base.parent, struct rxkb_layout, base);
273 if (list_is_last(&layout->iso639s, &code->base.link))
276 next = list_first_entry(&code->base.link, code, base.link);
281 DECLARE_REF_UNREF_FOR_TYPE(rxkb_iso639_code);
282 DECLARE_CREATE_FOR_TYPE(rxkb_iso639_code);
283 DECLARE_GETTER_FOR_TYPE(rxkb_iso639_code, code);
286 rxkb_iso3166_code_destroy(struct rxkb_iso3166_code *code)
291 XKB_EXPORT struct rxkb_iso3166_code *
292 rxkb_layout_get_iso3166_first(struct rxkb_layout *layout)
294 struct rxkb_iso3166_code *code = NULL;
296 if (!list_empty(&layout->iso3166s))
297 code = list_first_entry(&layout->iso3166s, code, base.link);
302 XKB_EXPORT struct rxkb_iso3166_code *
303 rxkb_iso3166_code_next(struct rxkb_iso3166_code *code)
305 struct rxkb_iso3166_code *next = NULL;
306 struct rxkb_layout *layout;
308 layout = container_of(code->base.parent, struct rxkb_layout, base);
310 if (list_is_last(&layout->iso3166s, &code->base.link))
313 next = list_first_entry(&code->base.link, code, base.link);
318 DECLARE_REF_UNREF_FOR_TYPE(rxkb_iso3166_code);
319 DECLARE_CREATE_FOR_TYPE(rxkb_iso3166_code);
320 DECLARE_GETTER_FOR_TYPE(rxkb_iso3166_code, code);
323 rxkb_option_destroy(struct rxkb_option *o)
327 free(o->description);
330 DECLARE_REF_UNREF_FOR_TYPE(rxkb_option);
331 DECLARE_CREATE_FOR_TYPE(rxkb_option);
332 DECLARE_GETTER_FOR_TYPE(rxkb_option, name);
333 DECLARE_GETTER_FOR_TYPE(rxkb_option, brief);
334 DECLARE_GETTER_FOR_TYPE(rxkb_option, description);
335 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_option, popularity, enum rxkb_popularity);
336 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_option, rxkb_option_group, options);
339 rxkb_layout_destroy(struct rxkb_layout *l)
341 struct rxkb_iso639_code *iso639, *tmp_639;
342 struct rxkb_iso3166_code *iso3166, *tmp_3166;
346 free(l->description);
349 list_for_each_safe(iso639, tmp_639, &l->iso639s, base.link) {
350 rxkb_iso639_code_unref(iso639);
352 list_for_each_safe(iso3166, tmp_3166, &l->iso3166s, base.link) {
353 rxkb_iso3166_code_unref(iso3166);
357 DECLARE_REF_UNREF_FOR_TYPE(rxkb_layout);
358 DECLARE_CREATE_FOR_TYPE(rxkb_layout);
359 DECLARE_GETTER_FOR_TYPE(rxkb_layout, name);
360 DECLARE_GETTER_FOR_TYPE(rxkb_layout, brief);
361 DECLARE_GETTER_FOR_TYPE(rxkb_layout, description);
362 DECLARE_GETTER_FOR_TYPE(rxkb_layout, variant);
363 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_layout, popularity, enum rxkb_popularity);
364 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_layout, rxkb_context, layouts);
367 rxkb_model_destroy(struct rxkb_model *m)
371 free(m->description);
374 DECLARE_REF_UNREF_FOR_TYPE(rxkb_model);
375 DECLARE_CREATE_FOR_TYPE(rxkb_model);
376 DECLARE_GETTER_FOR_TYPE(rxkb_model, name);
377 DECLARE_GETTER_FOR_TYPE(rxkb_model, vendor);
378 DECLARE_GETTER_FOR_TYPE(rxkb_model, description);
379 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_model, popularity, enum rxkb_popularity);
380 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_model, rxkb_context, models);
383 rxkb_option_group_destroy(struct rxkb_option_group *og)
385 struct rxkb_option *o, *otmp;
388 free(og->description);
390 list_for_each_safe(o, otmp, &og->options, base.link) {
391 rxkb_option_unref(o);
396 rxkb_option_group_allows_multiple(struct rxkb_option_group *g)
398 return g->allow_multiple;
401 DECLARE_REF_UNREF_FOR_TYPE(rxkb_option_group);
402 DECLARE_CREATE_FOR_TYPE(rxkb_option_group);
403 DECLARE_GETTER_FOR_TYPE(rxkb_option_group, name);
404 DECLARE_GETTER_FOR_TYPE(rxkb_option_group, description);
405 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_option_group, popularity, enum rxkb_popularity);
406 DECLARE_FIRST_NEXT_FOR_TYPE(rxkb_option_group, rxkb_context, option_groups);
409 rxkb_context_destroy(struct rxkb_context *ctx)
411 struct rxkb_model *m, *mtmp;
412 struct rxkb_layout *l, *ltmp;
413 struct rxkb_option_group *og, *ogtmp;
416 list_for_each_safe(m, mtmp, &ctx->models, base.link)
418 assert(list_empty(&ctx->models));
420 list_for_each_safe(l, ltmp, &ctx->layouts, base.link)
421 rxkb_layout_unref(l);
422 assert(list_empty(&ctx->layouts));
424 list_for_each_safe(og, ogtmp, &ctx->option_groups, base.link)
425 rxkb_option_group_unref(og);
426 assert(list_empty(&ctx->option_groups));
428 darray_foreach(path, ctx->includes)
430 darray_free(ctx->includes);
432 assert(darray_empty(ctx->includes));
435 DECLARE_REF_UNREF_FOR_TYPE(rxkb_context);
436 DECLARE_CREATE_FOR_TYPE(rxkb_context);
437 DECLARE_TYPED_GETTER_FOR_TYPE(rxkb_context, log_level, enum rxkb_log_level);
440 rxkb_context_set_log_level(struct rxkb_context *ctx,
441 enum rxkb_log_level level)
443 ctx->log_level = level;
447 log_level_to_prefix(enum rxkb_log_level level)
450 case RXKB_LOG_LEVEL_DEBUG:
451 return "xkbregistry: DEBUG: ";
452 case RXKB_LOG_LEVEL_INFO:
453 return "xkbregistry: INFO: ";
454 case RXKB_LOG_LEVEL_WARNING:
455 return "xkbregistry: WARNING: ";
456 case RXKB_LOG_LEVEL_ERROR:
457 return "xkbregistry: ERROR: ";
458 case RXKB_LOG_LEVEL_CRITICAL:
459 return "xkbregistry: CRITICAL: ";
465 ATTR_PRINTF(3, 0) static void
466 default_log_fn(struct rxkb_context *ctx, enum rxkb_log_level level,
467 const char *fmt, va_list args)
469 const char *prefix = log_level_to_prefix(level);
472 fprintf(stderr, "%s", prefix);
473 vfprintf(stderr, fmt, args);
476 static enum rxkb_log_level
477 log_level(const char *level) {
479 enum rxkb_log_level lvl;
482 lvl = strtol(level, &endptr, 10);
483 if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
485 if (istreq_prefix("crit", level))
486 return RXKB_LOG_LEVEL_CRITICAL;
487 if (istreq_prefix("err", level))
488 return RXKB_LOG_LEVEL_ERROR;
489 if (istreq_prefix("warn", level))
490 return RXKB_LOG_LEVEL_WARNING;
491 if (istreq_prefix("info", level))
492 return RXKB_LOG_LEVEL_INFO;
493 if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
494 return RXKB_LOG_LEVEL_DEBUG;
496 return RXKB_LOG_LEVEL_ERROR;
499 XKB_EXPORT struct rxkb_context *
500 rxkb_context_new(enum rxkb_context_flags flags)
502 struct rxkb_context *ctx = rxkb_context_create(NULL);
508 ctx->context_state = CONTEXT_NEW;
509 ctx->load_extra_rules_files = flags & RXKB_CONTEXT_LOAD_EXOTIC_RULES;
510 ctx->log_fn = default_log_fn;
511 ctx->log_level = RXKB_LOG_LEVEL_ERROR;
513 /* Environment overwrites defaults. */
514 env = secure_getenv("RXKB_LOG_LEVEL");
516 rxkb_context_set_log_level(ctx, log_level(env));
518 list_init(&ctx->models);
519 list_init(&ctx->layouts);
520 list_init(&ctx->option_groups);
522 if (!(flags & RXKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
523 !rxkb_context_include_path_append_default(ctx)) {
524 rxkb_context_unref(ctx);
532 rxkb_context_set_log_fn(struct rxkb_context *ctx,
533 void (*log_fn)(struct rxkb_context *ctx,
534 enum rxkb_log_level level,
535 const char *fmt, va_list args))
537 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
541 rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path)
543 struct stat stat_buf;
546 char rules[PATH_MAX];
548 if (ctx->context_state != CONTEXT_NEW) {
549 log_err(ctx, "include paths can only be appended to a new context\n");
557 err = stat(path, &stat_buf);
560 if (!S_ISDIR(stat_buf.st_mode))
563 if (!check_eaccess(path, R_OK | X_OK))
566 /* Pre-filter for the 99.9% case - if we can't assemble the default ruleset
567 * path, complain here instead of during parsing later. The niche cases
568 * where this is the wrong behaviour aren't worth worrying about.
570 if (!snprintf_safe(rules, sizeof(rules), "%s/rules/%s.xml",
571 path, DEFAULT_XKB_RULES))
574 darray_append(ctx->includes, tmp);
584 rxkb_context_include_path_append_default(struct rxkb_context *ctx)
586 const char *home, *xdg, *root, *extra;
590 if (ctx->context_state != CONTEXT_NEW) {
591 log_err(ctx, "include paths can only be appended to a new context\n");
595 home = secure_getenv("HOME");
597 xdg = secure_getenv("XDG_CONFIG_HOME");
599 user_path = asprintf_safe("%s/xkb", xdg);
601 ret |= rxkb_context_include_path_append(ctx, user_path);
604 } else if (home != NULL) {
605 /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
606 user_path = asprintf_safe("%s/.config/xkb", home);
608 ret |= rxkb_context_include_path_append(ctx, user_path);
614 user_path = asprintf_safe("%s/.xkb", home);
616 ret |= rxkb_context_include_path_append(ctx, user_path);
621 extra = secure_getenv("XKB_CONFIG_EXTRA_PATH");
623 ret |= rxkb_context_include_path_append(ctx, extra);
625 ret |= rxkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_EXTRA_PATH);
627 root = secure_getenv("XKB_CONFIG_ROOT");
629 ret |= rxkb_context_include_path_append(ctx, root);
631 ret |= rxkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
637 rxkb_context_parse_default_ruleset(struct rxkb_context *ctx)
639 return rxkb_context_parse(ctx, DEFAULT_XKB_RULES);
643 rxkb_context_parse(struct rxkb_context *ctx, const char *ruleset)
646 bool success = false;
648 if (ctx->context_state != CONTEXT_NEW) {
649 log_err(ctx, "parse must only be called on a new context\n");
653 darray_foreach_reverse(path, ctx->includes) {
654 char rules[PATH_MAX];
656 if (snprintf_safe(rules, sizeof(rules), "%s/rules/%s.xml",
658 log_dbg(ctx, "Parsing %s\n", rules);
659 if (parse(ctx, rules, RXKB_POPULARITY_STANDARD))
663 if (ctx->load_extra_rules_files &&
664 snprintf_safe(rules, sizeof(rules), "%s/rules/%s.extras.xml",
666 log_dbg(ctx, "Parsing %s\n", rules);
667 if (parse(ctx, rules, RXKB_POPULARITY_EXOTIC))
672 ctx->context_state = success ? CONTEXT_PARSED : CONTEXT_FAILED;
679 rxkb_context_set_user_data(struct rxkb_context *ctx, void *userdata)
681 ctx->userdata = userdata;
685 rxkb_context_get_user_data(struct rxkb_context *ctx)
687 return ctx->userdata;
691 is_node(xmlNode *node, const char *name)
693 return node->type == XML_ELEMENT_NODE &&
694 xmlStrEqual(node->name, (const xmlChar*)name);
697 /* return a copy of the text content from the first text node of this node */
699 extract_text(xmlNode *node)
703 for (n = node->children; n; n = n->next) {
704 if (n->type == XML_TEXT_NODE)
705 return (char *)xmlStrdup(n->content);
711 parse_config_item(struct rxkb_context *ctx,
718 xmlNode *node = NULL;
721 for (ci = parent->children; ci; ci = ci->next) {
722 if (is_node(ci, "configItem")) {
728 for (node = ci->children; node; node = node->next) {
729 if (is_node(node, "name"))
730 *name = extract_text(node);
731 else if (is_node(node, "description"))
732 *description = extract_text(node);
733 else if (is_node(node, "shortDescription"))
734 *brief = extract_text(node);
735 else if (is_node(node, "vendor"))
736 *vendor = extract_text(node);
737 /* Note: the DTD allows for vendor + brief but models only use
738 * vendor and everything else only uses shortDescription */
741 if (!*name || !strlen(*name)) {
742 log_err(ctx, "xml:%d: missing required element 'name'\n",
747 return true; /* only one configItem allowed in the dtd */
755 parse_model(struct rxkb_context *ctx, xmlNode *model,
756 enum rxkb_popularity popularity)
758 char *name, *description, *brief, *vendor;
760 if (parse_config_item(ctx, model, &name, &description, &brief, &vendor)) {
761 struct rxkb_model *m;
763 list_for_each(m, &ctx->models, base.link) {
764 if (streq(m->name, name)) {
774 m = rxkb_model_create(&ctx->base);
776 m->description = description;
778 m->popularity = popularity;
779 list_append(&ctx->models, &m->base.link);
784 parse_model_list(struct rxkb_context *ctx, xmlNode *model_list,
785 enum rxkb_popularity popularity)
787 xmlNode *node = NULL;
789 for (node = model_list->children; node; node = node->next) {
790 if (is_node(node, "model"))
791 parse_model(ctx, node, popularity);
796 parse_language_list(xmlNode *language_list, struct rxkb_layout *layout)
798 xmlNode *node = NULL;
799 struct rxkb_iso639_code *code;
801 for (node = language_list->children; node; node = node->next) {
802 if (is_node(node, "iso639Id")) {
803 char *str = extract_text(node);
804 struct rxkb_object *parent;
806 parent = &layout->base;
807 code = rxkb_iso639_code_create(parent);
809 list_append(&layout->iso639s, &code->base.link);
815 parse_country_list(xmlNode *country_list, struct rxkb_layout *layout)
817 xmlNode *node = NULL;
818 struct rxkb_iso3166_code *code;
820 for (node = country_list->children; node; node = node->next) {
821 if (is_node(node, "iso3166Id")) {
822 char *str = extract_text(node);
823 struct rxkb_object *parent;
825 parent = &layout->base;
826 code = rxkb_iso3166_code_create(parent);
828 list_append(&layout->iso3166s, &code->base.link);
834 parse_variant(struct rxkb_context *ctx, struct rxkb_layout *l,
835 xmlNode *variant, enum rxkb_popularity popularity)
838 char *name, *description, *brief, *vendor;
840 if (parse_config_item(ctx, variant, &name, &description, &brief, &vendor)) {
841 struct rxkb_layout *v;
844 list_for_each(v, &ctx->layouts, base.link) {
845 if (streq(v->name, name) && streq(v->name, l->name)) {
852 v = rxkb_layout_create(&ctx->base);
853 list_init(&v->iso639s);
854 list_init(&v->iso3166s);
855 v->name = strdup(l->name);
857 v->description = description;
859 v->popularity = popularity;
860 list_append(&ctx->layouts, &v->base.link);
862 for (ci = variant->children; ci; ci = ci->next) {
865 if (!is_node(ci, "configItem"))
868 for (node = ci->children; node; node = node->next) {
869 if (is_node(node, "languageList"))
870 parse_language_list(node, v);
871 if (is_node(node, "countryList"))
872 parse_country_list(node, v);
885 parse_variant_list(struct rxkb_context *ctx, struct rxkb_layout *l,
886 xmlNode *variant_list, enum rxkb_popularity popularity)
888 xmlNode *node = NULL;
890 for (node = variant_list->children; node; node = node->next) {
891 if (is_node(node, "variant"))
892 parse_variant(ctx, l, node, popularity);
897 parse_layout(struct rxkb_context *ctx, xmlNode *layout,
898 enum rxkb_popularity popularity)
900 char *name, *description, *brief, *vendor;
901 struct rxkb_layout *l;
902 xmlNode *node = NULL;
905 if (!parse_config_item(ctx, layout, &name, &description, &brief, &vendor))
908 list_for_each(l, &ctx->layouts, base.link) {
909 if (streq(l->name, name) && l->variant == NULL) {
916 l = rxkb_layout_create(&ctx->base);
917 list_init(&l->iso639s);
918 list_init(&l->iso3166s);
921 l->description = description;
923 l->popularity = popularity;
924 list_append(&ctx->layouts, &l->base.link);
932 for (node = layout->children; node; node = node->next) {
933 if (is_node(node, "variantList")) {
934 parse_variant_list(ctx, l, node, popularity);
936 if (!exists && is_node(node, "configItem")) {
938 for (ll = node->children; ll; ll = ll->next) {
939 if (is_node(ll, "languageList"))
940 parse_language_list(ll, l);
941 if (is_node(ll, "countryList"))
942 parse_country_list(ll, l);
949 parse_layout_list(struct rxkb_context *ctx, xmlNode *layout_list,
950 enum rxkb_popularity popularity)
952 xmlNode *node = NULL;
954 for (node = layout_list->children; node; node = node->next) {
955 if (is_node(node, "layout"))
956 parse_layout(ctx, node, popularity);
961 parse_option(struct rxkb_context *ctx, struct rxkb_option_group *group,
962 xmlNode *option, enum rxkb_popularity popularity)
964 char *name, *description, *brief, *vendor;
966 if (parse_config_item(ctx, option, &name, &description, &brief, &vendor)) {
967 struct rxkb_option *o;
969 list_for_each(o, &group->options, base.link) {
970 if (streq(o->name, name)) {
979 o = rxkb_option_create(&group->base);
981 o->description = description;
982 o->popularity = popularity;
983 list_append(&group->options, &o->base.link);
988 parse_group(struct rxkb_context *ctx, xmlNode *group,
989 enum rxkb_popularity popularity)
991 char *name, *description, *brief, *vendor;
992 struct rxkb_option_group *g;
993 xmlNode *node = NULL;
997 if (!parse_config_item(ctx, group, &name, &description, &brief, &vendor))
1000 list_for_each(g, &ctx->option_groups, base.link) {
1001 if (streq(g->name, name)) {
1008 g = rxkb_option_group_create(&ctx->base);
1010 g->description = description;
1011 g->popularity = popularity;
1013 multiple = xmlGetProp(group, (const xmlChar*)"allowMultipleSelection");
1014 if (multiple && xmlStrEqual(multiple, (const xmlChar*)"true"))
1015 g->allow_multiple = true;
1018 list_init(&g->options);
1019 list_append(&ctx->option_groups, &g->base.link);
1027 for (node = group->children; node; node = node->next) {
1028 if (is_node(node, "option"))
1029 parse_option(ctx, g, node, popularity);
1034 parse_option_list(struct rxkb_context *ctx, xmlNode *option_list,
1035 enum rxkb_popularity popularity)
1037 xmlNode *node = NULL;
1039 for (node = option_list->children; node; node = node->next) {
1040 if (is_node(node, "group"))
1041 parse_group(ctx, node, popularity);
1046 parse_rules_xml(struct rxkb_context *ctx, xmlNode *root,
1047 enum rxkb_popularity popularity)
1049 xmlNode *node = NULL;
1051 for (node = root->children; node; node = node->next) {
1052 if (is_node(node, "modelList"))
1053 parse_model_list(ctx, node, popularity);
1054 else if (is_node(node, "layoutList"))
1055 parse_layout_list(ctx, node, popularity);
1056 else if (is_node(node, "optionList"))
1057 parse_option_list(ctx, node, popularity);
1063 xml_error_func(void *ctx, const char *msg, ...)
1065 static char buf[PATH_MAX];
1066 static int slen = 0;
1070 /* libxml2 prints IO errors from bad includes paths by
1071 * calling the error function once per word. So we get to
1072 * re-assemble the message here and print it when we get
1073 * the line break. My enthusiasm about this is indescribable.
1075 va_start(args, msg);
1076 rc = vsnprintf(&buf[slen], sizeof(buf) - slen, msg, args);
1079 /* This shouldn't really happen */
1081 log_err(ctx, "+++ out of cheese error. redo from start +++\n");
1083 memset(buf, 0, sizeof(buf));
1088 if (slen >= (int)sizeof(buf)) {
1089 /* truncated, let's flush this */
1090 buf[sizeof(buf) - 1] = '\n';
1094 /* We're assuming here that the last character is \n. */
1095 if (buf[slen - 1] == '\n') {
1096 log_err(ctx, "%s", buf);
1097 memset(buf, 0, sizeof(buf));
1103 validate(struct rxkb_context *ctx, xmlDoc *doc)
1105 bool success = false;
1106 xmlValidCtxt *dtdvalid = NULL;
1108 xmlParserInputBufferPtr buf = NULL;
1109 /* This is a modified version of the xkeyboard-config xkb.dtd. That one
1110 * requires modelList, layoutList and optionList, we
1111 * allow for any of those to be missing.
1113 const char dtdstr[] =
1114 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1115 "<!ELEMENT xkbConfigRegistry (modelList?, layoutList?, optionList?)>\n"
1116 "<!ATTLIST xkbConfigRegistry version CDATA \"1.1\">\n"
1117 "<!ELEMENT modelList (model*)>\n"
1118 "<!ELEMENT model (configItem)>\n"
1119 "<!ELEMENT layoutList (layout*)>\n"
1120 "<!ELEMENT layout (configItem, variantList?)>\n"
1121 "<!ELEMENT optionList (group*)>\n"
1122 "<!ELEMENT variantList (variant*)>\n"
1123 "<!ELEMENT variant (configItem)>\n"
1124 "<!ELEMENT group (configItem, option*)>\n"
1125 "<!ATTLIST group allowMultipleSelection (true|false) \"false\">\n"
1126 "<!ELEMENT option (configItem)>\n"
1127 "<!ELEMENT configItem (name, shortDescription?, description?, vendor?, countryList?, languageList?, hwList?)>\n"
1128 "<!ATTLIST configItem popularity (standard|exotic) \"standard\">\n"
1129 "<!ELEMENT name (#PCDATA)>\n"
1130 "<!ELEMENT shortDescription (#PCDATA)>\n"
1131 "<!ELEMENT description (#PCDATA)>\n"
1132 "<!ELEMENT vendor (#PCDATA)>\n"
1133 "<!ELEMENT countryList (iso3166Id+)>\n"
1134 "<!ELEMENT iso3166Id (#PCDATA)>\n"
1135 "<!ELEMENT languageList (iso639Id+)>\n"
1136 "<!ELEMENT iso639Id (#PCDATA)>\n"
1137 "<!ELEMENT hwList (hwId+)>\n"
1138 "<!ELEMENT hwId (#PCDATA)>\n";
1140 /* Note: do not use xmlParserInputBufferCreateStatic, it generates random
1141 * DTD validity errors for unknown reasons */
1142 buf = xmlParserInputBufferCreateMem(dtdstr, sizeof(dtdstr),
1143 XML_CHAR_ENCODING_UTF8);
1147 dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_UTF8);
1149 log_err(ctx, "Failed to load DTD\n");
1153 dtdvalid = xmlNewValidCtxt();
1154 if (xmlValidateDtd(dtdvalid, doc, dtd))
1160 xmlFreeValidCtxt(dtdvalid);
1166 parse(struct rxkb_context *ctx, const char *path,
1167 enum rxkb_popularity popularity)
1169 bool success = false;
1171 xmlNode *root = NULL;
1173 if (!check_eaccess(path, R_OK))
1178 xmlSetGenericErrorFunc(ctx, xml_error_func);
1180 doc = xmlParseFile(path);
1184 if (!validate(ctx, doc)) {
1185 log_err(ctx, "XML error: failed to validate document at %s\n", path);
1189 root = xmlDocGetRootElement(doc);
1190 parse_rules_xml(ctx, root, popularity);