-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from martinh/key-value-sections
Free-form dynamic key=value sections
- Loading branch information
Showing
12 changed files
with
274 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ nested | |
deprecated | ||
addsec | ||
parsebuf | ||
env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Example of how an application can allow free-form key=value strings as settings, | ||
* e.g., custom environment variables the program should set for child processes. | ||
* | ||
* env.conf: | ||
* env { | ||
* foo = bar | ||
* baz = foo | ||
* } | ||
*/ | ||
#include <err.h> | ||
#include <confuse.h> | ||
#include <stdio.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
const char *file = "env.conf"; | ||
cfg_opt_t opts[] = { | ||
CFG_SEC("env", NULL, CFGF_KEYSTRVAL), | ||
CFG_END() | ||
}; | ||
cfg_t *cfg, *sec; | ||
int rc; | ||
|
||
if (argc > 1) | ||
file = argv[1]; | ||
|
||
cfg = cfg_init(opts, 0); | ||
if (!cfg) | ||
err(1, "Failed cfg_init()"); | ||
|
||
rc = cfg_parse(cfg, file); | ||
if (rc != CFG_SUCCESS) { | ||
if (rc == CFG_FILE_ERROR) | ||
err(1, "Failed opening %s", file); | ||
|
||
errx(1, "Failed parsing %s", file); | ||
} | ||
|
||
sec = cfg_getsec(cfg, "env"); | ||
if (sec) { | ||
unsigned int i; | ||
|
||
for (i = 0; i < cfg_num(sec); i++) { | ||
cfg_opt_t *opt = cfg_getnopt(sec, i); | ||
|
||
printf("%s = \"%s\"\n", cfg_opt_name(opt), cfg_opt_getstr(opt)); | ||
} | ||
} | ||
|
||
// cfg_print(cfg, stdout); | ||
cfg_free(cfg); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
env { | ||
foo = bar | ||
baz = foo | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ | ||
/* | ||
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include <sys/types.h> | ||
#include <errno.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX | ||
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW | ||
*/ | ||
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) | ||
|
||
void * | ||
reallocarray(void *optr, size_t nmemb, size_t size) | ||
{ | ||
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && | ||
nmemb > 0 && SIZE_MAX / nmemb < size) { | ||
errno = ENOMEM; | ||
return NULL; | ||
} | ||
return realloc(optr, size * nmemb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* Test key=value pairs, e.g. env vars. a program should set to children */ | ||
|
||
#include "config.h" | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "check_confuse.h" | ||
|
||
static void check_byname(cfg_t *cfg, const char *key, const char *val) | ||
{ | ||
char *value; | ||
|
||
printf("Checking for key:%s, expected value %s\n", key, val); | ||
|
||
value = cfg_getstr(cfg, key); | ||
printf("Found value %s\n", value); | ||
|
||
fail_unless(value != NULL); | ||
fail_unless(!strcmp(value, val)); | ||
} | ||
|
||
static void check_keyval(cfg_t *cfg, unsigned int index, const char *key, const char *val) | ||
{ | ||
cfg_opt_t *opt; | ||
int rc; | ||
|
||
printf("Checking index %u for key:%s val:%s\n", index, key, val); | ||
|
||
opt = cfg_getnopt(cfg, index); | ||
fail_unless(opt != NULL); | ||
|
||
printf("Found key:%s val:%s\n", cfg_opt_name(opt), cfg_opt_getstr(opt)); | ||
|
||
rc =strcmp(cfg_opt_name(opt), key); | ||
fail_unless(rc == 0); | ||
|
||
rc =strcmp(cfg_opt_getstr(opt), val); | ||
fail_unless(rc == 0); | ||
} | ||
|
||
int main(void) | ||
{ | ||
cfg_opt_t opts[] = { | ||
CFG_SEC("env", NULL, CFGF_KEYSTRVAL), | ||
CFG_END() | ||
}; | ||
unsigned int num; | ||
cfg_t *cfg, *sec; | ||
cfg_opt_t *opt; | ||
char *key; | ||
int rc; | ||
|
||
cfg = cfg_init(opts, CFGF_NONE); | ||
fail_unless(cfg != NULL); | ||
|
||
rc = cfg_parse_buf(cfg, "env {\n" | ||
" foo=bar\n" | ||
" bar=for" | ||
" baz=qux\n" | ||
" bar=xyzzy\n" /* Should replace previous 'bar' */ | ||
"}"); | ||
fail_unless(rc == CFG_SUCCESS); | ||
|
||
sec = cfg_getsec(cfg, "env"); | ||
fail_unless(sec != NULL); | ||
|
||
/* Fuzz internals a bit, check for non-existing key */ | ||
cfg_getstr(sec, "some-key-not-in-the-config-file"); | ||
|
||
num = cfg_num(sec); | ||
fail_unless(num == 3); /* { foo, bar, baz } */ | ||
|
||
check_keyval(sec, 0, "foo", "bar"); | ||
check_keyval(sec, 1, "bar", "xyzzy"); | ||
check_keyval(sec, 2, "baz", "qux"); | ||
|
||
check_byname(sec, "foo", "bar"); | ||
check_byname(sec, "baz", "qux"); | ||
check_byname(sec, "bar", "xyzzy"); | ||
|
||
printf("PASS\n"); | ||
cfg_free(cfg); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Local Variables: | ||
* indent-tabs-mode: t | ||
* c-file-style: "linux" | ||
* End: | ||
*/ |