1 /**
2 * collectd - src/liboconfig/oconfig.c
3 * Copyright (C) 2006,2007 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
26
27 #include <assert.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "oconfig.h"
34
35 extern FILE *yyin;
36 extern int yyparse(void);
37
38 oconfig_item_t *ci_root;
39 const char *c_file;
40
yyset_in(FILE * fd)41 static void yyset_in(FILE *fd) { yyin = fd; } /* void yyset_in */
42
oconfig_parse_fh(FILE * fh)43 static oconfig_item_t *oconfig_parse_fh(FILE *fh) {
44 int status;
45 oconfig_item_t *ret;
46
47 char file[10];
48
49 yyset_in(fh);
50
51 if (NULL == c_file) {
52 status = snprintf(file, sizeof(file), "<fd#%d>", fileno(fh));
53
54 if ((status < 0) || (((size_t)status) >= sizeof(file))) {
55 c_file = "<unknown>";
56 } else {
57 file[sizeof(file) - 1] = '\0';
58 c_file = file;
59 }
60 }
61
62 status = yyparse();
63 if (status != 0) {
64 fprintf(stderr, "yyparse returned error #%i\n", status);
65 return NULL;
66 }
67
68 c_file = NULL;
69
70 ret = ci_root;
71 ci_root = NULL;
72 yyset_in((FILE *)0);
73
74 return ret;
75 } /* oconfig_item_t *oconfig_parse_fh */
76
oconfig_parse_file(const char * file)77 oconfig_item_t *oconfig_parse_file(const char *file) {
78 FILE *fh;
79 oconfig_item_t *ret;
80
81 c_file = file;
82
83 fh = fopen(file, "r");
84 if (fh == NULL) {
85 fprintf(stderr, "fopen (%s) failed: %s\n", file, strerror(errno));
86 return NULL;
87 }
88
89 ret = oconfig_parse_fh(fh);
90 fclose(fh);
91
92 c_file = NULL;
93
94 return ret;
95 } /* oconfig_item_t *oconfig_parse_file */
96
oconfig_clone(const oconfig_item_t * ci_orig)97 oconfig_item_t *oconfig_clone(const oconfig_item_t *ci_orig) {
98 oconfig_item_t *ci_copy;
99
100 ci_copy = calloc(1, sizeof(*ci_copy));
101 if (ci_copy == NULL) {
102 fprintf(stderr, "calloc failed.\n");
103 return NULL;
104 }
105 ci_copy->values = NULL;
106 ci_copy->parent = NULL;
107 ci_copy->children = NULL;
108
109 ci_copy->key = strdup(ci_orig->key);
110 if (ci_copy->key == NULL) {
111 fprintf(stderr, "strdup failed.\n");
112 free(ci_copy);
113 return NULL;
114 }
115
116 if (ci_orig->values_num > 0) /* {{{ */
117 {
118 ci_copy->values = calloc(ci_orig->values_num, sizeof(*ci_copy->values));
119 if (ci_copy->values == NULL) {
120 fprintf(stderr, "calloc failed.\n");
121 free(ci_copy->key);
122 free(ci_copy);
123 return NULL;
124 }
125 ci_copy->values_num = ci_orig->values_num;
126
127 for (int i = 0; i < ci_copy->values_num; i++) {
128 ci_copy->values[i].type = ci_orig->values[i].type;
129 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING) {
130 ci_copy->values[i].value.string =
131 strdup(ci_orig->values[i].value.string);
132 if (ci_copy->values[i].value.string == NULL) {
133 fprintf(stderr, "strdup failed.\n");
134 oconfig_free(ci_copy);
135 return NULL;
136 }
137 } else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
138 {
139 ci_copy->values[i].value = ci_orig->values[i].value;
140 }
141 }
142 } /* }}} if (ci_orig->values_num > 0) */
143
144 if (ci_orig->children_num > 0) /* {{{ */
145 {
146 ci_copy->children =
147 calloc(ci_orig->children_num, sizeof(*ci_copy->children));
148 if (ci_copy->children == NULL) {
149 fprintf(stderr, "calloc failed.\n");
150 oconfig_free(ci_copy);
151 return NULL;
152 }
153 ci_copy->children_num = ci_orig->children_num;
154
155 for (int i = 0; i < ci_copy->children_num; i++) {
156 oconfig_item_t *child;
157
158 child = oconfig_clone(ci_orig->children + i);
159 if (child == NULL) {
160 oconfig_free(ci_copy);
161 return NULL;
162 }
163 child->parent = ci_copy;
164 ci_copy->children[i] = *child;
165 free(child);
166 } /* for (i = 0; i < ci_copy->children_num; i++) */
167 } /* }}} if (ci_orig->children_num > 0) */
168
169 return ci_copy;
170 } /* oconfig_item_t *oconfig_clone */
171
oconfig_free_all(oconfig_item_t * ci)172 static void oconfig_free_all(oconfig_item_t *ci) {
173 if (ci == NULL)
174 return;
175
176 if (ci->key != NULL)
177 free(ci->key);
178
179 for (int i = 0; i < ci->values_num; i++)
180 if ((ci->values[i].type == OCONFIG_TYPE_STRING) &&
181 (NULL != ci->values[i].value.string))
182 free(ci->values[i].value.string);
183
184 if (ci->values != NULL)
185 free(ci->values);
186
187 for (int i = 0; i < ci->children_num; i++)
188 oconfig_free_all(ci->children + i);
189
190 if (ci->children != NULL)
191 free(ci->children);
192 }
193
oconfig_free(oconfig_item_t * ci)194 void oconfig_free(oconfig_item_t *ci) {
195 oconfig_free_all(ci);
196 free(ci);
197 }
198