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 static int download_config(struct discover_context *ctx, char **buf, int *len)
54 struct load_url_result *result;
57 result = load_url(ctx, ctx->conf_url);
61 rc = read_file(ctx, result->local, buf, len);
68 if (result->cleanup_local)
69 unlink(result->local);
73 static void iterate_parser_files(struct discover_context *ctx,
74 const struct parser *parser)
76 const char * const *filename;
79 if (!parser->filenames)
82 for (filename = parser->filenames; *filename; filename++) {
86 path = local_path(ctx, ctx->device, *filename);
90 rc = read_file(ctx, path, &buf, &len);
92 pb_log("Running parser %s on file %s\n",
93 parser->name, *filename);
94 parser->parse(ctx, buf, len);
100 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
106 pb_log("trying parsers for %s\n", ctx->device->device->id);
109 case CONF_METHOD_LOCAL_FILE:
110 list_for_each_entry(&parsers, i, list) {
111 if (i->parser->method != CONF_METHOD_LOCAL_FILE)
114 pb_log("\ttrying parser '%s'\n", i->parser->name);
115 ctx->parser = i->parser;
116 iterate_parser_files(ctx, ctx->parser);
121 case CONF_METHOD_DHCP:
122 rc = download_config(ctx, &buf, &len);
124 pb_log("\tdownload failed, aborting\n");
128 list_for_each_entry(&parsers, i, list) {
129 if (i->parser->method != method)
132 pb_log("\ttrying parser '%s'\n", i->parser->name);
133 ctx->parser = i->parser;
134 i->parser->parse(ctx, buf, len);
139 case CONF_METHOD_UNKNOWN:
145 void __register_parser(struct parser *parser)
147 struct p_item* i = talloc(NULL, struct p_item);
149 printf("%s: %s\n", __func__, parser->name);
152 list_add(&parsers, &i->list);
155 void parser_init(void)