[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Trapping Control-C in an interactive Lua program
- From: steve donovan <steve.j.donovan@...>
- Date: Mon, 12 Apr 2010 14:02:55 +0200
On Mon, Apr 12, 2010 at 12:19 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> Thanks, Jerome, that does seem to help.
The final solution is a little ... bizarre. Here is the function that
grabs user input:
local stat,res = pcall(io.read)
pcall(io.write,'')
if not res then
print 'ctrl-c caught'
signal(2,1) -- SIGINT,SIG_IGN
return ''
end
return res
This is compensating for the following SIGINT handler in lua.c:
static void lstop (lua_State *L, lua_Debug *ar) {
(void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0);
luaL_error(L, "interrupted!");
}
static void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
So we catch 'interrupted!' in the protected io.write() call , and can
then switch off ctrl-C handling.
This is on Windows, don't know if the trickery is portable yet.
steve d.