diff --git a/clite/commands.js b/clite/commands.js index 61914a6..65a36cc 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -32,6 +32,8 @@ Use view to view the content, eg: view web/about clite.commands.load('ls',function(args,env,io) { var dirs = []; var long = false; + var stdlib = io.include('stdlib'); + var stdio = io.include('stdio'); function help() { io.write(` ls - list directory contents @@ -71,7 +73,7 @@ Options: } } }else{ - dirs.push(clite.lib.resolvePath(args[i],false)); + dirs.push(stdlib.resolvePath(args[i],false)); } } @@ -81,21 +83,21 @@ Options: dirs.forEach(function(dir,index) { if (dirs.length > 1) io.write(dir+':'); - var fd = clite.io.open(dir); + var fd = stdio.open(dir); if (!fd) { io.error('cannot open directory: '+dir); return; } if (!fd.node.data.isdir) { writeNodeData(fd.node); - clite.io.close(fd); + stdio.close(fd); return; } var e; - while ((e = clite.io.read(fd)) != null) { + while ((e = stdio.read(fd)) != null) { writeNodeData(e); } - clite.io.close(fd); + stdio.close(fd); }); return 0; @@ -105,6 +107,8 @@ clite.commands.load('cat',function(args,env,io) { var files = []; var download = false; var pending = 0; + var stdlib = io.include('stdlib'); + var stdio = io.include('stdio'); function help() { io.write(` @@ -133,7 +137,7 @@ Options: } } }else{ - files.push(clite.lib.resolvePath(args[i],false)); + files.push(stdlib.resolvePath(args[i],false)); } } @@ -145,7 +149,7 @@ Options: if (fd.node.data.isdir || fd.node.data.islink || fd.node.data.isdev) { io.error('not a normal file: '+file); }else{ - var d = clite.io.readAll(fd); + var d = stdio.readAll(fd); if (d != null) { if (download) { var link = document.createElement("a"); @@ -162,13 +166,13 @@ Options: } } pending--; - clite.io.close(fd); + stdio.close(fd); if (pending <1) io.exit(0); } files.forEach(function(file,index) { - var fd = clite.io.open(file,fcb); + var fd = stdio.open(file,fcb); if (!fd) { io.error('cannot open file: '+file); pending--; @@ -189,6 +193,8 @@ Options: clite.commands.load('touch',function(args,env,io) { var file = null; + var stdlib = io.include('stdlib'); + var stdio = io.include('stdio'); function help() { io.write(` touch - create a new empty file @@ -215,7 +221,7 @@ Options: if (args[i][0] == '/') { file = args[i]; }else{ - file = clite.lib.resolvePath(args[i],false); + file = stdlib.resolvePath(args[i],false); } }else{ io.error('unknown argument: '+args[i]); @@ -227,7 +233,7 @@ Options: return 1; } - if (!clite.io.creat(file)) { + if (!stdio.creat(file)) { io.error('unable to create file: '+file); return 1; } diff --git a/clite/core.js b/clite/core.js index 16b7835..bd8cb82 100644 --- a/clite/core.js +++ b/clite/core.js @@ -788,6 +788,20 @@ clite.shell = { } clite.io.close(fd); }, + include:function(name) { + switch (name) { + case 'stdlib': + return clite.lib; + break; + case 'stdio': + return clite.io; + break; + default: + return null; + break; + } + return null; + }, preprocess:function(txt) { var parts = txt.split('$'); var pptxt = ''; @@ -841,6 +855,7 @@ clite.shell = { write:clite.term.writeLine, error:clite.term.writeLine, exit:clite.shell.exit, // equivalent to C's exit(), needed by asynchronous programs + include:clite.shell.include, // include libraries, allows them to be used in a program istty:{ stdin:true, // false if stdin (read) is not clite.shell.readLine stdout:true // false if stdout (write) is not clite.term.writeLine