Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active November 5, 2015 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/a360ac322855931461b8 to your computer and use it in GitHub Desktop.
Save creationix/a360ac322855931461b8 to your computer and use it in GitHub Desktop.
local ffi = require('ffi')
local uv = require('uv')
local wrapStream = require('coro-channel').wrapStream
local wrapRead = require('coro-channel').wrapRead
local split = require('coro-split')
ffi.cdef[[
struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* unused */
unsigned short ws_ypixel; /* unused */
};
int openpty(int *amaster, int *aslave, char *name,
void *termp,
const struct winsize *winp);
]]
local master = ffi.new("int[1]")
local slave = ffi.new("int[1]")
local name = ffi.new("char[1024]")
local winp = ffi.new("struct winsize")
winp.ws_row = 24
winp.ws_col = 80
-- Create the pty
ffi.C.openpty(master, slave, name, nil, winp)
-- Convert to lua values, freeing the ffi values.
master, slave, name = master[0], slave[0], ffi.string(name)
p {
master = master,
slave = slave,
name = name
}
uv.spawn("/bin/stty", {
args = {"-a"},
stdio = {slave, slave, slave}
}, function (...)
p("onexit", ...)
end)
local pipe = uv.new_pipe(false)
pipe:open(master)
pipe:read_start(function (err, data)
assert(not err, err)
print(data)
end)
uv.run()
uv.tty_reset_mode()
@creationix
Copy link
Author

Sample Output:

{ slave = 26, master = 27, name = '/dev/ttys046' }
speed 9600 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
    -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -iutf8
    -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
    -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
    eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
    min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
    stop = ^S; susp = ^Z; time = 0; werase = ^W;

'onexit'    0   0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment