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;
29 oconfig_item_t *ci_root;
30 const char *c_file;
32 static void yyset_in (FILE *fd)
33 {
34 yyin = fd;
35 } /* void yyset_in */
37 oconfig_item_t *oconfig_parse_fh (FILE *fh)
38 {
39 int status;
40 oconfig_item_t *ret;
42 char file[10];
44 yyset_in (fh);
46 if (NULL == c_file) {
47 int status;
49 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
51 if ((status < 0) || (status >= sizeof (file))) {
52 c_file = "<unknown>";
53 }
54 else {
55 file[sizeof (file) - 1] = '\0';
56 c_file = file;
57 }
58 }
60 status = yyparse ();
61 if (status != 0)
62 {
63 fprintf (stderr, "yyparse returned error #%i\n", status);
64 c_file = NULL;
65 return (NULL);
66 }
68 c_file = NULL;
70 ret = ci_root;
71 ci_root = NULL;
72 yyset_in ((FILE *) 0);
74 return (ret);
75 } /* oconfig_item_t *oconfig_parse_fh */
77 oconfig_item_t *oconfig_parse_file (const char *file)
78 {
79 FILE *fh;
80 oconfig_item_t *ret;
82 c_file = file;
84 fh = fopen (file, "r");
85 if (fh == NULL)
86 {
87 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
88 return (NULL);
89 }
91 ret = oconfig_parse_fh (fh);
92 fclose (fh);
94 c_file = NULL;
96 return (ret);
97 } /* oconfig_item_t *oconfig_parse_file */
99 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
100 {
101 oconfig_item_t *ci_copy;
103 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
104 if (ci_copy == NULL)
105 {
106 fprintf (stderr, "malloc failed.\n");
107 return (NULL);
108 }
109 memset (ci_copy, 0, sizeof (*ci_copy));
110 ci_copy->values = NULL;
111 ci_copy->parent = NULL;
112 ci_copy->children = NULL;
114 ci_copy->key = strdup (ci_orig->key);
115 if (ci_copy->key == NULL)
116 {
117 fprintf (stderr, "strdup failed.\n");
118 free (ci_copy);
119 return (NULL);
120 }
122 if (ci_orig->values_num > 0) /* {{{ */
123 {
124 int i;
126 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
127 sizeof (*ci_copy->values));
128 if (ci_copy->values == NULL)
129 {
130 fprintf (stderr, "calloc failed.\n");
131 free (ci_copy->key);
132 free (ci_copy);
133 return (NULL);
134 }
135 ci_copy->values_num = ci_orig->values_num;
137 for (i = 0; i < ci_copy->values_num; i++)
138 {
139 ci_copy->values[i].type = ci_orig->values[i].type;
140 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
141 {
142 ci_copy->values[i].value.string
143 = strdup (ci_orig->values[i].value.string);
144 if (ci_copy->values[i].value.string == NULL)
145 {
146 fprintf (stderr, "strdup failed.\n");
147 oconfig_free (ci_copy);
148 return (NULL);
149 }
150 }
151 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
152 {
153 ci_copy->values[i].value = ci_orig->values[i].value;
154 }
155 }
156 } /* }}} if (ci_orig->values_num > 0) */
158 if (ci_orig->children_num > 0) /* {{{ */
159 {
160 int i;
162 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
163 sizeof (*ci_copy->children));
164 if (ci_copy->children == NULL)
165 {
166 fprintf (stderr, "calloc failed.\n");
167 oconfig_free (ci_copy);
168 return (NULL);
169 }
170 ci_copy->children_num = ci_orig->children_num;
172 for (i = 0; i < ci_copy->children_num; i++)
173 {
174 oconfig_item_t *child;
176 child = oconfig_clone (ci_orig->children + i);
177 if (child == NULL)
178 {
179 oconfig_free (ci_copy);
180 return (NULL);
181 }
182 child->parent = ci_copy;
183 ci_copy->children[i] = *child;
184 free (child);
185 } /* for (i = 0; i < ci_copy->children_num; i++) */
186 } /* }}} if (ci_orig->children_num > 0) */
188 return (ci_copy);
189 } /* oconfig_item_t *oconfig_clone */
191 void oconfig_free (oconfig_item_t *ci)
192 {
193 int i;
195 if (ci == NULL)
196 return;
198 if (ci->key != NULL)
199 free (ci->key);
201 for (i = 0; i < ci->values_num; i++)
202 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
203 && (NULL != ci->values[i].value.string))
204 free (ci->values[i].value.string);
206 if (ci->values != NULL)
207 free (ci->values);
209 for (i = 0; i < ci->children_num; i++)
210 oconfig_free (ci->children + i);
212 if (ci->children != NULL)
213 free (ci->children);
214 }
216 /*
217 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
218 */