* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
#include <assert.h>
#include <ctype.h>
if (!s)
return NULL;
+ if (!strlen(s))
+ return NULL;
+
while (*s == '"' || *s == '\'' || isspace(*s))
s++;
}
/**
- * conf_get_param_pair - Get the next 'name=value' parameter pair.
+ * conf_replace_char - replace one char with another.
+ */
+
+char *conf_replace_char(char *s, char from, char to)
+{
+ if (!s)
+ return NULL;
+
+ for ( ; *s; s++)
+ if (*s == from)
+ *s = to;
+
+ return s;
+}
+
+/**
+ * conf_get_pair - Get the next 'name/value' parameter pair.
* @str: The string to process.
* @name_out: Returns a pointer to the name.
* @value_out: Returns a pointer to the value.
- * @terminator: The pair separator/terminator.
+ * @tdelimiter: The pair separator.
+ * @terminator: The pair terminator.
*
* Parses a name=value pair returning pointers in @name_out and @value_out.
* The pair can be terminated by @terminator or a zero.
* string.
*/
-char *conf_get_param_pair(char *str, char **name_out, char **value_out,
- char terminator)
+char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
+ char **name_out, char **value_out, char delimiter, char terminator)
{
char *sep, *end;
+ *name_out = *value_out = NULL;
+
/* terminate the value */
end = strchr(str, terminator);
if (end)
*end = 0;
- sep = strchr(str, '=');
+ conf_replace_char(str, '\t', ' ');
+
+ str = conf_strip_str(str);
+
+ if (!str)
+ goto exit;
+
+ sep = strchr(str, delimiter);
if (!sep) {
*name_out = NULL;
*value_out = conf_strip_str(sep + 1);
}
- pb_log("%s: @%s@%s@\n", __func__, *name_out, *value_out);
-
+exit:
return end ? end + 1 : NULL;
}
return 0;
}
+/**
+ * conf_init_global_options - Zero the global option table.
+ */
+
+void conf_init_global_options(struct conf_context *conf)
+{
+ int i;
+
+ if (!conf->global_options)
+ return;
+
+ for (i = 0; conf->global_options[i].name; i++)
+ conf->global_options[i].value = NULL;
+}
+
/**
* conf_set_global_option - Set a value in the global option table.
*
{
int i;
+ assert(conf->global_options);
+
for (i = 0; conf->global_options[i].name; i++) {
if (streq(name, conf->global_options[i].name)) {
conf->global_options[i].value
= talloc_strdup(conf, value);
- pb_log("%s: %s:%s\n", __func__, name, value);
+ pb_debug("%s: %s = '%s'\n", __func__, name, value);
return 1;
}
}
{
int i;
+ assert(conf->global_options);
+
for (i = 0; conf->global_options[i].name ;i++)
if (streq(name, conf->global_options[i].name))
return conf->global_options[i].value;
* Called from conf_parse() with data read from a conf file.
*/
-static void conf_parse_buf(struct conf_context *conf)
+void conf_parse_buf(struct conf_context *conf, char *buf,
+ int len __attribute__((unused)))
{
char *pos, *name, *value;
- for (pos = conf->buf; pos;) {
- pos = conf_get_param_pair(pos, &name, &value, '\n');
+ assert(conf->get_pair);
+ assert(conf->process_pair);
+
+ for (pos = buf; pos;) {
+ pos = conf->get_pair(conf, pos, &name, &value, '\n');
if (!value)
continue;
if (*value == '#')
continue;
- value = conf_strip_str(value);
-
- if (!value)
- continue;
-
conf->process_pair(conf, name, value);
}
if (conf->finish)
conf->finish(conf);
}
-
-/**
- * conf_parse - The common parser entry.
- *
- * Called from the parser specific setup routines. Searches for .conf
- * files, reads data into buffers, and calls conf_parse_buf().
- */
-
-int conf_parse(struct conf_context *conf)
-{
- int fd, rc;
- unsigned int i;
- struct stat stat;
- ssize_t len;
-
- rc = 0;
- fd = -1;
- len = 0;
-
- /* The parser is only run on the first file found. */
- /* FIXME: Could try others on error, etc. */
-
- for (i = 0; conf->conf_files[i]; i++) {
- char *filepath = resolve_path(conf->dc,
- conf->conf_files[i], conf->dc->device_path);
-
- pb_log("%s: try: %s\n", __func__, filepath);
-
- fd = open(filepath, O_RDONLY);
-
- talloc_free(filepath);
-
- if (fd < 0) {
- pb_log("%s: open failed: %s\n", __func__,
- strerror(errno));
- continue;
- }
-
- if (fstat(fd, &stat)) {
- pb_log("%s: fstat failed: %s\n", __func__,
- strerror(errno));
- continue;
- }
-
- conf->buf = talloc_array(conf, char, stat.st_size + 1);
-
- len = read(fd, conf->buf, stat.st_size);
-
- if (len < 0) {
- pb_log("%s: read failed: %s\n", __func__,
- strerror(errno));
- continue;
- }
- conf->buf[len] = 0;
-
- break;
- }
-
- if (fd >= 0)
- close(fd);
-
- if (len <= 0)
- goto out;
-
- if (!conf->dc->device->icon_file)
- conf->dc->device->icon_file = talloc_strdup(conf->dc,
- generic_icon_file(guess_device_type(conf->dc)));
-
- conf_parse_buf(conf);
-
- rc = 1;
-
-out:
- pb_log("%s: %s\n", __func__, (rc ? "ok" : "failed"));
- return rc;
-}
-