f8d18499d6caacb5eba0524a82ccef4767e9b2e0
[liboconfig.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 /* Functions provided by the scanner */
28 void yyset_in  (FILE *);
29
30 oconfig_item_t *ci_root;
31
32 oconfig_item_t *oconfig_parse_fh (FILE *fh)
33 {
34   int status;
35   oconfig_item_t *ret;
36
37   yyset_in (fh);
38
39   status = yyparse ();
40   if (status != 0)
41   {
42     fprintf (stderr, "yyparse returned error #%i\n", status);
43     return (NULL);
44   }
45
46   ret = ci_root;
47   ci_root = NULL;
48   yyset_in ((FILE *) 0);
49
50   return (ret);
51 } /* oconfig_item_t *oconfig_parse_fh */
52
53 oconfig_item_t *oconfig_parse_file (const char *file)
54 {
55   FILE *fh;
56   oconfig_item_t *ret;
57
58   fh = fopen (file, "r");
59   if (fh == NULL)
60   {
61     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
62     return (NULL);
63   }
64
65   ret = oconfig_parse_fh (fh);
66   fclose (fh);
67
68   return (ret);
69 } /* oconfig_item_t *oconfig_parse_file */
70
71 void oconfig_free (oconfig_item_t *ci)
72 {
73   int i;
74
75   if (ci->values != NULL)
76     free (ci->values);
77
78   for (i = 0; i < ci->children_num; i++)
79     oconfig_free (ci->children + i);
80 }
81
82 /*
83  * vim:shiftwidth=2:tabstop=8:softtabstop=2
84  */