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