Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / liboconfig / oconfig.c
1 /**
2  * SysDB - src/liboconfig/oconfig.c
3  * Based on collectd's liboconfig, <http://collectd.org>
4  * Copyright (C) 2006,2007  Florian Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
25 #if HAVE_CONFIG_H
26 #       include "config.h"
27 #endif /* HAVE_CONFIG_H */
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <errno.h>
35 #include "oconfig.h"
37 extern FILE *yyin;
38 int yyparse (void);
40 oconfig_item_t *ci_root;
41 const char     *c_file;
43 static void yyset_in  (FILE *fd)
44 {
45   yyin = fd;
46 } /* void yyset_in */
48 oconfig_item_t *oconfig_parse_fh (FILE *fh)
49 {
50   int status;
51   oconfig_item_t *ret;
53   char file[10];
55   yyset_in (fh);
57   if (NULL == c_file) {
58     int status;
60     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
62     if ((status < 0) || (status >= sizeof (file))) {
63       c_file = "<unknown>";
64     }
65     else {
66       file[sizeof (file) - 1] = '\0';
67       c_file = file;
68     }
69   }
71   status = yyparse ();
72   if (status != 0)
73   {
74     fprintf (stderr, "yyparse returned error #%i\n", status);
75     c_file = NULL;
76     return (NULL);
77   }
79   c_file = NULL;
81   ret = ci_root;
82   ci_root = NULL;
83   yyset_in ((FILE *) 0);
85   return (ret);
86 } /* oconfig_item_t *oconfig_parse_fh */
88 oconfig_item_t *oconfig_parse_file (const char *file)
89 {
90   FILE *fh;
91   oconfig_item_t *ret;
93   c_file = file;
95   fh = fopen (file, "r");
96   if (fh == NULL)
97   {
98     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
99     return (NULL);
100   }
102   ret = oconfig_parse_fh (fh);
103   fclose (fh);
105   c_file = NULL;
107   return (ret);
108 } /* oconfig_item_t *oconfig_parse_file */
110 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
112   oconfig_item_t *ci_copy;
114   ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
115   if (ci_copy == NULL)
116   {
117     fprintf (stderr, "malloc failed.\n");
118     return (NULL);
119   }
120   memset (ci_copy, 0, sizeof (*ci_copy));
121   ci_copy->values = NULL;
122   ci_copy->parent = NULL;
123   ci_copy->children = NULL;
125   ci_copy->key = strdup (ci_orig->key);
126   if (ci_copy->key == NULL)
127   {
128     fprintf (stderr, "strdup failed.\n");
129     free (ci_copy);
130     return (NULL);
131   }
133   if (ci_orig->values_num > 0) /* {{{ */
134   {
135     int i;
137     ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
138         sizeof (*ci_copy->values));
139     if (ci_copy->values == NULL)
140     {
141       fprintf (stderr, "calloc failed.\n");
142       free (ci_copy->key);
143       free (ci_copy);
144       return (NULL);
145     }
146     ci_copy->values_num = ci_orig->values_num;
148     for (i = 0; i < ci_copy->values_num; i++)
149     {
150        ci_copy->values[i].type = ci_orig->values[i].type;
151        if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
152        {
153          ci_copy->values[i].value.string
154            = strdup (ci_orig->values[i].value.string);
155          if (ci_copy->values[i].value.string == NULL)
156          {
157            fprintf (stderr, "strdup failed.\n");
158            oconfig_free (ci_copy);
159            return (NULL);
160          }
161        }
162        else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
163        {
164          ci_copy->values[i].value = ci_orig->values[i].value;
165        }
166     }
167   } /* }}} if (ci_orig->values_num > 0) */
169   if (ci_orig->children_num > 0) /* {{{ */
170   {
171     int i;
173     ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
174         sizeof (*ci_copy->children));
175     if (ci_copy->children == NULL)
176     {
177       fprintf (stderr, "calloc failed.\n");
178       oconfig_free (ci_copy);
179       return (NULL);
180     }
181     ci_copy->children_num = ci_orig->children_num;
183     for (i = 0; i < ci_copy->children_num; i++)
184     {
185       oconfig_item_t *child;
186       
187       child = oconfig_clone (ci_orig->children + i);
188       if (child == NULL)
189       {
190         oconfig_free (ci_copy);
191         return (NULL);
192       }
193       child->parent = ci_copy;
194       ci_copy->children[i] = *child;
195       free (child);
196     } /* for (i = 0; i < ci_copy->children_num; i++) */
197   } /* }}} if (ci_orig->children_num > 0) */
199   return (ci_copy);
200 } /* oconfig_item_t *oconfig_clone */
202 void oconfig_free (oconfig_item_t *ci)
204   int i;
206   if (ci == NULL)
207     return;
209   if (ci->key != NULL)
210     free (ci->key);
212   for (i = 0; i < ci->values_num; i++)
213     if ((ci->values[i].type == OCONFIG_TYPE_STRING)
214         && (NULL != ci->values[i].value.string))
215       free (ci->values[i].value.string);
217   if (ci->values != NULL)
218     free (ci->values);
220   for (i = 0; i < ci->children_num; i++)
221     oconfig_free (ci->children + i);
223   if (ci->children != NULL)
224     free (ci->children);
227 /*
228  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
229  */