allow setting child process stdio in kitn.createProcess

main
cinder 5 months ago
parent 8f6594152f
commit db5910b6d5

@ -1,6 +1,3 @@
local ok, err = xpcall(function(...)
local tty = ...
io.stdin, io.stdout = tty, tty
local term = dofile('/lib/term.lua').wrap(io) --TODO require()
term:write(_VERSION, ' // ', _OSVERSION, '\n')
@ -49,7 +46,5 @@ end
if results.n > 1 then term:write('\n') end
goto repl
end, function(e) return debug.traceback(tostring(e)) end, ...)
if not ok then error(err, 0) end
-- vi: set ts=2:

@ -144,7 +144,7 @@ local function createTerm(screen, prog, ...)
component.invoke(gpu, 'setResolution', w, h)
component.invoke(gpu, 'fill', 1, 1, w, h, ' ')
local process = assert(kitn.createProcess(prog, stdio, ...)) --TODO set io via createProcess args
local process = assert(kitn.createProcess({ path = prog, stdin = stdio, stdout = stdio, stderr = stdio }, ...))
return process, stdio
end

@ -60,7 +60,11 @@ do
if ok then
return ok
else
if type(i) == 'number' then
error(format('bad argument #%d (%s expected, got %s)', i, concat({...}, ' or '), t), 3)
else
error(format('bad argument %s (%s expected, got %s)', i, concat({...}, ' or '), t), 3)
end
end
end
end

@ -9,7 +9,7 @@ given <https://ocdoc.cil.li/tutorial:custom_oses#what_s_available>:
]]
envBase._OSVERSION = 'kitn 0.2.1-beta.5' --prefer [openloader, openos] "$name $ver" over plan9k "$name/$ver"
envBase._OSVERSION = 'kitn 0.2.1-beta.6' --prefer [openloader, openos] "$name $ver" over plan9k "$name/$ver"
envBase.kitn = {}
envBase._G, envBase.load = nil
@ -159,10 +159,33 @@ function fillEnv(proc, uenv)
return co
end
function ukitn.createProcess(path, ...)
checkArg(1, path, 'string')
local function assertFile(label, file)
if isFile(file) then
return file
else
error(label .. ' is not a file', 3)
end
end
function ukitn.createProcess(init, ...)
local initT = checkArgEx(1, init, 'string', 'table')
local path, stdin, stdout, stderr
if initT == 'string' then
path = init
else
path, stdin, stdout, stderr = init.path, init.stdin, init.stdout, init.stderr
end
stdin = stdin and assertFile('stdin', stdin) or assertFile('inherited stdin', uio.stdin)
stdout = stdout and assertFile('stdout', stdout) or assertFile('inherited stdout', uio.stdout)
stderr = stderr and assertFile('stderr', stderr) or assertFile('inherited stderr', uio.stderr)
local co, info = createProcess(path, proc)
if not co then return nil, info end
local newio = info.env.io
newio.stdin, newio.stdout, newio.stderr = stdin, stdout, stderr
runqueue[co] = pack(...)
return co
end

@ -61,8 +61,15 @@ end
--write strings to stderr with optional styling.
--see `mt:write` for how styling is interpreted.
function mt:error(...)
--TODO use wrapped stderr
return self:write({ fg = 0xff0000, ... })
local args = table.pack(...)
args.fg = 0xff0000
local caps = self.stderr.tty
if caps then
writeStyled(caps, self.stderr, args)
else
writeUnstyled(self.stderr, args)
end
return self
end
--prompt the user for a line of input.

Loading…
Cancel
Save