Skip to contentSkip to conversation

On GRUB script lexer rewrite using flex

PATCH BVK Chaitanya 1 participant 1 message Dec 20, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi,

I was working on rewriting GRUB script lexer using Flex from last couple of weeks, and this work is now complete. Changes for this are available in grub-script branch here
http://bzr.savannah.gnu.org/r/grub/people/bvk/ The same is attached as a patch with this mail.

This branch also includes code from my unit-testing branch, that has unit testing framework and some GRUB script unit tests, which also need reviewing.

Three important points to note are:

1. Flex generated lexer always hard-codes libc headers (stdio.h, string.h, etc) which are not available for GRUB environment. To get around this, I am removing the #include lines from the Flex output files, using make rules. (See common.rmk and any-emu.rmk)

2. Another Flex problem is, Flex generated files use FILE*, errno, etc. libc features, which are again not available for GRUB. I used some pre-processor hacks to get around these items. (See %top section in yylex.l)

3. Lexer generated using Flex is now 30KB in size and makes sh.mod to 46KB. Our existing, hand-coded lexer is only of 5KB (and sh.mod is 15KB).

Let me know your comments.

thanks,

Signature · 1 line
bvk.chaitanya

=== added file 'ChangeLog.scripting'

ChangeLog.scripting +47 -0
@@ -0,0 +1,47 @@
+2009-12-20  BVK Chaitanya  <[email protected]>
+
+	* conf/any-emu.rmk: Makerules for flex based lexer.
+	* conf/common.rmk: Likewise.
+	* conf/i386-coreboot.rmk: Likewise.
+	* conf/i386-efi.rmk: Likewise.
+	* conf/i386-ieee1275.rmk: Likewise.
+	* conf/i386-pc.rmk: Likewise.
+	* conf/mips.rmk: Likewise.
+	* conf/powerpc-ieee1275.rmk: Likewise.
+	* conf/x86_64-efi.rmk: Likewise.
+	* configure.ac: Configure rule for flex.
+	* include/grub/script_sh.h (grub_lexer_param): Simplified.
+	(grub_script_lexer_fini): New function to deinit grub_lexer_param.
+	(grub_script_lexer_init): Takes grub_parser_param parameter.
+	(grub_script_lexer_record_start): Likewise.
+	(grub_script_lexer_record_stop): Likewise.
+	(grub_script_lexer_yywrap): New function to feed more data to
+	lexer.
+	(grub_script_lexer_record): New function to save source code
+	tokens.
+	(grub_script_yylex): Rewrote to use yylex for token parsing.
+	* script/lexer.c: New defintions for above functions.
+	* script/parser.y: Better and more complete grammar.
+	* script/script.c: Cleanup.
+	(grub_script_parse): grub_lexer_param deinitialized using new
+	grub_script_lexer_fini function.
+	* script/yylex.l: Newfile. Flex script for GRUB Script.
+
+2009-12-20  BVK Chaitanya  <bvk@dbook>
+
+	Added --verbose (-v) option to grub-script-check.
+
+	* util/grub-script-check.c (main): Added --verbose option.
+
+2009-12-19  BVK Chaitanya  <bvk@dbook>
+
+	Added grub-script-check utility.
+
+	* util/grub-script-check.c: Newfile.
+	* conf/common.rmk: Makerules for building grub-script-check.
+
+2009-12-17  BVK Chaitanya  <[email protected]>
+
+	Bug fixes in GRUB script parsing.
+
+	* script/parser.y: Better grammar.

=== added file 'ChangeLog.unit-testing'

