2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
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.
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.
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
27 #include <sys/types.h>
31 #include "talloc/talloc.h"
32 #include "parser-conf.h"
33 #include "parser-utils.h"
37 * conf_strip_str - Remove quotes and/or whitespace around a string.
39 * Returns the next byte to process, or NULL if the string is empty.
42 char *conf_strip_str(char *s)
49 while (*s == '"' || *s == '\'' || isspace(*s))
52 e = s + strlen(s) - 1;
54 while (*e == '"' || *e == '\'' || isspace(*e))
57 return strlen(s) ? s : NULL;
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.
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
71 * If the value is empty *value_out is set to NULL.
72 * The string is modified in place.
74 * Returns the next byte to process, or NULL if we've hit the end of the
78 char *conf_get_param_pair(char *str, char **name_out, char **value_out,
83 /* terminate the value */
84 end = strchr(str, terminator);
89 sep = strchr(str, '=');
93 *value_out = conf_strip_str(str);
96 *name_out = conf_strip_str(str);
97 *value_out = conf_strip_str(sep + 1);
100 pb_log("%s: @%s@%s@\n", __func__, *name_out, *value_out);
102 return end ? end + 1 : NULL;
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.
110 * Retuns 1 if @param is found in @list, 0 if @param is not found.
113 int conf_param_in_list(const char *const *list, const char *param)
115 const char *const *str;
117 for (str = list; *str; str++)
118 if (streq(*str, param))
124 * conf_init_global_options - Zero the global option table.
127 void conf_init_global_options(struct conf_context *conf)
131 for (i = 0; conf->global_options[i].name; i++)
132 conf->global_options[i].value = NULL;
136 * conf_set_global_option - Set a value in the global option table.
138 * Check if an option (name=value) is a global option. If so, store it in
139 * the global options table, and return 1. Otherwise, return 0.
142 int conf_set_global_option(struct conf_context *conf, const char *name,
147 for (i = 0; conf->global_options[i].name; i++) {
148 if (streq(name, conf->global_options[i].name)) {
149 conf->global_options[i].value
150 = talloc_strdup(conf, value);
151 pb_log("%s: @%s@%s@\n", __func__, name, value);
159 * conf_get_global_option - Get a value from the global option table.
160 * @conf: The parser struct conf_context.
161 * @name: The name of the (name:value) to retrieve.
163 * Returns the value if @name is found in the table, or NULL if @name
167 const char *conf_get_global_option(struct conf_context *conf,
172 for (i = 0; conf->global_options[i].name ;i++)
173 if (streq(name, conf->global_options[i].name)) {
174 pb_log("%s: @%s@%s@\n", __func__, name,
175 conf->global_options[i].value);
176 return conf->global_options[i].value;
179 assert(0 && "unknown global name");
184 * conf_parse_buf - The main parser loop.
186 * Called from conf_parse() with data read from a conf file.
189 static void conf_parse_buf(struct conf_context *conf)
191 char *pos, *name, *value;
193 for (pos = conf->buf; pos;) {
194 pos = conf_get_param_pair(pos, &name, &value, '\n');
199 if (name && *name == '#')
205 conf->process_pair(conf, name, value);
213 * conf_parse - The common parser entry.
215 * Called from the parser specific setup routines. Searches for .conf
216 * files, reads data into buffers, and calls conf_parse_buf().
219 int conf_parse(struct conf_context *conf)
230 /* The parser is only run on the first file found. */
231 /* FIXME: Could try others on error, etc. */
233 for (i = 0; conf->conf_files[i]; i++) {
234 char *filepath = resolve_path(conf->dc,
235 conf->conf_files[i], conf->dc->device_path);
237 pb_log("%s: try: %s\n", __func__, filepath);
239 fd = open(filepath, O_RDONLY);
241 talloc_free(filepath);
244 pb_log("%s: open failed: %s\n", __func__,
249 if (fstat(fd, &stat)) {
250 pb_log("%s: fstat failed: %s\n", __func__,
255 conf->buf = talloc_array(conf, char, stat.st_size + 1);
257 len = read(fd, conf->buf, stat.st_size);
260 pb_log("%s: read failed: %s\n", __func__,
275 if (!conf->dc->device->icon_file)
276 conf->dc->device->icon_file = talloc_strdup(conf->dc,
277 generic_icon_file(guess_device_type(conf->dc)));
279 conf_parse_buf(conf);
284 pb_log("%s: %s\n", __func__, (rc ? "ok" : "failed"));