pipe is wip but && operator kinda works

This commit is contained in:
minjaesong
2022-05-13 11:18:14 +09:00
parent 761f9b6264
commit 359ef6d235

View File

@@ -235,8 +235,6 @@ shell.parse = function(input) {
tokens.push(stringBuffer); tokens.push(stringBuffer);
} }
println(tokens)
return tokens; return tokens;
} }
shell.resolvePathInput = function(input) { shell.resolvePathInput = function(input) {
@@ -385,16 +383,51 @@ shell.coreutils.chdir = shell.coreutils.cd;
Object.freeze(shell.coreutils); Object.freeze(shell.coreutils);
shell.execute = function(line) { shell.execute = function(line) {
if (0 == line.size) return; if (0 == line.size) return;
var tokens = shell.parse(line); let parsedTokens = shell.parse(line); // echo, "hai", |, less
var cmd = tokens[0]; let statements = [] // [[echo, "hai"], [less]]
if (cmd === undefined || cmd === '') return 0; let operators = [] // [|]
let opRegex = /[|>&<]/
let stmtBuf = []
parsedTokens.forEach((tok, i) => {
if (tok.match(opRegex)) {
operators.push(tok)
statements.push(stmtBuf.slice())
stmtBuf = []
}
else {
stmtBuf.push(tok)
}
})
if (stmtBuf[0] !== undefined) {
statements.push(stmtBuf.slice())
}
let retValue = undefined // return value of the previous statement
for (let c = 0; c < statements.length; c++) {
let op = operators[c]
// TODO : if operator is not undefined, swap built-in print functions with ones that 'prints' on pipes instead of stdout
let tokens = statements[c]
let cmd = tokens[0];
if (cmd === undefined || cmd === '') {
retValue = 0;
continue
}
// handle Ctrl-C // handle Ctrl-C
if (con.hitterminate()) return 1; if (con.hitterminate()) {
retValue = 1;
continue
}
if (shell.coreutils[cmd.toLowerCase()] !== undefined) { if (shell.coreutils[cmd.toLowerCase()] !== undefined) {
var retval = shell.coreutils[cmd.toLowerCase()](tokens); var retval = shell.coreutils[cmd.toLowerCase()](tokens);
return retval|0; // return value of undefined will cast into 0 retValue = retval|0; // return value of undefined will cast into 0
continue
} }
else { else {
// search through PATH for execution // search through PATH for execution
@@ -428,7 +461,8 @@ shell.execute = function(line) {
if (!fileExists) { if (!fileExists) {
printerrln('Bad command or filename: "'+cmd+'"'); printerrln('Bad command or filename: "'+cmd+'"');
return 127; retValue = 127;
continue
} }
else { else {
var programCode = filesystem.readAll(CURRENT_DRIVE); var programCode = filesystem.readAll(CURRENT_DRIVE);
@@ -439,7 +473,7 @@ shell.execute = function(line) {
if ("BAT" == extension) { if ("BAT" == extension) {
// parse and run as batch file // parse and run as batch file
var lines = programCode.split('\n').filter(function(it) { return it.length > 0; }); var lines = programCode.split('\n').filter(function(it) { return it.length > 0 }); // this return is not shell's return!
lines.forEach(function(line) { lines.forEach(function(line) {
shell.execute(line); shell.execute(line);
}); });
@@ -482,11 +516,15 @@ shell.execute = function(line) {
sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]); sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]);
//serial.println(_G.shellProgramTitles); //serial.println(_G.shellProgramTitles);
return errorlevel; retValue = errorlevel;
continue
} }
} }
} }
} }
return retValue
}
}; };
shell.pipes = {}; // syntax: _G.shell.pipes[name] = contents; all pipes are named pipes just like in Windows shell.pipes = {}; // syntax: _G.shell.pipes[name] = contents; all pipes are named pipes just like in Windows
shell.currentlyActivePipes = []; // pipe queue. Use shell.getPipe() to dequeue and shell.pushPipe() to enqueue. shell.currentlyActivePipes = []; // pipe queue. Use shell.getPipe() to dequeue and shell.pushPipe() to enqueue.