ChangeLog.unit-testing +35 -0
@@ -0,0 +1,35 @@
+2009-12-20  BVK Chaitanya  <bvk@dbook>
+
+	* Makefile.in: Unit tests are skipped for platform=emu.
+
+2009-12-19  BVK Chaitanya  <bvk@dbook>
+
+	New unit tests for GRUB script parser.
+
+	* conf/tests.rmk: Make rules for new unit tests.
+	* tests/grub_script_echo1.in: New unit test.
+	* tests/grub_script_functions1.in: New unit test.
+
+
+2009-12-17  BVK Chaitanya  <[email protected]>
+
+	Unit testing framework for GRUB
+
+	* Makefile.in: Added make check rules.
+	* genmk.rb: Added TESTS type target for unit tests.
+	* ChangeLog.unit-testing: Newfile. ChangeLog for unit tests
+	branch.
+	* conf/tests.rmk: Newfile. Makefile for tests and test framework.
+	* conf/util/grub-shell-tester.in: Newfile. Tool for runing grub
+	script tests.
+	* include/grub/test.h: Newfile. Testing framework header file.
+	* tests/test.c: Newfile. Common definitions for testing framework.
+	* test/unit_test.c: Newfile.  Unit tests framework.
+	* tests/functional_test.c: Newfile. Functional tests framework.
+	* tests/example_functional_test.c: Newfile. Functional test
+	example.
+	* tests/example_scripted_test.in: Newfile. Scripted test example.
+	* tests/example_unit_test.c: Newfile. Unit test example.
+	* tests/grub_sprintf.c: Newfile. Unittest for grub_printf logic.
+	* tests/gettext_1.in: Newfile. Scripted test for gettext.
+	* tests/echo_keywords.in: Newfile. Scripted test for GRUB parser.

=== modified file 'Makefile.in'

Makefile.in +12 -1
@@ -138,6 +138,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_TESTS) $(check_SCRIPTS) $(check_MODULES)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -164,6 +165,8 @@
 include $(srcdir)/conf/any-emu.mk
 else
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
+# For tests.
+include $(srcdir)/conf/tests.mk
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 endif
@@ -452,7 +455,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: all $(TESTS)
+	@list="$(check_TESTS) $(check_SCRIPTS)"; \
+	for file in $$list; do \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

=== modified file 'conf/any-emu.rmk'

conf/any-emu.rmk +8 -1
@@ -1,7 +1,7 @@
 # -*- makefile -*-
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 sbin_UTILITIES += grub-emu
 util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
@@ -32,6 +32,7 @@
 	normal/menu_text.c						\
 	script/main.c script/execute.c script/function.c		\
 	script/lexer.c script/script.c grub_script.tab.c		\
+	grub_script.yy.c							\
 	partmap/amiga.c	partmap/apple.c partmap/msdos.c partmap/sun.c	\
 	partmap/acorn.c partmap/gpt.c					\
 	\
@@ -123,3 +124,9 @@
 grub_script.tab.c grub_script.tab.h: script/parser.y
 	$(YACC) -d -p grub_script_yy -b grub_script $(srcdir)/script/parser.y
 DISTCLEANFILES += grub_script.tab.c grub_script.tab.h
+
+grub_script.yy.c grub_script.yy.h: script/yylex.l
+	$(LEX) -o grub_script.yy.c --header-file=grub_script.yy.h $(srcdir)/script/yylex.l
+	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.h
+	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.c
+DISTCLEANFILES += grub_script.yy.c grub_script.yy.h

=== modified file 'conf/common.rmk'

conf/common.rmk +30 -1
@@ -88,11 +88,40 @@
 bin_UTILITIES += grub-mkrelpath
 grub_mkrelpath_SOURCES = gnulib/progname.c util/grub-mkrelpath.c util/misc.c
 
+# For the lexer.
+grub_script.yy.c grub_script.yy.h: script/yylex.l
+	$(LEX) -o grub_script.yy.c --header-file=grub_script.yy.h $(srcdir)/script/yylex.l
+	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.h
+	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.c
+DISTCLEANFILES += grub_script.yy.c grub_script.yy.h
+
+# For grub-script-check.
+bin_UTILITIES += grub-script-check
+util/grub-script-check.c_DEPENDENCIES = grub_script_check_init.h
+grub_script_check_SOURCES = gnulib/progname.c util/grub-script-check.c util/misc.c \
+	script/main.c script/script.c script/function.c script/lexer.c \
+	kern/handler.c kern/err.c kern/parser.c kern/list.c \
+	kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \
+	grub_script.yy.c
+
 # For the parser.
 grub_script.tab.c grub_script.tab.h: script/parser.y
 	$(YACC) -d -p grub_script_yy -b grub_script $(srcdir)/script/parser.y
 DISTCLEANFILES += grub_script.tab.c grub_script.tab.h
 
