4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@googlemail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Implementation of the configuration parsers.
40 #include <xkbcommon/xkbcommon.h>
45 #define LOG_SUBSYSTEM "conf"
52 struct conf_option *opts;
57 int conf_ctx_new(struct conf_ctx **out, const struct conf_option *opts,
58 size_t onum, void *mem)
63 if (!out || !opts || !onum)
66 size = sizeof(*ctx) + onum * sizeof(*opts);
69 log_error("cannot allocate memory for config context");
73 ctx->opts = (void*)((char*)ctx + sizeof(*ctx));
76 memcpy(ctx->opts, opts, onum * sizeof(*opts));
84 void conf_ctx_free(struct conf_ctx *ctx)
93 void conf_ctx_reset(struct conf_ctx *ctx)
100 for (i = 0; i < ctx->onum; ++i) {
101 if (ctx->opts[i].type->free)
102 ctx->opts[i].type->free(&ctx->opts[i]);
103 ctx->opts[i].flags = 0;
104 if (ctx->opts[i].type->set_default)
105 ctx->opts[i].type->set_default(&ctx->opts[i]);
109 void *conf_ctx_get_mem(struct conf_ctx *ctx)
118 * Copy all entries from \src into \ctx
119 * This calls the "copy" callback for each option inside of \ctx with the
120 * corresponding option inside of \src if both have the same type. If the types
121 * do not match, nothing is done.
123 int conf_ctx_parse_ctx(struct conf_ctx *ctx, const struct conf_ctx *src)
126 struct conf_option *d, *s, *o;
132 for (i = 0; i < ctx->onum && i < src->onum; ++i) {
136 if (d->type != s->type)
138 if (d->flags & CONF_LOCKED)
141 if (s->flags & CONF_LOCKED)
142 d->flags |= CONF_LOCKED;
145 ret = d->type->copy(d, s);
157 for (i = 0; i < ctx->onum; ++i) {
160 ret = o->aftercheck(o, 0, NULL, 0);
170 * Parse command line arguments
171 * This temporarily allocates the short_options and long_options arrays so we
172 * can use the getopt_long() library call. It locks all arguments after they
173 * have been set so command-line options will always overwrite config-options.
175 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv)
178 struct option *long_options;
180 struct conf_option *o;
187 short_options = malloc(sizeof(char) * (ctx->onum + 1) * 2);
188 if (!short_options) {
189 log_error("out of memory to parse cmd-line arguments (%d): %m",
194 long_options = malloc(sizeof(struct option) * ctx->onum * 2);
196 log_error("out of memory to parse cmd-line arguments (%d): %m",
203 short_options[pos++] = ':';
205 for (i = 0; i < ctx->onum; ++i) {
206 if (ctx->opts[i].short_name) {
207 short_options[pos++] = ctx->opts[i].short_name;
208 if (ctx->opts[i].type->flags & CONF_HAS_ARG)
209 short_options[pos++] = ':';
212 if (ctx->opts[i].long_name) {
213 /* skip the "no-" prefix */
214 opt->name = &ctx->opts[i].long_name[3];
215 opt->has_arg = !!(ctx->opts[i].type->flags &
218 opt->val = 100000 + i;
221 /* boolean args are also added with "no-" prefix */
222 if (!(ctx->opts[i].type->flags & CONF_HAS_ARG)) {
223 opt->name = ctx->opts[i].long_name;
226 opt->val = 200000 + i;
231 short_options[pos++] = 0;
235 c = getopt_long(argc, argv, short_options,
239 } else if (c == ':') {
240 fprintf(stderr, "Missing argument for: %s\n",
243 } else if (c == '?') {
244 if (optopt && optopt < 100000)
245 fprintf(stderr, "Unknown argument: -%c\n",
248 fprintf(stderr, "Unknown argument: %s\n",
251 fprintf(stderr, "Option takes no arg: %s\n",
254 } else if (c < 100000) {
255 for (i = 0; i < ctx->onum; ++i) {
258 if (o->short_name != c)
261 if (o->type->parse) {
262 ret = o->type->parse(o, true, optarg);
267 o->flags |= CONF_LOCKED;
270 } else if (c < 200000) {
274 if (o->type->parse) {
275 ret = o->type->parse(o, true, optarg);
280 o->flags |= CONF_LOCKED;
285 if (o->type->parse) {
286 ret = o->type->parse(o, false, NULL);
291 o->flags |= CONF_LOCKED;
298 /* Perform aftercheck:
299 * All arguments that provide an aftercheck will be passed the remaining
300 * arguments in order. If they return a negative error code, it is
301 * interpreted as fatal error and returned to the caller. A positive
302 * error code is interpreted as the amount of remaining arguments that
303 * have been consumed by this aftercheck. 0 means nothing has been
305 * The next argument's aftercheck will only get the now remaning
306 * arguments passed in. If not all arguments are consumed, then this
307 * function will report an error to the caller. */
308 for (i = 0; i < ctx->onum; ++i) {
311 ret = o->aftercheck(o, argc, argv, optind);
319 fprintf(stderr, "Unparsed remaining args starting with: %s\n",
327 static int parse_kv_pair(struct conf_option *opts, size_t len,
328 const char *key, const char *value)
333 struct conf_option *opt;
335 for (i = 0; i < len; ++i) {
340 if (!strcmp(key, opt->long_name))
342 else if (!strcmp(key, &opt->long_name[3]))
347 /* ignore if already set by command-line arguments */
348 if (opt->flags & CONF_LOCKED)
352 ret = opt->file(opt, set, value);
358 if (opt->type->flags & CONF_HAS_ARG && !value) {
359 log_error("config option '%s' requires an argument",
362 } else if (!(opt->type->flags & CONF_HAS_ARG) && value) {
363 log_error("config option '%s' does not take arguments",
368 if (opt->type->parse) {
369 ret = opt->type->parse(opt, set, value);
377 log_error("unknown config option '%s'", key);
381 static void strip_spaces(char **buf)
385 while (**buf == ' ' || **buf == '\r' || **buf == '\t')
397 while (*tail == ' ' || *tail == '\r' || *tail == '\t')
401 static int parse_line(struct conf_option *opts, size_t olen,
402 char **buf, size_t *size)
429 } else if (*line == '\n') {
432 } else if (*line == '#') {
435 } else if (*line != '=') {
456 } else if (*line == '\n') {
459 } else if (*line == '#') {
481 strip_spaces(&value);
483 ret = parse_kv_pair(opts, olen, key, value);
499 static int parse_buffer(struct conf_option *opts, size_t len,
500 char *buf, size_t size)
503 struct conf_option *o;
506 while (!ret && size > 0)
507 ret = parse_line(opts, len, &buf, &size);
512 for (i = 0; i < len; ++i) {
515 ret = o->aftercheck(o, 0, NULL, 0);
524 /* chunk size when reading config files */
525 #define CONF_BUFSIZE 4096
527 /* This reads the file at \path in memory and parses it as if it was given as
528 * command line options. */
529 static int conf_parse_file(struct conf_option *opts, size_t len,
536 if (!opts || !len || !path)
539 if (access(path, F_OK))
542 if (access(path, R_OK)) {
543 log_error("read access to config file %s denied", path);
547 log_info("reading config file %s", path);
548 fd = open(path, O_RDONLY | O_CLOEXEC | O_NOCTTY);
550 log_error("cannot open %s (%d): %m", path, errno);
559 if (size - pos < CONF_BUFSIZE) {
560 tmp = realloc(buf, size + CONF_BUFSIZE + 1);
562 log_error("cannot allocate enough memory to parse config file %s (%d): %m",
568 size += CONF_BUFSIZE;
571 ret = read(fd, &buf[pos], CONF_BUFSIZE);
573 log_error("cannot read from config file %s (%d): %m",
582 ret = parse_buffer(opts, len, buf, pos);
590 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...)
599 va_start(list, format);
600 ret = vasprintf(&path, format, list);
604 log_error("cannot allocate memory for config-file path");
608 ret = conf_parse_file(ctx->opts, ctx->onum, path);
615 * Each option that can be parsed must be a specific config-type. A config-type
616 * is used to parse, free and reset the value. It must implement the following
618 * set_default: This should link opt->mem to opt->def and must not fail. It is
619 * called during initialization and reset.
620 * free: This should free any allocated memory and reset the option to the
621 * initial state. It must not fail.
622 * parse: This should parse a command-line option. Return 0 on success.
623 * copy: Copy data from source into destination. Return 0 on success.
625 * The backing memory is zeroed on reset so a config-type must be able to handle
626 * this as "not set". Also, the "free" callback should reset it to zero (which
627 * is the initial state).
630 /* Miscellaneous helper */
632 static void conf_free_value(struct conf_option *opt)
634 if (*(void**)opt->mem) {
635 if (*(void**)opt->mem != opt->def)
636 free(*(void**)opt->mem);
637 *(void**)opt->mem = NULL;
643 static void conf_default_bool(struct conf_option *opt)
645 *(bool*)opt->mem = (bool)opt->def;
648 static void conf_free_bool(struct conf_option *opt)
650 *(bool*)opt->mem = false;
653 static int conf_parse_bool(struct conf_option *opt, bool on, const char *arg)
655 *(bool*)opt->mem = on;
659 static int conf_copy_bool(struct conf_option *opt,
660 const struct conf_option *src)
662 *(bool*)opt->mem = *(bool*)src->mem;
666 const struct conf_type conf_bool = {
668 .set_default = conf_default_bool,
669 .free = conf_free_bool,
670 .parse = conf_parse_bool,
671 .copy = conf_copy_bool,
676 static int conf_parse_int(struct conf_option *opt, bool on, const char *arg)
678 *(int*)opt->mem = atoi(arg);
682 static void conf_free_int(struct conf_option *opt)
687 static void conf_default_int(struct conf_option *opt)
689 *(int*)opt->mem = (int)(unsigned long)opt->def;
692 static int conf_copy_int(struct conf_option *opt,
693 const struct conf_option *src)
695 *(int*)opt->mem = *(int*)src->mem;
699 const struct conf_type conf_int = {
700 .flags = CONF_HAS_ARG,
701 .set_default = conf_default_int,
702 .free = conf_free_int,
703 .parse = conf_parse_int,
704 .copy = conf_copy_int,
707 /* Unsigned Int Option */
709 static void conf_default_uint(struct conf_option *opt)
711 *(unsigned int*)opt->mem = (unsigned int)(unsigned long)opt->def;
714 static void conf_free_uint(struct conf_option *opt)
716 *(unsigned int*)opt->mem = 0;
719 static int conf_parse_uint(struct conf_option *opt, bool on, const char *arg)
721 *(unsigned int*)opt->mem = atoi(arg);
725 static int conf_copy_uint(struct conf_option *opt,
726 const struct conf_option *src)
728 *(unsigned int*)opt->mem = *(unsigned int*)src->mem;
732 const struct conf_type conf_uint = {
733 .flags = CONF_HAS_ARG,
734 .set_default = conf_default_uint,
735 .free = conf_free_uint,
736 .parse = conf_parse_uint,
737 .copy = conf_copy_uint,
742 static void conf_default_string(struct conf_option *opt)
744 opt->type->free(opt);
745 *(void**)opt->mem = opt->def;
748 static int conf_parse_string(struct conf_option *opt, bool on, const char *arg)
750 char *val = strdup(arg);
754 opt->type->free(opt);
755 *(void**)opt->mem = val;
759 static int conf_copy_string(struct conf_option *opt,
760 const struct conf_option *src)
764 if (!*(void**)src->mem) {
767 val = strdup(*(void**)src->mem);
772 opt->type->free(opt);
773 *(void**)opt->mem = val;
777 const struct conf_type conf_string = {
778 .flags = CONF_HAS_ARG,
779 .set_default = conf_default_string,
780 .free = conf_free_value,
781 .parse = conf_parse_string,
782 .copy = conf_copy_string,
785 /* Stringlist Option */
787 static void conf_default_string_list(struct conf_option *opt)
789 opt->type->free(opt);
790 *(void**)opt->mem = opt->def;
793 static int conf_parse_string_list(struct conf_option *opt, bool on,
799 ret = shl_split_string(arg, &list, NULL, ',', true);
803 opt->type->free(opt);
804 *(char***)opt->mem = list;
808 static int conf_copy_string_list(struct conf_option *opt,
809 const struct conf_option *src)
814 if (!(void***)src->mem) {
817 ret = shl_dup_array(&t, *(char***)src->mem);
822 opt->type->free(opt);
823 *(char***)opt->mem = t;
827 const struct conf_type conf_string_list = {
828 .flags = CONF_HAS_ARG,
829 .set_default = conf_default_string_list,
830 .free = conf_free_value,
831 .parse = conf_parse_string_list,
832 .copy = conf_copy_string_list,
837 static void conf_default_grab(struct conf_option *opt)
839 opt->type->free(opt);
840 *(void**)opt->mem = opt->def;
843 static void conf_free_grab(struct conf_option *opt)
845 struct conf_grab *grab;
848 grab = *(void**)opt->mem;
849 *(void**)opt->mem = NULL;
851 if (!grab || grab == opt->def)
854 for (i = 0; i < grab->num; ++i)
855 free(grab->keysyms[i]);
858 free(grab->num_syms);
863 static int parse_single_grab(char *arg, unsigned int *mods,
864 uint32_t *keysym, bool allow_mods)
866 char *tmp, *start, *end;
879 while (*tmp && *tmp != '>')
883 log_error("missing '>' near '%s'", start);
889 if (!strcasecmp(start, "shift")) {
890 *mods |= SHL_SHIFT_MASK;
891 } else if (!strcasecmp(start, "lock")) {
892 *mods |= SHL_LOCK_MASK;
893 } else if (!strcasecmp(start, "control") ||
894 !strcasecmp(start, "ctrl")) {
895 *mods |= SHL_CONTROL_MASK;
896 } else if (!strcasecmp(start, "alt")) {
897 *mods |= SHL_ALT_MASK;
898 } else if (!strcasecmp(start, "logo")) {
899 *mods |= SHL_LOGO_MASK;
901 log_error("invalid modifier '%s'", start);
912 while (*tmp && *tmp != ' ')
926 *keysym = xkb_keysym_from_name(start, 0);
928 *keysym = xkb_keysym_from_name(start,
929 XKB_KEYSYM_CASE_INSENSITIVE);
931 log_error("invalid key '%s'", start);
935 xkb_keysym_get_name(*keysym, buf, sizeof(buf));
936 log_warning("invalid keysym '%s', did you mean '%s'? (keysyms are case-sensitive)",
944 static int conf_parse_grab(struct conf_option *opt, bool on, const char *arg)
947 unsigned int list_num, key_num, i, j, k, l;
949 struct conf_grab *grab;
951 ret = shl_split_string(arg, &list, &list_num, ',', false);
955 grab = malloc(sizeof(*grab));
960 memset(grab, 0, sizeof(*grab));
963 grab->mods = malloc(sizeof(*grab->mods) * list_num);
968 memset(grab->mods, 0, sizeof(*grab->mods) * list_num);
970 grab->num_syms = malloc(sizeof(*grab->num_syms) * list_num);
971 if (!grab->num_syms) {
975 memset(grab->num_syms, 0, sizeof(*grab->num_syms) * list_num);
977 grab->keysyms = malloc(sizeof(*grab->keysyms) * list_num);
978 if (!grab->keysyms) {
982 memset(grab->keysyms, 0, sizeof(*grab->keysyms) * list_num);
986 for (i = 0; i < list_num; ++i) {
987 ret = shl_split_string(list[i], &keys, &key_num, '+', false);
995 grab->keysyms[l] = malloc(sizeof(*grab->keysyms[l]) * key_num);
996 if (!grab->keysyms[l]) {
1003 for (j = 0; j < key_num; ++j) {
1004 ret = parse_single_grab(keys[j], &grab->mods[l],
1005 &grab->keysyms[l][k],
1008 log_error("cannot parse grab '%s' in '%s'",
1019 grab->num_syms[l] = k;
1025 opt->type->free(opt);
1026 *(void**)opt->mem = grab;
1030 for (i = 0; i < list_num; ++i)
1031 free(grab->keysyms[i]);
1033 free(grab->keysyms);
1034 free(grab->num_syms);
1042 static int conf_copy_grab(struct conf_option *opt,
1043 const struct conf_option *src)
1045 struct conf_grab *grab, *s;
1049 s = *(void**)src->mem;
1052 opt->type->free(opt);
1053 *(void**)opt->mem = NULL;
1057 grab = malloc(sizeof(*grab));
1060 memset(grab, 0, sizeof(*grab));
1064 grab->mods = malloc(sizeof(*grab->mods) * grab->num);
1069 memcpy(grab->mods, s->mods, sizeof(*grab->mods) * grab->num);
1071 grab->num_syms = malloc(sizeof(*grab->num_syms) * grab->num);
1072 if (!grab->num_syms) {
1076 memcpy(grab->num_syms, s->num_syms,
1077 sizeof(*grab->num_syms) * grab->num);
1079 grab->keysyms = malloc(sizeof(*grab->keysyms) * grab->num);
1080 if (!grab->keysyms) {
1084 memset(grab->keysyms, 0, sizeof(*grab->keysyms) * grab->num);
1087 for (i = 0; i < grab->num; ++i) {
1088 grab->keysyms[i] = malloc(sizeof(*s->keysyms[i]) *
1090 if (!grab->keysyms[i]) {
1094 memcpy(grab->keysyms[i], s->keysyms[i],
1095 sizeof(*s->keysyms[i]) * s->num_syms[i]);
1098 opt->type->free(opt);
1099 *(void**)opt->mem = grab;
1103 for (i = 0; i < grab->num; ++i)
1104 free(grab->keysyms[i]);
1106 free(grab->keysyms);
1107 free(grab->num_syms);
1113 const struct conf_type conf_grab = {
1114 .flags = CONF_HAS_ARG,
1115 .set_default = conf_default_grab,
1116 .free = conf_free_grab,
1117 .parse = conf_parse_grab,
1118 .copy = conf_copy_grab,