|
1 |
|
2 #include <lua.h> |
|
3 #include <lauxlib.h> |
|
4 #include <glib.h> |
|
5 #include <stdio.h> // popen, pclose, fileno |
|
6 |
|
7 #include "util.h" |
|
8 #include "glib_types.h" |
|
9 |
|
10 static gchar lglib_io_buffer[128]; |
|
11 |
|
12 void lglib_io_callback_destroy (lglib_io_callback_t *cb) |
|
13 { |
|
14 luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
15 pclose (cb->fd); |
|
16 luaL_free (cb->L, cb); |
|
17 } |
|
18 |
|
19 // data/nil |
|
20 static gboolean lglib_io_callback (GIOChannel *source, GIOCondition *condition, lglib_io_callback_t *cb) |
|
21 { |
|
22 gsize read = 0; |
|
23 int status; |
|
24 lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
25 status = g_io_channel_read_chars (source, lglib_io_buffer, 128, &read, NULL); |
|
26 if (read) { |
|
27 lua_pushlstring (cb->L, lglib_io_buffer, read); |
|
28 } else if (status == G_IO_STATUS_EOF) { |
|
29 lua_pushnil (cb->L); |
|
30 } else { |
|
31 return status != G_IO_STATUS_ERROR; |
|
32 } |
|
33 if (lua_pcall (cb->L, 1, 1, 0)) |
|
34 lua_error (cb->L); |
|
35 return lua_toboolean (cb->L, -1); |
|
36 } |
|
37 |
|
38 // command function |
|
39 static int lglib_io_new (lua_State *L) |
|
40 { |
|
41 const char *command = luaL_checkstring (L, 1); |
|
42 lglib_io_callback_t *cb; |
|
43 FILE *fd; |
|
44 GIOChannel *channel; |
|
45 //const char *charset; |
|
46 luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
47 |
|
48 fd = popen (command, "r"); |
|
49 if (!fd) { |
|
50 lua_pushstring (L, "Error opening pipe"); |
|
51 lua_error (L); |
|
52 } |
|
53 channel = g_io_channel_unix_new (fileno (fd)); |
|
54 //if (!g_get_charset (&charset)) |
|
55 // g_io_channel_set_encoding (channel, charset, NULL); |
|
56 |
|
57 cb = luaL_malloc (L, sizeof (lglib_io_callback_t)); |
|
58 cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
59 cb->L = L; |
|
60 cb->fd = fd; |
|
61 |
|
62 g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN, |
|
63 (GSourceFunc)lglib_io_callback, cb, |
|
64 (GDestroyNotify)lglib_io_callback_destroy); |
|
65 return 0; |
|
66 } |
|
67 |
|
68 static const luaL_Reg lglib_io_reg_f[] = { |
|
69 { "new", lglib_io_new }, |
|
70 { NULL, NULL }, |
|
71 }; |
|
72 |
|
73 int luaopen_glib_io (lua_State *L) |
|
74 { |
|
75 luaL_register (L, "g.io", lglib_io_reg_f); |
|
76 return 1; |
|
77 } |
|
78 |