get the shell exec working right

This commit is contained in:
Lisa Milne 2023-11-22 17:34:47 +10:00
parent 4baa505b4d
commit 03bc7b2e66

View file

@ -419,7 +419,11 @@ clite.user = {
name:'root',
uid:0,
gid:0,
groups:[]
groups:[],
env:{
HOME:'/',
USER:udata.name,
}
};
clite.user.getUID = function() {
return udata.uid;
@ -439,6 +443,9 @@ clite.user = {
udata.uid = uid;
udata.gid = gid;
udata.groups = groups;
udata.env.HOME = '/usr/home/'+user;
udata.env.PWD = udata.env.HOME;
udata.env.USER = udata.name;
clite.shell.env.HOME = '/usr/home/'+user;
clite.shell.env.PWD = '/usr/home/'+user;
clite.shell.env.USER = user;
@ -1321,35 +1328,6 @@ clite.shell = {
});
return path;
},
exec:function(args,io) {
var path = clite.shell.resolvePath(args[0]);
if (!path) {
clite.term.writeLine('Shell: unknown command: '+args[0]);
clite.shell.exit(-1,-1);
return;
}
function execFunc(env,io) {
var r = clite.lib.exec(path,args,env,io);
if (r == 0)
return;
if (r == -1) {
clite.term.writeLine('Shell: unknown command: '+args[0]);
clite.shell.exit(-1,-2);
return;
}
if (r == -2) {
clite.term.writeLine('Shell: error in command:'+args[0]);
clite.shell.exit(-1,-3);
}
}
if (!clite.lib.fork(clite.shell.env,io,execFunc)) {
clite.term.writeLine('Shell: internal error');
clite.shell.exit(-1,-1);
}
},
include:function(name) {
switch (name) {
case 'stdlib':
@ -1429,16 +1407,16 @@ clite.shell = {
clite.term.preventInput();
// split command line into arguments
var args = clite.lib.strToArgs(txt);
// TODO: check for io redirects and piping, then setup stdio accordingly
args.forEach(function(val,i) {
args[i] = clite.shell.preprocess(val);
});
// prepare stdio (stdin,stdout,stderr)
var stdio = {
read:clite.shell.readLine,
write:clite.term.writeLine,
error:clite.term.writeLine,
exit:clite.shell.exit, // equivalent to C's exit(), needed by asynchronous programs
// prepare io (stdin,stdout,stderr)
// TODO: check for io redirects and piping, then setup stdio accordingly
var io = {
read:clite.shell.readLine, // stdin
write:clite.term.writeLine, // stdout
error:clite.term.writeLine, // stderr
exit:clite.shell.exit, // equivalent to C's exit(), needed by asynchronous programs
include:clite.shell.include, // include libraries, allows them to be used in a program
istty:{
stdin:true, // false if stdin (read) is not clite.shell.readLine
@ -1448,10 +1426,37 @@ clite.shell = {
// check for a macro
// then either run the macro or expand the program to be executed via PATH
if (typeof clite.shell.macro[args[0]] != 'undefined') {
var r = clite.shell.macro[args[0]](args,stdio);
var r = clite.shell.macro[args[0]](args,io);
clite.shell.exit(r);
}else{
clite.shell.exec(args,stdio);
return;
}
var path = clite.shell.resolvePath(args[0]);
if (!path) {
clite.term.writeLine('Shell: unknown command: '+args[0]);
clite.shell.exit(-1,-1);
return;
}
function execFunc(env,io) {
var r = clite.lib.exec(path,args,env,io);
if (r == 0)
return;
if (r == -1) {
clite.term.writeLine('Shell: unknown command: '+args[0]);
clite.shell.exit(-1,-2);
return;
}
if (r == -2) {
clite.term.writeLine('Shell: error in command:'+args[0]);
clite.shell.exit(-1,-3);
}
}
if (!clite.lib.fork(clite.shell.env,io,execFunc)) {
clite.term.writeLine('Shell: internal error');
clite.shell.exit(-1,-1);
}
},
tabfill:function() {