5 #include "types/types.h"
8 #include <talloc/talloc.h>
10 #include "device-handler.h"
12 #include "parser-utils.h"
16 struct list_item list;
17 struct parser *parser;
22 static char *local_path(struct discover_context *ctx,
23 struct discover_device *dev,
26 return join_paths(ctx, dev->root_path, filename);
29 int parser_request_file(struct discover_context *ctx,
30 struct discover_device *dev, const char *filename,
37 /* we only support local files at present */
41 path = local_path(ctx, dev, filename);
43 rc = read_file(ctx, path, buf, len);
50 int parser_stat_path(struct discover_context *ctx,
51 struct discover_device *dev, const char *path,
57 /* we only support local files at present */
61 full_path = local_path(ctx, dev, path);
63 rc = stat(full_path, statbuf);
71 talloc_free(full_path);
76 int parser_replace_file(struct discover_context *ctx,
77 struct discover_device *dev, const char *filename,
87 rc = device_request_write(dev, &release);
89 pb_log("Can't write file %s: device doesn't allow write\n",
94 path = local_path(ctx, dev, filename);
96 rc = replace_file(path, buf, len);
100 device_release_write(dev, release);
105 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
106 char **buf, int *len)
108 struct load_url_result *result;
111 result = load_url(ctx, url);
115 rc = read_file(ctx, result->local, buf, len);
117 pb_log("Read failed for the parser %s on file %s\n",
118 ctx->parser->name, result->local);
125 if (result->cleanup_local)
126 unlink(result->local);
131 void iterate_parsers(struct discover_context *ctx)
135 pb_log("trying parsers for %s\n", ctx->device->device->id);
137 list_for_each_entry(&parsers, i, list) {
138 pb_debug("\ttrying parser '%s'\n", i->parser->name);
139 ctx->parser = i->parser;
140 i->parser->parse(ctx);
145 static void *parsers_ctx;
147 void __register_parser(struct parser *parser)
152 parsers_ctx = talloc_new(NULL);
154 i = talloc(parsers_ctx, struct p_item);
156 list_add(&parsers, &i->list);
159 static __attribute__((destructor)) void cleanup_parsers(void)
161 talloc_free(parsers_ctx);
164 void parser_init(void)