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)
52 while (*s == '"' || *s == '\'' || isspace(*s))
55 e = s + strlen(s) - 1;
57 while (*e == '"' || *e == '\'' || isspace(*e))
60 return strlen(s) ? s : NULL;
64 * conf_replace_char - replace one char with another.
67 char *conf_replace_char(char *s, char from, char to)
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.
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
91 * If the value is empty *value_out is set to NULL.
92 * The string is modified in place.
94 * Returns the next byte to process, or NULL if we've hit the end of the
98 char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
99 char **name_out, char **value_out, char delimiter, char terminator)
103 *name_out = *value_out = NULL;
105 /* terminate the value */
106 end = strchr(str, terminator);
111 conf_replace_char(str, '\t', ' ');
113 str = conf_strip_str(str);
118 sep = strchr(str, delimiter);
122 *value_out = conf_strip_str(str);
125 *name_out = conf_strip_str(str);
126 *value_out = conf_strip_str(sep + 1);
130 return end ? end + 1 : NULL;
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.
138 * Retuns 1 if @param is found in @list, 0 if @param is not found.
141 int conf_param_in_list(const char *const *list, const char *param)
143 const char *const *str;
145 for (str = list; *str; str++)
146 if (streq(*str, param))
152 * conf_init_global_options - Zero the global option table.
155 void conf_init_global_options(struct conf_context *conf)
159 if (!conf->global_options)
162 for (i = 0; conf->global_options[i].name; i++)
163 conf->global_options[i].value = NULL;
167 * conf_set_global_option - Set a value in the global option table.
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.
173 int conf_set_global_option(struct conf_context *conf, const char *name,
178 assert(conf->global_options);
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_debug("%s: %s = '%s'\n", __func__, name, value);
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.
196 * Returns the value if @name is found in the table, or NULL if @name
200 const char *conf_get_global_option(struct conf_context *conf,
205 assert(conf->global_options);
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;
211 assert(0 && "unknown global name");
216 * conf_parse_buf - The main parser loop.
218 * Called from conf_parse() with data read from a conf file.
221 void conf_parse_buf(struct conf_context *conf, char *buf,
222 int len __attribute__((unused)))
224 char *pos, *name, *value;
226 assert(conf->get_pair);
227 assert(conf->process_pair);
229 for (pos = buf; pos;) {
230 pos = conf->get_pair(conf, pos, &name, &value, '\n');
235 if (name && *name == '#')
241 conf->process_pair(conf, name, value);