7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
17 static struct parser **parsers;
19 static const int max_file_size = 1024 * 1024;
21 static int read_file(struct discover_context *ctx,
22 const char *filename, char **bufp, int *lenp)
28 fd = open(filename, O_RDONLY);
32 rc = fstat(fd, &statbuf);
36 len = statbuf.st_size;
37 if (len > max_file_size)
40 buf = talloc_array(ctx, char, len + 1);
44 for (i = 0; i < len; i += rc) {
45 rc = read(fd, buf + i, len - i);
47 /* unexpected EOF: trim and return */
72 static char *local_path(struct discover_context *ctx,
75 return join_paths(ctx, ctx->device->mount_path, filename);
78 static int download_config(struct discover_context *ctx, char **buf, int *len)
84 file = load_url(ctx, ctx->conf_url, &tempfile);
88 rc = read_file(ctx, file, buf, len);
100 static void iterate_parser_files(struct discover_context *ctx,
101 const struct parser *parser)
103 const char * const *filename;
106 if (!parser->filenames)
109 for (filename = parser->filenames; *filename; filename++) {
113 path = local_path(ctx, *filename);
117 rc = read_file(ctx, path, &buf, &len);
119 pb_log("Running parser %s on file %s\n",
120 parser->name, *filename);
121 parser->parse(ctx, buf, len);
127 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
132 pb_log("trying parsers for %s\n", ctx->device->device->id);
135 case CONF_METHOD_LOCAL_FILE:
136 for (i = 0; i < n_parsers; i++) {
137 if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
140 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
141 ctx->parser = parsers[i];
142 iterate_parser_files(ctx, ctx->parser);
147 case CONF_METHOD_DHCP:
148 rc = download_config(ctx, &buf, &len);
150 pb_log("\tdownload failed, aborting\n");
154 for (i = 0; i < n_parsers; i++) {
155 if (parsers[i]->method != method)
158 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
159 ctx->parser = parsers[i];
160 parsers[i]->parse(ctx, buf, len);
165 case CONF_METHOD_UNKNOWN:
171 void __register_parser(struct parser *parser)
173 parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
174 parsers[n_parsers] = parser;
178 void parser_init(void)