Merge branch 'collectd-5.5'
[collectd.git] / src / liboconfig / oconfig.c
1 /**
2  * collectd - src/liboconfig/oconfig.c
3  * Copyright (C) 2006,2007  Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <errno.h>
32
33 #include "oconfig.h"
34
35 extern FILE *yyin;
36 extern int yyparse (void);
37
38 oconfig_item_t *ci_root;
39 const char     *c_file;
40
41 static void yyset_in  (FILE *fd)
42 {
43   yyin = fd;
44 } /* void yyset_in */
45
46 oconfig_item_t *oconfig_parse_fh (FILE *fh)
47 {
48   int status;
49   oconfig_item_t *ret;
50
51   char file[10];
52
53   yyset_in (fh);
54
55   if (NULL == c_file) {
56     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
57
58     if ((status < 0) || (((size_t) status) >= sizeof (file))) {
59       c_file = "<unknown>";
60     }
61     else {
62       file[sizeof (file) - 1] = '\0';
63       c_file = file;
64     }
65   }
66
67   status = yyparse ();
68   if (status != 0)
69   {
70     fprintf (stderr, "yyparse returned error #%i\n", status);
71     return (NULL);
72   }
73
74   c_file = NULL;
75
76   ret = ci_root;
77   ci_root = NULL;
78   yyset_in ((FILE *) 0);
79
80   return (ret);
81 } /* oconfig_item_t *oconfig_parse_fh */
82
83 oconfig_item_t *oconfig_parse_file (const char *file)
84 {
85   FILE *fh;
86   oconfig_item_t *ret;
87
88   c_file = file;
89
90   fh = fopen (file, "r");
91   if (fh == NULL)
92   {
93     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
94     return (NULL);
95   }
96
97   ret = oconfig_parse_fh (fh);
98   fclose (fh);
99
100   c_file = NULL;
101
102   return (ret);
103 } /* oconfig_item_t *oconfig_parse_file */
104
105 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
106 {
107   oconfig_item_t *ci_copy;
108
109   ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
110   if (ci_copy == NULL)
111   {
112     fprintf (stderr, "malloc failed.\n");
113     return (NULL);
114   }
115   memset (ci_copy, 0, sizeof (*ci_copy));
116   ci_copy->values = NULL;
117   ci_copy->parent = NULL;
118   ci_copy->children = NULL;
119
120   ci_copy->key = strdup (ci_orig->key);
121   if (ci_copy->key == NULL)
122   {
123     fprintf (stderr, "strdup failed.\n");
124     free (ci_copy);
125     return (NULL);
126   }
127
128   if (ci_orig->values_num > 0) /* {{{ */
129   {
130     int i;
131
132     ci_copy->values = (oconfig_value_t *) calloc ((size_t) ci_orig->values_num,
133         sizeof (*ci_copy->values));
134     if (ci_copy->values == NULL)
135     {
136       fprintf (stderr, "calloc failed.\n");
137       free (ci_copy->key);
138       free (ci_copy);
139       return (NULL);
140     }
141     ci_copy->values_num = ci_orig->values_num;
142
143     for (i = 0; i < ci_copy->values_num; i++)
144     {
145        ci_copy->values[i].type = ci_orig->values[i].type;
146        if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
147        {
148          ci_copy->values[i].value.string = 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) */
162
163   if (ci_orig->children_num > 0) /* {{{ */
164   {
165     int i;
166
167     ci_copy->children = (oconfig_item_t *) calloc ((size_t) 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;
176
177     for (i = 0; i < ci_copy->children_num; i++)
178     {
179       oconfig_item_t *child;
180       
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) */
192
193   return (ci_copy);
194 } /* oconfig_item_t *oconfig_clone */
195
196 void oconfig_free_all (oconfig_item_t *ci)
197 {
198   int i;
199
200   if (ci == NULL)
201     return;
202
203   if (ci->key != NULL)
204     free (ci->key);
205
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);
210
211   if (ci->values != NULL)
212     free (ci->values);
213
214   for (i = 0; i < ci->children_num; i++)
215     oconfig_free_all (ci->children + i);
216
217   if (ci->children != NULL)
218     free (ci->children);
219 }
220
221 void oconfig_free (oconfig_item_t *ci)
222 {
223   oconfig_free_all (ci);
224   free (ci);
225   ci = NULL;
226 }
227
228 /*
229  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
230  */