preparing for tty reading

This commit is contained in:
Lisa Milne 2023-11-25 11:46:06 +10:00
parent 3b9fe3fd6b
commit 279d1afbc2
2 changed files with 86 additions and 5 deletions

View file

@ -639,5 +639,21 @@ Options:
return 0;
});
if (window.location.protocol == 'file:')
clite.commands.load('test',function(args,env,io) {
var stdio = io.include('stdio');
var fd = stdio.open('/dev/tty');
stdio.write(fd,'Hello world');
function rcb(data) {
stdio.write(fd,data);
stdio.close(fd);
io.exit(0);
}
stdio.read(fd,rcb);
return null;
});
// insert commands above this line
}

View file

@ -183,6 +183,43 @@ var clite = {
};
n.perms = 'crw-rw-rw-';
n.data.isdev = true;
// /dev/tty - the main tty device, writing to it prints to the terminal, reading from it reads keyboard input
vfsapi.mkFile('/dev/tty');
n = vfsapi.getNode('/dev/tty');
if (!n) {
clite.log.write('tty failure');
return false;
}
n.data.content = {
receive:{
callback:null,
buffer:[],
input:function(str) {
if (n.data.content.receive.callback != null) {
clite.core.execSafeAsync(function() {
n.data.content.receive.callback(str);
});
n.data.content.receive.callback = null;
return;
}
n.data.content.receive.buffer.push(str);
}
},
read:function(cb) {
if (n.data.content.receive.buffer.length > 0) {
var str = n.data.content.receive.buffer.shift();
cb(str);
return true;
}
n.data.content.receive.callback = cb;
return true;
},
write:clite.term.writeLine
};
clite.term.data.handler = n.data.content.receive.input;
n.perms = 'crw-rw-rw-';
n.data.isdev = true;
n.data.istty = true;
return true;
}
@ -664,10 +701,18 @@ clite.io = {
delete fd;
} catch(err) {}
}
clite.io.read = function(fd) {
clite.io.read = function(fd,cb) {
if (fd.node.data.content == null)
return null;
if (fd.node.data.isdev) {
if (fd.node.data.istty) {
try{
if (typeof cb === 'function') {
fd.node.data.content.read(cb);
return true;
}
} catch(err) {}
return false;
}else if (fd.node.data.isdev) {
if (typeof fd.node.data.content !== 'string') {
try{
return fd.node.data.content.read();
@ -684,10 +729,18 @@ clite.io = {
}
return fd.node.data.content[fd.pos++];
}
clite.io.readLine = function(fd) {
clite.io.readLine = function(fd,cb) {
if (fd.node.data.content == null)
return null;
if (fd.node.data.isdev) {
if (fd.node.data.istty) {
try{
if (typeof cb === 'function') {
fd.node.data.content.read(cb);
return true;
}
} catch(err) {}
return false;
}else if (fd.node.data.isdev) {
if (typeof fd.node.data.content !== 'string') {
try{
return fd.node.data.content.read();
@ -712,6 +765,8 @@ clite.io = {
return l;
}
clite.io.readAll = function(fd) {
if (fd.node.data.istty)
return false;
if (fd.node.data.isdev) {
if (typeof fd.node.data.content !== 'string') {
try{
@ -921,6 +976,7 @@ clite.vfs = {
isdir:false,
islink:false,
isdev:false,
istty:false,
parent:null,
content:null
}
@ -1117,7 +1173,8 @@ clite.term = {
data:{
type:'text',
form:null,
field:null
field:null,
handler:null
},
events:{
keydown:function(e) {
@ -1132,6 +1189,13 @@ clite.term = {
return true;
},
keyup:function(e) {
//if (e.key.length > 1 && e.key != 'Spacebar' && clite.term.data.handler != null) {
//if (e.key == 'Enter') {
//clite.term.data.handler(e.target.value);
//}else{
//clite.term.data.handler(e.key);
//}
//}
try {
if (e.key == 'Down' || e.key == 'ArrowDown') {
clite.shell.history.down();
@ -1184,6 +1248,7 @@ clite.term = {
clite.term.genForm();
},
preventInput:function() {
return;
var f = document.getElementById('form');
if (!f)
return;