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