+# For grub-check.
+grub_script_check_init.lst: geninit.sh $(filter-out grub_script_check_init.c,$(grub_script_check_SOURCES))
+	rm -f $@; grep GRUB_MOD_INIT $(filter %.c,$^) /dev/null > $@
+DISTCLEANFILES += grub_script_check_init.lst
+
+grub_script_check_init.h: grub_script_check_init.lst $(filter-out grub_script_check_init.c,$(grub_script_check_SOURCES)) geninitheader.sh
+	rm -f $@; sh $(srcdir)/geninitheader.sh $< > $@
+DISTCLEANFILES += grub_script_check_init.h
+
+grub_script_check_init.c: grub_script_check_init.lst $(filter-out grub_script_check_init.c,$(grub_script_check_SOURCES)) geninit.sh
+	rm -f $@; sh $(srcdir)/geninit.sh $< $(filter %.c,$^) > $@
+DISTCLEANFILES += grub_script_check_init.c
+
 # For grub-probe.
 grub_probe_init.lst: geninit.sh $(filter-out grub_probe_init.c,$(grub_probe_SOURCES))
 	rm -f $@; grep GRUB_MOD_INIT $(filter %.c,$^) /dev/null > $@
@@ -592,7 +621,7 @@
 
 # For sh.mod.
 sh_mod_SOURCES = script/main.c script/script.c script/execute.c \
-	script/function.c script/lexer.c grub_script.tab.c
+	script/function.c script/lexer.c grub_script.tab.c grub_script.yy.c
 sh_mod_CFLAGS = $(COMMON_CFLAGS)
 sh_mod_LDFLAGS = $(COMMON_LDFLAGS)
 

=== modified file 'conf/i386-coreboot.rmk'

conf/i386-coreboot.rmk +1 -1
@@ -5,7 +5,7 @@
 COMMON_LDFLAGS	= -m32 -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Images.
 

=== modified file 'conf/i386-efi.rmk'

conf/i386-efi.rmk +1 -1
@@ -5,7 +5,7 @@
 COMMON_LDFLAGS = -melf_i386 -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Utilities.
 bin_UTILITIES = grub-mkimage

=== modified file 'conf/i386-ieee1275.rmk'

conf/i386-ieee1275.rmk +1 -1
@@ -5,7 +5,7 @@
 COMMON_LDFLAGS	= -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Images.
 pkglib_PROGRAMS = kernel.img

=== modified file 'conf/i386-pc.rmk'

conf/i386-pc.rmk +1 -1
@@ -7,7 +7,7 @@
 COMMON_LDFLAGS = -m32 -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Images.
 pkglib_IMAGES = boot.img cdboot.img diskboot.img kernel.img lnxboot.img \

=== modified file 'conf/mips.rmk'

conf/mips.rmk +1 -1
@@ -6,7 +6,7 @@
 COMMON_LDFLAGS += -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Images.
 

=== modified file 'conf/powerpc-ieee1275.rmk'

conf/powerpc-ieee1275.rmk +1 -1
@@ -6,7 +6,7 @@
 COMMON_LDFLAGS += -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Images.
 

=== added file 'conf/tests.rmk'

conf/tests.rmk +47 -0

47 lines not on this page · raw message

=== modified file 'conf/x86_64-efi.rmk'

conf/x86_64-efi.rmk +1 -1
@@ -5,7 +5,7 @@
 COMMON_LDFLAGS = -melf_x86_64 -nostdlib
 
 # Used by various components.  These rules need to precede them.
-script/lexer.c_DEPENDENCIES = grub_script.tab.h
+script/lexer.c_DEPENDENCIES = grub_script.tab.h grub_script.yy.h
 
 # Utilities.
 bin_UTILITIES = grub-mkimage

=== modified file 'configure.ac'

configure.ac +5 -0
@@ -158,6 +158,11 @@
   AC_MSG_ERROR([cmp is not found])
 fi
 
+AC_CHECK_PROGS([LEX], [flex])
+if test "x$LEX" = x; then
+  AC_MSG_ERROR([flex is not found])
+fi
+
 AC_CHECK_PROGS([YACC], [bison])
 if test "x$YACC" = x; then
   AC_MSG_ERROR([bison is not found])

