From f740ef7733b72fd7e13ff1992953e2137322160b Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Fri, 10 Nov 2023 16:47:15 +1000 Subject: [PATCH] get standard input working --- clite/core.js | 91 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 18 deletions(-) diff --git a/clite/core.js b/clite/core.js index 66a2ab3..7f52bc1 100644 --- a/clite/core.js +++ b/clite/core.js @@ -367,8 +367,8 @@ clite.io = { fd.canread = perms.checkReadable(fd.node.perms,fd.node.uid,fd.node.gid); fd.canwrite = perms.checkWritable(fd.node.perms,fd.node.uid,fd.node.gid); fd.canexec = perms.checkExecutable(fd.node.perms,fd.node.uid,fd.node.gid); - if (!p && cb != null) { - clite.core.execSafeAsync(function() {cb(fd);}); + if (!p && fd.remote.callback != null) { + clite.core.execSafeAsync(function() {fd.remote.callback(fd);}); } return fd; } @@ -719,7 +719,7 @@ clite.term = { }, keyup:function(e) { return clite.term.tty.data.data.onkeypress(e); - }, + } }, clear:function(form) { document.getElementById('terminal').innerHTML = ''; @@ -770,6 +770,10 @@ clite.term = { i.onkeydown = clite.term.tty.keydown; i.onkeypress = clite.term.tty.keypress; i.onkeyup = clite.term.tty.keyup; + }else if (clite.shell.reading.is) { + i.onkeydown = clite.shell.reading.keydown; + i.onkeypress = clite.shell.reading.keypress; + i.onkeyup = clite.shell.reading.keyup; }else{ i.onkeydown = clite.events.keydown; i.onkeypress = clite.events.keypress; @@ -885,6 +889,36 @@ clite.term = { }; clite.shell = { + reading:{ + is:false, + buff:'', + callback:null, + keydown:function(e) { + if (e.key == 'Tab') + return false; + }, + keypress:function(e) { + if (e.key == 'Tab') { + clite.events.refocus(); + return false; + } + // urgh, no. handle press in keyup because fuck js + //return clite.term.tty.data.data.onkeypress(e); + }, + keyup:function(e) { + if (e.key.length > 1 && e.key != 'Spacebar') { + clite.shell.reading.is = false; + clite.term.preventInput(); + if (e.key == 'Enter') { + clite.shell.reading.callback(clite.shell.reading.buff); + }else{ + clite.shell.reading.callback(e); + } + return false; + } + clite.shell.reading.buff = e.target.value; + } + }, exit:function(v) { if (clite.term.tty.data != null) clite.term.api.closeTTY(clite.term.tty.data); @@ -892,20 +926,38 @@ clite.shell = { clite.events.refocus(); }, readLine:function(cb) { - // TODO: read data + clite.shell.reading.is = true; + clite.shell.reading.buff = ''; + clite.shell.reading.callback = cb; + clite.events.refocus(); }, writeLine:function(txt) { clite.term.writeLine(txt); clite.events.refocus(); }, resolvePath:function(txt) { - // TODO: resolve using PATH (check each colon separated path) - return clite.lib.resolvePath(txt,clite.shell.env.PATH) + var paths = clite.shell.env.PATH.split(':'); + var path = null; + paths.forEach(function(p) { + if (path) + return; + var rp = clite.lib.resolvePath(txt,p); + var fd = clite.io.open(rp,false); + if (fd) { + clite.io.close(fd); + path = rp; + } + }); + return path; }, exec:function(args,io) { var path = clite.shell.resolvePath(args[0]); + if (!path) { + clite.term.writeLine('Shell: unknown command: '+args[0]); + io.exit(-1); + return; + } var fd = clite.io.open(path); - // TODO: should errors here write to stderr? (io.error) if (!fd) { clite.term.writeLine('Shell: unknown command: '+args[0]); io.exit(-1); @@ -1025,15 +1077,19 @@ clite.shell = { }); if (add == '') { // test for commands - // TODO: test all of PATH - var fd = clite.io.open('/bin'); - var f; - while (add == '' && (f = clite.io.read(fd)) != null) { - if (f.name.substring(0,a.length) == a) { - add = f.name.substring(a.length); + var pparts = clite.shell.env.PATH.split(':'); + pparts.forEach(function(pp) { + if (add != '') + return; + var fd = clite.io.open('/bin'); + var f; + while (add == '' && (f = clite.io.read(fd)) != null) { + if (f.name.substring(0,a.length) == a) { + add = f.name.substring(a.length)+' '; + } } - } - clite.io.close(fd); + clite.io.close(fd); + }); } }else{ // TODO: path fill @@ -1238,9 +1294,8 @@ clite.lib = { path = '/'+parts.join('/'); return path; }, - // convert a string into an argument array: 'ls /etc' -> ['ls',/etc] + // convert a string into an argument array: 'ls /etc' -> ['ls','/etc'] strToArgs:function(txt) { - // TODO: split this properly (escape strings and insert env variables) var parts = []; var s = ''; var e = false; @@ -1280,7 +1335,7 @@ clite.lib = { } if (s.length > 0) parts.push(s); - return parts;//txt.split(' '); + return parts; }, getFileType:function(fd) { // returns an int identifier for the file type: