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