glib_io.c
author Myhailo Danylenko <isbear@ukrpost.net>
Mon, 09 Feb 2009 13:00:42 +0200
changeset 2 34b6fedde9eb
parent 0 84fdfb0344c9
permissions -rw-r--r--
Switch to cmake


#include <lua.h>
#include <lauxlib.h>
#include <glib.h>
#include <stdio.h>    // popen, pclose, fileno

#include "util.h"
#include "glib_types.h"

static gchar lglib_io_buffer[128];

void lglib_io_callback_destroy (lglib_io_callback_t *cb)
{
	luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
	pclose (cb->fd);
	luaL_free (cb->L, cb);
}

// data/nil
static gboolean lglib_io_callback (GIOChannel *source, GIOCondition *condition, lglib_io_callback_t *cb)
{
	gsize read = 0;
	int status;
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
	status = g_io_channel_read_chars (source, lglib_io_buffer, 128, &read, NULL);
	if (read) {
		lua_pushlstring (cb->L, lglib_io_buffer, read);
	} else if (status == G_IO_STATUS_EOF) {
		lua_pushnil (cb->L);
	} else {
		return status != G_IO_STATUS_ERROR;
	}
	if (lua_pcall (cb->L, 1, 1, 0))
		lua_error (cb->L);
	return lua_toboolean (cb->L, -1);
}

// command function
static int lglib_io_new (lua_State *L)
{
	const char *command = luaL_checkstring (L, 1);
	lglib_io_callback_t *cb;
	FILE *fd;
	GIOChannel *channel;
	//const char *charset;
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");

	fd = popen (command, "r");
	if (!fd) {
		lua_pushstring (L, "Error opening pipe");
		lua_error (L);
	}
	channel = g_io_channel_unix_new (fileno (fd));
	//if (!g_get_charset (&charset))
	//	g_io_channel_set_encoding (channel, charset, NULL);

	cb = luaL_malloc (L, sizeof (lglib_io_callback_t));
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
	cb->L         = L;
	cb->fd        = fd;

	g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN,
				(GSourceFunc)lglib_io_callback, cb,
				(GDestroyNotify)lglib_io_callback_destroy);
	return 0;
}

static const luaL_Reg lglib_io_reg_f[] = {
	{ "new", lglib_io_new },
	{ NULL,  NULL         },
};

int luaopen_glib_io (lua_State *L)
{
	luaL_register (L, "g.io", lglib_io_reg_f);
	return 1;
}