From db5910b6d5ca10a50e7c287b0d0e23d40f090634 Mon Sep 17 00:00:00 2001 From: cinder <> Date: Fri, 24 May 2024 14:05:56 -0700 Subject: [PATCH] allow setting child process stdio in kitn.createProcess --- src/bin/lua.lua | 5 ----- src/bin/multitty.lua | 2 +- src/kernel/00_preamble.lua | 6 +++++- src/kernel/04_environment.lua | 29 ++++++++++++++++++++++++++--- src/lib/term.lua | 11 +++++++++-- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/bin/lua.lua b/src/bin/lua.lua index 0276c68..2aaafd6 100644 --- a/src/bin/lua.lua +++ b/src/bin/lua.lua @@ -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: diff --git a/src/bin/multitty.lua b/src/bin/multitty.lua index 19bdf07..a2c64dd 100644 --- a/src/bin/multitty.lua +++ b/src/bin/multitty.lua @@ -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 diff --git a/src/kernel/00_preamble.lua b/src/kernel/00_preamble.lua index 21bcfde..48c1518 100644 --- a/src/kernel/00_preamble.lua +++ b/src/kernel/00_preamble.lua @@ -60,7 +60,11 @@ do if ok then return ok else - error(format('bad argument #%d (%s expected, got %s)', i, concat({...}, ' or '), t), 3) + 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 diff --git a/src/kernel/04_environment.lua b/src/kernel/04_environment.lua index 279d85f..0e0d423 100644 --- a/src/kernel/04_environment.lua +++ b/src/kernel/04_environment.lua @@ -9,7 +9,7 @@ given : ]] -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 diff --git a/src/lib/term.lua b/src/lib/term.lua index d435394..d5e114a 100644 --- a/src/lib/term.lua +++ b/src/lib/term.lua @@ -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.