discover/grub2: Clean up error-handling for grub2 parser & lexer
authorJeremy Kerr <jk@ozlabs.org>
Thu, 26 Sep 2013 07:45:58 +0000 (15:45 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 26 Sep 2013 07:45:58 +0000 (15:45 +0800)
Rather than printf() & exit(), use the pb logging functions and abort
the parse.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/grub2/lexer.l
discover/grub2/parser.y
discover/grub2/script.c
test/parser/Makefile.am
test/parser/test-grub2-lexer-error.c [new file with mode: 0644]
test/parser/test-grub2-parser-error.c [new file with mode: 0644]

index 0558ed9d791c38c12b852e1eba95389aa65b0915..e1aad9931252f98be623a5fc87035a61f3de4ef2 100644 (file)
@@ -3,6 +3,8 @@
 #include "grub2.h"
 #include "parser.h"
 #include <talloc/talloc.h>
+
+void yyerror(struct grub2_parser *parser, const char *fmt, ...);
 %}
 
 %option nounput noinput
@@ -118,7 +120,10 @@ VARNAME ([[:alpha:]][_[:alnum:]]*|[0-9]|[\?@\*#])
  /* strip comments */
 #.*    ;
 
-.      printf("unknown token '%s'\n", yytext); exit(1);
+.      {
+               yyerror(yyget_extra(yyscanner), "unknown token '%s'\n", yytext);
+               yyterminate();
+       }
 
 %%
 
index 02ca7b2799f53f57bc9ef55f6dcfacbdae880a67..8ab17a6ab50c45ca49f8ebc8d566508bf607c38e 100644 (file)
@@ -6,17 +6,15 @@
 
 %{
 #include <talloc/talloc.h>
+#include <log/log.h>
 
 #include "grub2.h"
 #include "parser.h"
 #include "lexer.h"
 
-static void print_token(FILE *fp, int type, YYSTYPE value);
-
 #define YYLEX_PARAM parser->scanner
-#define YYPRINT(f, t, v) print_token(f, t, v)
 
-static void yyerror(struct grub2_parser *, char const *s);
+void yyerror(struct grub2_parser *parser, const char *fmt, ...);
 %}
 
 %union {
@@ -148,18 +146,17 @@ word:     TOKEN_WORD
        }
 
 %%
-void yyerror(struct grub2_parser *parser, char const *s)
+void yyerror(struct grub2_parser *parser, const char *fmt, ...)
 {
-       fprintf(stderr, "%d: error: %s '%s'\n",
-                       yyget_lineno(parser->scanner),
-                       s, yyget_text(parser->scanner));
-}
+       const char *str;
+       va_list ap;
 
-static void print_token(FILE *fp, int type, YYSTYPE value)
-{
-       if (type != TOKEN_WORD)
-               return;
-       fprintf(fp, "%s", value.word->text);
+       va_start(ap, fmt);
+       str = talloc_vasprintf(parser, fmt, ap);
+       va_end(ap);
+
+       pb_log("parse error: %d('%s'): %s\n", yyget_lineno(parser->scanner),
+                                       yyget_text(parser->scanner), str);
 }
 
 struct grub2_statements *create_statements(struct grub2_parser *parser)
index 0cf21967450efb3fbdf22c76f901c1c312b4d9d1..d0b824aebcb9965e63d2ec0d0cb631da0a2dfc5d 100644 (file)
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <stdlib.h>
 
+#include <log/log.h>
 #include <types/types.h>
 #include <talloc/talloc.h>
 
@@ -278,7 +279,7 @@ int statement_simple_execute(struct grub2_script *script,
 
        entry = script_lookup_function(script, st->argv->argv[0]);
        if (!entry) {
-               fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]);
+               pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
                return 1;
        }
 
index 8159bd677647ef9186323e521add3e2204764435..e7146c5545ec43be0883867cacadba912f431146 100644 (file)
@@ -35,6 +35,8 @@ TESTS = \
        test-grub2-single-line-if \
        test-grub2-f18-ppc64 \
        test-grub2-ubuntu-13_04-x86 \
+       test-grub2-lexer-error \
+       test-grub2-parser-error \
        test-kboot-single \
        test-yaboot-single \
        test-yaboot-partition \
diff --git a/test/parser/test-grub2-lexer-error.c b/test/parser/test-grub2-lexer-error.c
new file mode 100644 (file)
index 0000000..503ec99
--- /dev/null
@@ -0,0 +1,13 @@
+
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+&
+#endif
+
+void run_test(struct parser_test *test)
+{
+       test_read_conf_embedded(test);
+       test_run_parser(test, "grub2");
+       check_boot_option_count(test->ctx, 0);
+}
diff --git a/test/parser/test-grub2-parser-error.c b/test/parser/test-grub2-parser-error.c
new file mode 100644 (file)
index 0000000..a7a00a4
--- /dev/null
@@ -0,0 +1,13 @@
+
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+{
+#endif
+
+void run_test(struct parser_test *test)
+{
+       test_read_conf_embedded(test);
+       test_run_parser(test, "grub2");
+       check_boot_option_count(test->ctx, 0);
+}