]> git.ozlabs.org Git - petitboot/blob - discover/parser-conf.c
discover/grub2: Implement statement blocks
[petitboot] / discover / parser-conf.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include "log/log.h"
31 #include "talloc/talloc.h"
32 #include "parser-conf.h"
33 #include "parser-utils.h"
34 #include "paths.h"
35
36 /**
37  * conf_strip_str - Remove quotes and/or whitespace around a string.
38  *
39  * Returns the next byte to process, or NULL if the string is empty.
40  */
41
42 char *conf_strip_str(char *s)
43 {
44         char *e;
45
46         if (!s)
47                 return NULL;
48
49         if (!strlen(s))
50                 return NULL;
51
52         while (*s == '"' || *s == '\'' || isspace(*s))
53                 s++;
54
55         e = s + strlen(s) - 1;
56
57         while (*e == '"' || *e == '\'' || isspace(*e))
58                 *(e--) = 0;
59
60         return strlen(s) ? s : NULL;
61 }
62
63 /**
64  * conf_replace_char - replace one char with another.
65  */
66
67 char *conf_replace_char(char *s, char from, char to)
68 {
69         if (!s)
70                 return NULL;
71
72         for ( ; *s; s++)
73                 if (*s == from)
74                         *s = to;
75
76         return s;
77 }
78
79 /**
80  * conf_get_pair - Get the next 'name/value' parameter pair.
81  * @str: The string to process.
82  * @name_out: Returns a pointer to the name.
83  * @value_out: Returns a pointer to the value.
84  * @tdelimiter: The pair separator.
85  * @terminator: The pair terminator.
86  *
87  * Parses a name=value pair returning pointers in @name_out and @value_out.
88  * The pair can be terminated by @terminator or a zero.
89  * If no '=' character is found @value_out is set and @name_out is
90  * set to NULL.
91  * If the value is empty *value_out is set to NULL.
92  * The string is modified in place.
93  *
94  * Returns the next byte to process, or NULL if we've hit the end of the
95  * string.
96  */
97
98 char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
99         char **name_out, char **value_out, char delimiter, char terminator)
100 {
101         char *sep, *end;
102
103         *name_out = *value_out = NULL;
104
105         /* terminate the value */
106         end = strchr(str, terminator);
107
108         if (end)
109                 *end = 0;
110
111         conf_replace_char(str, '\t', ' ');
112
113         str = conf_strip_str(str);
114
115         if (!str)
116                 goto exit;
117
118         sep = strchr(str, delimiter);
119
120         if (!sep) {
121                 *name_out = NULL;
122                 *value_out = conf_strip_str(str);
123         } else {
124                 *sep = 0;
125                 *name_out = conf_strip_str(str);
126                 *value_out = conf_strip_str(sep + 1);
127         }
128
129 exit:
130         return end ? end + 1 : NULL;
131 }
132
133 /**
134  * conf_param_in_list - Search a list of strings for an entry.
135  * @list: A NULL treminated array of pointers to strings.
136  * @param: A string to search for.
137  *
138  * Retuns 1 if @param is found in @list, 0 if @param is not found.
139  */
140
141 int conf_param_in_list(const char *const *list, const char *param)
142 {
143         const char *const *str;
144
145         for (str = list; *str; str++)
146                 if (streq(*str, param))
147                         return 1;
148         return 0;
149 }
150
151 /**
152  * conf_init_global_options - Zero the global option table.
153  */
154
155 void conf_init_global_options(struct conf_context *conf)
156 {
157         int i;
158
159         if (!conf->global_options)
160                 return;
161
162         for (i = 0; conf->global_options[i].name; i++)
163                 conf->global_options[i].value = NULL;
164 }
165
166 /**
167  * conf_set_global_option - Set a value in the global option table.
168  *
169  * Check if an option (name=value) is a global option. If so, store it in
170  * the global options table, and return 1. Otherwise, return 0.
171  */
172
173 int conf_set_global_option(struct conf_context *conf, const char *name,
174         const char *value)
175 {
176         int i;
177
178         assert(conf->global_options);
179
180         for (i = 0; conf->global_options[i].name; i++) {
181                 if (streq(name, conf->global_options[i].name)) {
182                         conf->global_options[i].value
183                                 = talloc_strdup(conf, value);
184                         pb_log("%s: %s = '%s'\n", __func__, name, value);
185                         return 1;
186                 }
187         }
188         return 0;
189 }
190
191 /**
192  * conf_get_global_option - Get a value from the global option table.
193  * @conf: The parser struct conf_context.
194  * @name: The name of the (name:value) to retrieve.
195  *
196  * Returns the value if @name is found in the table, or NULL if @name
197  * is not found.
198  */
199
200 const char *conf_get_global_option(struct conf_context *conf,
201         const char *name)
202 {
203         int i;
204
205         assert(conf->global_options);
206
207         for (i = 0; conf->global_options[i].name ;i++)
208                 if (streq(name, conf->global_options[i].name))
209                         return conf->global_options[i].value;
210
211         assert(0 && "unknown global name");
212         return NULL;
213 }
214
215 /**
216  * conf_parse_buf - The main parser loop.
217  *
218  * Called from conf_parse() with data read from a conf file.
219  */
220
221 void conf_parse_buf(struct conf_context *conf, char *buf,
222                 int len __attribute__((unused)))
223 {
224         char *pos, *name, *value;
225
226         assert(conf->get_pair);
227         assert(conf->process_pair);
228
229         for (pos = buf; pos;) {
230                 pos = conf->get_pair(conf, pos, &name, &value, '\n');
231
232                 if (!value)
233                         continue;
234
235                 if (name && *name == '#')
236                         continue;
237
238                 if (*value == '#')
239                         continue;
240
241                 conf->process_pair(conf, name, value);
242         }
243
244         if (conf->finish)
245                 conf->finish(conf);
246 }