4 * Copyright (c) 2012 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 "config"
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;
132 for (i = 0; i < ctx->onum && i < src->onum; ++i) {
136 if (d->type != s->type)
141 ret = d->type->copy(d, s);
150 * Parse command line arguments
151 * This temporarily allocates the short_options and long_options arrays so we
152 * can use the getopt_long() library call. It locks all arguments after they
153 * have been set so command-line options will always overwrite config-options.
155 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv)
158 struct option *long_options;
160 struct conf_option *o;
167 short_options = malloc(sizeof(char) * (ctx->onum + 1) * 2);
168 if (!short_options) {
169 log_error("out of memory to parse cmd-line arguments (%d): %m",
174 long_options = malloc(sizeof(struct option) * ctx->onum * 2);
176 log_error("out of memory to parse cmd-line arguments (%d): %m",
183 short_options[pos++] = ':';
185 for (i = 0; i < ctx->onum; ++i) {
186 if (ctx->opts[i].short_name) {
187 short_options[pos++] = ctx->opts[i].short_name;
188 if (ctx->opts[i].type->flags & CONF_HAS_ARG)
189 short_options[pos++] = ':';
192 if (ctx->opts[i].long_name) {
193 /* skip the "no-" prefix */
194 opt->name = &ctx->opts[i].long_name[3];
195 opt->has_arg = !!(ctx->opts[i].type->flags &
198 opt->val = 100000 + i;
201 /* boolean args are also added with "no-" prefix */
202 if (!(ctx->opts[i].type->flags & CONF_HAS_ARG)) {
203 opt->name = ctx->opts[i].long_name;
206 opt->val = 200000 + i;
211 short_options[pos++] = 0;
215 c = getopt_long(argc, argv, short_options,
219 } else if (c == ':') {
220 fprintf(stderr, "Missing argument for: %s\n",
223 } else if (c == '?') {
224 if (optopt && optopt < 100000)
225 fprintf(stderr, "Unknown argument: -%c\n",
228 fprintf(stderr, "Unknown argument: %s\n",
231 fprintf(stderr, "Option takes no arg: %s\n",
234 } else if (c < 100000) {
235 for (i = 0; i < ctx->onum; ++i) {
238 if (o->short_name != c)
241 if (o->type->parse) {
242 ret = o->type->parse(o, true, optarg);
247 o->flags |= CONF_LOCKED;
248 o->flags |= CONF_DONE;
251 } else if (c < 200000) {
255 if (o->type->parse) {
256 ret = o->type->parse(o, true, optarg);
261 o->flags |= CONF_LOCKED;
262 o->flags |= CONF_DONE;
267 if (o->type->parse) {
268 ret = o->type->parse(o, false, NULL);
273 o->flags |= CONF_LOCKED;
274 o->flags |= CONF_DONE;
281 /* Perform aftercheck:
282 * All arguments that provide an aftercheck will be passed the remaining
283 * arguments in order. If they return a negative error code, it is
284 * interpreted as fatal error and returned to the caller. A positive
285 * error code is interpreted as the amount of remaining arguments that
286 * have been consumed by this aftercheck. 0 means nothing has been
288 * The next argument's aftercheck will only get the now remaning
289 * arguments passed in. If not all arguments are consumed, then this
290 * function will report an error to the caller. */
291 for (i = 0; i < ctx->onum; ++i) {
294 ret = o->aftercheck(o, argc, argv, optind);
302 fprintf(stderr, "Unparsed remaining args starting with: %s\n",
310 static int parse_kv_pair(struct conf_option *opts, size_t len,
311 const char *key, const char *value)
316 struct conf_option *opt;
318 for (i = 0; i < len; ++i) {
323 if (!strcmp(key, opt->long_name))
325 else if (!strcmp(key, &opt->long_name[3]))
330 if (opt->type->flags & CONF_HAS_ARG && !value) {
331 log_error("config option '%s' requires an argument",
334 } else if (!(opt->type->flags & CONF_HAS_ARG) && value) {
335 log_error("config option '%s' does not take arguments",
340 /* ignore if already set by command-line arguments */
341 if (opt->flags & CONF_LOCKED)
344 if (opt->type->parse) {
345 ret = opt->type->parse(opt, set, value);
350 opt->flags |= CONF_DONE;
354 log_error("unknown config option '%s'", key);
358 static void strip_spaces(char **buf)
362 while (**buf == ' ' || **buf == '\r' || **buf == '\t')
374 while (*tail == ' ' || *tail == '\r' || *tail == '\t')
378 static int parse_line(struct conf_option *opts, size_t olen,
379 char **buf, size_t *size)
406 } else if (*line == '\n') {
409 } else if (*line == '#') {
412 } else if (*line != '=') {
433 } else if (*line == '\n') {
436 } else if (*line == '#') {
458 strip_spaces(&value);
460 ret = parse_kv_pair(opts, olen, key, value);
476 static int parse_buffer(struct conf_option *opts, size_t len,
477 char *buf, size_t size)
481 while (!ret && size > 0)
482 ret = parse_line(opts, len, &buf, &size);
487 /* chunk size when reading config files */
488 #define CONF_BUFSIZE 4096
490 /* This reads the file at \path in memory and parses it as if it was given as
491 * command line options. */
492 static int conf_parse_file(struct conf_option *opts, size_t len,
499 if (!opts || !len || !path)
502 if (access(path, F_OK))
505 if (access(path, R_OK)) {
506 log_error("read access to config file %s denied", path);
510 log_info("reading config file %s", path);
511 fd = open(path, O_RDONLY | O_CLOEXEC | O_NOCTTY);
513 log_error("cannot open %s (%d): %m", path, errno);
522 if (size - pos < CONF_BUFSIZE) {
523 tmp = realloc(buf, size + CONF_BUFSIZE + 1);
525 log_error("cannot allocate enough memory to parse config file %s (%d): %m",
531 size += CONF_BUFSIZE;
534 ret = read(fd, &buf[pos], CONF_BUFSIZE);
536 log_error("cannot read from config file %s (%d): %m",
545 ret = parse_buffer(opts, len, buf, pos);
553 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...)
562 va_start(list, format);
563 ret = vasprintf(&path, format, list);
567 log_error("cannot allocate memory for config-file path");
571 ret = conf_parse_file(ctx->opts, ctx->onum, path);
578 * Each option that can be parsed must be a specific config-type. A config-type
579 * is used to parse, free and reset the value. It must implement the following
581 * set_default: This should link opt->mem to opt->def and must not fail. It is
582 * called during initialization and reset.
583 * free: This should free any allocated memory and reset the option to the
584 * initial state. It must not fail.
585 * parse: This should parse a command-line option. Return 0 on success.
586 * copy: Copy data from source into destination. Return 0 on success.
588 * The backing memory is zeroed on reset so a config-type must be able to handle
589 * this as "not set". Also, the "free" callback should reset it to zero (which
590 * is the initial state).
593 /* Miscellaneous helper */
595 static void conf_free_value(struct conf_option *opt)
597 if (*(void**)opt->mem) {
598 if (*(void**)opt->mem != opt->def)
599 free(*(void**)opt->mem);
600 *(void**)opt->mem = NULL;
606 static void conf_default_bool(struct conf_option *opt)
608 *(bool*)opt->mem = (bool)opt->def;
611 static void conf_free_bool(struct conf_option *opt)
613 *(bool*)opt->mem = false;
616 static int conf_parse_bool(struct conf_option *opt, bool on, const char *arg)
618 *(bool*)opt->mem = on;
622 static int conf_copy_bool(struct conf_option *opt,
623 const struct conf_option *src)
625 *(bool*)opt->mem = *(bool*)src->mem;
629 const struct conf_type conf_bool = {
631 .set_default = conf_default_bool,
632 .free = conf_free_bool,
633 .parse = conf_parse_bool,
634 .copy = conf_copy_bool,
639 static int conf_parse_int(struct conf_option *opt, bool on, const char *arg)
641 *(int*)opt->mem = atoi(arg);
645 static void conf_free_int(struct conf_option *opt)
650 static void conf_default_int(struct conf_option *opt)
652 *(int*)opt->mem = (int)(unsigned long)opt->def;
655 static int conf_copy_int(struct conf_option *opt,
656 const struct conf_option *src)
658 *(int*)opt->mem = *(int*)src->mem;
662 const struct conf_type conf_int = {
663 .flags = CONF_HAS_ARG,
664 .set_default = conf_default_int,
665 .free = conf_free_int,
666 .parse = conf_parse_int,
667 .copy = conf_copy_int,
670 /* Unsigned Int Option */
672 static void conf_default_uint(struct conf_option *opt)
674 *(unsigned int*)opt->mem = (unsigned int)(unsigned long)opt->def;
677 static void conf_free_uint(struct conf_option *opt)
679 *(unsigned int*)opt->mem = 0;
682 static int conf_parse_uint(struct conf_option *opt, bool on, const char *arg)
684 *(unsigned int*)opt->mem = atoi(arg);
688 static int conf_copy_uint(struct conf_option *opt,
689 const struct conf_option *src)
691 *(unsigned int*)opt->mem = *(unsigned int*)src->mem;
695 const struct conf_type conf_uint = {
696 .flags = CONF_HAS_ARG,
697 .set_default = conf_default_uint,
698 .free = conf_free_uint,
699 .parse = conf_parse_uint,
700 .copy = conf_copy_uint,
705 static void conf_default_string(struct conf_option *opt)
707 opt->type->free(opt);
708 *(void**)opt->mem = opt->def;
711 static int conf_parse_string(struct conf_option *opt, bool on, const char *arg)
713 char *val = strdup(arg);
717 opt->type->free(opt);
718 *(void**)opt->mem = val;
722 static int conf_copy_string(struct conf_option *opt,
723 const struct conf_option *src)
727 if (!*(void**)src->mem) {
730 val = strdup(*(void**)src->mem);
735 opt->type->free(opt);
736 *(void**)opt->mem = val;
740 const struct conf_type conf_string = {
741 .flags = CONF_HAS_ARG,
742 .set_default = conf_default_string,
743 .free = conf_free_value,
744 .parse = conf_parse_string,
745 .copy = conf_copy_string,
748 /* Stringlist Option */
750 static void conf_default_string_list(struct conf_option *opt)
752 opt->type->free(opt);
753 *(void**)opt->mem = opt->def;
756 static int conf_parse_string_list(struct conf_option *opt, bool on,
762 ret = shl_split_string(arg, &list, NULL, ',', true);
766 opt->type->free(opt);
767 *(char***)opt->mem = list;
771 static int conf_copy_string_list(struct conf_option *opt,
772 const struct conf_option *src)
777 if (!(void***)src->mem) {
780 ret = shl_dup_array(&t, *(char***)src->mem);
785 opt->type->free(opt);
786 *(char***)opt->mem = t;
790 const struct conf_type conf_string_list = {
791 .flags = CONF_HAS_ARG,
792 .set_default = conf_default_string_list,
793 .free = conf_free_value,
794 .parse = conf_parse_string_list,
795 .copy = conf_copy_string_list,
800 static void conf_default_grab(struct conf_option *opt)
802 opt->type->free(opt);
803 *(void**)opt->mem = opt->def;
806 static void conf_free_grab(struct conf_option *opt)
808 struct conf_grab *grab;
811 grab = *(void**)opt->mem;
812 *(void**)opt->mem = NULL;
814 if (!grab || grab == opt->def)
817 for (i = 0; i < grab->num; ++i)
818 free(grab->keysyms[i]);
821 free(grab->num_syms);
826 static int parse_single_grab(char *arg, unsigned int *mods,
827 uint32_t *keysym, bool allow_mods)
829 char *tmp, *start, *end;
841 while (*tmp && *tmp != '>')
845 log_error("missing '>' near '%s'", start);
851 if (!strcasecmp(start, "shift")) {
852 *mods |= SHL_SHIFT_MASK;
853 } else if (!strcasecmp(start, "lock")) {
854 *mods |= SHL_LOCK_MASK;
855 } else if (!strcasecmp(start, "control") ||
856 !strcasecmp(start, "ctrl")) {
857 *mods |= SHL_CONTROL_MASK;
858 } else if (!strcasecmp(start, "alt")) {
859 *mods |= SHL_ALT_MASK;
860 } else if (!strcasecmp(start, "logo")) {
861 *mods |= SHL_LOGO_MASK;
863 log_error("invalid modifier '%s'", start);
874 while (*tmp && *tmp != ' ')
888 *keysym = xkb_keysym_from_name(start);
890 log_error("invalid key '%s'", start);
897 static int conf_parse_grab(struct conf_option *opt, bool on, const char *arg)
900 unsigned int list_num, key_num, i, j, k, l;
902 struct conf_grab *grab;
904 ret = shl_split_string(arg, &list, &list_num, ',', false);
908 grab = malloc(sizeof(*grab));
913 memset(grab, 0, sizeof(*grab));
916 grab->mods = malloc(sizeof(*grab->mods) * list_num);
921 memset(grab->mods, 0, sizeof(*grab->mods) * list_num);
923 grab->num_syms = malloc(sizeof(*grab->num_syms) * list_num);
924 if (!grab->num_syms) {
928 memset(grab->num_syms, 0, sizeof(*grab->num_syms) * list_num);
930 grab->keysyms = malloc(sizeof(*grab->keysyms) * list_num);
931 if (!grab->keysyms) {
935 memset(grab->keysyms, 0, sizeof(*grab->keysyms) * list_num);
939 for (i = 0; i < list_num; ++i) {
940 ret = shl_split_string(list[i], &keys, &key_num, '+', false);
948 grab->keysyms[l] = malloc(sizeof(*grab->keysyms[l]) * key_num);
949 if (!grab->keysyms[l]) {
956 for (j = 0; j < key_num; ++j) {
957 ret = parse_single_grab(keys[j], &grab->mods[l],
958 &grab->keysyms[l][k],
961 log_error("cannot parse grab '%s' in '%s'",
972 grab->num_syms[l] = k;
978 opt->type->free(opt);
979 *(void**)opt->mem = grab;
983 for (i = 0; i < list_num; ++i)
984 free(grab->keysyms[i]);
987 free(grab->num_syms);
995 static int conf_copy_grab(struct conf_option *opt,
996 const struct conf_option *src)
998 struct conf_grab *grab, *s;
1002 s = *(void**)src->mem;
1005 opt->type->free(opt);
1006 *(void**)opt->mem = NULL;
1010 grab = malloc(sizeof(*grab));
1013 memset(grab, 0, sizeof(*grab));
1017 grab->mods = malloc(sizeof(*grab->mods) * grab->num);
1022 memcpy(grab->mods, s->mods, sizeof(*grab->mods) * grab->num);
1024 grab->num_syms = malloc(sizeof(*grab->num_syms) * grab->num);
1025 if (!grab->num_syms) {
1029 memcpy(grab->num_syms, s->num_syms,
1030 sizeof(*grab->num_syms) * grab->num);
1032 grab->keysyms = malloc(sizeof(*grab->keysyms) * grab->num);
1033 if (!grab->keysyms) {
1037 memset(grab->keysyms, 0, sizeof(*grab->keysyms) * grab->num);
1040 for (i = 0; i < grab->num; ++i) {
1041 grab->keysyms[i] = malloc(sizeof(*s->keysyms[i]) *
1043 if (!grab->keysyms[i]) {
1047 memcpy(grab->keysyms[i], s->keysyms[i],
1048 sizeof(*s->keysyms[i]) * s->num_syms[i]);
1051 opt->type->free(opt);
1052 *(void**)opt->mem = grab;
1056 for (i = 0; i < grab->num; ++i)
1057 free(grab->keysyms[i]);
1059 free(grab->keysyms);
1060 free(grab->num_syms);
1066 const struct conf_type conf_grab = {
1067 .flags = CONF_HAS_ARG,
1068 .set_default = conf_default_grab,
1069 .free = conf_free_grab,
1070 .parse = conf_parse_grab,
1071 .copy = conf_copy_grab,