Blob


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 $ */
4 /*
5 * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
6 * Copyright (c) 2000, 2001, 2002 Håkan Olsson. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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.
27 */
29 #include <sys/types.h>
30 #include <sys/stat.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <errno.h>
41 #include "got_compat.h"
43 #include "got_error.h"
45 #include "got_lib_gitconfig.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 #define LOG_MISC 0
52 #define LOG_REPORT 1
53 #ifdef GITCONFIG_DEBUG
54 #define LOG_DBG(x) log_debug x
55 #else
56 #define LOG_DBG(x)
57 #endif
59 #define log_print printf
60 #define log_error printf
62 #ifdef GITCONFIG_DEBUG
63 static void
64 log_debug(int cls, int level, const char *fmt, ...)
65 {
66 va_list ap;
68 va_start(ap, fmt);
69 vfprintf(stderr, fmt, ap);
70 va_end(ap);
71 }
72 #endif
74 struct got_gitconfig_trans {
75 TAILQ_ENTRY(got_gitconfig_trans) link;
76 int trans;
77 enum got_gitconfig_op {
78 CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION
79 } op;
80 char *section;
81 char *tag;
82 char *value;
83 int override;
84 int is_default;
85 };
87 TAILQ_HEAD(got_gitconfig_trans_head, got_gitconfig_trans);
89 struct got_gitconfig_binding {
90 LIST_ENTRY(got_gitconfig_binding) link;
91 char *section;
92 char *tag;
93 char *value;
94 int is_default;
95 };
97 LIST_HEAD(got_gitconfig_bindings, got_gitconfig_binding);
99 struct got_gitconfig {
100 struct got_gitconfig_bindings bindings[256];
101 struct got_gitconfig_trans_head trans_queue;
102 char *addr;
103 int seq;
104 };
106 static __inline__ u_int8_t
107 conf_hash(char *s)
109 u_int8_t hash = 0;
111 while (*s) {
112 hash = ((hash << 1) | (hash >> 7)) ^ tolower((unsigned char)*s);
113 s++;
115 return hash;
118 /*
119 * Insert a tag-value combination from LINE (the equal sign is at POS)
120 */
121 static int
122 conf_remove_now(struct got_gitconfig *conf, char *section, char *tag)
124 struct got_gitconfig_binding *cb, *next;
126 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
127 cb = next) {
128 next = LIST_NEXT(cb, link);
129 if (strcasecmp(cb->section, section) == 0 &&
130 strcasecmp(cb->tag, tag) == 0) {
131 LIST_REMOVE(cb, link);
132 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
133 tag, cb->value));
134 free(cb->section);
135 free(cb->tag);
136 free(cb->value);
137 free(cb);
138 return 0;
141 return 1;
144 static int
145 conf_remove_section_now(struct got_gitconfig *conf, char *section)
147 struct got_gitconfig_binding *cb, *next;
148 int unseen = 1;
150 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
151 cb = next) {
152 next = LIST_NEXT(cb, link);
153 if (strcasecmp(cb->section, section) == 0) {
154 unseen = 0;
155 LIST_REMOVE(cb, link);
156 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
157 cb->tag, cb->value));
158 free(cb->section);
159 free(cb->tag);
160 free(cb->value);
161 free(cb);
164 return unseen;
167 /*
168 * Insert a tag-value combination from LINE (the equal sign is at POS)
169 * into SECTION of our configuration database.
170 */
171 static int
172 conf_set_now(struct got_gitconfig *conf, char *section, char *tag,
173 char *value, int override, int is_default)
175 struct got_gitconfig_binding *node = 0;
177 if (override)
178 conf_remove_now(conf, section, tag);
179 else if (got_gitconfig_get_str(conf, section, tag)) {
180 if (!is_default)
181 LOG_DBG((LOG_MISC,
182 "conf_set_now: duplicate tag [%s]:%s, "
183 "ignoring...\n", section, tag));
184 return 1;
186 node = calloc(1, sizeof *node);
187 if (!node) {
188 log_error("conf_set_now: calloc (1, %lu) failed",
189 (unsigned long)sizeof *node);
190 return 1;
192 node->section = node->tag = node->value = NULL;
193 if ((node->section = strdup(section)) == NULL)
194 goto fail;
195 if ((node->tag = strdup(tag)) == NULL)
196 goto fail;
197 if ((node->value = strdup(value)) == NULL)
198 goto fail;
199 node->is_default = is_default;
201 LIST_INSERT_HEAD(&conf->bindings[conf_hash(section)], node, link);
202 LOG_DBG((LOG_MISC, 95, "conf_set_now: [%s]:%s->%s", node->section,
203 node->tag, node->value));
204 return 0;
205 fail:
206 free(node->value);
207 free(node->tag);
208 free(node->section);
209 free(node);
210 return 1;
213 /*
214 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
215 * headers and feed tag-value pairs into our configuration database.
216 */
217 static const struct got_error *
218 conf_parse_line(char **section, struct got_gitconfig *conf, int trans,
219 char *line, int ln, size_t sz)
221 char *val;
222 size_t i;
223 int j;
225 /* Lines starting with '#' or ';' are comments. */
226 if (*line == '#' || *line == ';')
227 return NULL;
229 /* '[section]' parsing... */
230 if (*line == '[') {
231 for (i = 1; i < sz; i++)
232 if (line[i] == ']')
233 break;
234 free(*section);
235 if (i == sz) {
236 log_print("conf_parse_line: %d:"
237 "unmatched ']', ignoring until next section", ln);
238 *section = NULL;
239 return NULL;
241 *section = malloc(i);
242 if (*section == NULL)
243 return got_error_from_errno("malloc");
244 strlcpy(*section, line + 1, i);
245 return NULL;
247 while (isspace((unsigned char)*line))
248 line++;
250 /* Deal with assignments. */
251 for (i = 0; i < sz; i++)
252 if (line[i] == '=') {
253 /* If no section, we are ignoring the lines. */
254 if (!*section) {
255 log_print("conf_parse_line: %d: ignoring line "
256 "due to no section", ln);
257 return NULL;
259 line[strcspn(line, " \t=")] = '\0';
260 val = line + i + 1 + strspn(line + i + 1, " \t");
261 /* Skip trailing whitespace, if any */
262 for (j = sz - (val - line) - 1; j > 0 &&
263 isspace((unsigned char)val[j]); j--)
264 val[j] = '\0';
265 /* XXX Perhaps should we not ignore errors? */
266 got_gitconfig_set(conf, trans, *section, line, val,
267 0, 0);
268 return NULL;
270 /* Other non-empty lines are weird. */
271 i = strspn(line, " \t");
272 if (line[i])
273 log_print("conf_parse_line: %d: syntax error", ln);
275 return NULL;
278 /* Parse the mapped configuration file. */
279 static const struct got_error *
280 conf_parse(struct got_gitconfig *conf, int trans, char *buf, size_t sz)
282 const struct got_error *err = NULL;
283 char *cp = buf;
284 char *bufend = buf + sz;
285 char *line, *section = NULL;
286 int ln = 1;
288 line = cp;
289 while (cp < bufend) {
290 if (*cp == '\n') {
291 /* Check for escaped newlines. */
292 if (cp > buf && *(cp - 1) == '\\')
293 *(cp - 1) = *cp = ' ';
294 else {
295 *cp = '\0';
296 err = conf_parse_line(&section, conf, trans,
297 line, ln, cp - line);
298 if (err)
299 return err;
300 line = cp + 1;
302 ln++;
304 cp++;
306 if (cp != line)
307 log_print("conf_parse: last line unterminated, ignored.");
308 return NULL;
311 const struct got_error *
312 got_gitconfig_open(struct got_gitconfig **conf, int fd)
314 size_t i;
316 *conf = calloc(1, sizeof(**conf));
317 if (*conf == NULL)
318 return got_error_from_errno("malloc");
320 for (i = 0; i < nitems((*conf)->bindings); i++)
321 LIST_INIT(&(*conf)->bindings[i]);
322 TAILQ_INIT(&(*conf)->trans_queue);
323 return got_gitconfig_reinit(*conf, fd);
326 static void
327 conf_clear(struct got_gitconfig *conf)
329 struct got_gitconfig_binding *cb;
330 size_t i;
332 if (conf->addr) {
333 for (i = 0; i < nitems(conf->bindings); i++)
334 for (cb = LIST_FIRST(&conf->bindings[i]); cb;
335 cb = LIST_FIRST(&conf->bindings[i]))
336 conf_remove_now(conf, cb->section, cb->tag);
337 free(conf->addr);
338 conf->addr = NULL;
342 /* Execute all queued operations for this transaction. Cleanup. */
343 static int
344 conf_end(struct got_gitconfig *conf, int transaction, int commit)
346 struct got_gitconfig_trans *node, *next;
348 for (node = TAILQ_FIRST(&conf->trans_queue); node; node = next) {
349 next = TAILQ_NEXT(node, link);
350 if (node->trans == transaction) {
351 if (commit)
352 switch (node->op) {
353 case CONF_SET:
354 conf_set_now(conf, node->section,
355 node->tag, node->value,
356 node->override, node->is_default);
357 break;
358 case CONF_REMOVE:
359 conf_remove_now(conf, node->section,
360 node->tag);
361 break;
362 case CONF_REMOVE_SECTION:
363 conf_remove_section_now(conf, node->section);
364 break;
365 default:
366 log_print("got_gitconfig_end: unknown "
367 "operation: %d", node->op);
369 TAILQ_REMOVE(&conf->trans_queue, node, link);
370 free(node->section);
371 free(node->tag);
372 free(node->value);
373 free(node);
376 return 0;
380 void
381 got_gitconfig_close(struct got_gitconfig *conf)
383 conf_clear(conf);
384 free(conf);
387 static int
388 conf_begin(struct got_gitconfig *conf)
390 return ++conf->seq;
393 /* Open the config file and map it into our address space, then parse it. */
394 const struct got_error *
395 got_gitconfig_reinit(struct got_gitconfig *conf, int fd)
397 const struct got_error *err = NULL;
398 int trans;
399 size_t sz;
400 char *new_conf_addr = 0;
401 struct stat st;
403 if (fstat(fd, &st)) {
404 err = got_error_from_errno("fstat");
405 goto fail;
408 sz = st.st_size;
409 new_conf_addr = malloc(sz);
410 if (new_conf_addr == NULL) {
411 err = got_error_from_errno("malloc");
412 goto fail;
414 /* XXX I assume short reads won't happen here. */
415 if (read(fd, new_conf_addr, sz) != (int)sz) {
416 err = got_error_from_errno("read");
417 goto fail;
420 trans = conf_begin(conf);
422 err = conf_parse(conf, trans, new_conf_addr, sz);
423 if (err)
424 goto fail;
426 /* Free potential existing configuration. */
427 conf_clear(conf);
428 conf_end(conf, trans, 1);
429 conf->addr = new_conf_addr;
430 return NULL;
432 fail:
433 free(new_conf_addr);
434 return err;
437 /*
438 * Return the numeric value denoted by TAG in section SECTION or DEF
439 * if that tag does not exist.
440 */
441 int
442 got_gitconfig_get_num(struct got_gitconfig *conf, char *section, char *tag,
443 int def)
445 char *value = got_gitconfig_get_str(conf, section, tag);
447 if (value)
448 return atoi(value);
449 return def;
452 /* Validate X according to the range denoted by TAG in section SECTION. */
453 int
454 got_gitconfig_match_num(struct got_gitconfig *conf, char *section, char *tag,
455 int x)
457 char *value = got_gitconfig_get_str(conf, section, tag);
458 int val, min, max, n;
460 if (!value)
461 return 0;
462 n = sscanf(value, "%d,%d:%d", &val, &min, &max);
463 switch (n) {
464 case 1:
465 LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d==%d?",
466 section, tag, val, x));
467 return x == val;
468 case 3:
469 LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d<=%d<=%d?",
470 section, tag, min, x, max));
471 return min <= x && max >= x;
472 default:
473 log_error("got_gitconfig_match_num: section %s tag %s: invalid number "
474 "spec %s", section, tag, value);
476 return 0;
479 /* Return the string value denoted by TAG in section SECTION. */
480 char *
481 got_gitconfig_get_str(struct got_gitconfig *conf, char *section, char *tag)
483 struct got_gitconfig_binding *cb;
485 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
486 cb = LIST_NEXT(cb, link))
487 if (strcasecmp(section, cb->section) == 0 &&
488 strcasecmp(tag, cb->tag) == 0) {
489 LOG_DBG((LOG_MISC, 95, "got_gitconfig_get_str: [%s]:%s->%s",
490 section, tag, cb->value));
491 return cb->value;
493 LOG_DBG((LOG_MISC, 95,
494 "got_gitconfig_get_str: configuration value not found [%s]:%s", section,
495 tag));
496 return 0;
499 const struct got_error *
500 got_gitconfig_get_section_list(struct got_gitconfig_list **sections,
501 struct got_gitconfig *conf)
503 const struct got_error *err = NULL;
504 struct got_gitconfig_list *list = NULL;
505 struct got_gitconfig_list_node *node = 0;
506 struct got_gitconfig_binding *cb;
507 size_t i;
509 *sections = NULL;
511 list = malloc(sizeof *list);
512 if (!list)
513 return got_error_from_errno("malloc");
514 TAILQ_INIT(&list->fields);
515 list->cnt = 0;
516 for (i = 0; i < nitems(conf->bindings); i++) {
517 for (cb = LIST_FIRST(&conf->bindings[i]); cb;
518 cb = LIST_NEXT(cb, link)) {
519 int section_present = 0;
520 TAILQ_FOREACH(node, &list->fields, link) {
521 if (strcmp(node->field, cb->section) == 0) {
522 section_present = 1;
523 break;
526 if (section_present)
527 continue;
528 list->cnt++;
529 node = calloc(1, sizeof *node);
530 if (!node) {
531 err = got_error_from_errno("calloc");
532 goto cleanup;
534 node->field = strdup(cb->section);
535 if (!node->field) {
536 err = got_error_from_errno("strdup");
537 goto cleanup;
539 TAILQ_INSERT_TAIL(&list->fields, node, link);
543 *sections = list;
544 return NULL;
546 cleanup:
547 free(node);
548 if (list)
549 got_gitconfig_free_list(list);
550 return err;
553 /*
554 * Build a list of string values out of the comma separated value denoted by
555 * TAG in SECTION.
556 */
557 struct got_gitconfig_list *
558 got_gitconfig_get_list(struct got_gitconfig *conf, char *section, char *tag)
560 char *liststr = 0, *p, *field, *t;
561 struct got_gitconfig_list *list = 0;
562 struct got_gitconfig_list_node *node = 0;
564 list = malloc(sizeof *list);
565 if (!list)
566 goto cleanup;
567 TAILQ_INIT(&list->fields);
568 list->cnt = 0;
569 liststr = got_gitconfig_get_str(conf, section, tag);
570 if (!liststr)
571 goto cleanup;
572 liststr = strdup(liststr);
573 if (!liststr)
574 goto cleanup;
575 p = liststr;
576 while ((field = strsep(&p, ",")) != NULL) {
577 /* Skip leading whitespace */
578 while (isspace((unsigned char)*field))
579 field++;
580 /* Skip trailing whitespace */
581 if (p)
582 for (t = p - 1; t > field && isspace((unsigned char)*t); t--)
583 *t = '\0';
584 if (*field == '\0') {
585 log_print("got_gitconfig_get_list: empty field, ignoring...");
586 continue;
588 list->cnt++;
589 node = calloc(1, sizeof *node);
590 if (!node)
591 goto cleanup;
592 node->field = strdup(field);
593 if (!node->field)
594 goto cleanup;
595 TAILQ_INSERT_TAIL(&list->fields, node, link);
597 free(liststr);
598 return list;
600 cleanup:
601 free(node);
602 if (list)
603 got_gitconfig_free_list(list);
604 free(liststr);
605 return 0;
608 struct got_gitconfig_list *
609 got_gitconfig_get_tag_list(struct got_gitconfig *conf, char *section)
611 struct got_gitconfig_list *list = 0;
612 struct got_gitconfig_list_node *node = 0;
613 struct got_gitconfig_binding *cb;
615 list = malloc(sizeof *list);
616 if (!list)
617 goto cleanup;
618 TAILQ_INIT(&list->fields);
619 list->cnt = 0;
620 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
621 cb = LIST_NEXT(cb, link))
622 if (strcasecmp(section, cb->section) == 0) {
623 list->cnt++;
624 node = calloc(1, sizeof *node);
625 if (!node)
626 goto cleanup;
627 node->field = strdup(cb->tag);
628 if (!node->field)
629 goto cleanup;
630 TAILQ_INSERT_TAIL(&list->fields, node, link);
632 return list;
634 cleanup:
635 free(node);
636 if (list)
637 got_gitconfig_free_list(list);
638 return 0;
641 void
642 got_gitconfig_free_list(struct got_gitconfig_list *list)
644 struct got_gitconfig_list_node *node = TAILQ_FIRST(&list->fields);
646 while (node) {
647 TAILQ_REMOVE(&list->fields, node, link);
648 free(node->field);
649 free(node);
650 node = TAILQ_FIRST(&list->fields);
652 free(list);
655 static int
656 got_gitconfig_trans_node(struct got_gitconfig *conf, int transaction,
657 enum got_gitconfig_op op, char *section, char *tag, char *value,
658 int override, int is_default)
660 struct got_gitconfig_trans *node;
662 node = calloc(1, sizeof *node);
663 if (!node) {
664 log_error("got_gitconfig_trans_node: calloc (1, %lu) failed",
665 (unsigned long)sizeof *node);
666 return 1;
668 node->trans = transaction;
669 node->op = op;
670 node->override = override;
671 node->is_default = is_default;
672 if (section && (node->section = strdup(section)) == NULL)
673 goto fail;
674 if (tag && (node->tag = strdup(tag)) == NULL)
675 goto fail;
676 if (value && (node->value = strdup(value)) == NULL)
677 goto fail;
678 TAILQ_INSERT_TAIL(&conf->trans_queue, node, link);
679 return 0;
681 fail:
682 free(node->section);
683 free(node->tag);
684 free(node->value);
685 free(node);
686 return 1;
689 /* Queue a set operation. */
690 int
691 got_gitconfig_set(struct got_gitconfig *conf, int transaction, char *section,
692 char *tag, char *value, int override, int is_default)
694 return got_gitconfig_trans_node(conf, transaction, CONF_SET, section,
695 tag, value, override, is_default);
698 /* Queue a remove operation. */
699 int
700 got_gitconfig_remove(struct got_gitconfig *conf, int transaction,
701 char *section, char *tag)
703 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE,
704 section, tag, NULL, 0, 0);
707 /* Queue a remove section operation. */
708 int
709 got_gitconfig_remove_section(struct got_gitconfig *conf, int transaction,
710 char *section)
712 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE_SECTION,
713 section, NULL, NULL, 0, 0);