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