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/queue.h>
31 #include <sys/stat.h>
33 #include <ctype.h>
34 #include <fcntl.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <errno.h>
42 #include "got_error.h"
44 #include "got_lib_gitconfig.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 #define LOG_MISC 0
51 #define LOG_REPORT 1
52 #ifdef GITCONFIG_DEBUG
53 #define LOG_DBG(x) log_debug x
54 #else
55 #define LOG_DBG(x)
56 #endif
58 #define log_print printf
59 #define log_error printf
61 #ifdef GITCONFIG_DEBUG
62 static void
63 log_debug(int cls, int level, const char *fmt, ...)
64 {
65 va_list ap;
67 va_start(ap, fmt);
68 vfprintf(stderr, fmt, ap);
69 va_end(ap);
70 }
71 #endif
73 struct got_gitconfig_trans {
74 TAILQ_ENTRY(got_gitconfig_trans) link;
75 int trans;
76 enum got_gitconfig_op {
77 CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION
78 } op;
79 char *section;
80 char *tag;
81 char *value;
82 int override;
83 int is_default;
84 };
86 TAILQ_HEAD(got_gitconfig_trans_head, got_gitconfig_trans);
88 struct got_gitconfig_binding {
89 LIST_ENTRY(got_gitconfig_binding) link;
90 char *section;
91 char *tag;
92 char *value;
93 int is_default;
94 };
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;
101 char *addr;
102 int seq;
103 };
105 static __inline__ u_int8_t
106 conf_hash(char *s)
108 u_int8_t hash = 0;
110 while (*s) {
111 hash = ((hash << 1) | (hash >> 7)) ^ tolower((unsigned char)*s);
112 s++;
114 return hash;
117 /*
118 * Insert a tag-value combination from LINE (the equal sign is at POS)
119 */
120 static int
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;
126 cb = next) {
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,
132 tag, cb->value));
133 free(cb->section);
134 free(cb->tag);
135 free(cb->value);
136 free(cb);
137 return 0;
140 return 1;
143 static int
144 conf_remove_section_now(struct got_gitconfig *conf, char *section)
146 struct got_gitconfig_binding *cb, *next;
147 int unseen = 1;
149 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
150 cb = next) {
151 next = LIST_NEXT(cb, link);
152 if (strcasecmp(cb->section, section) == 0) {
153 unseen = 0;
154 LIST_REMOVE(cb, link);
155 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
156 cb->tag, cb->value));
157 free(cb->section);
158 free(cb->tag);
159 free(cb->value);
160 free(cb);
163 return unseen;
166 /*
167 * Insert a tag-value combination from LINE (the equal sign is at POS)
168 * into SECTION of our configuration database.
169 */
170 static int
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;
176 if (override)
177 conf_remove_now(conf, section, tag);
178 else if (got_gitconfig_get_str(conf, section, tag)) {
179 if (!is_default)
180 log_print("conf_set_now: duplicate tag [%s]:%s, "
181 "ignoring...\n", section, tag);
182 return 1;
184 node = calloc(1, sizeof *node);
185 if (!node) {
186 log_error("conf_set_now: calloc (1, %lu) failed",
187 (unsigned long)sizeof *node);
188 return 1;
190 node->section = node->tag = node->value = NULL;
191 if ((node->section = strdup(section)) == NULL)
192 goto fail;
193 if ((node->tag = strdup(tag)) == NULL)
194 goto fail;
195 if ((node->value = strdup(value)) == NULL)
196 goto fail;
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));
202 return 0;
203 fail:
204 free(node->value);
205 free(node->tag);
206 free(node->section);
207 free(node);
208 return 1;
211 /*
212 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
213 * headers and feed tag-value pairs into our configuration database.
214 */
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)
219 char *val;
220 size_t i;
221 int j;
223 /* Lines starting with '#' or ';' are comments. */
224 if (*line == '#' || *line == ';')
225 return NULL;
227 /* '[section]' parsing... */
228 if (*line == '[') {
229 for (i = 1; i < sz; i++)
230 if (line[i] == ']')
231 break;
232 free(*section);
233 if (i == sz) {
234 log_print("conf_parse_line: %d:"
235 "unmatched ']', ignoring until next section", ln);
236 *section = NULL;
237 return NULL;
239 *section = malloc(i);
240 if (*section == NULL)
241 return got_error_from_errno("malloc");
242 strlcpy(*section, line + 1, i);
243 return NULL;
245 while (isspace((unsigned char)*line))
246 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. */
252 if (!*section) {
253 log_print("conf_parse_line: %d: ignoring line "
254 "due to no section", ln);
255 return NULL;
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--)
262 val[j] = '\0';
263 /* XXX Perhaps should we not ignore errors? */
264 got_gitconfig_set(conf, trans, *section, line, val,
265 0, 0);
266 return NULL;
268 /* Other non-empty lines are weird. */
269 i = strspn(line, " \t");
270 if (line[i])
271 log_print("conf_parse_line: %d: syntax error", ln);
273 return NULL;
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;
281 char *cp = buf;
282 char *bufend = buf + sz;
283 char *line, *section = NULL;
284 int ln = 1;
286 line = cp;
287 while (cp < bufend) {
288 if (*cp == '\n') {
289 /* Check for escaped newlines. */
290 if (cp > buf && *(cp - 1) == '\\')
291 *(cp - 1) = *cp = ' ';
292 else {
293 *cp = '\0';
294 err = conf_parse_line(&section, conf, trans,
295 line, ln, cp - line);
296 if (err)
297 return err;
298 line = cp + 1;
300 ln++;
302 cp++;
304 if (cp != line)
305 log_print("conf_parse: last line unterminated, ignored.");
306 return NULL;
309 const struct got_error *
310 got_gitconfig_open(struct got_gitconfig **conf, int fd)
312 unsigned int i;
314 *conf = calloc(1, sizeof(**conf));
315 if (*conf == NULL)
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);
324 static void
325 conf_clear(struct got_gitconfig *conf)
327 struct got_gitconfig_binding *cb;
328 int i;
330 if (conf->addr) {
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);
335 free(conf->addr);
336 conf->addr = NULL;
340 /* Execute all queued operations for this transaction. Cleanup. */
341 static int
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) {
349 if (commit)
350 switch (node->op) {
351 case CONF_SET:
352 conf_set_now(conf, node->section,
353 node->tag, node->value,
354 node->override, node->is_default);
355 break;
356 case CONF_REMOVE:
357 conf_remove_now(conf, node->section,
358 node->tag);
359 break;
360 case CONF_REMOVE_SECTION:
361 conf_remove_section_now(conf, node->section);
362 break;
363 default:
364 log_print("got_gitconfig_end: unknown "
365 "operation: %d", node->op);
367 TAILQ_REMOVE(&conf->trans_queue, node, link);
368 free(node->section);
369 free(node->tag);
370 free(node->value);
371 free(node);
374 return 0;
378 void
379 got_gitconfig_close(struct got_gitconfig *conf)
381 conf_clear(conf);
382 free(conf);
385 static int
386 conf_begin(struct got_gitconfig *conf)
388 return ++conf->seq;
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;
396 int trans;
397 size_t sz;
398 char *new_conf_addr = 0;
399 struct stat st;
401 if (fstat(fd, &st)) {
402 err = got_error_from_errno("fstat");
403 goto fail;
406 sz = st.st_size;
407 new_conf_addr = malloc(sz);
408 if (new_conf_addr == NULL) {
409 err = got_error_from_errno("malloc");
410 goto fail;
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");
415 goto fail;
418 trans = conf_begin(conf);
420 err = conf_parse(conf, trans, new_conf_addr, sz);
421 if (err)
422 goto fail;
424 /* Free potential existing configuration. */
425 conf_clear(conf);
426 conf_end(conf, trans, 1);
427 conf->addr = new_conf_addr;
428 return NULL;
430 fail:
431 free(new_conf_addr);
432 return err;
435 /*
436 * Return the numeric value denoted by TAG in section SECTION or DEF
437 * if that tag does not exist.
438 */
439 int
440 got_gitconfig_get_num(struct got_gitconfig *conf, char *section, char *tag,
441 int def)
443 char *value = got_gitconfig_get_str(conf, section, tag);
445 if (value)
446 return atoi(value);
447 return def;
450 /* Validate X according to the range denoted by TAG in section SECTION. */
451 int
452 got_gitconfig_match_num(struct got_gitconfig *conf, char *section, char *tag,
453 int x)
455 char *value = got_gitconfig_get_str(conf, section, tag);
456 int val, min, max, n;
458 if (!value)
459 return 0;
460 n = sscanf(value, "%d,%d:%d", &val, &min, &max);
461 switch (n) {
462 case 1:
463 LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d==%d?",
464 section, tag, val, x));
465 return x == val;
466 case 3:
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;
470 default:
471 log_error("got_gitconfig_match_num: section %s tag %s: invalid number "
472 "spec %s", section, tag, value);
474 return 0;
477 /* Return the string value denoted by TAG in section SECTION. */
478 char *
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));
489 return cb->value;
491 LOG_DBG((LOG_MISC, 95,
492 "got_gitconfig_get_str: configuration value not found [%s]:%s", section,
493 tag));
494 return 0;
497 /*
498 * Build a list of string values out of the comma separated value denoted by
499 * TAG in SECTION.
500 */
501 struct got_gitconfig_list *
502 got_gitconfig_get_list(struct got_gitconfig *conf, char *section, char *tag)
504 char *liststr = 0, *p, *field, *t;
505 struct got_gitconfig_list *list = 0;
506 struct got_gitconfig_list_node *node = 0;
508 list = malloc(sizeof *list);
509 if (!list)
510 goto cleanup;
511 TAILQ_INIT(&list->fields);
512 list->cnt = 0;
513 liststr = got_gitconfig_get_str(conf, section, tag);
514 if (!liststr)
515 goto cleanup;
516 liststr = strdup(liststr);
517 if (!liststr)
518 goto cleanup;
519 p = liststr;
520 while ((field = strsep(&p, ",")) != NULL) {
521 /* Skip leading whitespace */
522 while (isspace((unsigned char)*field))
523 field++;
524 /* Skip trailing whitespace */
525 if (p)
526 for (t = p - 1; t > field && isspace((unsigned char)*t); t--)
527 *t = '\0';
528 if (*field == '\0') {
529 log_print("got_gitconfig_get_list: empty field, ignoring...");
530 continue;
532 list->cnt++;
533 node = calloc(1, sizeof *node);
534 if (!node)
535 goto cleanup;
536 node->field = strdup(field);
537 if (!node->field)
538 goto cleanup;
539 TAILQ_INSERT_TAIL(&list->fields, node, link);
541 free(liststr);
542 return list;
544 cleanup:
545 free(node);
546 if (list)
547 got_gitconfig_free_list(list);
548 free(liststr);
549 return 0;
552 struct got_gitconfig_list *
553 got_gitconfig_get_tag_list(struct got_gitconfig *conf, char *section)
555 struct got_gitconfig_list *list = 0;
556 struct got_gitconfig_list_node *node = 0;
557 struct got_gitconfig_binding *cb;
559 list = malloc(sizeof *list);
560 if (!list)
561 goto cleanup;
562 TAILQ_INIT(&list->fields);
563 list->cnt = 0;
564 for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
565 cb = LIST_NEXT(cb, link))
566 if (strcasecmp(section, cb->section) == 0) {
567 list->cnt++;
568 node = calloc(1, sizeof *node);
569 if (!node)
570 goto cleanup;
571 node->field = strdup(cb->tag);
572 if (!node->field)
573 goto cleanup;
574 TAILQ_INSERT_TAIL(&list->fields, node, link);
576 return list;
578 cleanup:
579 free(node);
580 if (list)
581 got_gitconfig_free_list(list);
582 return 0;
585 void
586 got_gitconfig_free_list(struct got_gitconfig_list *list)
588 struct got_gitconfig_list_node *node = TAILQ_FIRST(&list->fields);
590 while (node) {
591 TAILQ_REMOVE(&list->fields, node, link);
592 free(node->field);
593 free(node);
594 node = TAILQ_FIRST(&list->fields);
596 free(list);
599 static int
600 got_gitconfig_trans_node(struct got_gitconfig *conf, int transaction,
601 enum got_gitconfig_op op, char *section, char *tag, char *value,
602 int override, int is_default)
604 struct got_gitconfig_trans *node;
606 node = calloc(1, sizeof *node);
607 if (!node) {
608 log_error("got_gitconfig_trans_node: calloc (1, %lu) failed",
609 (unsigned long)sizeof *node);
610 return 1;
612 node->trans = transaction;
613 node->op = op;
614 node->override = override;
615 node->is_default = is_default;
616 if (section && (node->section = strdup(section)) == NULL)
617 goto fail;
618 if (tag && (node->tag = strdup(tag)) == NULL)
619 goto fail;
620 if (value && (node->value = strdup(value)) == NULL)
621 goto fail;
622 TAILQ_INSERT_TAIL(&conf->trans_queue, node, link);
623 return 0;
625 fail:
626 free(node->section);
627 free(node->tag);
628 free(node->value);
629 free(node);
630 return 1;
633 /* Queue a set operation. */
634 int
635 got_gitconfig_set(struct got_gitconfig *conf, int transaction, char *section,
636 char *tag, char *value, int override, int is_default)
638 return got_gitconfig_trans_node(conf, transaction, CONF_SET, section,
639 tag, value, override, is_default);
642 /* Queue a remove operation. */
643 int
644 got_gitconfig_remove(struct got_gitconfig *conf, int transaction,
645 char *section, char *tag)
647 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE,
648 section, tag, NULL, 0, 0);
651 /* Queue a remove section operation. */
652 int
653 got_gitconfig_remove_section(struct got_gitconfig *conf, int transaction,
654 char *section)
656 return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE_SECTION,
657 section, NULL, NULL, 0, 0);