mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-10 15:04:03 +09:00
migration to graal
This commit is contained in:
@@ -49,9 +49,9 @@ function greet() {
|
||||
}
|
||||
|
||||
function trimStartRevSlash(s) {
|
||||
let cnt = 0;
|
||||
var cnt = 0;
|
||||
while (cnt < s.length) {
|
||||
let chr = s[cnt];
|
||||
var chr = s[cnt];
|
||||
|
||||
if (chr != '\\') break;
|
||||
|
||||
@@ -66,10 +66,10 @@ shell.getPwd = function() { return shell_pwd; }
|
||||
shell.getCurrentDrive = function() { return CURRENT_DRIVE; }
|
||||
// example input: echo "the string" > subdir\test.txt
|
||||
shell.parse = function(input) {
|
||||
let tokens = [];
|
||||
let stringBuffer = "";
|
||||
let mode = "LITERAL"; // LITERAL, QUOTE, ESCAPE, LIMBO
|
||||
let i = 0
|
||||
var tokens = [];
|
||||
var stringBuffer = "";
|
||||
var mode = "LITERAL"; // LITERAL, QUOTE, ESCAPE, LIMBO
|
||||
var i = 0
|
||||
while (i < input.length) {
|
||||
const c = input[i];
|
||||
/*digraph g {
|
||||
@@ -136,12 +136,12 @@ shell.parse = function(input) {
|
||||
}
|
||||
shell.resolvePathInput = function(input) {
|
||||
// replace slashes into revslashes
|
||||
let pathstr = input.replaceAll('/','\\\\');
|
||||
let startsWithSlash = input.startsWith('\\');
|
||||
let newPwd = [];
|
||||
var pathstr = input.replaceAll('/','\\\\');
|
||||
var startsWithSlash = input.startsWith('\\');
|
||||
var newPwd = [];
|
||||
|
||||
// split them into an array while filtering empty elements except for the root 'head'
|
||||
let ipwd = (startsWithSlash ? [""] : shell_pwd).concat(pathstr.split("\\").filter(function(it) { return (it.length > 0); }));
|
||||
var ipwd = (startsWithSlash ? [""] : shell_pwd).concat(pathstr.split("\\").filter(function(it) { return (it.length > 0); }));
|
||||
|
||||
serial.println("command.js > resolvePathInput > ipwd = "+ipwd);
|
||||
serial.println("command.js > resolvePathInput > newPwd = "+newPwd);
|
||||
@@ -177,12 +177,12 @@ shell.coreutils = {
|
||||
println(CURRENT_DRIVE+":"+shell_pwd.join("\\"));
|
||||
return
|
||||
}
|
||||
let path = shell.resolvePathInput(args[1])
|
||||
var path = shell.resolvePathInput(args[1])
|
||||
if (DEBUG_PRINT) serial.println("command.js > cd > pathstr = "+path.string);
|
||||
|
||||
// check if path is valid
|
||||
filesystem.open(CURRENT_DRIVE, path.string, 'R');
|
||||
let dirOpened = filesystem.isDirectory(CURRENT_DRIVE); // open a dir; if path is nonexistent, file won't actually be opened
|
||||
var dirOpened = filesystem.isDirectory(CURRENT_DRIVE); // open a dir; if path is nonexistent, file won't actually be opened
|
||||
if (!dirOpened) { printerrln("CHDIR failed for '"+path.string+"'"); return; } // if file is not opened, FALSE will be returned
|
||||
|
||||
shell_pwd = path.pwd;
|
||||
@@ -192,12 +192,12 @@ shell.coreutils = {
|
||||
printerrln("Syntax error");
|
||||
return
|
||||
}
|
||||
let path = shell.resolvePathInput(args[1])
|
||||
var path = shell.resolvePathInput(args[1])
|
||||
if (DEBUG_PRINT) serial.println("command.js > mkdir > pathstr = "+path.string);
|
||||
|
||||
// check if path is valid
|
||||
let dirOpened = filesystem.open(CURRENT_DRIVE, path.string, 'W');
|
||||
let mkdird = filesystem.mkDir(CURRENT_DRIVE);
|
||||
var dirOpened = filesystem.open(CURRENT_DRIVE, path.string, 'W');
|
||||
var mkdird = filesystem.mkDir(CURRENT_DRIVE);
|
||||
if (!mkdird) { printerrln("MKDIR failed for '"+path.string+"'"); return; }
|
||||
},
|
||||
cls: function(args) {
|
||||
@@ -225,13 +225,13 @@ shell.coreutils = {
|
||||
}
|
||||
else {
|
||||
// parse key-value pair with splitter '='
|
||||
let key = undefined; let value = undefined;
|
||||
var key = undefined; var value = undefined;
|
||||
// if syntax "<key> = <value>" is used?
|
||||
if ('=' == args[2]) {
|
||||
key = args[1].toUpperCase(); value = args[3];
|
||||
}
|
||||
else if (args[2] === undefined) {
|
||||
let pair = args[1].split('=');
|
||||
var pair = args[1].split('=');
|
||||
key = pair[0].toUpperCase(); value = pair[1];
|
||||
}
|
||||
|
||||
@@ -251,13 +251,13 @@ shell.coreutils = {
|
||||
}
|
||||
},
|
||||
dir: function(args) {
|
||||
let pathstr = (args[1] !== undefined) ? args[1] : "\\"+shell_pwd.join("\\");
|
||||
var pathstr = (args[1] !== undefined) ? args[1] : "\\"+shell_pwd.join("\\");
|
||||
|
||||
// check if path is valid
|
||||
let pathOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R');
|
||||
var pathOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R');
|
||||
if (!pathOpened) { printerrln("File not found"); return; }
|
||||
|
||||
let port = filesystem._toPorts(CURRENT_DRIVE)[0]
|
||||
var port = filesystem._toPorts(CURRENT_DRIVE)[0]
|
||||
com.sendMessage(port, "LIST");
|
||||
println(com.pullMessage(port));
|
||||
}
|
||||
@@ -266,24 +266,24 @@ shell.coreutils.chdir = shell.coreutils.cd;
|
||||
Object.freeze(shell.coreutils);
|
||||
shell.execute = function(line) {
|
||||
if (0 == line.size) return;
|
||||
let tokens = shell.parse(line);
|
||||
let cmd = tokens[0];
|
||||
var tokens = shell.parse(line);
|
||||
var cmd = tokens[0];
|
||||
if (cmd === undefined || cmd === '') return 0;
|
||||
|
||||
// handle Ctrl-C
|
||||
if (con.hitterminate()) return 1;
|
||||
|
||||
if (shell.coreutils[cmd.toLowerCase()] !== undefined) {
|
||||
let retval = shell.coreutils[cmd.toLowerCase()](tokens);
|
||||
var retval = shell.coreutils[cmd.toLowerCase()](tokens);
|
||||
return retval|0; // return value of undefined will cast into 0
|
||||
}
|
||||
else {
|
||||
// search through PATH for execution
|
||||
|
||||
let fileExists = false;
|
||||
let searchDir = (cmd.startsWith("\\")) ? [""] : ["\\"+shell_pwd.join("\\")].concat(_TVDOS.getPath());
|
||||
var fileExists = false;
|
||||
var searchDir = (cmd.startsWith("\\")) ? [""] : ["\\"+shell_pwd.join("\\")].concat(_TVDOS.getPath());
|
||||
|
||||
let pathExt = [];
|
||||
var pathExt = []; // it seems Nashorn does not like 'let' too much? this line gets ignored sometimes
|
||||
// fill pathExt using %PATHEXT% but also capitalise them
|
||||
if (cmd.split(".")[1] === undefined)
|
||||
_TVDOS.variables.PATHEXT.split(';').forEach(function(it) { pathExt.push(it); pathExt.push(it.toUpperCase()); });
|
||||
@@ -291,10 +291,10 @@ shell.execute = function(line) {
|
||||
pathExt.push(""); // final empty extension
|
||||
|
||||
searchLoop:
|
||||
for (let i = 0; i < searchDir.length; i++) {
|
||||
for (let j = 0; j < pathExt.length; j++) {
|
||||
let search = searchDir[i]; if (!search.endsWith('\\')) search += '\\';
|
||||
let path = trimStartRevSlash(search + cmd + pathExt[j]);
|
||||
for (var i = 0; i < searchDir.length; i++) {
|
||||
for (var j = 0; j < pathExt.length; j++) {
|
||||
var search = searchDir[i]; if (!search.endsWith('\\')) search += '\\';
|
||||
var path = trimStartRevSlash(search + cmd + pathExt[j]);
|
||||
|
||||
if (DEBUG_PRINT) {
|
||||
serial.println("[command.js > shell.execute] file search path: "+path);
|
||||
@@ -312,21 +312,21 @@ shell.execute = function(line) {
|
||||
return 127;
|
||||
}
|
||||
else {
|
||||
let prg = filesystem.readAll(CURRENT_DRIVE);
|
||||
let extension = undefined;
|
||||
var programCode = filesystem.readAll(CURRENT_DRIVE);
|
||||
var extension = undefined;
|
||||
// get proper extension
|
||||
let dotSepTokens = cmd.split('.');
|
||||
var dotSepTokens = cmd.split('.');
|
||||
if (dotSepTokens.length > 1) extension = dotSepTokens[dotSepTokens.length - 1].toUpperCase();
|
||||
|
||||
if ("BAT" == extension) {
|
||||
// parse and run as batch file
|
||||
let lines = prg.split('\n').filter(function(it) { return it.length > 0; });
|
||||
var lines = programCode.split('\n').filter(function(it) { return it.length > 0; });
|
||||
lines.forEach(function(line) {
|
||||
shell.execute(line);
|
||||
});
|
||||
}
|
||||
else {
|
||||
return execApp(prg, tokens)|0; // return value of undefined will cast into 0
|
||||
return execApp(programCode, tokens)|0; // return value of undefined will cast into 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -339,7 +339,7 @@ _G.shell = shell;
|
||||
|
||||
if (exec_args[1] !== undefined) {
|
||||
// only meaningful switches would be either /c or /k anyway
|
||||
let firstSwitch = exec_args[1].toLowerCase();
|
||||
var firstSwitch = exec_args[1].toLowerCase();
|
||||
|
||||
// command /c <commands>
|
||||
// ^[0] ^[1] ^[2]
|
||||
@@ -376,14 +376,14 @@ if (goInteractive) {
|
||||
con.reset_graphics();
|
||||
print_prompt_text();
|
||||
|
||||
let cmdbuf = "";
|
||||
var cmdbuf = "";
|
||||
|
||||
while (true) {
|
||||
let key = con.getch();
|
||||
var key = con.getch();
|
||||
|
||||
// printable chars
|
||||
if (key >= 32 && key <= 126) {
|
||||
let s = String.fromCharCode(key);
|
||||
var s = String.fromCharCode(key);
|
||||
cmdbuf += s;
|
||||
print(s);
|
||||
}
|
||||
@@ -421,7 +421,7 @@ if (goInteractive) {
|
||||
cmdHistoryScroll += 1;
|
||||
|
||||
// back the cursor in order to type new cmd
|
||||
let x = 0;
|
||||
var x = 0;
|
||||
for (x = 0; x < cmdbuf.length; x++) print(String.fromCharCode(8));
|
||||
cmdbuf = cmdHistory[cmdHistory.length - cmdHistoryScroll];
|
||||
// re-type the new command
|
||||
@@ -432,7 +432,7 @@ if (goInteractive) {
|
||||
else if (key === 20) {
|
||||
if (cmdHistoryScroll > 0) {
|
||||
// back the cursor in order to type new cmd
|
||||
let x = 0;
|
||||
var x = 0;
|
||||
for (x = 0; x < cmdbuf.length; x++) print(String.fromCharCode(8));
|
||||
cmdbuf = cmdHistory[cmdHistory.length - cmdHistoryScroll];
|
||||
// re-type the new command
|
||||
@@ -442,7 +442,7 @@ if (goInteractive) {
|
||||
}
|
||||
else {
|
||||
// back the cursor in order to type new cmd
|
||||
let x = 0;
|
||||
var x = 0;
|
||||
for (x = 0; x < cmdbuf.length; x++) print(String.fromCharCode(8));
|
||||
cmdbuf = "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user