Autotoolization.
[collection4.git] / src / 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  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <errno.h>
24
25 #include "oconfig.h"
26
27 extern FILE *yyin;
28 int yyparse (void);
29
30 oconfig_item_t *ci_root;
31 const char     *c_file;
32
33 static void yyset_in  (FILE *fd)
34 {
35   yyin = fd;
36 } /* void yyset_in */
37
38 oconfig_item_t *oconfig_parse_fh (FILE *fh)
39 {
40   int status;
41   oconfig_item_t *ret;
42
43   char file[10];
44
45   yyset_in (fh);
46
47   if (NULL == c_file) {
48     int status;
49
50     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
51
52     if ((status < 0) || (((size_t) status) >= sizeof (file))) {
53       c_file = "<unknown>";
54     }
55     else {
56       file[sizeof (file) - 1] = '\0';
57       c_file = file;
58     }
59   }
60
61   status = yyparse ();
62   if (status != 0)
63   {
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
77 oconfig_item_t *oconfig_parse_file (const char *file)
78 {
79   FILE *fh;
80   oconfig_item_t *ret;
81
82   c_file = file;
83
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   }
90
91   ret = oconfig_parse_fh (fh);
92   fclose (fh);
93
94   c_file = NULL;
95
96   return (ret);
97 } /* oconfig_item_t *oconfig_parse_file */
98
99 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
100 {
101   oconfig_item_t *ci_copy;
102
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;
113
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   }
121
122   if (ci_orig->values_num > 0) /* {{{ */
123   {
124     int i;
125
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;
136
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) */
157
158   if (ci_orig->children_num > 0) /* {{{ */
159   {
160     int i;
161
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;
171
172     for (i = 0; i < ci_copy->children_num; i++)
173     {
174       oconfig_item_t *child;
175       
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) */
187
188   return (ci_copy);
189 } /* oconfig_item_t *oconfig_clone */
190
191 void oconfig_free (oconfig_item_t *ci)
192 {
193   int i;
194
195   if (ci == NULL)
196     return;
197
198   if (ci->key != NULL)
199     free (ci->key);
200
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);
205
206   if (ci->values != NULL)
207     free (ci->values);
208
209   for (i = 0; i < ci->children_num; i++)
210     oconfig_free (ci->children + i);
211
212   if (ci->children != NULL)
213     free (ci->children);
214 }
215
216 /*
217  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
218  */