Table of Contents

Add support for Lua scripts to the Loader

Project description

The project aims to design and implement a modular interface for the loader script
interpreter decoupling the interpreter from the loader.

Thus one can divide the project in two tasks:

interpreters (Simple and Forth) to this interface.

scripts.

With this approach one want to provide a easy way to choose (at compile time) which
interpreter the loader will use, and also provide a skeleton for adding others scripting
languages.

Approach to solving the problem

Interface design

src/sys/boot/common/interp.h

typedef void	interp_init_t(void *ctx);
typedef int	interp_run_t(void *ctx, const char *input);
typedef int	interp_incl_t(void *ctx, const char *filename);
typedef int	interp_load_def_t(void *ctx);

struct interp {
	interp_init_t	*init;
	interp_run_t	*run;
	interp_incl_t	*incl;
	interp_load_def_t *load_configs;
	void		*context;
};

The above C code shows the interpreter interface which contains:

Lua interp implementation

The lua interpreter is written in C and it uses some standard C functions. To embedded
Lua into to loader the missing standard C functions (not present in libstand) where added
in src/sys/lua/src/lstd.h and src/sys/lua/src/lstd.c.

With the Lua embedded into the loader the interp interface was implemented in
src/sys/boot/common/interp_lua.c.

Lua scripts

The scripts where implemented to match all the features present in the Forth scripts
except for the PNP script (src/sys/boot/forth/pnp.4th) which I could not find any reference
to its functions and I treated it as deprecated.

The scripts are located at src/sys/boot/lua together with lutils.h and lutils.c
which implements the following builtin lua functions:

If size parameter is nil the function reads the whole file.

Besides these builtin functions all functions from Lua baselib and stringlib are provided.

List of lua scripts:

Deliverables

Milestones

Interpreter interface definition DONE
Adapt existing interpretes to the interface DONE
Lua interpreter working with the loader DONE
Lua loader builtin functions
loader.**** functions DONE
Lua file io functions DONE
Lua base library DONE
Lua string library DONE
Kernel loading functions DONE
Config file parsing and module loading DONE
Boot menu DONE
PnP config & setup DEPRECATED

New milestones

Fix drawer.isColorEnabled() DONE
Fix menu.run() proper environment variable checks DONE
Merge menu.options.alias with menu.options TODO
Fix kernels menu option logic DONE
Add dynamic menu options ordering (boot simple/multi) TODO
Fix ANSI vs non-ANSI coloring NEED TEST

Testing

Checkout the code:

$svn co https://socsvn.freebsd.org/socsvn/soc2014/pedrosouza/lua_loader/

By default the Lua interpreter is enabled, to revert to the Forth interpreter edit /etc/src.conf and add the following lines:

WITH_FORTH="yes"
WITHOUT_LUA="yes"

or if you want to disable both Lua and Forth and use the simple interpreter:

WITHOUT_FORTH="yes"
WITHOUT_LUA="yes"

Then build and install the boot loader:

$cd head/sys/boot
$make
$su
$make install

The Code

https://socsvn.freebsd.org/socsvn/soc2014/pedrosouza/lua_loader/

http://www.lua.org