Code

1f7093d334eb20de7420307c9225f4b6f0d8eb6a
[sysdb.git] / src / liboconfig / oconfig.c
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 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     int status;
50     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
52     if ((status < 0) || (status >= sizeof (file))) {
53       c_file = "<unknown>";
54     }
55     else {
56       file[sizeof (file) - 1] = '\0';
57       c_file = file;
58     }
59   }
61   status = yyparse ();
62   if (status != 0)
63   {
64     fprintf (stderr, "yyparse returned error #%i\n", status);
65     c_file = NULL;
66     return (NULL);
67   }
69   c_file = NULL;
71   ret = ci_root;
72   ci_root = NULL;
73   yyset_in ((FILE *) 0);
75   return (ret);
76 } /* oconfig_item_t *oconfig_parse_fh */
78 oconfig_item_t *oconfig_parse_file (const char *file)
79 {
80   FILE *fh;
81   oconfig_item_t *ret;
83   c_file = file;
85   fh = fopen (file, "r");
86   if (fh == NULL)
87   {
88     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
89     return (NULL);
90   }
92   ret = oconfig_parse_fh (fh);
93   fclose (fh);
95   c_file = NULL;
97   return (ret);
98 } /* oconfig_item_t *oconfig_parse_file */
100 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
102   oconfig_item_t *ci_copy;
104   ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
105   if (ci_copy == NULL)
106   {
107     fprintf (stderr, "malloc failed.\n");
108     return (NULL);
109   }
110   memset (ci_copy, 0, sizeof (*ci_copy));
111   ci_copy->values = NULL;
112   ci_copy->parent = NULL;
113   ci_copy->children = NULL;
115   ci_copy->key = strdup (ci_orig->key);
116   if (ci_copy->key == NULL)
117   {
118     fprintf (stderr, "strdup failed.\n");
119     free (ci_copy);
120     return (NULL);
121   }
123   if (ci_orig->values_num > 0) /* {{{ */
124   {
125     int i;
127     ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
128         sizeof (*ci_copy->values));
129     if (ci_copy->values == NULL)
130     {
131       fprintf (stderr, "calloc failed.\n");
132       free (ci_copy->key);
133       free (ci_copy);
134       return (NULL);
135     }
136     ci_copy->values_num = ci_orig->values_num;
138     for (i = 0; i < ci_copy->values_num; i++)
139     {
140        ci_copy->values[i].type = ci_orig->values[i].type;
141        if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
142        {
143          ci_copy->values[i].value.string
144            = strdup (ci_orig->values[i].value.string);
145          if (ci_copy->values[i].value.string == NULL)
146          {
147            fprintf (stderr, "strdup failed.\n");
148            oconfig_free (ci_copy);
149            return (NULL);
150          }
151        }
152        else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
153        {
154          ci_copy->values[i].value = ci_orig->values[i].value;
155        }
156     }
157   } /* }}} if (ci_orig->values_num > 0) */
159   if (ci_orig->children_num > 0) /* {{{ */
160   {
161     int i;
163     ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
164         sizeof (*ci_copy->children));
165     if (ci_copy->children == NULL)
166     {
167       fprintf (stderr, "calloc failed.\n");
168       oconfig_free (ci_copy);
169       return (NULL);
170     }
171     ci_copy->children_num = ci_orig->children_num;
173     for (i = 0; i < ci_copy->children_num; i++)
174     {
175       oconfig_item_t *child;
176       
177       child = oconfig_clone (ci_orig->children + i);
178       if (child == NULL)
179       {
180         oconfig_free (ci_copy);
181         return (NULL);
182       }
183       child->parent = ci_copy;
184       ci_copy->children[i] = *child;
185       free (child);
186     } /* for (i = 0; i < ci_copy->children_num; i++) */
187   } /* }}} if (ci_orig->children_num > 0) */
189   return (ci_copy);
190 } /* oconfig_item_t *oconfig_clone */
192 void oconfig_free (oconfig_item_t *ci)
194   int i;
196   if (ci == NULL)
197     return;
199   if (ci->key != NULL)
200     free (ci->key);
202   for (i = 0; i < ci->values_num; i++)
203     if ((ci->values[i].type == OCONFIG_TYPE_STRING)
204         && (NULL != ci->values[i].value.string))
205       free (ci->values[i].value.string);
207   if (ci->values != NULL)
208     free (ci->values);
210   for (i = 0; i < ci->children_num; i++)
211     oconfig_free (ci->children + i);
213   if (ci->children != NULL)
214     free (ci->children);
217 /*
218  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
219  */