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,
27 return join_paths(ctx, ctx->device->mount_path, filename);
30 static int download_config(struct discover_context *ctx, char **buf, int *len)
32 struct load_url_result *result;
35 result = load_url(ctx, ctx->conf_url);
39 rc = read_file(ctx, result->local, buf, len);
46 if (result->cleanup_local)
47 unlink(result->local);
51 static void iterate_parser_files(struct discover_context *ctx,
52 const struct parser *parser)
54 const char * const *filename;
57 if (!parser->filenames)
60 for (filename = parser->filenames; *filename; filename++) {
64 path = local_path(ctx, *filename);
68 rc = read_file(ctx, path, &buf, &len);
70 pb_log("Running parser %s on file %s\n",
71 parser->name, *filename);
72 parser->parse(ctx, buf, len);
78 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
84 pb_log("trying parsers for %s\n", ctx->device->device->id);
87 case CONF_METHOD_LOCAL_FILE:
88 list_for_each_entry(&parsers, i, list) {
89 if (i->parser->method != CONF_METHOD_LOCAL_FILE)
92 pb_log("\ttrying parser '%s'\n", i->parser->name);
93 ctx->parser = i->parser;
94 iterate_parser_files(ctx, ctx->parser);
99 case CONF_METHOD_DHCP:
100 rc = download_config(ctx, &buf, &len);
102 pb_log("\tdownload failed, aborting\n");
106 list_for_each_entry(&parsers, i, list) {
107 if (i->parser->method != method)
110 pb_log("\ttrying parser '%s'\n", i->parser->name);
111 ctx->parser = i->parser;
112 i->parser->parse(ctx, buf, len);
117 case CONF_METHOD_UNKNOWN:
123 void __register_parser(struct parser *parser)
125 struct p_item* i = talloc(NULL, struct p_item);
127 printf("%s: %s\n", __func__, parser->name);
130 list_add(&parsers, &i->list);
133 void parser_init(void)