7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
18 struct list_item list;
19 struct parser *parser;
24 static char *local_path(struct discover_context *ctx,
25 struct discover_device *dev,
28 return join_paths(ctx, dev->mount_path, filename);
31 int parser_request_file(struct discover_context *ctx,
32 struct discover_device *dev, const char *filename,
39 /* we only support local files at present */
43 path = local_path(ctx, dev, filename);
45 rc = read_file(ctx, path, buf, len);
52 int parser_replace_file(struct discover_context *ctx,
53 struct discover_device *dev, const char *filename,
63 rc = device_request_write(dev, &release);
65 pb_log("Can't write file %s: device doesn't allow write\n",
70 path = local_path(ctx, dev, filename);
72 rc = replace_file(path, buf, len);
76 device_release_write(dev, release);
81 static int download_config(struct discover_context *ctx, char **buf, int *len)
83 struct load_url_result *result;
86 result = load_url(ctx, ctx->conf_url);
90 rc = read_file(ctx, result->local, buf, len);
97 if (result->cleanup_local)
98 unlink(result->local);
102 static void iterate_parser_files(struct discover_context *ctx,
103 const struct parser *parser)
105 const char * const *filename;
108 if (!parser->filenames)
111 for (filename = parser->filenames; *filename; filename++) {
115 path = local_path(ctx, ctx->device, *filename);
119 rc = read_file(ctx, path, &buf, &len);
121 pb_log("Running parser %s on file %s\n",
122 parser->name, *filename);
123 parser->parse(ctx, buf, len);
129 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
135 pb_log("trying parsers for %s\n", ctx->device->device->id);
138 case CONF_METHOD_LOCAL_FILE:
139 list_for_each_entry(&parsers, i, list) {
140 if (i->parser->method != CONF_METHOD_LOCAL_FILE)
143 pb_log("\ttrying parser '%s'\n", i->parser->name);
144 ctx->parser = i->parser;
145 iterate_parser_files(ctx, ctx->parser);
150 case CONF_METHOD_DHCP:
151 rc = download_config(ctx, &buf, &len);
153 pb_log("\tdownload failed, aborting\n");
157 list_for_each_entry(&parsers, i, list) {
158 if (i->parser->method != method)
161 pb_log("\ttrying parser '%s'\n", i->parser->name);
162 ctx->parser = i->parser;
163 i->parser->parse(ctx, buf, len);
168 case CONF_METHOD_UNKNOWN:
174 void __register_parser(struct parser *parser)
176 struct p_item* i = talloc(NULL, struct p_item);
179 list_add(&parsers, &i->list);
182 void parser_init(void)