shell preprocessing (env variable substitution)

This commit is contained in:
Lisa Milne 2023-11-08 20:47:04 +10:00
parent 40efd18938
commit 682d832586
2 changed files with 46 additions and 8 deletions

View file

@ -15,9 +15,15 @@ ls - list directory contents
cat - concatenate files and print to std output or local file
touch - create a new file
less - simple text file viewer
view - file viewer
edit - text file editor
Running any command with the argument -? will give you help for that program.
Or you can run help <command> to get the same info.
Or you can run help <command> to get the same info.
As a basic guide for guests:
Use ls to see what content is available, eg: ls web
Use view to view the content, eg: view web/about
`);
return 0;

View file

@ -24,7 +24,7 @@ var clite = {
clite.shell.history.setCurrent(t);
return true;
}
clite.shell.parse(t);
clite.shell.run(t);
return false;
} catch(err) {return false;}
},
@ -686,7 +686,7 @@ clite.term = {
},
writeLine:function(txt) {
var a = document.createElement('article');
a.innerHTML = txt;
a.innerHTML = clite.lib.htmlEncode(txt);
var t = document.getElementById('terminal');
if (!t)
return;
@ -788,19 +788,53 @@ clite.shell = {
}
clite.io.close(fd);
},
parse:function(txt) {
preprocess:function(txt) {
var parts = txt.split('$');
var pptxt = '';
if (parts.length == 1)
return txt;
parts.forEach(function(p,i) {
if (i > 0) {
var b = p;
var e = '';
var pi = b.indexOf(' ');
if (pi > 0) {
e = b.substring(pi);
b = b.substring(0,pi);
}
if (b[0] == '{') {
var pi = b.indexOf('}');
b = b.substring(1,pi);
}
if (typeof clite.shell.env[b] === 'string') {
b = clite.shell.env[b];
}else{
b = '';
}
pptxt = b+e;
return;
}
pptxt += p;
});
// TODO: look for command substitutions
return pptxt;
},
run:function(txt) {
if (txt.length < 1) {
clite.shell.writeLine(clite.shell.prompt.getHTML());
clite.shell.writeLine(clite.shell.prompt.get());
return;
}
clite.shell.history.add(txt);
clite.shell.history.resetCurrent();
clite.shell.writeLine(clite.shell.prompt.getHTML()+txt);
clite.shell.writeLine(clite.shell.prompt.get()+txt);
clite.shell.prompt.push('');
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,
@ -973,8 +1007,6 @@ clite.lib = {
htmlEncode:function(txt) {
return txt
.replace(/&/g, '&amp')
.replace(/'/g, '&apos')
.replace(/"/g, '&quot')
.replace(/>/g, '&gt')
.replace(/</g, '&lt')
.replace(/ /g, '&nbsp;');