]> git.ozlabs.org Git - petitboot/blob - discover/parser-conf.c
a21efc4aced845e10fec38469e9abcc72bc85ed9
[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         while (*s == '"' || *s == '\'' || isspace(*s))
50                 s++;
51
52         e = s + strlen(s) - 1;
53
54         while (*e == '"' || *e == '\'' || isspace(*e))
55                 *(e--) = 0;
56
57         return strlen(s) ? s : NULL;
58 }
59
60 /**
61  * conf_replace_char - replace one char with another.
62  */
63
64 char *conf_replace_char(char *s, char from, char to)
65 {
66         if (!s)
67                 return NULL;
68
69         for ( ; *s; s++)
70                 if (*s == from)
71                         *s = to;
72
73         return s;
74 }
75
76 /**
77  * conf_get_pair - Get the next 'name/value' parameter pair.
78  * @str: The string to process.
79  * @name_out: Returns a pointer to the name.
80  * @value_out: Returns a pointer to the value.
81  * @tdelimiter: The pair separator.
82  * @terminator: The pair terminator.
83  *
84  * Parses a name=value pair returning pointers in @name_out and @value_out.
85  * The pair can be terminated by @terminator or a zero.
86  * If no '=' character is found @value_out is set and @name_out is
87  * set to NULL.
88  * If the value is empty *value_out is set to NULL.
89  * The string is modified in place.
90  *
91  * Returns the next byte to process, or NULL if we've hit the end of the
92  * string.
93  */
94
95 char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
96         char **name_out, char **value_out, char delimiter, char terminator)
97 {
98         char *sep, *end;
99
100         *name_out = *value_out = NULL;
101
102         /* terminate the value */
103         end = strchr(str, terminator);
104
105         if (end)
106                 *end = 0;
107
108         conf_replace_char(str, '\t', ' ');
109
110         str = conf_strip_str(str);
111
112         if (!str)
113                 goto exit;
114
115         sep = strchr(str, delimiter);
116
117         if (!sep) {
118                 *name_out = NULL;
119                 *value_out = conf_strip_str(str);
120         } else {
121                 *sep = 0;
122                 *name_out = conf_strip_str(str);
123                 *value_out = conf_strip_str(sep + 1);
124         }
125
126 exit:
127         pb_log("%s: @%s@%s@\n", __func__, *name_out, *value_out);
128
129         return end ? end + 1 : NULL;
130 }
131
132 /**
133  * conf_param_in_list - Search a list of strings for an entry.
134  * @list: A NULL treminated array of pointers to strings.
135  * @param: A string to search for.
136  *
137  * Retuns 1 if @param is found in @list, 0 if @param is not found.
138  */
139
140 int conf_param_in_list(const char *const *list, const char *param)
141 {
142         const char *const *str;
143
144         for (str = list; *str; str++)
145                 if (streq(*str, param))
146                         return 1;
147         return 0;
148 }
149
150 /**
151  * conf_init_global_options - Zero the global option table.
152  */
153
154 void conf_init_global_options(struct conf_context *conf)
155 {
156         int i;
157
158         for (i = 0; conf->global_options[i].name; i++)
159                 conf->global_options[i].value = NULL;
160 }
161
162 /**
163  * conf_set_global_option - Set a value in the global option table.
164  *
165  * Check if an option (name=value) is a global option. If so, store it in
166  * the global options table, and return 1. Otherwise, return 0.
167  */
168
169 int conf_set_global_option(struct conf_context *conf, const char *name,
170         const char *value)
171 {
172         int i;
173
174         for (i = 0; conf->global_options[i].name; i++) {
175                 if (streq(name, conf->global_options[i].name)) {
176                         conf->global_options[i].value
177                                 = talloc_strdup(conf, value);
178                         pb_log("%s: @%s@%s@\n", __func__, name, value);
179                         return 1;
180                 }
181         }
182         return 0;
183 }
184
185 /**
186  * conf_get_global_option - Get a value from the global option table.
187  * @conf: The parser struct conf_context.
188  * @name: The name of the (name:value) to retrieve.
189  *
190  * Returns the value if @name is found in the table, or NULL if @name
191  * is not found.
192  */
193
194 const char *conf_get_global_option(struct conf_context *conf,
195         const char *name)
196 {
197         int i;
198
199         for (i = 0; conf->global_options[i].name ;i++)
200                 if (streq(name, conf->global_options[i].name)) {
201                         pb_log("%s: @%s@%s@\n", __func__, name,
202                                 conf->global_options[i].value);
203                         return conf->global_options[i].value;
204                 }
205
206         assert(0 && "unknown global name");
207         return NULL;
208 }
209
210 /**
211  * conf_parse_buf - The main parser loop.
212  *
213  * Called from conf_parse() with data read from a conf file.
214  */
215
216 static void conf_parse_buf(struct conf_context *conf)
217 {
218         char *pos, *name, *value;
219
220         for (pos = conf->buf; pos;) {
221                 pos = conf_get_pair_equal(conf, pos, &name, &value, '\n');
222
223                 if (!value)
224                         continue;
225
226                 if (name && *name == '#')
227                         continue;
228
229                 if (*value == '#')
230                         continue;
231
232                 conf->process_pair(conf, name, value);
233         }
234
235         if (conf->finish)
236                 conf->finish(conf);
237 }
238
239 /**
240  * conf_parse - The common parser entry.
241  *
242  * Called from the parser specific setup routines.  Searches for .conf
243  * files, reads data into buffers, and calls conf_parse_buf().
244  */
245
246 int conf_parse(struct conf_context *conf)
247 {
248         int fd, rc;
249         unsigned int i;
250         struct stat stat;
251         ssize_t len;
252
253         rc = 0;
254         fd = -1;
255         len = 0;
256
257         /* The parser is only run on the first file found. */
258         /* FIXME: Could try others on error, etc. */
259
260         for (i = 0; conf->conf_files[i]; i++) {
261                 char *filepath = resolve_path(conf->dc,
262                         conf->conf_files[i], conf->dc->device_path);
263
264                 pb_log("%s: try: %s\n", __func__, filepath);
265
266                 fd = open(filepath, O_RDONLY);
267
268                 talloc_free(filepath);
269
270                 if (fd < 0) {
271                         pb_log("%s: open failed: %s\n", __func__,
272                                 strerror(errno));
273                         continue;
274                 }
275
276                 if (fstat(fd, &stat)) {
277                         pb_log("%s: fstat failed: %s\n", __func__,
278                                 strerror(errno));
279                         continue;
280                 }
281
282                 conf->buf = talloc_array(conf, char, stat.st_size + 1);
283
284                 len = read(fd, conf->buf, stat.st_size);
285
286                 if (len < 0) {
287                         pb_log("%s: read failed: %s\n", __func__,
288                                 strerror(errno));
289                         continue;
290                 }
291                 conf->buf[len] = 0;
292
293                 break;
294         }
295
296         if (fd >= 0)
297                 close(fd);
298
299         if (len <= 0)
300                 goto out;
301
302         if (!conf->dc->device->icon_file)
303                 conf->dc->device->icon_file = talloc_strdup(conf->dc,
304                         generic_icon_file(guess_device_type(conf->dc)));
305
306         conf_parse_buf(conf);
307
308         rc = 1;
309
310 out:
311         pb_log("%s: %s\n", __func__, (rc ? "ok" : "failed"));
312         return rc;
313 }
314