7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
17 struct list_item list;
18 struct parser *parser;
23 static const int max_file_size = 1024 * 1024;
25 static int read_file(struct discover_context *ctx,
26 const char *filename, char **bufp, int *lenp)
32 fd = open(filename, O_RDONLY);
36 rc = fstat(fd, &statbuf);
40 len = statbuf.st_size;
41 if (len > max_file_size)
44 buf = talloc_array(ctx, char, len + 1);
48 for (i = 0; i < len; i += rc) {
49 rc = read(fd, buf + i, len - i);
51 /* unexpected EOF: trim and return */
76 static char *local_path(struct discover_context *ctx,
79 return join_paths(ctx, ctx->device->mount_path, filename);
82 static int download_config(struct discover_context *ctx, char **buf, int *len)
88 file = load_url(ctx, ctx->conf_url, &tempfile);
92 rc = read_file(ctx, file, buf, len);
104 static void iterate_parser_files(struct discover_context *ctx,
105 const struct parser *parser)
107 const char * const *filename;
110 if (!parser->filenames)
113 for (filename = parser->filenames; *filename; filename++) {
117 path = local_path(ctx, *filename);
121 rc = read_file(ctx, path, &buf, &len);
123 pb_log("Running parser %s on file %s\n",
124 parser->name, *filename);
125 parser->parse(ctx, buf, len);
131 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
137 pb_log("trying parsers for %s\n", ctx->device->device->id);
140 case CONF_METHOD_LOCAL_FILE:
141 list_for_each_entry(&parsers, i, list) {
142 if (i->parser->method != CONF_METHOD_LOCAL_FILE)
145 pb_log("\ttrying parser '%s'\n", i->parser->name);
146 ctx->parser = i->parser;
147 iterate_parser_files(ctx, ctx->parser);
152 case CONF_METHOD_DHCP:
153 rc = download_config(ctx, &buf, &len);
155 pb_log("\tdownload failed, aborting\n");
159 list_for_each_entry(&parsers, i, list) {
160 if (i->parser->method != method)
163 pb_log("\ttrying parser '%s'\n", i->parser->name);
164 ctx->parser = i->parser;
165 i->parser->parse(ctx, buf, len);
170 case CONF_METHOD_UNKNOWN:
176 void __register_parser(struct parser *parser)
178 struct p_item* i = talloc(NULL, struct p_item);
181 list_add(&parsers, &i->list);
184 void parser_init(void)