it is now possible to run shell scripts

This commit is contained in:
Lisa Milne 2023-12-14 17:46:08 +10:00
parent 376d425330
commit 162d63f4e1
3 changed files with 41 additions and 15 deletions

View file

@ -2717,7 +2717,20 @@ clite.lib = {
return -3;
}
if (clite.lib.getFileType(fd) != clite.io.types.FT_BINARY) {
var ft = clite.lib.getFileType(fd);
if (ft == clite.io.types.FT_SCRIPT) {
var fl = clite.io.readLine(io.pid,fd);
clite.io.close(fd);
if (!fl)
return -5;
var e = fl.substring(2);
fd = clite.io.open(io.pid,e,clite.io.flags.O_RDONLY|clite.io.flags.O_EXEC|clite.io.flags.O_SYNC);
if (!fd)
return -6;
args = [args[0],path];
ft = clite.lib.getFileType(fd);
}
if (ft != clite.io.types.FT_BINARY) {
clite.io.close(io.pid,fd);
return -4;
}

View file

@ -8,9 +8,10 @@ return Object.create({
resolvePath:function(txt,base) {
if (txt == '/')
return txt;
if (txt[0] == '~') {
if (txt[0] == '~')
txt = env.HOME+txt.substring(1);
}
if (txt[0] == '.')
txt = env.PWD+'/'+txt;
var path = txt;
if (txt[0] != '/') {
if (typeof base != 'string')

View file

@ -367,16 +367,36 @@ Options:
if (r == -1) {
stdlib.fprintf(io.stderr,'Shell: unknown command: %s\n',args[0]);
io.exit(-1);
return;
}
if (r == -2) {
stdio.fprintf(io.stderr,'Shell: error in command: %s\n',args[0]);
io.exit(-1);
return;
}
if (r == -3 || r == -4) {
stdio.fprintf(io.stderr,'Shell: not an executable file: %s\n',args[0]);
io.exit(-3);
return;
}
if (r == -5) {
stdio.fprintf(io.stderr,'Shell: invalid parser for file: %s\n',args[0]);
io.exit(-3);
return;
}
if (r == -6) {
stdio.fprintf(io.stderr,'Shell: no valid parser for file: %s\n',args[0]);
io.exit(-3);
return;
}
if (r<0) {
stdio.fprintf(io.stderr,'Shell: could not exec file: %s\n',args[0]);
io.exit(-3);
}
}
var pid = stdlib.fork(env,fio,execFunc);
@ -506,24 +526,16 @@ Options:
// run a shell script if one is specified
if (f != null) {
var fd = stdio.open(f,stdio.flags.O_RDONLY);
if (!fd)
return 1;
parseScript(fd);
stdio.close(fd);
stdlib.waitall(function() {
parser.queue(f);
parser.run(function() {
io.exit(0);
});
// run a shell script from stdin
}else if (!stdio.isatty(io.stdin)) {
if (!s)
return 0;
parseScript(io.stdin);
stdlib.waitall(function() {
io.exit(0);
});
// TODO: parser stdin support
return 1;
// otherwise we have an interactive shell
// so run /etc/shrc and ~/.shrc
}else{