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 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <errno.h>
25 #include "oconfig.h"
27 extern FILE *yyin;
28 extern int yyparse (void);
30 oconfig_item_t *ci_root;
31 const char *c_file;
33 static void yyset_in (FILE *fd)
34 {
35 yyin = fd;
36 } /* void yyset_in */
38 oconfig_item_t *oconfig_parse_fh (FILE *fh)
39 {
40 int status;
41 oconfig_item_t *ret;
43 char file[10];
45 yyset_in (fh);
47 if (NULL == c_file) {
48 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
50 if ((status < 0) || (status >= sizeof (file))) {
51 c_file = "<unknown>";
52 }
53 else {
54 file[sizeof (file) - 1] = '\0';
55 c_file = file;
56 }
57 }
59 status = yyparse ();
60 if (status != 0)
61 {
62 fprintf (stderr, "yyparse returned error #%i\n", status);
63 return (NULL);
64 }
66 c_file = NULL;
68 ret = ci_root;
69 ci_root = NULL;
70 yyset_in ((FILE *) 0);
72 return (ret);
73 } /* oconfig_item_t *oconfig_parse_fh */
75 oconfig_item_t *oconfig_parse_file (const char *file)
76 {
77 FILE *fh;
78 oconfig_item_t *ret;
80 c_file = file;
82 fh = fopen (file, "r");
83 if (fh == NULL)
84 {
85 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
86 return (NULL);
87 }
89 ret = oconfig_parse_fh (fh);
90 fclose (fh);
92 c_file = NULL;
94 return (ret);
95 } /* oconfig_item_t *oconfig_parse_file */
97 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
98 {
99 oconfig_item_t *ci_copy;
101 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
102 if (ci_copy == NULL)
103 {
104 fprintf (stderr, "malloc failed.\n");
105 return (NULL);
106 }
107 memset (ci_copy, 0, sizeof (*ci_copy));
108 ci_copy->values = NULL;
109 ci_copy->parent = NULL;
110 ci_copy->children = NULL;
112 ci_copy->key = strdup (ci_orig->key);
113 if (ci_copy->key == NULL)
114 {
115 fprintf (stderr, "strdup failed.\n");
116 free (ci_copy);
117 return (NULL);
118 }
120 if (ci_orig->values_num > 0) /* {{{ */
121 {
122 int i;
124 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
125 sizeof (*ci_copy->values));
126 if (ci_copy->values == NULL)
127 {
128 fprintf (stderr, "calloc failed.\n");
129 free (ci_copy->key);
130 free (ci_copy);
131 return (NULL);
132 }
133 ci_copy->values_num = ci_orig->values_num;
135 for (i = 0; i < ci_copy->values_num; i++)
136 {
137 ci_copy->values[i].type = ci_orig->values[i].type;
138 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
139 {
140 ci_copy->values[i].value.string
141 = strdup (ci_orig->values[i].value.string);
142 if (ci_copy->values[i].value.string == NULL)
143 {
144 fprintf (stderr, "strdup failed.\n");
145 oconfig_free (ci_copy);
146 return (NULL);
147 }
148 }
149 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
150 {
151 ci_copy->values[i].value = ci_orig->values[i].value;
152 }
153 }
154 } /* }}} if (ci_orig->values_num > 0) */
156 if (ci_orig->children_num > 0) /* {{{ */
157 {
158 int i;
160 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
161 sizeof (*ci_copy->children));
162 if (ci_copy->children == NULL)
163 {
164 fprintf (stderr, "calloc failed.\n");
165 oconfig_free (ci_copy);
166 return (NULL);
167 }
168 ci_copy->children_num = ci_orig->children_num;
170 for (i = 0; i < ci_copy->children_num; i++)
171 {
172 oconfig_item_t *child;
174 child = oconfig_clone (ci_orig->children + i);
175 if (child == NULL)
176 {
177 oconfig_free (ci_copy);
178 return (NULL);
179 }
180 child->parent = ci_copy;
181 ci_copy->children[i] = *child;
182 free (child);
183 } /* for (i = 0; i < ci_copy->children_num; i++) */
184 } /* }}} if (ci_orig->children_num > 0) */
186 return (ci_copy);
187 } /* oconfig_item_t *oconfig_clone */
189 static void oconfig_free_all (oconfig_item_t *ci)
190 {
191 int i;
193 if (ci == NULL)
194 return;
196 if (ci->key != NULL)
197 free (ci->key);
199 for (i = 0; i < ci->values_num; i++)
200 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
201 && (NULL != ci->values[i].value.string))
202 free (ci->values[i].value.string);
204 if (ci->values != NULL)
205 free (ci->values);
207 for (i = 0; i < ci->children_num; i++)
208 oconfig_free_all (ci->children + i);
210 if (ci->children != NULL)
211 free (ci->children);
212 }
214 void oconfig_free (oconfig_item_t *ci)
215 {
216 oconfig_free_all (ci);
217 free (ci);
218 ci = NULL;
219 }
221 /*
222 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
223 */