1 /**
2 * oconfig - src/oconfig.c
3 * Copyright (C) 2006,2007 Florian octo Forster <octo at verplant.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif /* HAVE_CONFIG_H */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
29 #include "oconfig.h"
31 extern FILE *yyin;
32 int yyparse (void);
34 oconfig_item_t *ci_root;
35 const char *c_file;
37 static void yyset_in (FILE *fd)
38 {
39 yyin = fd;
40 } /* void yyset_in */
42 oconfig_item_t *oconfig_parse_fh (FILE *fh)
43 {
44 int status;
45 oconfig_item_t *ret;
47 char file[10];
49 yyset_in (fh);
51 if (NULL == c_file) {
52 int status;
54 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
56 if ((status < 0) || (status >= sizeof (file))) {
57 c_file = "<unknown>";
58 }
59 else {
60 file[sizeof (file) - 1] = '\0';
61 c_file = file;
62 }
63 }
65 status = yyparse ();
66 if (status != 0)
67 {
68 fprintf (stderr, "yyparse returned error #%i\n", status);
69 c_file = NULL;
70 return (NULL);
71 }
73 c_file = NULL;
75 ret = ci_root;
76 ci_root = NULL;
77 yyset_in ((FILE *) 0);
79 return (ret);
80 } /* oconfig_item_t *oconfig_parse_fh */
82 oconfig_item_t *oconfig_parse_file (const char *file)
83 {
84 FILE *fh;
85 oconfig_item_t *ret;
87 c_file = file;
89 fh = fopen (file, "r");
90 if (fh == NULL)
91 {
92 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
93 return (NULL);
94 }
96 ret = oconfig_parse_fh (fh);
97 fclose (fh);
99 c_file = NULL;
101 return (ret);
102 } /* oconfig_item_t *oconfig_parse_file */
104 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
105 {
106 oconfig_item_t *ci_copy;
108 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
109 if (ci_copy == NULL)
110 {
111 fprintf (stderr, "malloc failed.\n");
112 return (NULL);
113 }
114 memset (ci_copy, 0, sizeof (*ci_copy));
115 ci_copy->values = NULL;
116 ci_copy->parent = NULL;
117 ci_copy->children = NULL;
119 ci_copy->key = strdup (ci_orig->key);
120 if (ci_copy->key == NULL)
121 {
122 fprintf (stderr, "strdup failed.\n");
123 free (ci_copy);
124 return (NULL);
125 }
127 if (ci_orig->values_num > 0) /* {{{ */
128 {
129 int i;
131 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
132 sizeof (*ci_copy->values));
133 if (ci_copy->values == NULL)
134 {
135 fprintf (stderr, "calloc failed.\n");
136 free (ci_copy->key);
137 free (ci_copy);
138 return (NULL);
139 }
140 ci_copy->values_num = ci_orig->values_num;
142 for (i = 0; i < ci_copy->values_num; i++)
143 {
144 ci_copy->values[i].type = ci_orig->values[i].type;
145 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
146 {
147 ci_copy->values[i].value.string
148 = strdup (ci_orig->values[i].value.string);
149 if (ci_copy->values[i].value.string == NULL)
150 {
151 fprintf (stderr, "strdup failed.\n");
152 oconfig_free (ci_copy);
153 return (NULL);
154 }
155 }
156 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
157 {
158 ci_copy->values[i].value = ci_orig->values[i].value;
159 }
160 }
161 } /* }}} if (ci_orig->values_num > 0) */
163 if (ci_orig->children_num > 0) /* {{{ */
164 {
165 int i;
167 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
168 sizeof (*ci_copy->children));
169 if (ci_copy->children == NULL)
170 {
171 fprintf (stderr, "calloc failed.\n");
172 oconfig_free (ci_copy);
173 return (NULL);
174 }
175 ci_copy->children_num = ci_orig->children_num;
177 for (i = 0; i < ci_copy->children_num; i++)
178 {
179 oconfig_item_t *child;
181 child = oconfig_clone (ci_orig->children + i);
182 if (child == NULL)
183 {
184 oconfig_free (ci_copy);
185 return (NULL);
186 }
187 child->parent = ci_copy;
188 ci_copy->children[i] = *child;
189 free (child);
190 } /* for (i = 0; i < ci_copy->children_num; i++) */
191 } /* }}} if (ci_orig->children_num > 0) */
193 return (ci_copy);
194 } /* oconfig_item_t *oconfig_clone */
196 void oconfig_free (oconfig_item_t *ci)
197 {
198 int i;
200 if (ci == NULL)
201 return;
203 if (ci->key != NULL)
204 free (ci->key);
206 for (i = 0; i < ci->values_num; i++)
207 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
208 && (NULL != ci->values[i].value.string))
209 free (ci->values[i].value.string);
211 if (ci->values != NULL)
212 free (ci->values);
214 for (i = 0; i < ci->children_num; i++)
215 oconfig_free (ci->children + i);
217 if (ci->children != NULL)
218 free (ci->children);
219 }
221 /*
222 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
223 */