lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Wed, May 22, 2019 at 4:20 AM Francisco Olarte <folarte@peoplecall.com> wrote:
Baozeng:

On Wed, May 22, 2019 at 4:15 AM Baozeng <sploving1@gmail.com> wrote:
> I have a simple Lua file, for example, hello.lua. The contents of the file starts with a shebang

This is not a simple lua file, shebangs are not valid lua.

>  I first read the file content into a buffer. And then use luaL_loadbuffer and lua_pcall to call the function. However, I found that luaL_loadbuffer cannot return normally because it does not deal with the buffer started with a shebang.

If you take a look at the sources of luaL_loadfile() you'll notice it
skips initial garbage ( BOMs, shell comments ) with an auxiliary
skipcomment() function.

You have two simple solutions:

1.- If you can, use luaL_loadfile instead of loading yourself, let
auxlib do the work.

2.- Otherwise, reimplement ( by creative imitation if you want ) skip
comment to skip the shebang lines on the buffer ( test * buf, if it's
# increment it until you hit end of buffer decrementing sz along the
way, the usual stuff ) ( I would wrap this in a
load_buffer_skip_shebang ).

Francisco Olarte.


Option 3: if (buffer[0] == '#' && buffer[1] == '!') { buffer[0] = '-'; buffer[1] = '-'; }

A shebang is syntactically a comment in shell scripting, so replacing one comment syntax with another is a fairly elegant solution IMO. It's an extremely cheap check, an extremely cheap patch, and completely safe.

/s/ Adam