diff options
author | Samuel Mendoza-Jonas <sam@mendozajonas.com> | 2018-10-30 15:26:04 +1100 |
---|---|---|
committer | Samuel Mendoza-Jonas <sam@mendozajonas.com> | 2018-11-16 13:55:13 +1100 |
commit | 646d77d8156ad72da1c24f734a029a525ba4bed9 (patch) | |
tree | 8eaa5db080f5c2b733b506fd82eec773ac2afdb3 /discover/native | |
parent | 638f16c7683db165154bbe53772c4b864f9dc90d (diff) | |
download | talos-petitboot-646d77d8156ad72da1c24f734a029a525ba4bed9.tar.gz talos-petitboot-646d77d8156ad72da1c24f734a029a525ba4bed9.zip |
discover: Reimplement native-parser as a Bison parser
Occasionally you look at some code and realise that a) this never gets
built, and b) even if it did it would never compile. Today's example is
native-parser.c which we must have just assumed worked for quite a
while.
The native parser has bitrotted entirely and needs to be brought up to
date. While we're here, lets take the chance to implement a proper
grammar for it. This helps us reason more effectively about the parser,
lets us extend it easily in the future, and.. I wanted to write a Bison
parser too.
This implements most of the old functionality, but drops off some
smaller details like settings icons which needs some separate attention
to bring up to date.
Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'discover/native')
-rw-r--r-- | discover/native/Makefile.am | 54 | ||||
-rw-r--r-- | discover/native/native-lexer.l | 65 | ||||
-rw-r--r-- | discover/native/native-parser.y | 200 | ||||
-rw-r--r-- | discover/native/native.c | 55 | ||||
-rw-r--r-- | discover/native/native.h | 27 |
5 files changed, 401 insertions, 0 deletions
diff --git a/discover/native/Makefile.am b/discover/native/Makefile.am new file mode 100644 index 0000000..f120e70 --- /dev/null +++ b/discover/native/Makefile.am @@ -0,0 +1,54 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +noinst_PROGRAMS += discover/native/native-parser.ro + +discover_native_native_parser_ro_SOURCES = \ + discover/native/native.h \ + discover/native/native.c \ + discover/native/native-lexer.l \ + discover/native/native-parser.y + +BUILT_SOURCES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.h \ + discover/native/native-lexer.c + +CLEANFILES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.c \ + discover/native/native-lexer.h + +discover_native_native_parser_ro_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/discover/native \ + -I$(top_builddir)/discover/native + +discover_native_native_parser_ro_LINK = \ + $(LD) -r -o $@ + +# ylwrap doesn't handle flex header files well; use our own rule here. +discover/native/native-lexer.h discover/native/native-lexer.c: \ + $(top_srcdir)/discover/native/native-lexer.l + $(AM_V_LEX)$(LEXCOMPILE) --header-file=discover/native/native-lexer.h \ + -o discover/native/native-lexer.c $^ + +# We need to loosen our warnings for the generated lexer code. +discover/native/%native-lexer.o discover/native/native-lexer.o: \ + AM_CFLAGS += -Wno-unused-parameter -Wno-missing-prototypes \ + -Wno-missing-declarations -Wno-sign-compare + +$(discover_native_native_parser_ro_OBJECTS): discover/native/native-parser.h diff --git a/discover/native/native-lexer.l b/discover/native/native-lexer.l new file mode 100644 index 0000000..bf1408a --- /dev/null +++ b/discover/native/native-lexer.l @@ -0,0 +1,65 @@ +%{ +#include "native.h" +#include "native-parser.h" +#include <talloc/talloc.h> + +#define YYSTYPE NSTYPE + +void yyerror(struct native_parser *parser, const char *fmt, ...); +%} + +%option nounput noinput +%option batch never-interactive +%option warn +%option noyywrap +%option reentrant +%option bison-bridge +%option yylineno +%option noyyalloc noyyfree noyyrealloc +%option extra-type="struct native_parser *" +%option prefix="n" + +%x label +%x args + +DELIM [ \t]+ +NUMBER 0|[1-9][0-9]* +WORDS [^\n]+ +NEWLINE [\n]+ + +%% + +name { BEGIN(label); return TOKEN_NAME; } +image { BEGIN(label); return TOKEN_IMAGE; } +initrd { BEGIN(label); return TOKEN_INITRD; } +args { BEGIN(label); return TOKEN_ARGS; } +dtb { BEGIN(label); return TOKEN_DTB; } +description { BEGIN(label); return TOKEN_DESCRIPTION; } +default { BEGIN(label); return TOKEN_DEFAULT; } +dev_description { BEGIN(label); return TOKEN_DEV_DESCRIPTION; } +{DELIM} { ; } +{NEWLINE} { ; } +<label>{DELIM} { BEGIN(args); return TOKEN_DELIM; } +<args>{WORDS} { yylval->word = strdup(yytext); return TOKEN_WORD; } +<args>{NEWLINE} { BEGIN(INITIAL); ; } + +%% + +struct native_parser; + +void *yyalloc(size_t bytes, void *yyscanner) +{ + struct native_parser *parser = yyget_extra(yyscanner); + return talloc_size(parser, bytes); +} + +void *yyrealloc(void *ptr, size_t bytes, void *yyscanner) +{ + struct native_parser *parser = yyget_extra(yyscanner); + return talloc_realloc_size(parser, ptr, bytes); +} + +void yyfree(void *ptr, void *yyscanner __attribute__((unused))) +{ + talloc_free(ptr); +} diff --git a/discover/native/native-parser.y b/discover/native/native-parser.y new file mode 100644 index 0000000..bafcf49 --- /dev/null +++ b/discover/native/native-parser.y @@ -0,0 +1,200 @@ + +%pure-parser +%lex-param { nscan_t scanner } +%parse-param { struct native_parser *parser } +%parse-param { void *scanner } +%error-verbose + +%define api.prefix {n} +%{ +#include <talloc/talloc.h> +#include <log/log.h> +#include "discover/resource.h" +#include "discover/parser-utils.h" + +#include "native.h" + +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...); +%} + +%union { + char *word; + int num; +} + +%token <word> TOKEN_WORD +%token <num> TOKEN_NUMBER +%token <num> TOKEN_DELIM + +%token TOKEN_DEFAULT +%token TOKEN_DEV_DESCRIPTION + +%token TOKEN_NAME +%token TOKEN_IMAGE +%token TOKEN_INITRD +%token TOKEN_ARGS +%token TOKEN_DTB +%token TOKEN_DESCRIPTION +%token TOKEN_NEWLINE + +%{ +#include "native-lexer.h" +%} + +%% + +native: + globals boot_options { native_parser_finish(parser); } + | boot_options { native_parser_finish(parser); } + ; + +globals: globals global + | global + ; + +global: TOKEN_DEFAULT delims TOKEN_WORD { + if (parser->default_name) + pb_log_fn("Duplicate default option, ignoring\n"); + else + parser->default_name = talloc_strdup(parser, $3); + } + | TOKEN_DEV_DESCRIPTION delims TOKEN_WORD { + native_append_string(parser, + &parser->ctx->device->device->description, $3); + } + ; + +boot_options: + boot_options option + | option + ; + +option: name params + ; + +name: TOKEN_NAME delims TOKEN_WORD { + native_parser_create_option(parser, $3); + } + ; + +params: params param + | param + ; + +param: TOKEN_IMAGE delims TOKEN_WORD { + native_set_resource(parser, &parser->opt->boot_image, $3); + } + | TOKEN_INITRD delims TOKEN_WORD { + native_set_resource(parser, &parser->opt->initrd, $3); + } + | TOKEN_DTB delims TOKEN_WORD { + native_set_resource(parser, &parser->opt->dtb, $3); + } + | TOKEN_ARGS delims TOKEN_WORD { + native_append_string(parser, &parser->opt->option->boot_args, $3); + } + | TOKEN_DESCRIPTION delims TOKEN_WORD { + native_append_string(parser, &parser->opt->option->description, $3); + } + ; + +delims: delims TOKEN_DELIM + | TOKEN_DELIM + ; + +%% + +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...) +{ + const char *str; + va_list ap; + + va_start(ap, fmt); + str = talloc_vasprintf(parser, fmt, ap); + va_end(ap); + + pb_log("parse error: %d('%s'): %s\n", nget_lineno(scanner), + nget_text(scanner), str); +} + +void native_parser_finish(struct native_parser *parser) +{ + if (parser->opt) { + discover_context_add_boot_option(parser->ctx, parser->opt); + parser->opt = NULL; + } +} + +void native_set_resource(struct native_parser *parser, + struct resource ** resource, const char *path) +{ + if (*resource) { + pb_log_fn("Duplicate resource at line %d: %s\n", + nget_lineno(parser->scanner), path); + return; + } + + *resource = create_devpath_resource(parser->opt, parser->opt->device, + path); +} + +void native_append_string(struct native_parser *parser, + char **str, const char *append) +{ + if (*str) + *str = talloc_asprintf_append(*str, "%s", append); + else + *str = talloc_strdup(parser->opt, append); +} + +void native_parser_create_option(struct native_parser *parser, const char *name) +{ + struct discover_boot_option *opt = parser->opt; + + if (opt) + native_parser_finish(parser); + + opt = discover_boot_option_create(parser->ctx, parser->ctx->device); + opt->option->name = talloc_strdup(opt, name); + opt->option->id = talloc_asprintf(opt, "%s@%p", + parser->ctx->device->device->id, opt); + opt->option->type = DISCOVER_BOOT_OPTION; + opt->option->is_default = parser->default_name && + streq(parser->default_name, name); + parser->opt = opt; + return; +} + +struct native_parser *native_parser_create(struct discover_context *ctx) +{ + struct native_parser *parser; + + parser = talloc_zero(ctx, struct native_parser); + parser->ctx = ctx; + nlex_init_extra(parser, &parser->scanner); + + return parser; +} + +void native_parser_parse(struct native_parser *parser, const char *filename, + char *buf, int len) +{ + YY_BUFFER_STATE bufstate; + int rc; + + if (!len) + return; + + parser->filename = filename; + + bufstate = n_scan_bytes(buf, len - 1, parser->scanner); + nset_lineno(1, parser->scanner); + + rc = nparse(parser, parser->scanner); + + if (rc) + pb_log("Failed to parse %s\n", filename); + + n_delete_buffer(bufstate, parser->scanner); +} + diff --git a/discover/native/native.c b/discover/native/native.c new file mode 100644 index 0000000..964ad1d --- /dev/null +++ b/discover/native/native.c @@ -0,0 +1,55 @@ +#include <assert.h> +#include <string.h> +#include <i18n/i18n.h> + +#include <talloc/talloc.h> +#include <url/url.h> + +#include <discover/resource.h> +#include <discover/parser.h> +#include <discover/parser-utils.h> + +#include "native.h" + +static const char *const native_conf_files[] = { + "/boot/petitboot.conf", + "/petitboot.conf", + NULL +}; + +static int native_parse(struct discover_context *dc) +{ + const char * const *filename; + struct native_parser *parser; + int len, rc; + char *buf; + + /* Support block device boot only at present */ + if (dc->event) + return -1; + + for (filename = native_conf_files; *filename; filename++) { + rc = parser_request_file(dc, dc->device, *filename, &buf, &len); + if (rc) + continue; + + parser = native_parser_create(dc); + native_parser_parse(parser, *filename, buf, len); + device_handler_status_dev_info(dc->handler, dc->device, + _("Parsed native configuration from %s"), + *filename); + talloc_free(buf); + talloc_free(parser); + break; + } + + return 0; +} + +static struct parser native_parser = { + .name = "native", + .parse = native_parse, + .resolve_resource = resolve_devpath_resource, +}; + +register_parser(native_parser); diff --git a/discover/native/native.h b/discover/native/native.h new file mode 100644 index 0000000..1cf7d6a --- /dev/null +++ b/discover/native/native.h @@ -0,0 +1,27 @@ +#ifndef NATIVE_H +#define NATIVE_H + +#include <discover/device-handler.h> + +struct native_parser { + struct discover_context *ctx; + struct discover_boot_option *opt; + void *scanner; + const char *filename; + char *default_name; +}; + +void native_parser_finish(struct native_parser *parser); +void native_set_resource(struct native_parser *parser, struct resource **, + const char *path); +void native_append_string(struct native_parser *parser, + char **str, const char *append); +void native_parser_create_option(struct native_parser *parser, + const char *name); + +/* external parser api */ +struct native_parser *native_parser_create(struct discover_context *ctx); +void native_parser_parse(struct native_parser *parser, const char *filename, + char *buf, int len); +#endif /* NATIVE_H */ + |