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_compat.h"
44 #include "got_error.h"
46 #include "got_lib_gitconfig.h"
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #ifdef GITCONFIG_DEBUG
55 #define LOG_DBG(x) log_debug x
60 #define log_print(...) fprintf(stderr, __VA_ARGS__)
61 #define log_error(...) fprintf(stderr, __VA_ARGS__)
63 #ifdef GITCONFIG_DEBUG
65 log_debug(int cls
, int level
, const char *fmt
, ...)
70 vfprintf(stderr
, fmt
, ap
);
76 struct got_gitconfig_trans
{
77 TAILQ_ENTRY(got_gitconfig_trans
) link
;
79 enum got_gitconfig_op
{
80 CONF_SET
, CONF_REMOVE
, CONF_REMOVE_SECTION
89 TAILQ_HEAD(got_gitconfig_trans_head
, got_gitconfig_trans
);
91 struct got_gitconfig_binding
{
92 LIST_ENTRY(got_gitconfig_binding
) link
;
99 LIST_HEAD(got_gitconfig_bindings
, got_gitconfig_binding
);
101 struct got_gitconfig
{
102 struct got_gitconfig_bindings bindings
[256];
103 struct got_gitconfig_trans_head trans_queue
;
108 static __inline__ u_int8_t
109 conf_hash(const char *s
)
114 hash
= ((hash
<< 1) | (hash
>> 7)) ^ tolower((unsigned char)*s
);
121 * Insert a tag-value combination from LINE (the equal sign is at POS)
124 conf_remove_now(struct got_gitconfig
*conf
, char *section
, char *tag
)
126 struct got_gitconfig_binding
*cb
, *next
;
128 for (cb
= LIST_FIRST(&conf
->bindings
[conf_hash(section
)]); cb
;
130 next
= LIST_NEXT(cb
, link
);
131 if (strcasecmp(cb
->section
, section
) == 0 &&
132 strcasecmp(cb
->tag
, tag
) == 0) {
133 LIST_REMOVE(cb
, link
);
134 LOG_DBG((LOG_MISC
, 95, "[%s]:%s->%s removed", section
,
147 conf_remove_section_now(struct got_gitconfig
*conf
, char *section
)
149 struct got_gitconfig_binding
*cb
, *next
;
152 for (cb
= LIST_FIRST(&conf
->bindings
[conf_hash(section
)]); cb
;
154 next
= LIST_NEXT(cb
, link
);
155 if (strcasecmp(cb
->section
, section
) == 0) {
157 LIST_REMOVE(cb
, link
);
158 LOG_DBG((LOG_MISC
, 95, "[%s]:%s->%s removed", section
,
159 cb
->tag
, cb
->value
));
170 * Insert a tag-value combination from LINE (the equal sign is at POS)
171 * into SECTION of our configuration database.
174 conf_set_now(struct got_gitconfig
*conf
, char *section
, char *tag
,
175 char *value
, int override
, int is_default
)
177 struct got_gitconfig_binding
*node
= 0;
180 conf_remove_now(conf
, section
, tag
);
181 else if (got_gitconfig_get_str(conf
, section
, tag
)) {
183 LOG_DBG((LOG_MISC
, 95,
184 "conf_set_now: duplicate tag [%s]:%s, "
185 "ignoring...", section
, tag
));
188 node
= calloc(1, sizeof *node
);
190 log_error("conf_set_now: calloc (1, %lu) failed",
191 (unsigned long)sizeof *node
);
194 node
->section
= node
->tag
= node
->value
= NULL
;
195 if ((node
->section
= strdup(section
)) == NULL
)
197 if ((node
->tag
= strdup(tag
)) == NULL
)
199 if ((node
->value
= strdup(value
)) == NULL
)
201 node
->is_default
= is_default
;
203 LIST_INSERT_HEAD(&conf
->bindings
[conf_hash(section
)], node
, link
);
204 LOG_DBG((LOG_MISC
, 95, "conf_set_now: [%s]:%s->%s", node
->section
,
205 node
->tag
, node
->value
));
216 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
217 * headers and feed tag-value pairs into our configuration database.
219 static const struct got_error
*
220 conf_parse_line(char **section
, struct got_gitconfig
*conf
, int trans
,
221 char *line
, int ln
, size_t sz
)
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
= strndup(line
+ 1, i
- 1);
240 if (*section
== NULL
)
241 return got_error_from_errno("strndup");
244 while (sz
> 0 && isspace((unsigned char)*line
)) {
249 /* Lines starting with '#' or ';' are comments. */
250 if (*line
== '#' || *line
== ';')
253 /* Deal with assignments. */
254 for (i
= 0; i
< sz
; i
++)
255 if (line
[i
] == '=') {
256 /* If no section, we are ignoring the lines. */
258 log_print("conf_parse_line: %d: ignoring line "
259 "due to no section", ln
);
262 line
[strcspn(line
, " \t=")] = '\0';
263 val
= line
+ i
+ 1 + strspn(line
+ i
+ 1, " \t");
264 /* Skip trailing whitespace, if any */
265 for (j
= sz
- (val
- line
) - 1; j
> 0 &&
266 isspace((unsigned char)val
[j
]); j
--)
268 /* XXX Perhaps should we not ignore errors? */
269 got_gitconfig_set(conf
, trans
, *section
, line
, val
,
273 /* Other non-empty lines are weird. */
274 i
= strspn(line
, " \t");
276 log_print("conf_parse_line: %d: syntax error", ln
);
281 /* Parse the mapped configuration file. */
282 static const struct got_error
*
283 conf_parse(struct got_gitconfig
*conf
, int trans
, char *buf
, size_t sz
)
285 const struct got_error
*err
= NULL
;
287 char *bufend
= buf
+ sz
;
288 char *line
, *section
= NULL
;
292 while (cp
< bufend
) {
294 /* Check for escaped newlines. */
295 if (cp
> buf
&& *(cp
- 1) == '\\')
296 *(cp
- 1) = *cp
= ' ';
299 err
= conf_parse_line(§ion
, conf
, trans
,
300 line
, ln
, cp
- line
);
310 log_print("conf_parse: last line unterminated, ignored.");
314 const struct got_error
*
315 got_gitconfig_open(struct got_gitconfig
**conf
, int fd
)
319 *conf
= calloc(1, sizeof(**conf
));
321 return got_error_from_errno("malloc");
323 for (i
= 0; i
< nitems((*conf
)->bindings
); i
++)
324 LIST_INIT(&(*conf
)->bindings
[i
]);
325 TAILQ_INIT(&(*conf
)->trans_queue
);
326 return got_gitconfig_reinit(*conf
, fd
);
330 conf_clear(struct got_gitconfig
*conf
)
332 struct got_gitconfig_binding
*cb
;
336 for (i
= 0; i
< nitems(conf
->bindings
); i
++)
337 for (cb
= LIST_FIRST(&conf
->bindings
[i
]); cb
;
338 cb
= LIST_FIRST(&conf
->bindings
[i
]))
339 conf_remove_now(conf
, cb
->section
, cb
->tag
);
345 /* Execute all queued operations for this transaction. Cleanup. */
347 conf_end(struct got_gitconfig
*conf
, int transaction
, int commit
)
349 struct got_gitconfig_trans
*node
, *next
;
351 for (node
= TAILQ_FIRST(&conf
->trans_queue
); node
; node
= next
) {
352 next
= TAILQ_NEXT(node
, link
);
353 if (node
->trans
== transaction
) {
357 conf_set_now(conf
, node
->section
,
358 node
->tag
, node
->value
,
359 node
->override
, node
->is_default
);
362 conf_remove_now(conf
, node
->section
,
365 case CONF_REMOVE_SECTION
:
366 conf_remove_section_now(conf
, node
->section
);
369 log_print("got_gitconfig_end: unknown "
370 "operation: %d", node
->op
);
372 TAILQ_REMOVE(&conf
->trans_queue
, node
, link
);
384 got_gitconfig_close(struct got_gitconfig
*conf
)
391 conf_begin(struct got_gitconfig
*conf
)
396 /* Open the config file and map it into our address space, then parse it. */
397 const struct got_error
*
398 got_gitconfig_reinit(struct got_gitconfig
*conf
, int fd
)
400 const struct got_error
*err
= NULL
;
403 char *new_conf_addr
= 0;
406 if (fstat(fd
, &st
)) {
407 err
= got_error_from_errno("fstat");
412 new_conf_addr
= malloc(sz
);
413 if (new_conf_addr
== NULL
) {
414 err
= got_error_from_errno("malloc");
417 /* XXX I assume short reads won't happen here. */
418 if (read(fd
, new_conf_addr
, sz
) != (int)sz
) {
419 err
= got_error_from_errno("read");
423 trans
= conf_begin(conf
);
425 err
= conf_parse(conf
, trans
, new_conf_addr
, sz
);
429 /* Free potential existing configuration. */
431 conf_end(conf
, trans
, 1);
432 conf
->addr
= new_conf_addr
;
441 * Return the numeric value denoted by TAG in section SECTION or DEF
442 * if that tag does not exist.
445 got_gitconfig_get_num(struct got_gitconfig
*conf
, const char *section
,
446 const char *tag
, int def
)
448 char *value
= got_gitconfig_get_str(conf
, section
, tag
);
455 /* Validate X according to the range denoted by TAG in section SECTION. */
457 got_gitconfig_match_num(struct got_gitconfig
*conf
, char *section
, char *tag
,
460 char *value
= got_gitconfig_get_str(conf
, section
, tag
);
461 int val
, min
, max
, n
;
465 n
= sscanf(value
, "%d,%d:%d", &val
, &min
, &max
);
468 LOG_DBG((LOG_MISC
, 95, "got_gitconfig_match_num: %s:%s %d==%d?",
469 section
, tag
, val
, x
));
472 LOG_DBG((LOG_MISC
, 95, "got_gitconfig_match_num: %s:%s %d<=%d<=%d?",
473 section
, tag
, min
, x
, max
));
474 return min
<= x
&& max
>= x
;
476 log_error("got_gitconfig_match_num: section %s tag %s: invalid number "
477 "spec %s", section
, tag
, value
);
482 /* Return the string value denoted by TAG in section SECTION. */
484 got_gitconfig_get_str(struct got_gitconfig
*conf
, const char *section
,
487 struct got_gitconfig_binding
*cb
;
489 for (cb
= LIST_FIRST(&conf
->bindings
[conf_hash(section
)]); cb
;
490 cb
= LIST_NEXT(cb
, link
))
491 if (strcasecmp(section
, cb
->section
) == 0 &&
492 strcasecmp(tag
, cb
->tag
) == 0) {
493 LOG_DBG((LOG_MISC
, 95, "got_gitconfig_get_str: [%s]:%s->%s",
494 section
, tag
, cb
->value
));
497 LOG_DBG((LOG_MISC
, 95,
498 "got_gitconfig_get_str: configuration value not found [%s]:%s", section
,
503 const struct got_error
*
504 got_gitconfig_get_section_list(struct got_gitconfig_list
**sections
,
505 struct got_gitconfig
*conf
)
507 const struct got_error
*err
= NULL
;
508 struct got_gitconfig_list
*list
= NULL
;
509 struct got_gitconfig_list_node
*node
= 0;
510 struct got_gitconfig_binding
*cb
;
515 list
= malloc(sizeof *list
);
517 return got_error_from_errno("malloc");
518 TAILQ_INIT(&list
->fields
);
520 for (i
= 0; i
< nitems(conf
->bindings
); i
++) {
521 for (cb
= LIST_FIRST(&conf
->bindings
[i
]); cb
;
522 cb
= LIST_NEXT(cb
, link
)) {
523 int section_present
= 0;
524 TAILQ_FOREACH(node
, &list
->fields
, link
) {
525 if (strcmp(node
->field
, cb
->section
) == 0) {
533 node
= calloc(1, sizeof *node
);
535 err
= got_error_from_errno("calloc");
538 node
->field
= strdup(cb
->section
);
540 err
= got_error_from_errno("strdup");
543 TAILQ_INSERT_TAIL(&list
->fields
, node
, link
);
553 got_gitconfig_free_list(list
);
558 * Build a list of string values out of the comma separated value denoted by
561 struct got_gitconfig_list
*
562 got_gitconfig_get_list(struct got_gitconfig
*conf
, char *section
, char *tag
)
564 char *liststr
= 0, *p
, *field
, *t
;
565 struct got_gitconfig_list
*list
= 0;
566 struct got_gitconfig_list_node
*node
= 0;
568 list
= malloc(sizeof *list
);
571 TAILQ_INIT(&list
->fields
);
573 liststr
= got_gitconfig_get_str(conf
, section
, tag
);
576 liststr
= strdup(liststr
);
580 while ((field
= strsep(&p
, ",")) != NULL
) {
581 /* Skip leading whitespace */
582 while (isspace((unsigned char)*field
))
584 /* Skip trailing whitespace */
586 for (t
= p
- 1; t
> field
&& isspace((unsigned char)*t
); t
--)
588 if (*field
== '\0') {
589 log_print("got_gitconfig_get_list: empty field, ignoring...");
593 node
= calloc(1, sizeof *node
);
596 node
->field
= strdup(field
);
599 TAILQ_INSERT_TAIL(&list
->fields
, node
, link
);
607 got_gitconfig_free_list(list
);
612 struct got_gitconfig_list
*
613 got_gitconfig_get_tag_list(struct got_gitconfig
*conf
, const char *section
)
615 struct got_gitconfig_list
*list
= 0;
616 struct got_gitconfig_list_node
*node
= 0;
617 struct got_gitconfig_binding
*cb
;
619 list
= malloc(sizeof *list
);
622 TAILQ_INIT(&list
->fields
);
624 for (cb
= LIST_FIRST(&conf
->bindings
[conf_hash(section
)]); cb
;
625 cb
= LIST_NEXT(cb
, link
))
626 if (strcasecmp(section
, cb
->section
) == 0) {
628 node
= calloc(1, sizeof *node
);
631 node
->field
= strdup(cb
->tag
);
634 TAILQ_INSERT_TAIL(&list
->fields
, node
, link
);
641 got_gitconfig_free_list(list
);
646 got_gitconfig_free_list(struct got_gitconfig_list
*list
)
648 struct got_gitconfig_list_node
*node
= TAILQ_FIRST(&list
->fields
);
651 TAILQ_REMOVE(&list
->fields
, node
, link
);
654 node
= TAILQ_FIRST(&list
->fields
);
660 got_gitconfig_trans_node(struct got_gitconfig
*conf
, int transaction
,
661 enum got_gitconfig_op op
, char *section
, char *tag
, char *value
,
662 int override
, int is_default
)
664 struct got_gitconfig_trans
*node
;
666 node
= calloc(1, sizeof *node
);
668 log_error("got_gitconfig_trans_node: calloc (1, %lu) failed",
669 (unsigned long)sizeof *node
);
672 node
->trans
= transaction
;
674 node
->override
= override
;
675 node
->is_default
= is_default
;
676 if (section
&& (node
->section
= strdup(section
)) == NULL
)
678 if (tag
&& (node
->tag
= strdup(tag
)) == NULL
)
680 if (value
&& (node
->value
= strdup(value
)) == NULL
)
682 TAILQ_INSERT_TAIL(&conf
->trans_queue
, node
, link
);
693 /* Queue a set operation. */
695 got_gitconfig_set(struct got_gitconfig
*conf
, int transaction
, char *section
,
696 char *tag
, char *value
, int override
, int is_default
)
698 return got_gitconfig_trans_node(conf
, transaction
, CONF_SET
, section
,
699 tag
, value
, override
, is_default
);
702 /* Queue a remove operation. */
704 got_gitconfig_remove(struct got_gitconfig
*conf
, int transaction
,
705 char *section
, char *tag
)
707 return got_gitconfig_trans_node(conf
, transaction
, CONF_REMOVE
,
708 section
, tag
, NULL
, 0, 0);
711 /* Queue a remove section operation. */
713 got_gitconfig_remove_section(struct got_gitconfig
*conf
, int transaction
,
716 return got_gitconfig_trans_node(conf
, transaction
, CONF_REMOVE_SECTION
,
717 section
, NULL
, NULL
, 0, 0);