Bubble up C error messages
@@ -25,6 +25,7 @@ port = port and tonumber(port) or 6000
local ffi = require('ffi')
-- Define the bits of the system API we need.
ffi.cdef[[
+ char *strerror(int errnum);
int open(const char *pathname, int flags);
static const int O_RDWR = 2;
char *ptsname(int fd);
@@ -44,14 +45,26 @@ ffi.cdef[[
local C = ffi.C
+local function cassert(val, message)
+ if val then return val end
+ error(message .. ': ' .. ffi.string(C.strerror(ffi.errno())))
+end
+
+local function cwarn(val, message)
+ if not val then
+ print(message .. ': ' .. ffi.string(C.strerror(ffi.errno())))
+ end
+ return val
+end
+
local function openpty()
local master = C.open('/dev/ptmx', C.O_RDWR)
- assert(master > 0, 'Problem opening master for pty')
- assert(C.grantpt(master) == 0, 'Problem granting slave pts')
- assert(C.unlockpt(master) == 0, 'Problem unlocking slave pts')
+ cassert(master > 0, 'Problem opening master for pty')
+ cassert(C.grantpt(master) == 0, 'Problem granting slave pts')
+ cassert(C.unlockpt(master) == 0, 'Problem unlocking slave pts')
local slave_path = ffi.string(C.ptsname(master))
local slave = C.open(slave_path, C.O_RDWR)
- assert(slave > 0, 'Problem opening slave for pty')
+ cassert(slave > 0, 'Problem opening slave for pty')
return master, slave
end
@@ -70,7 +83,7 @@ function Process:list()
self:send('list', processes)
end
--- Takes in path and options, outputs pid
+-- Takes in path and options, outputs pid,
-- emits exit, stdout, and stderr events
function Process:spawn(path, options)
@@ -149,13 +162,12 @@ function Process:kill(signal)
self.handle:kill(signal or 15)
end
-function Process:resize(cols, rows)
local winp = ffi.new('struct winsize')
+function Process:resize(cols, rows)
winp.ws_row = rows
winp.ws_col = cols
- if C.ioctl(self.master, C.TIOCSWINSZ, winp) ~= 0 then
- print 'Error setting window size'
- end
+ cwarn(C.ioctl(self.master, C.TIOCSWINSZ, winp) == 0,
+ 'Problem setting window size')
self.handle:kill(C.SIGWINCH)
end
