1 /* $OpenBSD: conf.c,v 1.107 2017/10/27 08:29:32 mpi Exp $ */
2 /* $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $ */
5 * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
6 * Copyright (c) 2000, 2001, 2002 Håkan Olsson. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/queue.h>
42 #include "got_error.h"
44 #include "got_lib_gitconfig.h"
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 #ifdef GITCONFIG_DEBUG
53 #define LOG_DBG(x) log_debug x
58 #define log_print printf
59 #define log_error printf
61 #ifdef GITCONFIG_DEBUG
63 log_debug(int cls, int level, const char *fmt, ...)
68 vfprintf(stderr, fmt, ap);
73 struct got_gitconfig_trans {
74 TAILQ_ENTRY(got_gitconfig_trans) link;
76 enum got_gitconfig_op {
77 CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION
86 TAILQ_HEAD(got_gitconfig_trans_head, got_gitconfig_trans);
88 struct got_gitconfig_binding {
89 LIST_ENTRY(got_gitconfig_binding) link;
96 LIST_HEAD(got_gitconfig_bindings, got_gitconfig_binding);
98 struct got_gitconfig {
99 struct got_gitconfig_bindings bindings[256];
100 struct got_gitconfig_trans_head trans_queue;
105 static __inline__ u_int8_t
111 hash = ((hash << 1) | (hash >> 7)) ^ tolower((unsigned char)*s);
118 * Insert a tag-value combination from LINE (the equal sign is at POS)
121 conf_remove_now(struct got_gitconfig *conf, char *section, char *tag)
123 struct got_gitconfig_binding *cb, *next;
125 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
127 next = LIST_NEXT(cb, link);
128 if (strcasecmp(cb->section, section) == 0 &&
129 strcasecmp(cb->tag, tag) == 0) {
130 LIST_REMOVE(cb, link);
131 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
144 conf_remove_section_now(struct got_gitconfig *conf, char *section)
146 struct got_gitconfig_binding *cb, *next;
149 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
151 next = LIST_NEXT(cb, link);
152 if (strcasecmp(cb->section, section) == 0) {
154 LIST_REMOVE(cb, link);
155 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
156 cb->tag, cb->value));
167 * Insert a tag-value combination from LINE (the equal sign is at POS)
168 * into SECTION of our configuration database.
171 conf_set_now(struct got_gitconfig *conf, char *section, char *tag,
172 char *value, int override, int is_default)
174 struct got_gitconfig_binding *node = 0;
177 conf_remove_now(conf, section, tag);
178 else if (got_gitconfig_get_str(conf, section, tag)) {
180 log_print("conf_set_now: duplicate tag [%s]:%s, "
181 "ignoring...\n", section, tag);
184 node = calloc(1, sizeof *node);
186 log_error("conf_set_now: calloc (1, %lu) failed",
187 (unsigned long)sizeof *node);
190 node->section = node->tag = node->value = NULL;
191 if ((node->section = strdup(section)) == NULL)
193 if ((node->tag = strdup(tag)) == NULL)
195 if ((node->value = strdup(value)) == NULL)
197 node->is_default = is_default;
199 LIST_INSERT_HEAD(&conf->bindings[conf_hash(section)], node, link);
200 LOG_DBG((LOG_MISC, 95, "conf_set_now: [%s]:%s->%s", node->section,
201 node->tag, node->value));
212 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
213 * headers and feed tag-value pairs into our configuration database.
215 static const struct got_error *
216 conf_parse_line(char **section, struct got_gitconfig *conf, int trans,
217 char *line, int ln, size_t sz)
223 /* Lines starting with '#' or ';' are comments. */
224 if (*line == '#' || *line == ';')
227 /* '[section]' parsing... */
229 for (i = 1; i < sz; i++)
234 log_print("conf_parse_line: %d:"
235 "unmatched ']', ignoring until next section", ln);
239 *section = malloc(i);
240 if (*section == NULL)
241 return got_error_from_errno("malloc");
242 strlcpy(*section, line + 1, i);
245 while (isspace((unsigned char)*line))
248 /* Deal with assignments. */
249 for (i = 0; i < sz; i++)
250 if (line[i] == '=') {
251 /* If no section, we are ignoring the lines. */
253 log_print("conf_parse_line: %d: ignoring line "
254 "due to no section", ln);
257 line[strcspn(line, " \t=")] = '\0';
258 val = line + i + 1 + strspn(line + i + 1, " \t");
259 /* Skip trailing whitespace, if any */
260 for (j = sz - (val - line) - 1; j > 0 &&
261 isspace((unsigned char)val[j]); j--)
263 /* XXX Perhaps should we not ignore errors? */
264 got_gitconfig_set(conf, trans, *section, line, val,
268 /* Other non-empty lines are weird. */
269 i = strspn(line, " \t");
271 log_print("conf_parse_line: %d: syntax error", ln);
276 /* Parse the mapped configuration file. */
277 static const struct got_error *
278 conf_parse(struct got_gitconfig *conf, int trans, char *buf, size_t sz)
280 const struct got_error *err = NULL;
282 char *bufend = buf + sz;
283 char *line, *section = NULL;
287 while (cp < bufend) {
289 /* Check for escaped newlines. */
290 if (cp > buf && *(cp - 1) == '\\')
291 *(cp - 1) = *cp = ' ';
294 err = conf_parse_line(§ion, conf, trans,
295 line, ln, cp - line);
305 log_print("conf_parse: last line unterminated, ignored.");
309 const struct got_error *
310 got_gitconfig_open(struct got_gitconfig **conf, int fd)
314 *conf = calloc(1, sizeof(**conf));
316 return got_error_from_errno("malloc");
318 for (i = 0; i < nitems((*conf)->bindings); i++)
319 LIST_INIT(&(*conf)->bindings[i]);
320 TAILQ_INIT(&(*conf)->trans_queue);
321 return got_gitconfig_reinit(*conf, fd);
325 conf_clear(struct got_gitconfig *conf)
327 struct got_gitconfig_binding *cb;
331 for (i = 0; i < nitems(conf->bindings); i++)
332 for (cb = LIST_FIRST(&conf->bindings[i]); cb;
333 cb = LIST_FIRST(&conf->bindings[i]))
334 conf_remove_now(conf, cb->section, cb->tag);
340 /* Execute all queued operations for this transaction. Cleanup. */
342 conf_end(struct got_gitconfig *conf, int transaction, int commit)
344 struct got_gitconfig_trans *node, *next;
346 for (node = TAILQ_FIRST(&conf->trans_queue); node; node = next) {
347 next = TAILQ_NEXT(node, link);
348 if (node->trans == transaction) {
352 conf_set_now(conf, node->section,
353 node->tag, node->value,
354 node->override, node->is_default);
357 conf_remove_now(conf, node->section,
360 case CONF_REMOVE_SECTION:
361 conf_remove_section_now(conf, node->section);
364 log_print("got_gitconfig_end: unknown "
365 "operation: %d", node->op);
367 TAILQ_REMOVE(&conf->trans_queue, node, link);
379 got_gitconfig_close(struct got_gitconfig *conf)
386 conf_begin(struct got_gitconfig *conf)
391 /* Open the config file and map it into our address space, then parse it. */
392 const struct got_error *
393 got_gitconfig_reinit(struct got_gitconfig *conf, int fd)
395 const struct got_error *err = NULL;
398 char *new_conf_addr = 0;
401 if (fstat(fd, &st)) {
402 err = got_error_from_errno("fstat");
407 new_conf_addr = malloc(sz);
408 if (new_conf_addr == NULL) {
409 err = got_error_from_errno("malloc");
412 /* XXX I assume short reads won't happen here. */
413 if (read(fd, new_conf_addr, sz) != (int)sz) {
414 err = got_error_from_errno("read");
418 trans = conf_begin(conf);
420 err = conf_parse(conf, trans, new_conf_addr, sz);
424 /* Free potential existing configuration. */
426 conf_end(conf, trans, 1);
427 conf->addr = new_conf_addr;
436 * Return the numeric value denoted by TAG in section SECTION or DEF
437 * if that tag does not exist.
440 got_gitconfig_get_num(struct got_gitconfig *conf, char *section, char *tag,
443 char *value = got_gitconfig_get_str(conf, section, tag);
450 /* Validate X according to the range denoted by TAG in section SECTION. */
452 got_gitconfig_match_num(struct got_gitconfig *conf, char *section, char *tag,
455 char *value = got_gitconfig_get_str(conf, section, tag);
456 int val, min, max, n;
460 n = sscanf(value, "%d,%d:%d", &val, &min, &max);
463 LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d==%d?",
464 section, tag, val, x));
467 LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d<=%d<=%d?",
468 section, tag, min, x, max));
469 return min <= x && max >= x;
471 log_error("got_gitconfig_match_num: section %s tag %s: invalid number "
472 "spec %s", section, tag, value);
477 /* Return the string value denoted by TAG in section SECTION. */
479 got_gitconfig_get_str(struct got_gitconfig *conf, char *section, char *tag)
481 struct got_gitconfig_binding *cb;
483 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
484 cb = LIST_NEXT(cb, link))
485 if (strcasecmp(section, cb->section) == 0 &&
486 strcasecmp(tag, cb->tag) == 0) {
487 LOG_DBG((LOG_MISC, 95, "got_gitconfig_get_str: [%s]:%s->%s",
488 section, tag, cb->value));
491 LOG_DBG((LOG_MISC, 95,
492 "got_gitconfig_get_str: configuration value not found [%s]:%s", section,
497 const struct got_error *
498 got_gitconfig_get_section_list(struct got_gitconfig_list **sections,
499 struct got_gitconfig *conf)
501 const struct got_error *err = NULL;
502 struct got_gitconfig_list *list = NULL;
503 struct got_gitconfig_list_node *node = 0;
504 struct got_gitconfig_binding *cb;
509 list = malloc(sizeof *list);
511 return got_error_from_errno("malloc");
512 TAILQ_INIT(&list->fields);
514 for (i = 0; i < nitems(conf->bindings); i++) {
515 for (cb = LIST_FIRST(&conf->bindings[i]); cb;
516 cb = LIST_NEXT(cb, link)) {
518 node = calloc(1, sizeof *node);
520 err = got_error_from_errno("calloc");
523 node->field = strdup(cb->section);
525 err = got_error_from_errno("strdup");
528 TAILQ_INSERT_TAIL(&list->fields, node, link);
538 got_gitconfig_free_list(list);
543 * Build a list of string values out of the comma separated value denoted by
546 struct got_gitconfig_list *
547 got_gitconfig_get_list(struct got_gitconfig *conf, char *section, char *tag)
549 char *liststr = 0, *p, *field, *t;
550 struct got_gitconfig_list *list = 0;
551 struct got_gitconfig_list_node *node = 0;
553 list = malloc(sizeof *list);
556 TAILQ_INIT(&list->fields);
558 liststr = got_gitconfig_get_str(conf, section, tag);
561 liststr = strdup(liststr);
565 while ((field = strsep(&p, ",")) != NULL) {
566 /* Skip leading whitespace */
567 while (isspace((unsigned char)*field))
569 /* Skip trailing whitespace */
571 for (t = p - 1; t > field && isspace((unsigned char)*t); t--)
573 if (*field == '\0') {
574 log_print("got_gitconfig_get_list: empty field, ignoring...");
578 node = calloc(1, sizeof *node);
581 node->field = strdup(field);
584 TAILQ_INSERT_TAIL(&list->fields, node, link);
592 got_gitconfig_free_list(list);
597 struct got_gitconfig_list *
598 got_gitconfig_get_tag_list(struct got_gitconfig *conf, char *section)
600 struct got_gitconfig_list *list = 0;
601 struct got_gitconfig_list_node *node = 0;
602 struct got_gitconfig_binding *cb;
604 list = malloc(sizeof *list);
607 TAILQ_INIT(&list->fields);
609 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
610 cb = LIST_NEXT(cb, link))
611 if (strcasecmp(section, cb->section) == 0) {
613 node = calloc(1, sizeof *node);
616 node->field = strdup(cb->tag);
619 TAILQ_INSERT_TAIL(&list->fields, node, link);
626 got_gitconfig_free_list(list);
631 got_gitconfig_free_list(struct got_gitconfig_list *list)
633 struct got_gitconfig_list_node *node = TAILQ_FIRST(&list->fields);
636 TAILQ_REMOVE(&list->fields, node, link);
639 node = TAILQ_FIRST(&list->fields);
645 got_gitconfig_trans_node(struct got_gitconfig *conf, int transaction,
646 enum got_gitconfig_op op, char *section, char *tag, char *value,
647 int override, int is_default)
649 struct got_gitconfig_trans *node;
651 node = calloc(1, sizeof *node);
653 log_error("got_gitconfig_trans_node: calloc (1, %lu) failed",
654 (unsigned long)sizeof *node);
657 node->trans = transaction;
659 node->override = override;
660 node->is_default = is_default;
661 if (section && (node->section = strdup(section)) == NULL)
663 if (tag && (node->tag = strdup(tag)) == NULL)
665 if (value && (node->value = strdup(value)) == NULL)
667 TAILQ_INSERT_TAIL(&conf->trans_queue, node, link);
678 /* Queue a set operation. */
680 got_gitconfig_set(struct got_gitconfig *conf, int transaction, char *section,
681 char *tag, char *value, int override, int is_default)
683 return got_gitconfig_trans_node(conf, transaction, CONF_SET, section,
684 tag, value, override, is_default);
687 /* Queue a remove operation. */
689 got_gitconfig_remove(struct got_gitconfig *conf, int transaction,
690 char *section, char *tag)
692 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE,
693 section, tag, NULL, 0, 0);
696 /* Queue a remove section operation. */
698 got_gitconfig_remove_section(struct got_gitconfig *conf, int transaction,
701 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE_SECTION,
702 section, NULL, NULL, 0, 0);