2 * collectd - src/liboconfig/oconfig.c
3 * Copyright (C) 2006,2007 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
36 extern int yyparse(void);
38 oconfig_item_t *ci_root;
41 static void yyset_in(FILE *fd) { yyin = fd; } /* void yyset_in */
43 static oconfig_item_t *oconfig_parse_fh(FILE *fh) {
52 status = snprintf(file, sizeof(file), "<fd#%d>", fileno(fh));
54 if ((status < 0) || (((size_t)status) >= sizeof(file))) {
57 file[sizeof(file) - 1] = '\0';
64 fprintf(stderr, "yyparse returned error #%i\n", status);
75 } /* oconfig_item_t *oconfig_parse_fh */
77 oconfig_item_t *oconfig_parse_file(const char *file) {
83 fh = fopen(file, "r");
85 fprintf(stderr, "fopen (%s) failed: %s\n", file, strerror(errno));
89 ret = oconfig_parse_fh(fh);
95 } /* oconfig_item_t *oconfig_parse_file */
97 oconfig_item_t *oconfig_clone(const oconfig_item_t *ci_orig) {
98 oconfig_item_t *ci_copy;
100 ci_copy = calloc(1, sizeof(*ci_copy));
101 if (ci_copy == NULL) {
102 fprintf(stderr, "calloc failed.\n");
105 ci_copy->values = NULL;
106 ci_copy->parent = NULL;
107 ci_copy->children = NULL;
109 ci_copy->key = strdup(ci_orig->key);
110 if (ci_copy->key == NULL) {
111 fprintf(stderr, "strdup failed.\n");
116 if (ci_orig->values_num > 0) /* {{{ */
118 ci_copy->values = (oconfig_value_t *)calloc((size_t)ci_orig->values_num,
119 sizeof(*ci_copy->values));
120 if (ci_copy->values == NULL) {
121 fprintf(stderr, "calloc failed.\n");
126 ci_copy->values_num = ci_orig->values_num;
128 for (int i = 0; i < ci_copy->values_num; i++) {
129 ci_copy->values[i].type = ci_orig->values[i].type;
130 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING) {
131 ci_copy->values[i].value.string =
132 strdup(ci_orig->values[i].value.string);
133 if (ci_copy->values[i].value.string == NULL) {
134 fprintf(stderr, "strdup failed.\n");
135 oconfig_free(ci_copy);
138 } else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
140 ci_copy->values[i].value = ci_orig->values[i].value;
143 } /* }}} if (ci_orig->values_num > 0) */
145 if (ci_orig->children_num > 0) /* {{{ */
147 ci_copy->children = (oconfig_item_t *)calloc((size_t)ci_orig->children_num,
148 sizeof(*ci_copy->children));
149 if (ci_copy->children == NULL) {
150 fprintf(stderr, "calloc failed.\n");
151 oconfig_free(ci_copy);
154 ci_copy->children_num = ci_orig->children_num;
156 for (int i = 0; i < ci_copy->children_num; i++) {
157 oconfig_item_t *child;
159 child = oconfig_clone(ci_orig->children + i);
161 oconfig_free(ci_copy);
164 child->parent = ci_copy;
165 ci_copy->children[i] = *child;
167 } /* for (i = 0; i < ci_copy->children_num; i++) */
168 } /* }}} if (ci_orig->children_num > 0) */
171 } /* oconfig_item_t *oconfig_clone */
173 static void oconfig_free_all(oconfig_item_t *ci) {
180 for (int i = 0; i < ci->values_num; i++)
181 if ((ci->values[i].type == OCONFIG_TYPE_STRING) &&
182 (NULL != ci->values[i].value.string))
183 free(ci->values[i].value.string);
185 if (ci->values != NULL)
188 for (int i = 0; i < ci->children_num; i++)
189 oconfig_free_all(ci->children + i);
191 if (ci->children != NULL)
195 void oconfig_free(oconfig_item_t *ci) {
196 oconfig_free_all(ci);