]> git.ozlabs.org Git - petitboot/blobdiff - discover/parser.c
parsers: dynamically register parsers
[petitboot] / discover / parser.c
index 1f3674d3ee461cc24fac0f9ca5a8fc11d765fb1c..641e06b1cb54e9bdf34d1547e8a3de465a2097f2 100644 (file)
 #include "parser-utils.h"
 #include "paths.h"
 
-struct parser __grub2_parser;
-struct parser __kboot_parser;
-struct parser __native_parser;
-struct parser __yaboot_parser;
-
-static const struct parser *const parsers[] = {
-//     &__native_parser,
-       &__kboot_parser,
-       &__grub2_parser,
-       &__yaboot_parser,
-       NULL
-};
+static int n_parsers;
+static struct parser **parsers;
 
 static const int max_file_size = 1024 * 1024;
 
@@ -77,12 +67,17 @@ err_close:
        return -1;
 }
 
+static char *local_path(struct discover_context *ctx,
+               const char *filename)
+{
+       return join_paths(ctx, ctx->device->mount_path, filename);
+}
+
 static void iterate_parser_files(struct discover_context *ctx,
                const struct parser *parser)
 {
        const char * const *filename;
-       const char *path, *url;
-       unsigned int tempfile;
+       const char *path;
 
        if (!parser->filenames)
                return;
@@ -91,12 +86,7 @@ static void iterate_parser_files(struct discover_context *ctx,
                int rc, len;
                char *buf;
 
-               url = resolve_path(ctx, *filename, ctx->device->device_path);
-               if (!url)
-                       continue;
-
-               path = load_file(ctx, url, &tempfile);
-
+               path = local_path(ctx, *filename);
                if (!path)
                        continue;
 
@@ -105,12 +95,7 @@ static void iterate_parser_files(struct discover_context *ctx,
                        parser->parse(ctx, buf, len);
                        talloc_free(buf);
                }
-
-               if (tempfile)
-                       unlink(path);
-
        }
-
 }
 
 void iterate_parsers(struct discover_context *ctx)
@@ -119,10 +104,19 @@ void iterate_parsers(struct discover_context *ctx)
 
        pb_log("trying parsers for %s\n", ctx->device->device->id);
 
-       for (i = 0; parsers[i]; i++) {
+       for (i = 0; i < n_parsers; i++) {
                pb_log("\ttrying parser '%s'\n", parsers[i]->name);
+               ctx->parser = parsers[i];
                iterate_parser_files(ctx, parsers[i]);
        }
+       ctx->parser = NULL;
+}
+
+void __register_parser(struct parser *parser)
+{
+       parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
+       parsers[n_parsers] = parser;
+       n_parsers++;
 }
 
 void parser_init(void)