7 #include "types/types.h"
10 #include <talloc/talloc.h>
12 #include "device-handler.h"
14 #include "parser-utils.h"
18 struct list_item list;
19 struct parser *parser;
24 static char *local_path(struct discover_context *ctx,
25 struct discover_device *dev,
28 return join_paths(ctx, dev->root_path, filename);
31 int parser_request_file(struct discover_context *ctx,
32 struct discover_device *dev, const char *filename,
39 /* we only support local files at present */
43 path = local_path(ctx, dev, filename);
45 rc = read_file(ctx, path, buf, len);
52 int parser_check_dir(struct discover_context *ctx,
53 struct discover_device *dev, const char *dirname)
62 path = local_path(ctx, dev, dirname);
64 rc = stat(path, &statbuf);
69 return S_ISDIR(statbuf.st_mode) ? 0 : -1;
72 int parser_replace_file(struct discover_context *ctx,
73 struct discover_device *dev, const char *filename,
83 rc = device_request_write(dev, &release);
85 pb_log("Can't write file %s: device doesn't allow write\n",
90 path = local_path(ctx, dev, filename);
92 rc = replace_file(path, buf, len);
96 device_release_write(dev, release);
101 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
102 char **buf, int *len)
104 struct load_url_result *result;
107 result = load_url(ctx, url);
111 rc = read_file(ctx, result->local, buf, len);
113 pb_log("Read failed for the parser %s on file %s\n",
114 ctx->parser->name, result->local);
121 if (result->cleanup_local)
122 unlink(result->local);
127 void iterate_parsers(struct discover_context *ctx)
131 pb_log("trying parsers for %s\n", ctx->device->device->id);
133 list_for_each_entry(&parsers, i, list) {
134 pb_debug("\ttrying parser '%s'\n", i->parser->name);
135 ctx->parser = i->parser;
136 i->parser->parse(ctx);
141 static void *parsers_ctx;
143 void __register_parser(struct parser *parser)
148 parsers_ctx = talloc_new(NULL);
150 i = talloc(parsers_ctx, struct p_item);
152 list_add(&parsers, &i->list);
155 static __attribute__((destructor)) void cleanup_parsers(void)
157 talloc_free(parsers_ctx);
160 void parser_init(void)