Merge branch 'collectd-5.4' into 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     int status;
57
58     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
59
60     if ((status < 0) || (status >= sizeof (file))) {
61       c_file = "<unknown>";
62     }
63     else {
64       file[sizeof (file) - 1] = '\0';
65       c_file = file;
66     }
67   }
68
69   status = yyparse ();
70   if (status != 0)
71   {
72     fprintf (stderr, "yyparse returned error #%i\n", status);
73     return (NULL);
74   }
75
76   c_file = NULL;
77
78   ret = ci_root;
79   ci_root = NULL;
80   yyset_in ((FILE *) 0);
81
82   return (ret);
83 } /* oconfig_item_t *oconfig_parse_fh */
84
85 oconfig_item_t *oconfig_parse_file (const char *file)
86 {
87   FILE *fh;
88   oconfig_item_t *ret;
89
90   c_file = file;
91
92   fh = fopen (file, "r");
93   if (fh == NULL)
94   {
95     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
96     return (NULL);
97   }
98
99   ret = oconfig_parse_fh (fh);
100   fclose (fh);
101
102   c_file = NULL;
103
104   return (ret);
105 } /* oconfig_item_t *oconfig_parse_file */
106
107 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
108 {
109   oconfig_item_t *ci_copy;
110
111   ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
112   if (ci_copy == NULL)
113   {
114     fprintf (stderr, "malloc failed.\n");
115     return (NULL);
116   }
117   memset (ci_copy, 0, sizeof (*ci_copy));
118   ci_copy->values = NULL;
119   ci_copy->parent = NULL;
120   ci_copy->children = NULL;
121
122   ci_copy->key = strdup (ci_orig->key);
123   if (ci_copy->key == NULL)
124   {
125     fprintf (stderr, "strdup failed.\n");
126     free (ci_copy);
127     return (NULL);
128   }
129
130   if (ci_orig->values_num > 0) /* {{{ */
131   {
132     int i;
133
134     ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
135         sizeof (*ci_copy->values));
136     if (ci_copy->values == NULL)
137     {
138       fprintf (stderr, "calloc failed.\n");
139       free (ci_copy->key);
140       free (ci_copy);
141       return (NULL);
142     }
143     ci_copy->values_num = ci_orig->values_num;
144
145     for (i = 0; i < ci_copy->values_num; i++)
146     {
147        ci_copy->values[i].type = ci_orig->values[i].type;
148        if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
149        {
150          ci_copy->values[i].value.string
151            = strdup (ci_orig->values[i].value.string);
152          if (ci_copy->values[i].value.string == NULL)
153          {
154            fprintf (stderr, "strdup failed.\n");
155            oconfig_free (ci_copy);
156            return (NULL);
157          }
158        }
159        else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
160        {
161          ci_copy->values[i].value = ci_orig->values[i].value;
162        }
163     }
164   } /* }}} if (ci_orig->values_num > 0) */
165
166   if (ci_orig->children_num > 0) /* {{{ */
167   {
168     int i;
169
170     ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
171         sizeof (*ci_copy->children));
172     if (ci_copy->children == NULL)
173     {
174       fprintf (stderr, "calloc failed.\n");
175       oconfig_free (ci_copy);
176       return (NULL);
177     }
178     ci_copy->children_num = ci_orig->children_num;
179
180     for (i = 0; i < ci_copy->children_num; i++)
181     {
182       oconfig_item_t *child;
183       
184       child = oconfig_clone (ci_orig->children + i);
185       if (child == NULL)
186       {
187         oconfig_free (ci_copy);
188         return (NULL);
189       }
190       child->parent = ci_copy;
191       ci_copy->children[i] = *child;
192       free (child);
193     } /* for (i = 0; i < ci_copy->children_num; i++) */
194   } /* }}} if (ci_orig->children_num > 0) */
195
196   return (ci_copy);
197 } /* oconfig_item_t *oconfig_clone */
198
199 void oconfig_free_all (oconfig_item_t *ci)
200 {
201   int i;
202
203   if (ci == NULL)
204     return;
205
206   if (ci->key != NULL)
207     free (ci->key);
208
209   for (i = 0; i < ci->values_num; i++)
210     if ((ci->values[i].type == OCONFIG_TYPE_STRING)
211         && (NULL != ci->values[i].value.string))
212       free (ci->values[i].value.string);
213
214   if (ci->values != NULL)
215     free (ci->values);
216
217   for (i = 0; i < ci->children_num; i++)
218     oconfig_free_all (ci->children + i);
219
220   if (ci->children != NULL)
221     free (ci->children);
222 }
223
224 void oconfig_free (oconfig_item_t *ci)
225 {
226   oconfig_free_all (ci);
227   free (ci);
228   ci = NULL;
229 }
230
231 /*
232  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
233  */