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);
44 for (i = 0; i < len; i += rc) {
45 rc = read(fd, buf + i, len - i);
47 /* unexpected EOF: trim and return */
70 static char *local_path(struct discover_context *ctx,
73 return join_paths(ctx, ctx->device->mount_path, filename);
76 static int download_config(struct discover_context *ctx, char **buf, int *len)
82 file = load_url(ctx, ctx->conf_url, &tempfile);
86 rc = read_file(ctx, file, buf, len);
98 static void iterate_parser_files(struct discover_context *ctx,
99 const struct parser *parser)
101 const char * const *filename;
104 if (!parser->filenames)
107 for (filename = parser->filenames; *filename; filename++) {
111 path = local_path(ctx, *filename);
115 rc = read_file(ctx, path, &buf, &len);
117 parser->parse(ctx, buf, len);
123 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
128 pb_log("trying parsers for %s\n", ctx->device->device->id);
131 case CONF_METHOD_LOCAL_FILE:
132 for (i = 0; i < n_parsers; i++) {
133 if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
136 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
137 ctx->parser = parsers[i];
138 iterate_parser_files(ctx, ctx->parser);
143 case CONF_METHOD_DHCP:
144 rc = download_config(ctx, &buf, &len);
146 pb_log("\tdownload failed, aborting\n");
150 for (i = 0; i < n_parsers; i++) {
151 if (parsers[i]->method != method)
154 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
155 ctx->parser = parsers[i];
156 parsers[i]->parse(ctx, buf, len);
161 case CONF_METHOD_UNKNOWN:
167 void __register_parser(struct parser *parser)
169 parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
170 parsers[n_parsers] = parser;
174 void parser_init(void)