From 148b1dc133cbceb2af566e4a39d3486f41944028 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Tue, 7 Nov 2023 11:27:07 +1000 Subject: [PATCH] exec shell commands pt1 --- clite/commands.js | 10 +++++ clite/core.js | 109 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 clite/commands.js diff --git a/clite/commands.js b/clite/commands.js new file mode 100644 index 0000000..462f529 --- /dev/null +++ b/clite/commands.js @@ -0,0 +1,10 @@ +clite.commands.data = function() { +// insert commands below this line + +clite.commands.load('ls',function(args) { + clite.log.write('hello world (l)'); + clite.shell.writeLine('hello world (s)'); +}); + +// insert commands above this line +} diff --git a/clite/core.js b/clite/core.js index be94180..6aa960e 100644 --- a/clite/core.js +++ b/clite/core.js @@ -52,12 +52,8 @@ var clite = { script:function(name,callback) { var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); - var file = ''; - if (bios.lib.code.base != null) - file += bios.lib.code.base+'/'; - file +=name+'.js'; script.type = 'text/javascript'; - script.src = file; + script.src = name; script.onload = callback; head.appendChild(script); }, @@ -103,7 +99,8 @@ var clite = { return; } n.data.content = data; - n.perms = '-r--r-----'; + n.perms = 'cr--r-----'; + n.isdev = true; var fd = clite.io.open('/dev/wfs'); var l; while ((l = clite.io.readLine(fd)) != null) { @@ -136,30 +133,96 @@ var clite = { fn.name = clite.lib.basename(path); fn.data.remote = url; fn.data.content = null; - clite.log.write('added "'+path+'" ("'+fn.name+'")'); } clite.io.close(fd); - // populate /dev (data/filesys.txt is /dev/wfs + // populate /dev (data/filesys.txt is /dev/wfs already) clite.log.write('Populating /dev'); + // TODO: are there any other devices we need? + // populate /bin + clite.log.write('Populating /bin'); + // create a temporary function to load in commands + clite.commands = { + data:null, + load:function(name,fn) { + vfsapi.mkFile('/bin/'+name); + var f = vfsapi.getNode('/bin/'+name); + if (!f) + return; + f.perms = '-r-xr-xr-x'; + f.data.content = fn; + clite.log.write('loaded ('+name+')'); + } + } + clite.core.load.script('clite/commands.js',function() { + clite.core.execSafe(clite.commands.data); + clite.commands = null; // check cookies for login clite.log.write('Checking for user session'); - // if new user: - // read in /usr/share/introduction and write to shell - // if logged in: - // create user session + if (clite.user.init()) {// if logged in: + // create user session + }else{// if new user: + // read in /usr/share/introduction and write to shell + } clite.log.write('Creating Shell'); clite.events.refocus(); - }); + }); // end command load callback + }); // end filesys load callback }); } }; +clite.user = { + init:function() { // returns true if there's a user login or false for guest + return false; + }, + getUID:function() { // get the user's user id + return 0; + }, + getGID:function() { // get the user's group id + return 0; + }, + checkGID:function(gid) { // check if the user is in a group + return true; + } +}; + clite.io = { init:function() { var vfsapi = clite.vfs.getApi(); + var perms = { + // -rwxrwxrwx user1-3,group4-6,other7-9 + checkReadable:function(p,uid,gid) { + if (p[7] == 'r') + return true; + if (clite.user.checkGID(gid) && p[4] == 'r') + return true; + if (uid == clite.user.getUID() && p[1] == 'r') + return true; + return false; + }, + checkWritable:function(p,uid,gid) { + if (p[8] == 'w') + return true; + if (clite.user.checkGID(gid) && p[5] == 'w') + return true; + if (uid == clite.user.getUID() && p[2] == 'w') + return true; + return false; + }, + checkExecutable:function(p,uid,gid) { + if (p[9] == 'x') + return true; + if (clite.user.checkGID(gid) && p[6] == 'x') + return true; + if (uid == clite.user.getUID() && p[3] == 'x') + return true; + return false; + } + }; + function getFileDes(path,link) { var n = vfsapi.getNode(path); if (!n) @@ -170,7 +233,9 @@ clite.io = { var fd = Object.create({ node:n, pos:0, + canread:false, canwrite:false, + canexec:false, remote:{ ispending:p, callback:null @@ -183,7 +248,9 @@ clite.io = { fd.remote.callback(fd); } catch(err) {} }); - // TODO check permissions to see if this is writable + fd.canread = perms.checkReadable(fd.node.perms,fd.node.uid,fd.node.gid); + fd.canwrite = perms.checkWritable(fd.node.perms,fd.node.uid,fd.node.gid); + fd.canexec = perms.checkExecutable(fd.node.perms,fd.node.uid,fd.node.gid); return fd; } clite.io.creat = function(path,type) { @@ -339,6 +406,7 @@ clite.vfs = { remote:null, isdir:false, islink:false, + isdev:false, parent:null, content:null } @@ -587,7 +655,18 @@ clite.shell = { clite.events.refocus(); }, exec:function(txt) { - // TODO: actually execute the command + // TODO: split this better + var args = txt.split(' '); + // TODO: resolve using PATH + var path = '/bin/'+args[0]; + var fd = clite.io.open(path); + if (!fd) { + clite.shell.writeLine('Shell: unknown command: '+args[0]); + return; + } + if (!clite.core.execSafe(function() {fd.node.data.content(args)})) + clite.shell.writeLine('Shell: error in command:'+args[0]); + clite.io.close(fd); }, parse:function(txt) { if (txt.length < 1) {