summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2013-01-18 19:58:35 +0100
committerEmil Renner Berthing <esmil@mailme.dk>2013-01-20 21:37:54 +0100
commit8aa4f937267bd898da1d2ed49329e9529492b03d (patch)
tree6858fad95f53368e06c8ee128435a03dee78641a
parent4e249f90158c0b5250f176853bbec23af1c21177 (diff)
downloadlem-8aa4f937267bd898da1d2ed49329e9529492b03d.tar.gz
lem-8aa4f937267bd898da1d2ed49329e9529492b03d.tar.xz
lem-8aa4f937267bd898da1d2ed49329e9529492b03d.zip
io: always create stdin, stderr and stdout
-rw-r--r--lem/io/core.c62
1 files changed, 20 insertions, 42 deletions
diff --git a/lem/io/core.c b/lem/io/core.c
index 0b30dc1..97d780b 100644
--- a/lem/io/core.c
+++ b/lem/io/core.c
@@ -73,38 +73,6 @@ io_strerror(lua_State *T, int err)
#include "tcp.c"
#include "unix.c"
-static int
-module_index(lua_State *T)
-{
- const char *key = lua_tostring(T, 2);
- int fd;
-
- if (strcmp(key, "stdin") == 0)
- fd = 0;
- else if (strcmp(key, "stdout") == 0)
- fd = 1;
- else if (strcmp(key, "stderr") == 0)
- fd = 2;
- else
- return 0;
-
- /* make the socket non-blocking */
- if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
- lua_pushnil(T);
- lua_pushfstring(T, "error making filedescriptor non-blocking: %s",
- strerror(errno));
- return 2;
- }
-
- stream_new(T, fd, lua_upvalueindex(1));
-
- /* save this object so we don't initialize it again */
- lua_pushvalue(T, 2);
- lua_pushvalue(T, -2);
- lua_rawset(T, 1);
- return 1;
-}
-
struct open {
struct lem_async a;
const char *path;
@@ -239,6 +207,17 @@ io_open(lua_State *T)
return lua_yield(T, 3);
}
+static void
+push_stdstream(lua_State *L, int fd)
+{
+ /* make the socket non-blocking */
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
+ luaL_error(L, "error making fd %d non-blocking: %s",
+ fd, strerror(errno));
+
+ stream_new(L, fd, -2);
+}
+
int
luaopen_lem_io_core(lua_State *L)
{
@@ -306,6 +285,15 @@ luaopen_lem_io_core(lua_State *L)
/* mt.sendfile = <stream_sendfile> */
lua_pushcfunction(L, stream_sendfile);
lua_setfield(L, -2, "sendfile");
+ /* insert io.stdin stream */
+ push_stdstream(L, STDIN_FILENO);
+ lua_setfield(L, -3, "stdin");
+ /* insert io.stdout stream */
+ push_stdstream(L, STDOUT_FILENO);
+ lua_setfield(L, -3, "stdout");
+ /* insert io.stderr stream */
+ push_stdstream(L, STDERR_FILENO);
+ lua_setfield(L, -3, "stderr");
/* insert table */
lua_setfield(L, -2, "Stream");
@@ -380,15 +368,5 @@ luaopen_lem_io_core(lua_State *L)
/* insert the tcp table */
lua_setfield(L, -2, "unix");
- /* create metatable for the module */
- lua_newtable(L);
- /* insert the index function */
- lua_getfield(L, -2, "Stream"); /* upvalue 1 = Stream */
- lua_pushcclosure(L, module_index, 1);
- lua_setfield(L, -2, "__index");
-
- /* set the metatable */
- lua_setmetatable(L, -2);
-
return 1;
}