Exemplo n.º 1
0
/* lpty_readline
 *
 * read a line from the master side of a pty.
 *
 * Arguments:
 *	L	Lua State
 *
 * Lua Stack:
 *	1	lpty userdata
 * 	2	(optional) timeout in seconds
 *
 * Lua Returns:
 *	+1	the data read from the master side of the pty, or nil if the read timed
 * 		out
 * 
 * Note:
 * 	you also read back the stuff written to the pty with lpty_write() below!
 */
static int lpty_readline(lua_State *L)
{
	lPty *pty = lpty_checkLPty(L, 1);
	int wantnl = _lpty_optboolean(L, 2, 0);
	double timeo = (double)luaL_optnumber(L, 3, -1);
	char buf[READER_BUFSIZ]; // should probably be more flexible
	int rd = 0;
	int ok = 1, isline = 0;
	double start = _lpty_gettime();
	double tmo = timeo;

	if (start < 0)
		return _lpty_error(L, pty->flags.throwerrors, "lpty readline failed: (%d) %s", errno, strerror(errno));

	if (timeo < 0) {
		tmo = 0;
		ok = _lpty_waitfordata(pty, (2^32)-1, 0);
	}
		
	do {
		ok = _lpty_waitfordata(pty, tmo, 0);
			
		if (ok > 0) {
			if (read(pty->m_fd, buf + rd, 1) > 0) {
				if (buf[rd] == '\n')
					isline = 1;
				++rd;
			} else {
				ok = 0;
			}
		}
		if (!isline && ok && timeo > 0) {
			double now = _lpty_gettime();

			if (now < 0)
				return _lpty_error(L, pty->flags.throwerrors, "lpty readline failed: (%d) %s", errno, strerror(errno));
		
			if (now - timeo >= start)
				isline = 1;
			else {
				tmo = timeo + start - now;
				if (tmo < 0) tmo = 0;
				ok = 1;
			}
		}
	} while (rd < READER_BUFSIZ && !isline && ok);
	
	if (rd > 0) {
		if (!wantnl && buf[rd-1] == '\n') --rd;
		if (!wantnl && buf[rd-1] == '\r') --rd;
		buf[rd] = 0;
		lua_pushstring(L, buf);
	/* we don't consider EINTR and ECHILD errors */
	} else if (errno && (errno != EINTR) && (errno != ECHILD))
		return _lpty_error(L, pty->flags.throwerrors, "lpty readline failed: (%d) %s", errno, strerror(errno));
	else
		lua_pushnil(L);
	return 1;
}
Exemplo n.º 2
0
Arquivo: lpty.c Projeto: jgaskins/brat
/* lpty_sendok
 *
 * Check wether the master side of this pty can accept data from us
 *
 * Arguments:
 *	L	Lua State
 *
 * Lua Stack:
 *	1	lpty userdata
 * 	2	(optional) timeout in seconds
 *
 * Lua Returns:
 *	+1	true if pty can accept data, false if not.
 */
static int lpty_sendok(lua_State *L)
{
	lPty *pty = lpty_checkLPty(L, 1);
	double timeo = (double)luaL_optnumber(L, 2, 0);

	int ok = _lpty_waitfordata(pty, timeo, 1);
	lua_pushboolean(L, ok > 0);
	return 1;
}
Exemplo n.º 3
0
Arquivo: lpty.c Projeto: jgaskins/brat
/* lpty_send
 *
 * write data to the master side of a pty
 *
 * Arguments:
 *	L	Lua State
 *
 * Lua Stack:
 *	1	lpty userdata
 *  2	data to write
 * 	3	(optional) timeout in seconds
 *
 * Lua Returns:
 *	+1	the amount of bytes actually written, or nil if the write attempt timed
 * 		out
 */
static int lpty_send(lua_State *L)
{
	lPty *pty = lpty_checkLPty(L, 1);
	const char *data = luaL_checkstring(L, 2);
	double timeo = (double)luaL_optnumber(L, 3, -1);
	int written = -1;
	int ok = 1;

	if (timeo >= 0)
		ok = _lpty_waitfordata(pty, timeo, 1);
	if (ok > 0)
		written = write(pty->m_fd, data, strlen(data));
	if (written >= 0)
		lua_pushinteger(L, written);
	else if (errno && (errno != EINTR))
		return lpty_error(L, pty->flags.throwerrors, "lpty send failed: (%d) %s", errno, strerror(errno));
	else
		lua_pushnil(L);
	return 1;
}
Exemplo n.º 4
0
Arquivo: lpty.c Projeto: jgaskins/brat
/* lpty_read
 *
 * read data from the master side of a pty.
 *
 * Arguments:
 *	L	Lua State
 *
 * Lua Stack:
 *	1	lpty userdata
 * 	2	(optional) timeout in seconds
 *
 * Lua Returns:
 *	+1	the data read from the master side of the pty, or nil if the read timed
 * 		out
 * 
 * Note:
 * 	you also read back the stuff written to the pty with lpty_write() below!
 */
static int lpty_read(lua_State *L)
{
	lPty *pty = lpty_checkLPty(L, 1);
	double timeo = (double)luaL_optnumber(L, 2, -1);
	char buf[READER_BUFSIZ]; // should probably be more flexible
	int readn = -1;
	int ok = 1;

	if (timeo >= 0)
		ok = _lpty_waitfordata(pty, timeo, 0);
	if (ok > 0)
		readn = read(pty->m_fd, buf, READER_BUFSIZ);
	if (readn >= 0) {
		buf[readn] = 0;
		lua_pushstring(L, buf);
	/* we don't consider EINTR and ECHILD errors */
	} else if (errno && (errno != EINTR) && (errno != ECHILD))
		return lpty_error(L, pty->flags.throwerrors, "lpty read failed: (%d) %s", errno, strerror(errno));
	else
		lua_pushnil(L);
	return 1;
}