=== modified file 'genmk.rb'

genmk.rb +1 -1
@@ -413,7 +413,7 @@
 	    PModule.new(prefix, pmod)
 	  end
 
-	when 'UTILITIES'
+	when 'UTILITIES', 'TESTS'
 	  utils += args.split(/\s+/).collect do |util|
 	    Utility.new(prefix, util)
 	  end

=== modified file 'include/grub/script_sh.h'

include/grub/script_sh.h +29 -19

83 lines not on this page · raw message

=== added file 'include/grub/test.h'

include/grub/test.h +84 -0

84 lines not on this page · raw message

=== modified file 'script/lexer.c'

script/lexer.c +269 -372

671 lines not on this page · raw message

=== modified file 'script/parser.y'

script/parser.y +207 -153

380 lines not on this page · raw message

=== modified file 'script/script.c'

script/script.c +26 -22

144 lines not on this page · raw message

=== added file 'script/yylex.l'

script/yylex.l +281 -0

281 lines not on this page · raw message

=== added directory 'tests'
=== added file 'tests/echo_keywords.in'

tests/echo_keywords.in +3 -0
@@ -0,0 +1,3 @@
+#! @builddir@/grub-shell-tester
+
+echo if then else fi for do done

=== added file 'tests/example_functional_test.c'

tests/example_functional_test.c +17 -0
@@ -0,0 +1,17 @@
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Functional test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a functional test.  */
+GRUB_FUNCTIONAL_TEST ("example_functional_test", example_test);

=== added file 'tests/example_scripted_test.in'

tests/example_scripted_test.in +3 -0
@@ -0,0 +1,3 @@
+#!/bin/sh -e
+
+true

=== added file 'tests/example_unit_test.c'

tests/example_unit_test.c +20 -0
@@ -0,0 +1,20 @@
+/* Unit tests are normal programs, so they can include C library.  */
+#include <string.h>
+
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Unit test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a unit test.  */
+GRUB_UNIT_TEST ("example_unit_test", example_test);

=== added file 'tests/functional_test.c'

tests/functional_test.c +89 -0

89 lines not on this page · raw message

=== added file 'tests/gettext_1.in'

tests/gettext_1.in +4 -0
@@ -0,0 +1,4 @@
+#! @builddir@/grub-shell-tester --modules=gettext
+
+gettext "hello world"
+

=== added file 'tests/grub_script_echo1.in'

tests/grub_script_echo1.in +16 -0
@@ -0,0 +1,16 @@
+#! @builddir@/grub-shell-tester
+
+foo=bar
+echo $foo ${foo}
+echo "$foo" "${foo}"
+echo '$foo' '${foo}'
+echo a$foob a${foo}b
+echo ab"cd"ef$foo'gh'ij${foo}kl\ mn\"op\'qr\$st\(uv\<wx\>yz\)
+
+foo=c
+bar=h
+echo e"$foo"${bar}o
+e"$foo"${bar}o hello world
+
+foo=echo
+$foo 1234

=== added file 'tests/grub_script_functions1.in'

tests/grub_script_functions1.in +18 -0
@@ -0,0 +1,18 @@
+#! @builddir@/grub-shell-tester
+
+function foo { echo foo; }; foo
+function foo2
+{
+    echo foo2
+}
+foo2
+
+function bar { echo one; if true; then echo yes; else echo no; fi; echo bar; foo; }; bar
+function bar
+{
+    foo;
+    foo2;
+    bar;
+}
+
+

=== added file 'tests/grub_sprintf.c'

tests/grub_sprintf.c +162 -0

162 lines not on this page · raw message

=== added file 'tests/test.c'

tests/test.c +150 -0

150 lines not on this page · raw message

=== added file 'tests/unit_test.c'

tests/unit_test.c +122 -0

122 lines not on this page · raw message

=== added directory 'tests/util'
=== added file 'tests/util/grub-shell-tester.in'

tests/util/grub-shell-tester.in +128 -0

128 lines not on this page · raw message

=== added file 'util/grub-script-check.c'

util/grub-script-check.c +248 -0

248 lines not on this page · raw message